0

I am new to pygame and am trying to code the dino game that plays when internet is down on google. I've noticed that whenever I stop moving my mouse on the animation window, the frame rate drops to 0.I've looked around on the net and no one seems to have spoken about the issue before (or maybe im not getting my keywords down).

How do i get it to keep moving the same way irrespective of whether or not my mouse is in motion on the animation window?

Here's my code:

import pygame
from sys import exit


pygame.init()
pygame.display.set_caption("Runner")
clock = pygame.time.Clock()
display_surface = pygame.display.set_mode((800, 400))

#data values
score = 0
x_bound = 800
y_bound = 400
floor_y = 300
speed = 10
player_gravity = 0

test_font = pygame.font.Font('font/Pixeltype.ttf', 50)
score_surface = test_font.render(f'Score: {score}', False, (64, 64, 64))

sky_surface = pygame.image.load('graphics/Sky.png').convert()
ground_surface = pygame.image.load('graphics/ground.png').convert()
snail_surface = pygame.image.load('graphics/snail/snail1.png').convert_alpha()
player_surface = pygame.image.load('graphics/player/player_walk_1.png').convert_alpha()

#initializing rectangles
player_surface_rect = player_surface.get_rect(midbottom=(80, 300))
snail_surface_rect = snail_surface.get_rect(midbottom=(600, 300))
score_surface_rect = score_surface.get_rect(midtop=(400, 50))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

        if event.type == pygame.KEYDOWN:
            if event.type == 768: #768 --> spacebar
                player_gravity = -15


        display_surface.blit(sky_surface, (0, 0))
        display_surface.blit(ground_surface, (0, 300))

        #score 
        pygame.draw.rect(display_surface, '#c0e8ec', score_surface_rect)
        pygame.draw.rect(display_surface, '#c0e8ec', score_surface_rect, 5)
        display_surface.blit(score_surface, score_surface_rect)

        #snail
        snail_surface_rect.x -= speed
        if snail_surface_rect.right <= 0:
            snail_surface_rect.left = x_bound
        display_surface.blit(snail_surface, snail_surface_rect)

        #player
        #jump
        player_gravity += 1
        player_surface_rect.y += player_gravity

        #creating floor
        if player_surface_rect.bottom > floor_y:
            player_surface_rect.bottom = floor_y

        display_surface.blit(player_surface, player_surface_rect)

        
        pygame.display.update()
        clock.tick(60)



I looked around for modules on the net to stop this issue, but, failed to find any. I have been following along Clear Code's guide to pygame in this program

1 Answer 1

5

Your entire game logic is within your for event in pygame.event.get(): loop.

If there are no events (such as mouse movement), nothing happens.

Within your while True main loop, move everything from display_surface.blit(...) onwards one indentation level shallower, so they're out of the for event loop, i.e.

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            ...
        if event.type == pygame.KEYDOWN:
            ...

    display_surface.blit(sky_surface, (0, 0))
    ...
    pygame.display.update()
    clock.tick(60)
1
  • been trying to figure out the problem for a long time, thanks a lot boss!
    – Vrishank
    Commented Jul 9 at 14:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.