[英]python, pygame - jumping too fast?
I am messing around with pygame, and trying to create a simple jumping function (no physics yet).我在搞乱 pygame,并试图创建一个简单的跳跃 function(还没有物理)。
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.我缩短了代码量,但我认为这就是与问题相关的全部。
You've to turn the while
loops to if
conditions.您必须将while
循环转换为if
条件。 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.使用主应用程序循环来执行跳转。
See the example:请参阅示例:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.