0

here is my code which tries to update image on pygame screen after pressing numerical buttons (from 0 to 9)

import pygame
from pygame.locals import  *
import sys
pygame.init()
Width,Height =500,500
screen =pygame.display.set_mode((Width,Height))

img = pygame.image.load("Game_Images/Taxi.png")
x =Width //2
y =Height //2
screen.fill((255,255,255))
screen.blit(img,(Width//2,Height//2))
while True:
    # screen.fill((255,255,255))
    # screen.blit(img,(Width//2,Height//2))
    for event in pygame.event.get():
        if event.type ==QUIT:
            sys.exit()
        if event.type ==KEYDOWN:
            if event.key ==pygame.K_0:
                print("Stay")
            elif event.key ==pygame.K_1:
                x =x+1
                y =y+1
                screen.blit(img, (x, y))
                pygame.display.update()

            elif event.key ==pygame.K_2:
                x = x + 2
                y = y + 2
                screen.blit(img, (x, y))
                pygame.display.update()
            elif event.key ==pygame.K_3:
                x = x + 3
                y = y + 3
                screen.blit(img, (x, y))
                pygame.display.update()
            elif event.key ==pygame.K_4:
                x = x + 4
                y = y + 4
                screen.blit(img, (x, y))
                pygame.display.update()
            elif event.key ==pygame.K_5:
                x = x + 5
                y = y + 5
                screen.blit(img, (x, y))
                pygame.display.update()
            elif event.key ==pygame.K_6:
                x = x + 6
                y = y + 6
                screen.blit(img, (x, y))
                pygame.display.update()
            elif event.key ==pygame.K_7:
                x = x + 7
                y = y + 7
                screen.blit(img, (x, y))
                pygame.display.update()
            elif event.key ==pygame.K_8:
                x = x + 8
                y = y + 8
                screen.blit(img, (x, y))
                pygame.display.update()
            elif event.key ==pygame.K_9:
                x = x + 9
                y = y + 9
                screen.blit(img, (x, y))
                pygame.display.update()

    pygame.display.update()

original image is this : Original image

when i have pressed several times to 9 for insance, i got this result : Updated

how can i fix it? it seems that code makes reflection of the original image, actual movement is definitely done by updating coordinates right?so why it keeps original image?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
data science
  • 215
  • 9
  • it does not answer my question , it still duplicates and leaves tail – data science May 24 '23 at 07:28
  • 2
    The answer in the linked question applies to your problem 100%. Literally the first sentence: *You have to clear the display in every frame with pygame.Surface.fill*. – sloth May 24 '23 at 07:55
  • and what should be parameter? – data science May 24 '23 at 07:57
  • 1
    Of course, the duplicate answers the question. You need to draw the background in each frame. You have commented out `screen.fill((255,255,255))`. That is the reason for your problem. – Rabbid76 May 24 '23 at 08:41

0 Answers0