Pygame 按下按键时移动物体 [英] Pygame moving objects while pressing key
问题描述
我在按下某个键时连续移动对象时遇到问题.据我所知,pygame 事件仅在接收到新信号时触发,并且按下的键不是每帧都继续的信号.从下面的代码中,例如当我按下 w 时,对象仅在按下和释放键时移动.只要我一直按着键,我就无法实现物体移动.我还通过检查 event.key
而不是检查 get_pressed()
列表来尝试 if 语句,但我得出了相同的结果.
I have a problem moving the objects continuously while pressing a key.
As far as I know, pygame events only trigger when receiving a new signal and that the key pressed is not a signal that continues with every frame. From the code below, when I press w for example, the object only moves when pressing and when releasing the key. I can't achieve the object to move as long as I keep pressing the key. I've also tried out the if statements by checking event.key
instead of checking the get_pressed()
list, but I came up with the same result.
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
keys = pygame.key.get_pressed()
print(keys[pygame.K_w])
if keys[pygame.K_w] and keys[pygame.K_d]:
x_change = 3
x += x_change
y_change = -3
y += y_change
elif keys[pygame.K_w] and keys[pygame.K_a]:
x_change = -3
x += x_change
y_change = -3
y += y_change
elif keys[pygame.K_s] and keys[pygame.K_d]:
x_change = 3
x += x_change
y_change = 3
y += y_change
elif keys[pygame.K_s] and keys[pygame.K_a]:
x_change = -3
x += x_change
y_change = 3
y += y_change
elif keys[pygame.K_a]:
x_change = -3
x += x_change
elif keys[pygame.K_d]:
x_change = 3
x += x_change
elif keys[pygame.K_w]:
y_change = -3
y += y_change
elif keys[pygame.K_s]:
y_change = 3
y += y_change
#ERASE OLD
screen.fill(WHITE)
#FILL NEW
all_sprites_list.draw(screen)
wall.changePosition(x,y)
player.draw(start_x,start_y)
pygame.display.update()
clock.tick(60)
推荐答案
它看起来像 keys = pygame.key.get_pressed()
行和以下几行在您的事件循环中,并且意味着它们在事件队列中的每个事件只执行一次.它们实际上应该在外部 while 循环中,所以只需删除这些行:
It looks like the line keys = pygame.key.get_pressed()
and the following lines are in your event loop, and that means they are only executed once per event in the event queue. They should actually be in the outer while loop, so just dedent these lines:
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
keys = pygame.key.get_pressed()
if keys[pygame.K_w] and keys[pygame.K_d]:
x_change = 3
# etc.
你也可以大大缩短你的代码:
You can also shorten your code quite a bit:
x_change = 3
y_change = 3
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
x += -x_change
elif keys[pygame.K_d]:
x += x_change
if keys[pygame.K_w]:
y += -y_change
elif keys[pygame.K_s]:
y += y_change
另一种选择是删除 key.get_pressed
行,然后在事件循环中设置 x_change
和 y_change
:
Another alternative would be to remove the key.get_pressed
lines and just set the x_change
and y_change
in the event loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_d:
x_change = 3
# etc.
elif event.type == pygame.KEYUP:
if event.key == pygame.K_d and x_change > 0:
x_change = 0
# etc.
这篇关于Pygame 按下按键时移动物体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!