如何在我按住PyGame中的任何键时使角色移动? [英] How to set my character to move as long as I am holding any key in PyGame?
问题描述
我正在PyGame中创建一个简单的游戏.但是问题在于,每当我按住一个键时,角色都会停止移动一步,直到我松开键并再次按下它时才移动.代码如下.
I am creating a simple game in PyGame. But the problem is that whenever I hold down a key, my character stops moving after one step and does not move until I release the key and press it again. The code is following.
pygame.init()
screen_width = 800
screen_height = 800
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("SPACE INVADER")
icon = pygame.image.load("game-controller.png")
pygame.display.set_icon(icon)
player_img = pygame.image.load("Webp.net-resizeimage.png")
playerX = 400
playerY = 700
playerX_change = 0
def player(x, y):
screen.blit(player_img, (x, y))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -5
if event.key == pygame.K_RIGHT:
playerX_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
playerX += playerX_change
screen.fill((0, 0, 0))
player(playerX, playerY)
pygame.display.update()
推荐答案
这是缩进.您必须移动对象并在应用程序循环而不是事件循环中更新场景:
It's a matter of Indentation. You have to move the object and update the scene in the application loop instead of the event loop:
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -5
if event.key == pygame.K_RIGHT:
playerX_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
# INDENTATION
#<--|
playerX += playerX_change
screen.fill((0, 0, 0))
player(playerX, playerY)
pygame.display.update()
但是,请使用 pygame.key.get_pressed()
,而不是键盘事件.
However, use pygame.key.get_pressed()
instead of the keyboard events.
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
playerX -= 5
if keys[pygame.K_RIGHT]:
playerX += 5
playerX += playerX_change
screen.fill((0, 0, 0))
player(playerX, playerY)
pygame.display.update()
说明:
Explanation:
键盘事件(请参阅 pygame.event 模块)仅发生一次当键的状态改变时.每次按下一个键,都会发生一次 KEYDOWN
事件.每次释放键时,都会发生一次 KEYUP
.使用键盘事件可以执行单个操作或逐步移动.
The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN
event occurs once every time a key is pressed. KEYUP
occurs once every time a key is released. Use the keyboard events for a single action or a step-by-step movement.
pygame.key.get_pressed()
返回包含每个键状态的列表.如果按住某个键,则该键的状态为 True
,否则为 False
.使用 pygame.key.get_pressed()
评估按钮的当前状态并获得连续的移动.
pygame.key.get_pressed()
returns a list with the state of each key. If a key is held down, the state for the key is True
, otherwise False
. Use pygame.key.get_pressed()
to evaluate the current state of a button and get continuous movement.
这篇关于如何在我按住PyGame中的任何键时使角色移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!