所以我正在开发PacMan游戏,并且我已经设置了我的代码,这样如果你点击左键,它就会记住'然后等到玩家在没有墙壁的地方,让它向左移动。通过这种方式,玩家不必精确地按照墙壁开口的位置来定时击键。我的问题是,当我向左打时,它会播放左移动画,即使玩家没有朝那个方向移动,直到墙上有一个开口。
在我的代码中,我试图让它只在播放器向左移动时播放左侧动画。我试图这样做的方法是立刻获得玩家的位置,然后测试玩家现在是否在左边,这意味着它已经成功转弯并且现在应该播放左边的动画。以下是我在代码中的基本思路:
class Player (pygame.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.all_sprites
pygame.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = sprite_sheet('pacman-spritesheet.png').get_image(0, 0, 24, 24)
self.x = x * TILESIZE
self.y = y * TILESIZE
self.rect = self.image.get_rect()
self.vx, self.vy = 0, 0
self.spritesheet_index = 0
self.first_time = pygame.time.get_ticks()
self.delay = 43
self.input_list = ['--']
self.direction = ''
self.POINTS = 0
def get_keys(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.input_list.append('left')
if keys[pygame.K_RIGHT]:
self.input_list.append('right')
if keys[pygame.K_UP]:
self.input_list.append('up')
if keys[pygame.K_DOWN]:
self.input_list.append('down')
def last_direction(self):
return self.input_list[len(self.input_list) - 1]
def animate(self):
self.spritesheet_index += 1
if self.last_direction() == 'left':
self.second_time = pygame.time.get_ticks()
if self.second_time-self.first_time > self.delay and self.first_x < self.second_x:
self.image = PAC_LEFT[self.spritesheet_index % 4]
self.first_time = pygame.time.get_ticks()
if self.last_direction() == 'right':
self.second_time = pygame.time.get_ticks()
if self.second_time - self.first_time < self.delay:
self.image = PAC_RIGHT[self.spritesheet_index % 4]
self.first_time = pygame.time.get_ticks()
if self.last_direction() == 'up':
self.second_time = pygame.time.get_ticks()
if self.second_time - self.first_time > self.delay:
self.image = PAC_UP[self.spritesheet_index % 4]
self.first_time = pygame.time.get_ticks()
if self.last_direction() == 'down':
self.second_time = pygame.time.get_ticks()
if self.second_time - self.first_time > self.delay:
self.image = PAC_DOWN[self.spritesheet_index % 4]
self.first_time = pygame.time.get_ticks()
def set_velocity(self):
if self.last_direction() == 'left':
self.vx = -PLAYER_SPEED
if self.last_direction() == 'right':
self.vx = PLAYER_SPEED
if self.last_direction() == 'up':
self.vy = -PLAYER_SPEED
if self.last_direction() == 'down':
self.vy = PLAYER_SPEED
def move(self):
self.x += self.vx
self.y += self.vy
def update(self):
self.first_x = self.x
self.get_keys()
self.set_velocity()
self.move()
self.second_x = self.x
self.animate()
self.rect.x = self.x
self.collide_with_wall('x')
self.rect.y = self.y
self.collide_with_wall('y')
self.collide_with_point()
答案 0 :(得分:1)
您可以通过拥有属性self.last_rect
来执行此操作,您可以在更新方法结束时保存该属性。但是,我认为有更好的方法。既然你设法做到这一点,那么Pacman只有在墙上有一个开口时才开始向左移动,你应该能够在代码中使用相同的位置来改变精灵。但如果你仍然想按照你的要求去做,这大致是你需要做的事情。
PACMAN_SIZE = 32, 32
LEFT_SPRITES = load_left_sprites()
RIGHT_SPRITES = load_right_sprites()
class Pacman(pygame.sprite.Sprite):
def __init__(self)
# ... List all your attributes in the __init__ method ...
def get_direction(self):
return self.input_list[len(self.input_list)-1]
def set_velocity(self):
if self.get_direction() == 'left':
self.vx = -PLAYER_SPEED
elif self.get_direction() == 'right':
self.vx = PLAYER_SPEED
def move(self):
# Your move logic
# ... code ...
self.rect.x += self.vx
def animate(self):
if self.new_x > self.previous_x:
self.image = PAC_RIGHT[self.spritesheet_index % 4]
elif self.new_x < self.previous_x:
self.image = PAC_LEFT[self.spritesheet_index % 4]
def update(self, dt):
self.previous_x = self.rect.x
set_velocity()
move()
self.new_x = self.rect.x
animate()