簡體   English   中英

Pygame:同時觸發KEYDOWN和KEYUP導致沒有凈移動

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

我這樣做的目的是,當用戶單擊WASD時,它將smooth_x或smooth_y設置為5,以不斷添加到其x或y坐標以模擬運動。

但是我有一個問題,如果用戶按住A鍵,然后他們同時單擊D鍵,則會導致smooth_x為0,從而導致用戶停留在原位。

EG用戶單擊D(smooth_x = 5)用戶單擊A(smooth_x = -5)用戶按住D然后按住A,然后放開D,導致smooth_x = 0,導致用戶停止移動,這是我不希望的。 在這種情況下,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

像在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

我通常以這種方式處理運動:如果按下了某個鍵,我將x和y速度設置為所需的值,並更新了主循環中的位置。 要停止左右移動,在將值重新設置為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

對於上下運動來說,沒什么大不了的,因為很難同時按下上下兩個按鈕,但是當然您也可以進行確認。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM