Python Forum
[PyGame] object's movement to left leave shadow on the screen
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] object's movement to left leave shadow on the screen
#1
Hello everyone. I've wrote a simple code using pygame and I want my image to move right and left and after collision to wall change direction. The problem is that when I run it in movement to left it has some shadow! in screen. It does move but it leaves something like a shadow of itself on the screen. What would be the problem?

here is my code:
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')
playerX=370
playerY=400
def player(x,y):
    screen.blit(playerImg,(x,y))

running=True
while running:
    screen.fill((0,255,0))
    
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            running=False
    playerX+=0.2
    player(playerX,playerY)
    if playerX<=0:
        playerX+=0.2
        player(playerX,playerY)
    elif playerX>=746:
        while playerX>=0:
            playerX-=0.2
            player(playerX,playerY)
            pygame.display.update()
    pygame.display.update()
Reply
#2
Line 28. Why is there another while loop inside your event loop?
Reply
#3
Because I want it to move right again when it hits the left wall.
Reply
#4
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 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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] When I hit the space bar from the home screen, it sends me to the game over screen JesusisKing 1 953 Apr-30-2023, 10:11 PM
Last Post: deanhystad
  Laggy Game Movement game_slayer_99 12 4,218 Oct-05-2022, 11:34 AM
Last Post: metulburr
  [PyGame] Particle movement mystery XavierPlatinum 10 2,872 Jul-09-2022, 04:28 AM
Last Post: deanhystad
  [PyGame] Isometric Movement on Tiled Map Josselin 0 2,324 Nov-02-2021, 06:56 AM
Last Post: Josselin
  Using classes for room movement (text game) Lawr3y 3 6,484 Aug-20-2019, 12:40 AM
Last Post: Lawr3y
  Problem with coding for movement keys Aresofthesea 3 3,429 Jul-05-2019, 07:05 PM
Last Post: nilamo
  Pygame Movement X/Y coord TheHumbleIdiot 2 3,475 Mar-19-2019, 02:21 PM
Last Post: TheHumbleIdiot
  Movement after KEYUP, only after pause func esteel 2 3,249 Aug-22-2018, 03:03 PM
Last Post: Windspar
  [PyGame] How to stop the sprite's movement when it touches the edges of the screen? mrmn 5 11,446 May-13-2018, 06:33 PM
Last Post: mrmn

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020