Making Player Jump in Pygame

↘锁芯ラ 提交于 2019-12-25 07:34:01

问题


I'm making a very simple game using pygame to practice. Im trying to make my "player" jump when spacebar is pressed. Here is the loop code I'm trying:

x,y=10,300
movex,movey=0,0


if event.type==KEYDOWN:
    if event.key==K_LEFT:
        movex = -0.2
    elif event.key==K_RIGHT:
        movex=+0.2
    elif event.key==K_DOWN:
        movey=+0.2
    elif event.key==K_SPACE:
        movey=-0.4
        movey=+0.4

if event.type==KEYUP:
        if event.key==K_LEFT:
            movex = 0
        elif event.key==K_RIGHT:
            movex=0
        elif event.key==K_DOWN:
            movey=0
        elif event.key==K_SPACE:
            movey=0


x+=movex
y+=movey
screen.blit(player,(x,y))

The controls work except for the jump part (when I press the spacebar). It just like slides the player down. Can anyone tell me why it doesn't work and how to fix it?


回答1:


It doesn't work because on KEYDOWN event you are doing:

    movey=-0.4
    movey=+0.4

which in the end is equivalent to movey=0. To fix it you should do only movey=+0.4 and then on KEYUP it will revert to 0.



来源:https://stackoverflow.com/questions/16640239/making-player-jump-in-pygame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!