jumping too fast?

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

See the example:

gwhct

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()


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x