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
#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


Messages In This Thread
RE: object's movement to left leave shadow on the screen - by nilamo - Aug-02-2020, 09:59 PM

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 1,012 Apr-30-2023, 10:11 PM
Last Post: deanhystad
  Laggy Game Movement game_slayer_99 12 4,417 Oct-05-2022, 11:34 AM
Last Post: metulburr
  [PyGame] Particle movement mystery XavierPlatinum 10 2,997 Jul-09-2022, 04:28 AM
Last Post: deanhystad
  [PyGame] Isometric Movement on Tiled Map Josselin 0 2,378 Nov-02-2021, 06:56 AM
Last Post: Josselin
  Using classes for room movement (text game) Lawr3y 3 6,608 Aug-20-2019, 12:40 AM
Last Post: Lawr3y
  Problem with coding for movement keys Aresofthesea 3 3,489 Jul-05-2019, 07:05 PM
Last Post: nilamo
  Pygame Movement X/Y coord TheHumbleIdiot 2 3,534 Mar-19-2019, 02:21 PM
Last Post: TheHumbleIdiot
  Movement after KEYUP, only after pause func esteel 2 3,305 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,639 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