我一直在关注一个有一个敌人沿着一条线移动的pygame教程。我想让敌人移动到一个随机的x,y cordinate,到那里休息一段随机的时间并再次移动到一个随机的cordinate。我环顾四周,在pygame中找不到任何类似的东西。
我仍然对pygame相当新,所以这可能非常容易,我只是没有看到它。 到目前为止我的代码:
class enemey(object):
def __init__(self, x, y, width, height, end, yend):
self.x = x
self.y = y
self.width = width
self.height = height
self.end = end
self.walkCount = 0
self.vel = 3
self.path = [self.x, self.end] # change variables <- to random for random moving? Make sure to try!
self.yend = yend
walkRight = [pygame.image.load('R1E.png'), pygame.image.load('R2E.png'), pygame.image.load('R3E.png'),
pygame.image.load('R4E.png'), pygame.image.load('R5E.png'), pygame.image.load('R6E.png'),
pygame.image.load('R7E.png'), pygame.image.load('R8E.png'), pygame.image.load('R9E.png'),
pygame.image.load('R10E.png'), pygame.image.load('R11E.png')]
walkLeft = [pygame.image.load('L1E.png'), pygame.image.load('L2E.png'), pygame.image.load('L3E.png'),
pygame.image.load('L4E.png'), pygame.image.load('L5E.png'), pygame.image.load('L6E.png'),
pygame.image.load('L7E.png'), pygame.image.load('L8E.png'), pygame.image.load('L9E.png'),
pygame.image.load('L10E.png'), pygame.image.load('L11E.png')]
def draw(self, gameDisplay):
self.move()
if self.walkCount + 1 >= 33:
self.walkCount = 0
if self.vel > 0:
gameDisplay.blit(self.walkRight[self.walkCount // 3], (self.x, self.y))
self.walkCount += 1
else:
gameDisplay.blit(self.walkLeft[self.walkCount // 3], (self.x, self.y))
self.walkCount += 1
def move(self):
if self.vel > 0:
if self.x < self.path[1] + self.vel:
self.x += self.vel
else:
self.vel = self.vel * -1
self.x += self.vel
self.walkCount = 0
else:
if self.x > self.path[0] - self.vel:
self.x += self.vel
else:
self.vel = self.vel * -1
self.x += self.vel
self.walkCount = 0
self.walkCount = 0