2

hello i am currently trying to make a jumping game in pygame a lot like the chrome dino game. i have made some simple code to draw a square and make it jump. i will my code dow n bellow. my problem is with the jumping part. whenever i press w wichis the the jump button the square jumps multiple times(usauly 2 times). good people of stack overflow please help a man in need. here is my code

import pygame
    
pygame.init()

screen_width = 500
screen_height = 400
isJump = False
y = 350
x = 50
BLUE=(0,0,255)
run = True

screen = pygame.display.set_mode((screen_width, screen_height))
screen.fill((0,0,0))
pygame.display.set_caption("syoma n9ot intelent")
pygame.draw.rect(screen,BLUE,(x,y,50,50))

while run:
    pygame.display.flip()
    for event in pygame.event.get():
        keys = pygame.key.get_pressed()
        if keys[pygame.K_w]:
        for a in range (1,250): 
            y -= .5
            screen.fill((0,0,0))
            pygame.draw.rect(screen,BLUE,(x,y,50,50))
            pygame.display.flip()

            
        for a in range (1,250):
            y += .5
            screen.fill((0,0,0))
            pygame.draw.rect(screen,BLUE,(x,y,50,50))
            pygame.display.flip()

        if event.type == pygame.QUIT:
            run = False
            
pygame.quit() 
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Syoma
  • 85
  • 1
  • 1
  • 6

1 Answers1

1

Use the KEYDOWN event instead of pygame.key.get_pressed().

pygame.key.get_pressed() returns a sequence with the state of each key. If a key is held down, the state for the key is True, otherwise False. Use pygame.key.get_pressed() to evaluate the current state of a button and get continuous movement.

The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN event occurs once every time a key is pressed. KEYUP occurs once every time a key is released. Use the keyboard events for a single action or a step-by-step movement.

Use pygame.time.Clock to control the frames per second and thus the game speed.

The method tick() of a pygame.time.Clock object, delays the game in that way, that every iteration of the loop consumes the same period of time. See pygame.time.Clock.tick():

This method should be called once per frame.

That means that the loop:

clock = pygame.time.Clock()
run = True
while run:
   clock.tick(100)

runs 100 times per second.

Do not control the game with an extra loop in the application or event loop. Use the application loop. Use the variables isJump and jumpCount to control the jump. Set the variable isJump = True and jumpCount = 20 when w is started pressed. Decrement jumpCount in the application loop and change the y position of the player. Set isJump = False if jumpCount == -20:


Complete example:

import pygame

pygame.init()

screen_width = 500
screen_height = 400
isJump = False
jumpCount = 0
y = 350
x = 50
BLUE=(0,0,255)
run = True

screen = pygame.display.set_mode((screen_width, screen_height))
clock = pygame.time.Clock()
pygame.display.set_caption("syoma n9ot intelent")
pygame.draw.rect(screen,BLUE,(x,y,50,50))

while run:
    clock.tick(100)
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                isJump = True
                jumpCount = 20

    if isJump:
        if jumpCount > 0:
            y -= 5
        elif jumpCount <= 0:
            y += 5
        jumpCount -= 1
        if jumpCount == -20:
            isJump = False
          
    screen.fill((0,0,0))
    pygame.draw.rect(screen,BLUE,(x,y,50,50))
    pygame.display.flip()    
Rabbid76
  • 202,892
  • 27
  • 131
  • 174