Created
November 15, 2024 14:41
-
-
Save nst/1a7533e5021829aa0906eba7227617f6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
import random | |
WIDTH = 800 | |
HEIGHT = 600 | |
NB_GROUPS = 5 | |
HISTO_SIZE = 8 | |
LINE_COLOR = (255,255,255) | |
BKG_COLOR = (0,0,0) | |
REFRESH_MS = 100 | |
class MovingPoint(): | |
def __init__(self, x, y): | |
self.xs = [x] | |
self.ys = [y] | |
self.x_ = random.randint(-10, 10) | |
self.y_ = random.randint(-10, 10) | |
def move(self): | |
x = self.xs[0] + self.x_ | |
y = self.ys[0] + self.y_ | |
if not x in range(WIDTH): | |
self.x_ *= -1 | |
if not y in range(HEIGHT): | |
self.y_ *= -1 | |
self.xs = [x] + self.xs[:HISTO_SIZE] | |
self.ys = [y] + self.ys[:HISTO_SIZE] | |
points = [MovingPoint(random.randrange(WIDTH), random.randrange(HEIGHT)) for _ in range(NB_GROUPS*2)] | |
pygame.init() | |
screen = pygame.display.set_mode((WIDTH, HEIGHT)) | |
while True: | |
screen.fill(BKG_COLOR) | |
for p1, p2 in zip(points[::2], points[1::2]): | |
for i in range(len(p1.xs)): | |
pygame.draw.line(screen, LINE_COLOR, (p1.xs[i], p1.ys[i]), (p2.xs[i], p2.ys[i])) | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
pygame.quit() | |
[p.move() for p in points] | |
pygame.display.update() | |
pygame.time.delay(100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment