Python Forum
Python 25 Line Challenge
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 25 Line Challenge
#26
Here is my next submission, one of my favorite demos... bouncing balls. from 150 lines, I managed to drop it down to 30 with the help a dictionary rather than multiple lists. I could get the last five, but that would tip it into unreadable.

import tkinter, time, math, random

def onTimer():
    if len(ballstats) < 100:
        ballstats.append({"shape": None, "x":random.randrange(0,winWid),"y":random.randrange(0,winHei), "spdx":(random.random() * 2)-1, "spdy":(random.random() * 2)-1})
        color = "#"
        for i in range(0,6): color = color + random.choice(["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"])
        ballstats[len(ballstats)-1]["shape"] = canvas.create_oval(ballstats[len(ballstats)-1]["x"],ballstats[len(ballstats)-1]["y"], ballstats[len(ballstats)-1]["x"]+balldiameter,ballstats[len(ballstats)-1]["y"]+balldiameter, outline=color, fill=color)
    for i in range(0,len(ballstats)):
        ballstats[i]["x"] = ballstats[i]["x"] + ballstats[i]["spdx"]
        ballstats[i]["y"] = ballstats[i]["y"] + ballstats[i]["spdy"]
        for j in range(i+1,len(ballstats)):
            dist = math.sqrt(( (ballstats[i]["x"]+(balldiameter/2)) - (ballstats[j]["x"]+(balldiameter/2)))**2 + ( (ballstats[i]["y"]+(balldiameter/2)) - (ballstats[j]["y"]+(balldiameter/2)))**2) 
            if dist < balldiameter:
                ballstats[i]["spdx"], ballstats[j]["spdx"] = ballstats[j]["spdx"], ballstats[i]["spdx"]
                ballstats[i]["spdy"], ballstats[j]["spdy"] = ballstats[j]["spdy"], ballstats[i]["spdy"]
        if ballstats[i]["x"] < 0 or ballstats[i]["x"] > winWid-balldiameter: ballstats[i]["spdx"] = ballstats[i]["spdx"] * -1
        if ballstats[i]["y"] < 0 or ballstats[i]["y"] > winHei-balldiameter: ballstats[i]["spdy"] = ballstats[i]["spdy"] * -1
        canvas.coords(ballstats[i]["shape"], (ballstats[i]["x"], ballstats[i]["y"], ballstats[i]["x"]+balldiameter, ballstats[i]["y"]+balldiameter))
    timerhandle = window.after(2,onTimer)

winWid = 640
winHei = 480
balldiameter = 7
ballstats = []
window = tkinter.Tk()
canvas = tkinter.Canvas(window, width=winWid, height=winHei, bg="white")
canvas.pack()
timerhandle = window.after(2,onTimer)
window.mainloop()
Gribouillis and menator01 like this post
Reply


Messages In This Thread
Python 25 Line Challenge - by codingCat - May-02-2022, 01:08 PM
RE: Python 25 Line Challenge - by codingCat - May-02-2022, 01:11 PM
RE: Python 25 Line Challenge - by menator01 - May-02-2022, 08:02 PM
RE: Python 25 Line Challenge - by codingCat - May-03-2022, 11:23 AM
RE: Python 25 Line Challenge - by Gribouillis - May-03-2022, 12:24 PM
RE: Python 25 Line Challenge - by codingCat - May-04-2022, 06:52 PM
RE: Python 25 Line Challenge - by menator01 - May-03-2022, 07:49 PM
RE: Python 25 Line Challenge - by codingCat - May-04-2022, 06:55 PM
RE: Python 25 Line Challenge - by Gribouillis - May-03-2022, 09:24 PM
RE: Python 25 Line Challenge - by codingCat - May-04-2022, 07:06 PM
RE: Python 25 Line Challenge - by menator01 - May-05-2022, 07:10 PM
RE: Python 25 Line Challenge - by Coricoco_fr - May-09-2022, 01:11 PM
RE: Python 25 Line Challenge - by codingCat - May-09-2022, 02:27 PM
RE: Python 25 Line Challenge - by menator01 - May-06-2022, 07:33 PM
RE: Python 25 Line Challenge - by codingCat - May-09-2022, 11:49 AM
RE: Python 25 Line Challenge - by codingCat - May-09-2022, 12:17 PM
RE: Python 25 Line Challenge - by menator01 - May-09-2022, 06:04 PM
RE: Python 25 Line Challenge - by codingCat - May-09-2022, 06:33 PM
RE: Python 25 Line Challenge - by menator01 - May-09-2022, 06:36 PM
RE: Python 25 Line Challenge - by Coricoco_fr - May-11-2022, 05:34 PM
RE: Python 25 Line Challenge - by Gribouillis - May-09-2022, 06:30 PM
RE: Python 25 Line Challenge - by menator01 - May-09-2022, 06:33 PM
RE: Python 25 Line Challenge - by Gribouillis - May-09-2022, 06:51 PM
RE: Python 25 Line Challenge - by menator01 - May-09-2022, 07:03 PM
RE: Python 25 Line Challenge - by Gribouillis - May-09-2022, 07:07 PM
RE: Python 25 Line Challenge - by codingCat - May-10-2022, 01:43 PM
RE: Python 25 Line Challenge - by codingCat - May-10-2022, 02:55 PM
RE: Python 25 Line Challenge - by Gribouillis - May-10-2022, 07:03 PM
RE: Python 25 Line Challenge - by Gribouillis - May-11-2022, 05:51 AM
RE: Python 25 Line Challenge - by codingCat - May-11-2022, 11:33 AM
RE: Python 25 Line Challenge - by codingCat - May-12-2022, 07:10 PM
RE: Python 25 Line Challenge - by codingCat - May-13-2022, 07:00 PM
RE: Python 25 Line Challenge - by codingCat - May-17-2022, 01:50 PM
RE: Python 25 Line Challenge - by codingCat - May-17-2022, 06:24 PM
RE: Python 25 Line Challenge - by codingCat - May-18-2022, 07:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python 100 line Challenge codingCat 9 3,267 Jun-20-2022, 07:18 AM
Last Post: Coricoco_fr
  Zen Python Challenge ichabod801 3 4,111 Aug-13-2018, 12:02 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020