1
fps = 90
fpsclock = pygame.time.Clock()  
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
            a = False
        keys = pygame.key.get_pressed()
        if keys[pygame.K_SPACE]:
            #pygame.key.set_repeat(10,50)
            is_jump = True
            if True and icony <=340:
                icony -= jump_count**2*0.4*direction
                jump_count -= 1
                if jump_count == 0 :
                    direction = direction*-1
                if jump_count == -10:
                    is_jump = False 
                    jump_count = 10
                    direction = 1
                    icony = 340
            pygame.time.delay(10)
    fpsclock.tick(fps)
    pygame.display.update()

I want it to jump and then to stop for a second or so and then jump again instad of jumping continuously . please help

1 Answers1

0

It is a matter of Indentation. The code which performs the jump has to be executed in the application loop instead of the event loop.
Apart from this there are some logical problems in your code. See How can I do a double jump in pygame? or python, pygame - jumping too fast?

fps = 90
fpsclock = pygame.time.Clock()  
run = True
while run:
    for event in pygame.event.get():
        if event.type == QUIT:
            run = False
    
    #INDENTATION
    #<--|
    keys = pygame.key.get_pressed()
    if not is_jump and keys[pygame.K_SPACE]:
        is_jump = True
    if is_jump:
        icony -= jump_count**2*0.4*direction
        jump_count -= 1
        if jump_count == 0:
            direction = direction*-1
        if jump_count == -10:
            is_jump = False 
            jump_count = 10
            direction = 1
            icony = 340

    # [...]

    pygame.display.update()
    fpsclock.tick(fps)

Minimal example:

import pygame, sys
from pygame.locals import * 

pygame.init()
screen = pygame.display.set_mode((500, 500))

is_jump = False 
jump_count = 10
direction = 1
icony = 340

fps = 90
fpsclock = pygame.time.Clock()  
run = True
while run:
    for event in pygame.event.get():
        if event.type == QUIT:
            run = False
    
    keys = pygame.key.get_pressed()
    if not is_jump and keys[pygame.K_SPACE]:
        is_jump = True
    if is_jump:
        icony -= jump_count**2*0.4*direction
        jump_count -= 1
        if jump_count == 0:
            direction = direction*-1
        if jump_count == -10:
            is_jump = False 
            jump_count = 10
            direction = 1
            icony = 340

    screen.fill(0)
    pygame.draw.circle(screen, (255, 0, 0), (250, icony), 10)
    pygame.display.update()
    fpsclock.tick(fps)

pygame.quit()
sys.exit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174