Skip to content

Instantly share code, notes, and snippets.

@nst
Last active November 25, 2024 13:21
Show Gist options
  • Save nst/2afd9bfdf9698bdf5b7109841378d413 to your computer and use it in GitHub Desktop.
Save nst/2afd9bfdf9698bdf5b7109841378d413 to your computer and use it in GitHub Desktop.
import pygame as p
import random
R,r,W,H,G,N=range,random.randrange,480,320,3,5
g=[[r(W),r(H),r(W),r(H)]+[r(-9,9,2)for _ in R(4)] for _ in R(G)]
p.init()
s=p.display.set_mode((W,H))
f=lambda n,d,L:(n+(d:=d*(-1 if n+d not in R(L)else 1)),d)
while 1:
s.fill([0]*3)
for i,l in enumerate(g):
for j in R(N):
for k in R(4):
l[k],l[k+4]=f(l[k],l[k+4],W if k%2==0 else H)
g[i]=l.copy()if j==0 else g[i]
p.draw.line(s,[255]*3,l[0:2],l[2:4])
p.display.update()
p.event.get()
p.time.delay(100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment