0

So I am trying to figure out how to make the enemy move as this is the last thing I need to do! How am I supposed to move the enemy as I have been at this for an hour!!! I've tried updating the coordinates but that didn't turn out so well so can anyone give me the correct code/fix it? If not can someone at least tell me what to do next?!?

class Enemy(object):
    def __init__(self,pos):
        enemies.append(self)
        self.rect = pygame.Rect(x, y, 10, 10)

    def moveEnemy(self, dx, dy):

        # Move each axis separately. Note that this checks for collisions both times.
        if dx != 0:
            self.XandY(dx, 0)
        if dy != 0:
            self.XandY(0, dy)

    def XandYEnemy(self, dx, dy):

        # Move the rect
        self.rect.x += dx
        self.rect.y += dy

        for wall in walls:
            if self.rect.colliderect(wall.rect):
                if dx > 0: # Moving right; Hit the left side of the wall
                    self.rect.right = wall.rect.left
                if dx < 0: # Moving left; Hit the right side of the wall
                    self.rect.left = wall.rect.right
                if dy > 0: # Moving down; Hit the top side of the wall
                    self.rect.bottom = wall.rect.top
                if dy < 0: # Moving up; Hit the bottom side of the wall
                    self.rect.top = wall.rect.bottom

class Wall(object):
    def __init__(self, pos):
        walls.append(self)
        self.rect = pygame.Rect(pos[0], pos[1], 16, 16)

#Variables

walls = []
enemies = []

player = Player()


pygame.init()



# Set the width and height of the screen [width, height]
size = (700, 285)
screen = pygame.display.set_mode(size)

pygame.display.set_caption("Karl's Game")

#Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()
#Loading pictures

level = [
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
"W  WW            WW       W                W",
"W  WW  WW        WW       W        W       W",
"W  WW  WW  WWWWWWWW  WWW  WWWW  WWWW  WWW  W",
"W  WW  WW  WWWWWWWW  WWW  WWWW  WWWW  WWW  W",
"W  WW  WW  WWWWWWWW  WWW  WWWW  WWWW  WWW  W",
"W  WW  WW  WWWWWWWW  WWW  WWWW  WWWW  WWW  W",
"W  WW  WW  WWWWWWWW  WWW  WWWW  WWWW  WWW  W",
"W  B   WW      WWWW  WWW  WWWW  WWWW  WWW  W",
"W      WW      WWWW  WWW  WWWW  WWWW  WWW  W",
"W  WW  WW  WWWWWWWW  WWW  WWWW  WWWW  WWW  W",
"W  WW  WW  WWWWWWWW  WWW  WWWW  WWWW  WWW  W",
"W  WW  WW  WWWWWWWW  WWW  WWWW  WWWW  WWW  W",
"W  WW  WW  WWWWWWWW  WWW  WWWW  WWWW  WWW  W",
"W  WW  WW  WWWWWWWW  WWW  WWWW  WWWW  WWW  W",
"W  WW  WW            WWW           W  WWW  W",
"W  WW  WW            WWW           W  WWWE W",
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
]


# Turns the string above into the maze and W = Wall E = Exit
x = y = 0
for row in level:
    for col in row:
        if col == "W":
            Wall((x, y))
        if col == "B":
            Enemy((x,y))
        if col == "E":
            end_rect = pygame.Rect(x, y, 32, 16)
        x += 16
    y += 16
    x = 0

# -------- Main Program Loop -----------
while not done:
        # --- Limit to 60 frames per second
    clock.tick(60)
    # --- Main event loop
    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done = True # Flag that we are done so we exit this loop


    key = pygame.key.get_pressed()
    if key[pygame.K_LEFT]:
        player.move(-2, 0)
    if key[pygame.K_RIGHT]:
        player.move(2, 0)
    if key[pygame.K_UP]:
        player.move(0, -2)
    if key[pygame.K_DOWN]:
        player.move(0, 2)



    if player.rect.colliderect(end_rect):
        go = 2




    # --- Drawing code should go here
    if go == 1:
        screen.blit(back, [0, 0])


        for wall in walls:
            pygame.draw.rect(screen, NEON, wall.rect)
        pygame.draw.rect(screen, (255, 0, 0), end_rect)
        pygame.draw.rect(screen, (0, 200, 0), player.rect)
        for enemy in enemies:
            pygame.draw.rect(screen, PURPLE, enemy.rect)


    # --- Go ahead and update the screen with what we've drawn.
    pygame.display.flip()



# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit()
poop
  • 3
  • 1
  • 2
  • 5
  • In order to get better help try to narrow this down to just the section that's causing you problems and provide a very precise description of what is going wrong. – shuttle87 Dec 12 '15 at 02:22
  • 1
    There I have narrowed it down as much as I can :) – poop Dec 12 '15 at 02:26
  • I don't see `move_enemy` called from anywhere... Also in the `__init__` of Enemy `pos` is never used. – shuttle87 Dec 12 '15 at 02:36
  • I removed that because whenever I call that it says that enemy has not been initialized or created yet and when I try to create that it says that it takes 2 parameters but I don't know what the other one is... Also the __init__ is used in the for loop under the level string – poop Dec 12 '15 at 02:38
  • You need to fix the `__init__` of `Enemy` then you need to call `move_enemy` from somewhere. There's a whole host of other small changes that would improve the quality of this code like not adding the elements into a gloabl list in their `__init__`. Fix those major issues before you move on. It's a very good habit to get in the practice of fixing bugs before moving on to anything else. – shuttle87 Dec 12 '15 at 02:42
  • ;Ok I'll try to fix it and also how would I be able to update the x and y coordinates of the enemy inside of the class within the main program loop? – poop Dec 12 '15 at 02:43
  • You can make a method to update the coordinates. You can then call that method wherever you have an instance. – shuttle87 Dec 12 '15 at 02:45
  • Ok so I have made the enemy = Enemy() but I get an error saying pos has not been defined although it is under the code so how do I define this? – poop Dec 12 '15 at 02:48

0 Answers0