简体   繁体   English

如何在 Pygame 中为虚线设置动画

[英]How do I animate a dotted line in Pygame

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.但是我试图在 Class 中为那条线设置动画,因此它似乎在不断向下移动。 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.由于这个链接很安静并且没有得到我希望的反馈,我在 Pygame 中玩了更多,最后,只在道路线上使用了精灵图像。 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.我能够在他们自己的班级中使用大约 4 个独立的,从屏幕顶部的不同 rect.y 位置开始,并让 self.speedy 对他们所有人都一样,然后当到达屏幕底部时,所有从 -20 y 位置重新开始。 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.我仍然希望有人能告诉我如何在一个班级中做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM