繁体   English   中英

Python PyGame跳转代码在不应该运行时运行

[英]Python PyGame jump code running when it shouldnt

我对此一无所获,但由于某种原因,当我按下按钮时,我的跳转代码一直保持运行状态,因此,即使不应该,它也可以使您飞行,请提供帮助。

    while not gameExit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_q:
                pygame.quit()
                quit()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
               x_change = -5
            elif event.key ==  pygame.K_RIGHT:
                x_change = 5
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                x_change = 0
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP and jumped == 0:
               y_change = -10
               jumped = 1
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                y_change = 0
    if x < 32:
        x += 5
    if x > (display_width-32) - char_width:
        x += -5
    if y < 32:
        y += 5
    if y > display_height - char_height:
        y = display_height - char_height
    if y < (display_height-32) - char_height:
        y += 5
    if y > (display_height-32) - char_height:
        jumped = 0
    x2 = x
    y2 = y + char_height
    x3 = x2 + (26)
    if gameDisplay.get_at((x2,y2)) == (0,0,255) or gameDisplay.get_at((x,y)) == (0,0,255) or gameDisplay.get_at((x3,y2)) == (0,0,255):
        y+= -5
        jumped = 0

(当您与地面碰撞时,变量“ jumped”将重置)

我们实际上没有足够的代码来判断,所以我现在只是猜测。 您在此处显示的代码修改了字符的y位置,使其“跳转”。 如果角色连续停留在该高度,则可能应该在其他位置修改y位置以使角色退回。

暂无
暂无

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

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