Mr.R-Word
Mr.R-Word

Reputation: 21

Pygame dinosaur image keeps duplicating

I'm trying to make a dinosaur game in python (like the offline dino game in chrome). I want the dino to jump when space is pressed but when I press it,not only the image of dino is duped but it also doesn't come back.

import pygame
import time

pygame.init()

displayWidth = 700
displayHeight = 350

gameDisplay = pygame.display.set_mode((displayWidth,displayHeight))
pygame.display.set_caption("Dino-Run")

black = (0,0,0)
white = (255,255,255)

clock = pygame.time.Clock()
dinoimg = pygame.image.load("dino.png")

def dino(x,y):
    gameDisplay.blit(dinoimg,(x,y))

def gameloop():
    gameExit = False
    x = (displayWidth * 0.005)
    y = (displayHeight * 0.75)
    y_change = 0

    while not gameExit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    y_change = -5
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_RIGHT:
                    y_change = 0
            
            
        y += y_change
        dino(x,y)
        pygame.display.update()
        clock.tick(60)

Could someone please tell me how can I prevent the dino to dupe each time space is pressed and for the dino to come back to the ground.

Upvotes: 1

Views: 103

Answers (1)

Deepthought
Deepthought

Reputation: 118

You have to override everything before drawing new things to the screen.

Add this at the beginning of your loop:

gameDisplay.fill(color)

Upvotes: 1

Related Questions