Why don't the enemy object work

Question:
I need the enemy to move but not using vector in pygame
Repl link:
https://replit.com/@Mark01002/game?v=1

code snippet
import sys #1
#https://www.pygame.org/ftp/contrib/pygame_docs.pdf
import pygame #1
from pygame.locals import * #1
import time #1

pygame.init() #1
DISPLAYSURF = pygame.display.set_mode((700, 700)) #1
pygame.display.set_caption('Game') #1


while True: #1
    pygame.display.flip() #1
    for event in pygame.event.get(): #1
         if event.type == QUIT: #1
           pygame.quit() #1
           sys.exit() #1
         if event.type == MOUSEBUTTONUP: #1
           x,y = event.pos #1
           pos = (x,y) #1
    DISPLAYSURF.fill((0, 0, 0)) #1
    pygame.time.delay(10) #1
    rect = pygame.Rect((30,30,30,30)) #1
    
    enemy = pygame.Rect((30,30,30,30))
    enemy_x = enemy.x
    enemy_y = enemy.y
    tarx = rect.x
    tary = rect.y
    enemy.x += 10 if tarx > enemy.x else -10
    enemy.y += 10 if tary > enemy.y else -10
    enemy.x = enemy_x
    enemy.y = enemy_y
    pygame.draw.rect(DISPLAYSURF, "red",enemy, width=0) #1
      
    try: #1
      rect.center=pos #1
    except NameError: #1
      rect.center=0,0 #1
    pygame.draw.rect(DISPLAYSURF, "blue",rect, width=0) #1
    pygame.display.update() #1

Hello @Mark01002 and welcome to the community!

This will make your day more hard… but it can be done. Altough you will need to manually update the position of the enemy object in each iteration of your game loop,

Basically you need to define an initial position for your enemy (and the speed too).
After each interation of the game loop you update the enemy’s position based on it’s speed, and use this updated position to draw the enemy.

Picking up your code:

import sys
import pygame
from pygame.locals import *
import time

pygame.init()
DISPLAYSURF = pygame.display.set_mode((700, 700))
pygame.display.set_caption('Game')

# As I said, you need to define initial enemy position and speed
enemy_x, enemy_y = 10, 10  # Initial position
enemy_speed_x, enemy_speed_y = 2, 2  # Speed in x and y direction

pic = pygame.image.load("enemy.png").convert()
enemy = pygame.transform.rotate(pic, 120)

while True:
    pygame.display.flip()
    for event in pygame.event.get():
         if event.type == QUIT:
           pygame.quit()
           sys.exit()
         if event.type == MOUSEBUTTONUP:
           x, y = event.pos
           pos = (x, y)

    # Then you update enemy position
    enemy_x += enemy_speed_x
    enemy_y += enemy_speed_y

    # Check for boundary collision to change direction
    if enemy_x + 30 > 700 or enemy_x < 0:
        enemy_speed_x *= -1
    if enemy_y + 30 > 700 or enemy_y < 0:
        enemy_speed_y *= -1

    DISPLAYSURF.fill((0, 0, 0))
    pygame.time.delay(10)

    # And don't forget to draw the enemy at the new position
    DISPLAYSURF.blit(enemy, (enemy_x, enemy_y))

    try:
      rect = pygame.Rect((30, 30, 30, 30))
      rect.center = pos
    except NameError:
      rect = pygame.Rect((0, 0, 30, 30))

    pygame.draw.rect(DISPLAYSURF, "blue", rect, width=0)
    pygame.display.update()

You can adjust the position and the speed as you like.

3 Likes

The problem lies here.

3 Likes

Just a note, multiplying by -1 for borders is very bug prone. If they hit the wall while slowing down, they’ll get stuck in the wall.

(Also, using delta time is a big possible improvement here.)

3 Likes