所以我想弄清楚如何让敌人移动,因为这是我需要做的最后一件事!我该如何移动敌人,就像我在这里待了一个小时!我已经尝试过更新坐标,但结果并没有那么好,所以有人能给我正确的代码/解决它吗?如果没有,至少有人能告诉我接下来要做什么吗?!?
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()