如何解决玩家在 pygame 中达到边界后卡住
当我的播放器达到值为 0 的边界之一时,它完全没问题并且可以在世界范围内自由移动。但是当我的播放器达到值 485 的边界时,它会很好,直到您尝试向上移动。然后根据你击中的边界,它会卡在 playerX = 249 或 playerY = 249
导入pygame
导入时间
pygame.init()
playerX = 250 # Player X locale
playerY = 250 # Players Y locale
playerSpeed = 2
backgroundX = 0 # Background X locale
backgroundY = 0 # Background Y locale
### LOADS PLAYER IMAGES ###
playerFrontImg = pygame.image.load("Images/Player Images/playerBackOne.png")
playerBackImg = pygame.image.load("Images/Player Images/playerFrontOne.png")
playerLeftImg = pygame.image.load("Images/Player Images/playerLeftOne.png")
playerRightImg = pygame.image.load("Images/Player Images/playerRightOne.png")
playerCurrentImg = playerFrontImg
### LOADS PLAYER IMAGES ###
### LOADS MAP IMAGES ###
testMap = pygame.image.load("Images/Map Images/TEST_MAP.png")
### LOADS MAP IMAGES ###
screen = pygame.display.set_mode((500,500))
clock = pygame.time.Clock()
running = True
while running:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
### KEYBOARD INPUTS AND MOVEMENTS ###
keys = pygame.key.get_pressed() # gets pressed keys and assigns it TRUE or FALSE
if keys[pygame.K_w]: # Checks if "w" is pressed
playerCurrentImg = playerFrontImg # Changes current image to correct image
if playerY == 250:
if backgroundY < 0:
backgroundY += playerSpeed
if backgroundY >= 0 or playerY > 250:
playerY -= playerSpeed
if playerY <= 0:
playerY = 0
if keys[pygame.K_s]: # Checks if "s" is pressed
playerCurrentImg = playerBackImg # Changes current image to correct image
if playerY == 250:
if backgroundY > -500:
backgroundY -= playerSpeed
if backgroundY <= -500 or playerY < 250:
playerY += playerSpeed
if playerY >= 485:
playerY = 485 # THIS SEEMS TO BE WHERE THE ISSUE IS
if keys[pygame.K_a]:
playerCurrentImg = playerLeftImg # Changes current image to correct image
if playerX == 250:
if backgroundX < 0:
backgroundX += playerSpeed
if backgroundX >= 0 or playerX > 250:
playerX -= playerSpeed
if playerX <= 0:
playerX = 0
if keys[pygame.K_d]:
playerCurrentImg = playerRightImg # Changes current image to correct image
if playerX == 250:
if backgroundX > -500:
backgroundX -= playerSpeed
if backgroundX <= -500 or playerX < 250:
playerX += playerSpeed
if playerX >= 485:
playerX = 485
if keys[pygame.K_f]: # TROUBLE SHOOTING KEY
print(playerX,playerY,"\n",backgroundX,backgroundY)
time.sleep(1)
### KEYBOARD INPUTS AND MOVEMENTS ###
screen.blit(testMap,(backgroundX,backgroundY))
screen.blit(playerCurrentImg,(playerX,playerY))
pygame.display.update()
pygame.quit()
quit()
解决方法
单个符号的情况。
关键代码是这样的:
if keys[pygame.K_w]: # Checks if "w" is pressed
playerCurrentImg = playerFrontImg # Changes current image to correct image
if playerY == 250:
if backgroundY < 0:
backgroundY += playerSpeed
if backgroundY >= 0 or playerY > 250:
playerY -= playerSpeed
if playerY <= 0:
playerY = 0
它应该是 playerY <= 250
,最好的做法是使用 and
而不是嵌套的 if。您的代码应如下所示。
if keys[pygame.K_w]: # Checks if "w" is pressed
playerCurrentImg = playerFrontImg # Changes current image to correct image
if playerY <= 250 and backgroundY < 0:
backgroundY += playerSpeed
if backgroundY >= 0 or playerY > 250:
playerY -= playerSpeed
if playerY <= 0:
playerY = 0
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。