so I am trying to animate a dotted line moving down the screen... like the white dashed lines moving past you on a road. It was easy enough for me to draw the dotted line:
import pygame
GREY = (211, 211, 211)
WHITE = (255, 255, 255)
pygame.init()
screen = pygame.display.set_mode(800, 600)
pygame.display.set_caption("Line Test")
clock = pygame.time.Clock()
running = True
while running:
clock.tick(60)
for event in pygame.event,get():
if event.type == pygame.QUIT:
running = False
all_sprites.update()
screen.fill(GREY)
dash = 0
for i in range(30):
pygame.draw.line(screen, WHITE, [400, 1 + dash], [400, dash + 10], 6)
dash += 20
all_sprites.draw(screen)
pygame.display.flip()
pygame.quit()
But I am trying to animate that line in a Class so it appears it is constantly moving down. So far no luck, only being able to get a single dash moving down the screen with:
class Line(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((6, 10))
self.image.fill(WHITE)
self.rect = self.image.get_rect()
self.last_uodate = pygame.time.get_ticks()
self.rect.x = 400
dash = 0
for i in range(30):
self.rect.y = 0 + dash
dash += 20
self.speedy = 5
def update(self):
self.rect.y += self.speedy
if self.rect.y > 600:
now = pygame.time.get_ticks()
if now - self.last_update > 0:
self.last_update = now
self.rect.x = 400
self.rect.y = 0
self.speedy = 5
all_sprites = pygame.sprite.Group()
line = pygame.sprite.Group()
for i in range(30):
ln = Line()
all_sprites.add(ln)
line.add(ln)
Any suggestion or tell me where I've gone wrong? This only produces single dash going down screen and not full dotted line top to bottom constantly moving.
As this link was quiet and didn't get the feedback I was hoping for, I played around some more in Pygame and in the end, just used sprite images on road lines. I was able to use about 4 separate ones each in their own Class starting at different rect.y locations off the top of the screen and have the self.speedy the same for them all and then when reaching off the bottom of the screen, all restarting from -20 y position. This gave the seamless flow of it all being on continuous road line moving down the screen. If anyone wants the code I used I'd be happy to post.
I would still like if someone could tell me how to do it within one Class.