跳得太快? [英] jumping too fast?

查看:16
本文介绍了跳得太快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩 pygame,并试图创建一个简单的跳跃功能(还没有物理).

I am messing around with pygame, and trying to create a simple jumping function (no physics yet).

由于某种原因,我的跳转"在显示中不可见,即使我使用的值打印出来并且似乎按预期工作.我可能做错了什么?

For some reason my "jumps" are not visible in the display, even though the values I am using print out and seem to be working as intended. What could I be doing wrong?

isJump = False
jumpCount = 10
fallCount = 10

if keys[pygame.K_SPACE]:
    isJump = True
if isJump:
    while jumpCount > 0:
        y -= (jumpCount**1.5) / 3
        jumpCount -= 1
        print(jumpCount)
    while fallCount > 0:
        y += (fallCount**1.5) / 3
        fallCount -= 1
        print(fallCount)
    else:
        isJump = False
        jumpCount = 10
        fallCount = 10
        print(jumpCount, fallCount)

win.fill((53, 81, 92))
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.display.update()

我缩短了代码量,但我认为这就是问题所在.

I shortened the amount of code, but I think this is all that is related to the problem.

推荐答案

您必须将 while 循环转换为 if 条件.您不想在一帧中完成完整的跳转.
你必须每帧做一个跳跃的步骤".使用主应用程序循环执行跳转.

You've to turn the while loops to if conditions. You don't want to do the complete jump in a single frame.
You've to do a single "step" of the jump per frame. Use the main application loop to perform the jump.

看例子:

import pygame

pygame.init()
win = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()

isJump = False
jumpCount, fallCount = 10, 10
x, y, width, height = 200, 300, 20, 20

run = True
while run:
    clock.tick(20)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    keys = pygame.key.get_pressed()

    if keys[pygame.K_SPACE]:
        isJump = True
    if isJump:
        if jumpCount > 0:
            y -= (jumpCount**1.5) / 3
            jumpCount -= 1
            print(jumpCount)
        elif fallCount > 0:
            y += (fallCount**1.5) / 3
            fallCount -= 1
            print(fallCount)
        else:
            isJump = False
            jumpCount, fallCount = 10, 10
            print(jumpCount, fallCount)

    win.fill((53, 81, 92))
    pygame.draw.rect(win, (255, 0, 0), (x, y, width, height)) 
    pygame.display.flip()

这篇关于跳得太快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆