简体   繁体   English

Pygame:同时触发KEYDOWN和KEYUP导致没有净移动

[英]Pygame: KEYDOWN and KEYUP being triggered at the same time causing no net movement

I've made it so when a user clicks WASD it makes smooth_x or smooth_y to 5 to constantly add to their x or y coordinates to simulate motion. 我这样做的目的是,当用户单击WASD时,它将smooth_x或smooth_y设置为5,以不断添加到其x或y坐标以模拟运动。

However I have a problem where if the user is holding down A then they click D at the same time, it causes smooth_x to be 0, causing the user to stay in place. 但是我有一个问题,如果用户按住A键,然后他们同时单击D键,则会导致smooth_x为0,从而导致用户停留在原位。

EG User Clicks D (smooth_x = 5) User Clicks A (smooth_x = -5) User is holding D then holds A then lets go of D resulting in smooth_x being = 0 causing the user to stop moving which I don't want. EG用户单击D(smooth_x = 5)用户单击A(smooth_x = -5)用户按住D然后按住A,然后放开D,导致smooth_x = 0,导致用户停止移动,这是我不希望的。 In this scenario smooth_x should be = -5 在这种情况下,smooth_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

Use add/substract in KEYUP like you do in KEYDOWN but with the opposite sign. 像在KEYDOWN一样,在KEYUP使用加/减。

    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

I usually handle the movement in this way: I set the x- and y-velocity to the desired value if a key was pressed and update the position in the main loop. 我通常以这种方式处理运动:如果按下了某个键,我将x和y速度设置为所需的值,并更新了主循环中的位置。 To stop the left-right movement, I also check if the character is moving to the left or to the right smoothx > 0 before I set the value back to 0. Then the character won't stop if you press both the left and right key at the same time. 要停止左右移动,在将值重新设置为smoothx > 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

For the up-down movement it doesn't matter so much, because it's very hard to press both up and down at the same time, but of course you can check that as well to be sure. 对于上下运动来说,没什么大不了的,因为很难同时按下上下两个按钮,但是当然您也可以进行确认。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM