Aug-02-2020, 09:59 PM
Look closely at that, and try to rethink how you can do it.
That inner-while loop is drawing the player over and over in multiple places, without ever clearing the screen. That's why you have the shadow... you never clear the screen.
In general, if you have the line
For example, if you use a direction/velocity variable to keep track of which direction it should be moving (instead of a hardcoded +0.2 at the start of your event loop), you can just change the direction. Something like this (I don't have your images, so I changed the player to a white box):
That inner-while loop is drawing the player over and over in multiple places, without ever clearing the screen. That's why you have the shadow... you never clear the screen.
In general, if you have the line
pygame.display.update()
in more than one place, there's probably a better way to organize your code. For example, if you use a direction/velocity variable to keep track of which direction it should be moving (instead of a hardcoded +0.2 at the start of your event loop), you can just change the direction. Something like this (I don't have your images, so I changed the player to a white box):
import pygame pygame.init() screen=pygame.display.set_mode((800,600)) pygame.display.set_caption('visual rhythm') #icon=pygame.image.load("song.png") #pygame.display.set_icon(icon) #playerImg=pygame.image.load('spaceship.png') playerImg = pygame.Surface((10, 10)) playerImg.fill((255, 255, 255)) playerX=370 playerY=400 def player(x,y): screen.blit(playerImg,(x,y)) directionX = 0.2 running=True while running: screen.fill((0,255,0)) for event in pygame.event.get(): if event.type==pygame.QUIT: running=False playerX += directionX player(playerX,playerY) if playerX<=0: directionX = 0.2 player(playerX,playerY) elif playerX>=746: directionX = -0.2 pygame.display.update()