Python Forum
How can I get rid of the line following the sprites?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I get rid of the line following the sprites?
#1
So I've been following along with a tutorial I found online, basically this code allows me to spawn in sprites and have them move across the screen. I'm trying to figure out how to remove the lines following the sprites, is there a way to do so?

import pygame,random
 
 
class Player(pygame.sprite.Sprite):
    def __init__(self, *groups):
        super(Player, self).__init__(*groups)
        self.image = pygame.image.load('images/zombie.png')
        self.rect = pygame.rect.Rect((screen_width, (random.randrange(0,screen_height))), self.image.get_size())
        self.dx = -3
        self.pos = random.randrange(0,screen_height)
       
    def update(self):
       self.rect.centerx += self.dx
 
       if self.rect.right < 0:
           self.kill()
 
       
 
class Game(object):
    def main(self, screen):
        clock = pygame.time.Clock()
 
        sprites = pygame.sprite.Group()
        self.player = Player(sprites)
 
        while 1:
            clock.tick(40)
            numberAlien = 5

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    return
                if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                    return


                sprites.update()
                sprites.draw(screen)


                drone = Player()
                if sprites.__len__() < numberAlien:
                    self.y = random.randrange(0,screen_height)
                    sprites.add(drone)

                pygame.display.flip()

if __name__ == '__main__':
   pygame.init()
   screen_width = 700
   screen_height = 500
   screen = pygame.display.set_mode((screen_width, screen_height))
   Game().main(screen)
Reply
#2
Two main problems.  You aren't clearing the screen (this is causing the "lines") and you are updating and drawing in the event loop.

Here is a quick rough fix:
Also please look here for an example of a base pygame program:
Pygame Template
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pygame, sprites, and rects menator01 12 1,707 Dec-07-2023, 02:37 AM
Last Post: Benixon
  [PyGame] Sprites just randomly appear and dissapear in my pygame.sprite.GoupeSingle trueShadoWrr 2 1,941 Feb-13-2023, 09:34 AM
Last Post: Vadanane
  [PyGame] I found a way to generate sprites without .blit. Is it effecient? xBlackHeartx 19 8,232 Dec-07-2019, 01:06 PM
Last Post: metulburr
  [PyGame] Having 4 players(Sprites) all being able to jump ElijahCastle 5 3,927 May-07-2019, 05:04 PM
Last Post: SheeppOSU
  Sprites and Actor error ajlconsulting 6 9,209 Jan-30-2019, 12:50 AM
Last Post: metulburr
  draw not showing all sprites ethanstrominger 0 2,554 Jan-25-2019, 10:10 PM
Last Post: ethanstrominger
  [PyGame] move randomly sprites reutB 4 8,170 Mar-29-2017, 01:12 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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