向左或向右快速移动会导致 img 粘住
Move left or right to fast causes img to stick
我正在 python 3 中与 pygame 制作一款宇宙飞船入侵者游戏。我目前遇到宇宙飞船卡住的问题,让我双击左或右箭头键使其生效。这是我的代码:
import pygame
pygame.init()
display_width = 800
display_height = 600
black = (0, 0, 0)
white = (255, 255, 255)
#Window Size
gameDisplay = pygame.display.set_mode((display_width, display_height))
#Title Of Window
pygame.display.set_caption('A Bit Racey')
#FPS
clock = pygame.time.Clock()
spaceshipImg = pygame.image.load('SpaceShipSmall.png')
def spaceship(x,y):
gameDisplay.blit(spaceshipImg, (x,y))
x = (display_width * 0.45)
y = (display_height * 0.8)
x_change = 0
crashed = False
while not crashed:
# this will listen for any event every fps
for event in pygame.event.get():
if event.type == pygame.QUIT:
#change later
crashed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
gameDisplay.fill(white)
spaceship(x,y)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
在x +=前添加如下语句 x_change:
print (event.type, event.key, x)
这会将事件数据和 x 位置打印到控制台。
再次尝试您的脚本,尝试按住左键 3 秒,然后松开,并重复右键。查看在按住按键时是否看到多个按键事件。我认为您在发布时应该只看到一个 keyup 事件。
每次检测到 pygame.KEYUP
事件 时,您 重置 x_change
.即使您 按住 例如你的右箭头键 一个 左箭头键 按键停止移动 你的宇宙飞船。
要解决此问题,您可以使用pygame.key.get_pressed()
方法获取所有键盘按钮的状态。 此函数returns布尔值序列由pygames键常量值索引,表示键盘上每个键的状态。
因为你不需要在每次事件发生时调用pygame.key.get_pressed()
,更新后的主循环应该如下所示:
while not crashed:
# this will listen for any event every fps
for event in pygame.event.get():
if event.type == pygame.QUIT:
#change later
crashed = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
#get the state of all keyboard buttons
pressedKeys = pygame.key.get_pressed()
#change position if pygame.K_LEFT or pygame.K_RIGHT is pressed
if pressedKeys[pygame.K_LEFT]:
x += -5
elif pressedKeys[pygame.K_RIGHT]:
x += 5
gameDisplay.fill(white)
spaceship(x,y)
pygame.display.update()
clock.tick(60)
注意 pygame.K_LEFT
事件比 pygame.K_RIGHT
事件具有 更高的优先级。您可以使用 两个单独的 if
块 来更改此行为。 非常感谢 @sloth 指出这一点!:
#change position if either pygame.K_LEFT or pygame.K_RIGHT is pressed
if pressedKeys[pygame.K_LEFT]:
x += -5
if pressedKeys[pygame.K_RIGHT]:
x += 5
希望对您有所帮助:)
我正在 python 3 中与 pygame 制作一款宇宙飞船入侵者游戏。我目前遇到宇宙飞船卡住的问题,让我双击左或右箭头键使其生效。这是我的代码:
import pygame
pygame.init()
display_width = 800
display_height = 600
black = (0, 0, 0)
white = (255, 255, 255)
#Window Size
gameDisplay = pygame.display.set_mode((display_width, display_height))
#Title Of Window
pygame.display.set_caption('A Bit Racey')
#FPS
clock = pygame.time.Clock()
spaceshipImg = pygame.image.load('SpaceShipSmall.png')
def spaceship(x,y):
gameDisplay.blit(spaceshipImg, (x,y))
x = (display_width * 0.45)
y = (display_height * 0.8)
x_change = 0
crashed = False
while not crashed:
# this will listen for any event every fps
for event in pygame.event.get():
if event.type == pygame.QUIT:
#change later
crashed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
gameDisplay.fill(white)
spaceship(x,y)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
在x +=前添加如下语句 x_change:
print (event.type, event.key, x)
这会将事件数据和 x 位置打印到控制台。
再次尝试您的脚本,尝试按住左键 3 秒,然后松开,并重复右键。查看在按住按键时是否看到多个按键事件。我认为您在发布时应该只看到一个 keyup 事件。
每次检测到 pygame.KEYUP
事件 时,您 重置 x_change
.即使您 按住 例如你的右箭头键 一个 左箭头键 按键停止移动 你的宇宙飞船。
要解决此问题,您可以使用pygame.key.get_pressed()
方法获取所有键盘按钮的状态。 此函数returns布尔值序列由pygames键常量值索引,表示键盘上每个键的状态。
因为你不需要在每次事件发生时调用pygame.key.get_pressed()
,更新后的主循环应该如下所示:
while not crashed:
# this will listen for any event every fps
for event in pygame.event.get():
if event.type == pygame.QUIT:
#change later
crashed = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
#get the state of all keyboard buttons
pressedKeys = pygame.key.get_pressed()
#change position if pygame.K_LEFT or pygame.K_RIGHT is pressed
if pressedKeys[pygame.K_LEFT]:
x += -5
elif pressedKeys[pygame.K_RIGHT]:
x += 5
gameDisplay.fill(white)
spaceship(x,y)
pygame.display.update()
clock.tick(60)
注意 pygame.K_LEFT
事件比 pygame.K_RIGHT
事件具有 更高的优先级。您可以使用 两个单独的 if
块 来更改此行为。 非常感谢 @sloth 指出这一点!:
#change position if either pygame.K_LEFT or pygame.K_RIGHT is pressed
if pressedKeys[pygame.K_LEFT]:
x += -5
if pressedKeys[pygame.K_RIGHT]:
x += 5
希望对您有所帮助:)