'Pygame screen doesn't seem to refresh

I've been trying to make a Chrome Dino Game, however, I'm struggling with this problem: On every frame, it should draw a new one at the new position and delete the previous one to make it look as if it's moving. HOWEVER, it remains at its previous position and a new image appears on its next position. I did write the pygame.display.update() code at the end of my maintop. In the last time I ran into a similar problem, I managed to make it work by drawing a background image, but this time, it doesn't work.

following are my codes:

import pygame
import os
from random import randint
import schedule

pygame.init()

assets = os.path.join(os.path.dirname(__file__), "Assets")

screen_size = (screen_width, screen_height) = (1280, 720)
screen = pygame.display.set_mode(screen_size)

clock = pygame.time.Clock()
fps = 120

bg = pygame.image.load(os.path.join(assets, "IMG_15.png"))

ground = 700

running = True

spacebaridx = 0
gamestart = False
tick_on_start = 0

obs1 = pygame.image.load(os.path.join(assets, "colourmat/light_green.png"))
pygame.transform.scale(obs1, (100, 200))

obs2 = pygame.image.load(os.path.join(assets, "colourmat/light_green.png"))
pygame.transform.scale(obs2, (120, 200))

obs3 = pygame.image.load(os.path.join(assets, "colourmat/light_green.png"))
pygame.transform.scale(obs3, (150, 200))

ls_obs = []

def create_obs():
    k = randint(1, 3)
    if k == 1:
        info = {"type":1, "img":obs1, "x":screen_width, "y":ground - 200, "tox":2}
        ls_obs.append(info)
    if k == 2:
        info = {"type":2, "img":obs2, "x":screen_width, "y":ground - 200, "tox":2}
        ls_obs.append(info)
    else:
        info = {"type":3, "img":obs3, "x":screen_width, "y":ground - 200, "tox":2}
        ls_obs.append(info)

schedule.every(3).seconds.do(create_obs)

while running:
    dt = clock.tick(fps)

    if gamestart == True:
        game_ticks = pygame.time.get_ticks() - tick_on_start
        schedule.run_pending()
    else:
        game_ticks = pygame.time.get_ticks()


    for event in pygame.event.get():
        if event.type == pygame.QUIT: running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                if spacebaridx == 0: # Press space to start / to tell whether it's the first press
                    spacebaridx += 1
                    gamestart = True
                    tick_on_start = pygame.time.get_ticks()
                else:
                    pass # Jump

    for o in ls_obs:
        o["x"] += o["tox"] * -1

    screen.blit(bg, (0, 0))
    for o in ls_obs:
        screen.blit(o["img"], (o["x"], o["y"]))

    pygame.display.update()

pygame.quit()


Solution 1:[1]

This issue is occurring because you aren't clearing the display within each frame. In pygame, in order to clear the display, we need to use the fill method. So in your code, at the top of your game loop before the event loop, add screen.fill((0, 0, 0)). This will fill your screen in the color black. Don't worry, the black won't be shown if you draw the background on top of it. Now, when you add a new image, the previous images won’t be displayed.

Modified Game Loop

while running:
    screen.fill((0, 0, 0))
    dt = clock.tick(fps)

    if gamestart == True:
        game_ticks = pygame.time.get_ticks() - tick_on_start
        schedule.run_pending()
    else:
        game_ticks = pygame.time.get_ticks()


    for event in pygame.event.get():
        if event.type == pygame.QUIT: running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                if spacebaridx == 0: # Press space to start / to tell whether it's the first press
                    spacebaridx += 1
                    gamestart = True
                    tick_on_start = pygame.time.get_ticks()
                else:
                    pass # Jump

    for o in ls_obs:
        o["x"] += o["tox"] * -1

    screen.blit(bg, (0, 0))
    for o in ls_obs:
        screen.blit(o["img"], (o["x"], o["y"]))

    pygame.display.update()

pygame.quit()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Random Davis