如何解决我的太空飞船在pygame中不会向右移动
import pygame
#initialize the screen
pygame.init()
#create the screen
screen = pygame.display.set_mode((800,700))
#tile and icon
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load("spaceship.png")
pygame.display.set_icon(icon)
#Player
playerImg = pygame.image.load("player.png")
playerx = 370
playery = 600
playerx_change = 0.1
def player(x,y):
screen.blit(playerImg,(x,y))
running = True
while running:
screen.fill((0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#keystroke
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerx_change = -0.1
if event.type == pygame.K_RIGHT:
playerx_change = 0.1
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerx_change = 0
playerx += playerx_change
player(playerx,playery)
pygame.display.update()
当我按下riht键时,我的飞船不会向右侧移动。但如果我按向左键,它将向左侧移动,并且终端中未显示任何错误。我使用Visual Studio 2019的社区版本。
解决方法
您需要更改此内容:
if event.type == pygame.K_RIGHT:
为此(“类型”->“键”):
if event.key == pygame.K_RIGHT:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。