1

i am creating a clone of chrome dino game and when i press the space button the player jumps but when it comes down and touches the ground i does not jumps automatically even if the key is still pressed i want it to jump automatically if the key still pressed the necassary code is below if someone wants the full code tell me and also i want a to add a pause menu in the game so can someone tell me function similar to time.sleep() except instead of delaying the progam upto given number of second it dealys the program till a certain condition is true or something like that or i'll have to do it manually

class player(pygame.sprite.Sprite):
        def __init__(self, position):
            pygame.sprite.Sprite.__init__(self)
            # settings
            self.position = position
            self.index = 0
            self.timer = 0
            self.allow_jump = True
            self.velocity = 0
            self.gravity = 0.7
            self.cooldown = 30
            #imgaes
            self.player_list = [pygame.image.load(f"/home/kali_linux/Python code/pygame_projects/pixel_runner/images/player_img_{i}.png") for i in range(2)]
            self.jump_image = pygame.image.load( r"/home/kali_linux/Python code/pygame_projects/pixel_runner/images/player_jump_img.png")
            self.image = self.player_list[self.index]
            self.rect = self.image.get_rect(bottomleft=self.position)
            self.rect.x = 50
    
        def update(self):
            # movement
            if not window.game_over:
                self.rect.y += self.velocity
                self.velocity += self.gravity
               
                if window.score >= 100:
                    self.gravity = 1
            # animation
                self.timer += 1
                if self.timer >= self.cooldown:
                    self.index += 1
                    self.timer = 0
                if self.index >= len(self.player_list):
                    self.index = 0
                if self.rect.bottom >= 450:
                    self.rect.bottom = 450
                    self.allow_jump = True
                if self.rect.bottom < 450:
                    self.allow_jump = False
                    self.image = self.jump_image
            if pygame.sprite.spritecollide(self, obstacle_group, False, pygame.sprite.collide_mask) or pygame.sprite.spritecollide(self, flies_group, False, pygame.sprite.collide_mask):
                window.game_over = True
            self.image = self.player_list[self.index]

    def game_loop():
        # timers
        global obstacle_cooldown
        global obstacle_timer
        global flies_cooldown
        global flies_timer
        obstacle_timer = 0
        obstacle_cooldown = 150
        flies_cooldown = 200
        flies_timer = 0
        while True:
            window.draw_screen()
            
            alien_group.draw(window.screen)
            alien_group.update()
            
            flies_group.draw(window.screen)
            flies_group.update() 
            
            obstacle_timer += 1
            flies_timer += 1
            if obstacle_timer >= obstacle_cooldown and len(obstacle_group) <= 5:
                create_obstacle()
                obstacle_timer = 0
            obstacle_group.draw(window.screen)
            obstacle_group.update()
       
            
            
            if window.score >= 100 and flies_timer >= flies_cooldown and len(flies_group) <= 5:
                create_flies()
                flies_timer = 0
                flies_cooldown = random.randint(100,200)
            
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE and alien.allow_jump:
                        alien.velocity = -17
                if event.type == pygame.MOUSEBUTTONDOWN:
                    pos = pygame.mouse.get_pos()
                    if alien.rect.collidepoint(pos) and alien.allow_jump:
                        alien.velocity = -18
Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

2

Use pygame.key.get_pressed() instead of the pygame.KEYDOWN event. 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 1, otherwise 0. e.g.:

while True:
    # [...]

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        # DELETE
        #if event.type == pygame.KEYDOWN:
        #    if event.key == pygame.K_SPACE and alien.allow_jump:
        #        alien.velocity = -17
        if event.type == pygame.MOUSEBUTTONDOWN:
            if alien.rect.collidepoint(event.pos) and alien.allow_jump:
                alien.velocity = -18

    keys = pygame.key.get_pressed()
    if keys[pygame.K_SPACE] and alien.allow_jump:
        alien.velocity = -18
        alien.allow_jump = False

    # [...]

Also see How to get keyboard input in pygame? and How can I make a sprite move when key is held down

Rabbid76
  • 202,892
  • 27
  • 131
  • 174