Pygame: KEYDOWN与KEYUP同时触发导致无净移动

2024-04-24 17:07:41 发布

您现在位置:Python中文网/ 问答频道 /正文

你知道吗 我这样做了,当用户点击WASD时,它会使平滑x或平滑y变为5,以不断添加到他们的x或y坐标来模拟运动。你知道吗

但是我有一个问题,如果用户按住a,然后同时单击D,它会导致平滑的x为0,从而导致用户保持原位。你知道吗

例如,用户单击D(平滑x=5)用户单击A(平滑x=-5)用户按住D然后按住A然后放开D导致平滑x=0导致用户停止移动,这是我不想要的。在这种情况下,平滑的x应该是=-5 你知道吗

while gameloop == True:
num_scraps = 0
fps.tick(60) #Sets FPS to 60
for event in pygame.event.get(): #Checks each event
    if event.type == pygame.QUIT: #If one of the events are quit (when the user clicks the X in the top right corner) the window closes
        pygame.quit()
    if event.type == pygame.KEYUP:
        print(event)
        #If the user stop pressing one of the arrow keys it sets all the smooth values to 0 so it stops increasing the x or y coordinate
        if event.key == pygame.K_w:
            smoothy = 0
        if event.key == pygame.K_s:
            smoothy = 0 
        if event.key == pygame.K_a:
            smoothx = 0
        if event.key ==pygame.K_d:
            smoothx = 0

    if event.type == pygame.KEYDOWN: #Checks for a keypress
        print(event)
        if  event.key == pygame.K_w:
            smoothy -= 5 #reduces the y by 5 so player moves up
        if  event.key == pygame.K_s:
            smoothy += 5 #increases the y by 5 so player moves down
        if  event.key == pygame.K_a:
            smoothx -= 5 #reduces the x by 5 so player moves left
        if  event.key == pygame.K_d:
            smoothx += 5 #increases the x by 5 so player moves right

Tags: thetokey用户eventbyifso
2条回答

我通常以这种方式处理移动:如果按下一个键,我将x和y速度设置为所需的值,并更新主循环中的位置。为了停止左右移动,在将值设置回0之前,我还要检查字符是向左移动还是向右移动smoothx > 0。如果同时按下左键和右键,则角色不会停止。你知道吗

if event.type == pygame.KEYDOWN:
    if  event.key == pygame.K_w:
        smoothy = -5
    elif  event.key == pygame.K_s:
        smoothy = 5
    elif  event.key == pygame.K_a:
        smoothx = -5
    elif  event.key == pygame.K_d:
        smoothx = 5
elif event.type == pygame.KEYUP:
    if event.key == pygame.K_w and smoothy < 0:
        smoothy = 0
    elif event.key == pygame.K_s and smoothy > 0:
        smoothy = 0
    elif event.key == pygame.K_a and smoothx < 0:
        smoothx = 0
    elif event.key ==pygame.K_d and smoothx > 0:
        smoothx = 0

对于上下运动来说,这没什么大不了的,因为同时按下上下两个按钮是非常困难的,当然你也可以检查一下。你知道吗

KEYUP中使用add/substract,就像在KEYDOWN中一样,但使用相反的符号。你知道吗

    if event.type == pygame.KEYUP:
        print(event)
        if event.key == pygame.K_w:
            smoothy += 5
        if event.key == pygame.K_s:
            smoothy -= 5 
        if event.key == pygame.K_a:
            smoothx += 5
        if event.key ==pygame.K_d:
            smoothx -= 5

    if event.type == pygame.KEYDOWN: #Checks for a keypress
        print(event)
        if  event.key == pygame.K_w:
            smoothy -= 5 #reduces the y by 5 so player moves up
        if  event.key == pygame.K_s:
            smoothy += 5 #increases the y by 5 so player moves down
        if  event.key == pygame.K_a:
            smoothx -= 5 #reduces the x by 5 so player moves left
        if  event.key == pygame.K_d:
            smoothx += 5 #increases the x by 5 so player moves right

相关问题 更多 >