May-10-2022, 07:03 PM
(This post was last modified: May-10-2022, 07:03 PM by Gribouillis.)
I humanized the bouncing balls and made it 25 lines long.
import tkinter, time, math, random, dataclasses Ball = dataclasses.make_dataclass('Ball', ('shape', 'x', 'y', 'spdx', 'spdy', 'color')) def onTimer(): for i, ball in enumerate(ballstats): ball.x += ball.spdx ball.y += ball.spdy for other in ballstats[i+1:]: dist = math.sqrt((ball.x - other.x)**2 + (ball.y - other.y)**2) if dist < balldiameter: ball.spdx, other.spdx = other.spdx, ball.spdx ball.spdy, other.spdy = other.spdy, ball.spdy if ball.x < 0 or ball.x > winWid - balldiameter: ball.spdx = - ball.spdx if ball.y < 0 or ball.y > winHei - balldiameter: ball.spdy = - ball.spdy canvas.coords(ball.shape, (ball.x, ball.y, ball.x + balldiameter, ball.y + balldiameter)) timerhandle = window.after(2,onTimer) winWid, winHei = 640, 480 balldiameter = 7 window = tkinter.Tk() canvas = tkinter.Canvas(window, width=winWid, height=winHei, bg="white") canvas.pack() ballstats = [Ball(None, random.randrange(0, winWid), random.randrange(0, winHei), random.random() * 2 - 1, random.random() * 2 - 1, "#" + ''.join(random.choices("0123456789ABCDEF", k=6))) for i in range(100)] for ball in ballstats: ball.shape = canvas.create_oval(ball.x, ball.y, ball.x + balldiameter, ball.y + balldiameter, outline=ball.color, fill=ball.color) timerhandle = window.after(2,onTimer) window.mainloop()Some lines are too long. Normally I don't write lines larger than 80 characters.