Pygame:如何在不按任何键的情况下使精灵面对相同的方向
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Pygame:如何在不按任何键的情况下使精灵面对相同的方向相关的知识,希望对你有一定的参考价值。
我的角色能够正确移动。然而,当没有按键被按下时,出现问题。如何在没有按下KEY的情况下使精灵指向与上次面对的方向相同的方向?例如,如果我按下UP,它会面朝上,然后当我不按任何其他键时,它仍然会面朝上吗?同样与其他方向。另外,首次启动游戏时我会如何显示角色?就像,我的角色仅在按下键时才会显示,而在没有按键时仍然不可见。
这是我的代码:
Sprites:
# Protagonist Sprites protagR = [pygame.image.load("ASG2_Graphics/JoArrow_R1_1.png"), pygame.image.load("ASG2_Graphics/JoArrow_R1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_R1_3.png"), pygame.image.load("ASG2_Graphics/JoArrow_R1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_R1_1.png")] protagL = [pygame.image.load("ASG2_Graphics/JoArrow_L1_1.png"), pygame.image.load("ASG2_Graphics/JoArrow_L1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_L1_3.png"), pygame.image.load("ASG2_Graphics/JoArrow_L1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_L1_1.png")] protagU = [pygame.image.load("ASG2_Graphics/JoArrow_U1_1.png"), pygame.image.load("ASG2_Graphics/JoArrow_U1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_U1_3.png"), pygame.image.load("ASG2_Graphics/JoArrow_U1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_U1_1.png")] protagD = [pygame.image.load("ASG2_Graphics/JoArrow_D1_1.png"), pygame.image.load("ASG2_Graphics/JoArrow_D1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_D1_3.png"), pygame.image.load("ASG2_Graphics/JoArrow_D1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_D1_1.png")] # Bullet Sprites bulletsP = [pygame.image.load("ASG2_Graphics/Bullet_1.png"), pygame.image.load("ASG2_Graphics/Bullet_2.png"), pygame.image.load("ASG2_Graphics/Bullet_3.png"), pygame.image.load("ASG2_Graphics/Bullet_4.png")] bulletsE = [pygame.image.load("ASG2_Graphics/E_Bullet_1.png"), pygame.image.load("ASG2_Graphics/E_Bullet_2.png"), pygame.image.load("ASG2_Graphics/E_Bullet_3.png")] # Enemy Sprites enemyR = [pygame.image.load("ASG2_Graphics/Enemy_R1_1.png"), pygame.image.load("ASG2_Graphics/Enemy_R1_2.png"), pygame.image.load("ASG2_Graphics/Enemy_R1_3.png"), pygame.image.load("ASG2_Graphics/Enemy_R1_4.png"), pygame.image.load("ASG2_Graphics/Enemy_R1_5.png")] enemyL = [pygame.image.load("ASG2_Graphics/Enemy_L1_1.png"), pygame.image.load("ASG2_Graphics/Enemy_L1_2.png"), pygame.image.load("ASG2_Graphics/Enemy_L1_3.png"), pygame.image.load("ASG2_Graphics/Enemy_L1_4.png"), pygame.image.load("ASG2_Graphics/Enemy_L1_5.png")] enemyU = [pygame.image.load("ASG2_Graphics/Enemy_U1_1.png"), pygame.image.load("ASG2_Graphics/Enemy_U1_2.png"), pygame.image.load("ASG2_Graphics/Enemy_U1_3.png"), pygame.image.load("ASG2_Graphics/Enemy_U1_4.png"), pygame.image.load("ASG2_Graphics/Enemy_U1_5.png")] enemyD = [pygame.image.load("ASG2_Graphics/Enemy_D1_1.png"), pygame.image.load("ASG2_Graphics/Enemy_D1_2.png"), pygame.image.load("ASG2_Graphics/Enemy_D1_3.png"), pygame.image.load("ASG2_Graphics/Enemy_D1_4.png"), pygame.image.load("ASG2_Graphics/Enemy_D1_5.png")] # Background Sprite bg = pygame.image.load("Star_Crusaders_MAP.png")
main()
def main(): board = player(30, 45, 64, 64, 0) while True: movePlayer(board)
类播放器():
class player(): def __init__(self, x, y, width, height, walkCount): self.x = x self.y = y self.width = width self.height = height self.vel = 5 self.up = False self.down = False self.left = False self.right = False self.up = False self.down = False self.walkCount = walkCount
movePlayer()
def movePlayer(board): clock = pygame.time.Clock() run = True while run: clock.tick(15) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False arrowKeys = pygame.key.get_pressed() if arrowKeys[pygame.K_LEFT] and board.x > 25 + board.vel: board.x -= board.vel board.left = True board.right = False board.up = False board.down = False elif arrowKeys[pygame.K_RIGHT] and board.x < 485 - board.width - board.vel: board.x += board.vel board.right = True board.left = False board.up = False board.down = False elif arrowKeys[pygame.K_UP] and board.y > 40 + board.vel: board.y -= board.vel board.right = False board.left = False board.up = True board.down = False elif arrowKeys[pygame.K_DOWN] and board.y < 450 - board.height - board.vel: board.y += board.vel board.right = False board.left = False board.up = False board.down = True elif arrowKeys[pygame.K_ESCAPE]: # ESC key stopGame() else: board.right = False board.left = False board.up = False board.down = False board.walkCount = 0 redrawGameWindow(board)
redrawGameWindow()
def redrawGameWindow(board): window.blit(bg, (0,0)) dest = (board.x, board.y) if board.walkCount + 1 >= 30: board.walkCount = 0 if board.left: window.blit(protagL[0], dest) # currentSprite = protagL[0] elif board.right: window.blit(protagR[0], dest) # currentSprite = protagR[0] elif board.up: window.blit(protagU[0], dest) # currentSprite = protagU[0] elif board.down: window.blit(protagD[0], dest) # currentSprite = protagD[0] # stillSprite(currentSprite, board) pygame.display.update()
如您所见,我尝试使用currentSprite()变量和stillSprite()函数来尝试存储最后按下的键,但无济于事。谢谢您的帮助。
我的角色能够正确移动。然而,当没有按键被按下时,出现问题。我如何做到这一点,当没有按键被按下时,精灵将面对与上次相同的方向...
答案
问题是,由于必须停止移动,因此在释放键时会重置状态board.left
,board.right
,board.up
,board.down
。由于图像应保持方向,因此该状态不能用于该要求。您必须添加其他状态。
以上是关于Pygame:如何在不按任何键的情况下使精灵面对相同的方向的主要内容,如果未能解决你的问题,请参考以下文章