Python Forum
How can I get rid of the line following the sprites? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: How can I get rid of the line following the sprites? (/thread-1487.html)



How can I get rid of the line following the sprites? - Houston11 - Jan-06-2017

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)



RE: How can I get rid of the line following the sprites? - Mekire - Jan-06-2017

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