Reproducing an early 90s Macintosh screensaver with PyGame.
Originally created as a programming workshop for my kids.
import pygame as p
import random as m
R=range
r=m.randrange
S=(480,320)
def n(l,v):
for i in R(4):
v[i]*=-1+2*(l[i]+v[i]in R(S[i%2]))
return[l[i]+v[i]for i in R(4)]
V=[[r(-9,9,2)]*4 for _ in R(5)]
L=[[r(x)for x in S+S]for _ in R(5)]
p.init()
s=p.display.set_mode(S)
while 1:
L=[n(L[g],V[g])for g in R(5)]+L[:8*5]
s.fill([0]*3);[p.draw.line(s,[255]*3,l[0:2],l[2:4])for l in L]
p.display.update();p.event.get();p.time.delay(100)
Several versions:
https://gist.github.com/nst/b2b818c6c5c706b27ef6272a515ddc88 first take https://gist.github.com/nst/1a7533e5021829aa0906eba7227617f6 using a class https://gist.github.com/nst/65c1d4dbe207ec5ae269b767bbdd4a15 using no class https://gist.github.com/nst/7df6bb1e6a783de54e2da74921bdd4b8 using no class (shorter) https://gist.github.com/nst/3f987d236c9e7e7dffe1cf5f399bf7ab Mini version with 440 bytes
BlueSky post: https://bsky.app/profile/nst021.bsky.social/post/3lbamicbkes23
JavaScript version
https://gist.github.com/nst/f15776e81c16a72fe1d92b87440aed5a