Python Forum
Python 100 line Challenge
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 100 line Challenge
#2
Here is a first offering. Something I have dubbed a chaos spinner. Credit for the trig needed to rotate the spinners goes to its original author. My contribution was randomization and having it reset periodically... and of course converting it to Python.

Enjoy.

#Choas Spinner - Original code by Math Man in SmallBasic - Randomizing update by codingCat aka Matthew L. Parets for the 50 line challange. Python conversion in 2022.
from tkinter import *   #tools to build the window
from random import *    #tools for choosing random numbers
from time import *      #tools for checking the time, and pausing the app
from math import *      #tools for sin,cos

#--Record when a key is pressed
def onKeyDown(event):
    global lastkey, appOn
    lastkey = event.keysym
    if lastkey == "Escape" : appOn = False  #Close app when escape key is pressed

#--Allow for a graceful exit
def onShutdown():
    global appOn
    appOn = False

win = Tk()                          #Build a window
win.title('Chaos Spinner')          #Set the title
win.geometry("640x480")             #default window size
win.state('zoomed')                 #maximize the window
win.config(bg="#000000")            #background color of black
win.update()                        #update the window to show the user
winwid = win.winfo_width()          #get the resulting height and width for calculations
winhei = win.winfo_height()
canv = Canvas(win, width = winwid, height = winhei, bg="black")  #build a canvas to draw on
canv.pack()                         #add the canvas to the window
win.bind("<KeyPress>", onKeyDown)   #set the key down event
win.protocol("WM_DELETE_WINDOW",onShutdown)  #Set the shut down event

appOn = True                        #run the program while true
while appOn:                        #keep going until canceled by the app window closeing 
    lastkey = ""                    #reset the key press, changes design when space is pressed
    amount = randint(4,16)          #choose the number of segments in the pen
    total = 0                       #track the length of the pen
    angle = []                      #reset the lists that describe the pen and its location
    dist = []
    rate = []
    x = []
    y = []
    angrng = randint(1,12) * 15     #occationally limit the angle range to allow for more sweeping shapes
    for i in range(0,amount):       #for each pen segment
        angle.append(randint(-angrng,angrng)) #Randomly choose the speed around the circle for each pen segment
        dist.append(randint(16,round((winhei/1.1)/amount)))  #randomly choose the distance from the center - dividing by 1.1 ensures that most will stay on the screen
        total = total + dist[i]     #keep track of the distance
        rate.append(randint(-10,10) * 2)  #change speed of the angle
    for i in range(0,amount):       #randomly distribute what is left of distance from center to edge
        rndpos = randint(0,amount-1)#random position 
        dist[rndpos] = dist[rndpos] + (((winhei / 1.1) - total) // amount)
    x.append(winwid // 2)           #place the pen in the center of the window
    y.append(winhei // 2)
    for i in range(1,amount):       #for each segment of the pen
        x.append(round(x[i-1]+dist[i]*cos(radians(angle[i]))))  #set its location based on its angle and distance from middle
        y.append(round(y[i-1]+dist[i]*sin(radians(angle[i]))))
    x.append(x[amount-1])           #duplicate the last entry. This will help us draw connecting lines
    y.append(y[amount-1])

    pen = []                        #list to hold the lines of the pen
    for i in range(0,amount): pen.append(None)  #fill it with lots of nothing
    line = []                       #list to hold the connecting lines and point circles
    while appOn and lastkey != "space" and (time_ns() // 100000000) % 150 != 0 :  #while window is open, the space has not been pressed, and if seconds has not elapsed
        for i in range(1,amount):   #for each pen segment
            if pen[i] != None:      #Is there already a pen segment here?
                canv.delete(pen[i]) #delete the old pen segment
            angle[i] = angle[i]+rate[i]                                        #Calcualte the position of the new pen segment
            x[i] = x[i-1]+dist[i]*cos(radians(angle[i]))
            y[i] = y[i-1]+dist[i]*sin(radians(angle[i]))
            pen[i] = canv.create_line(x[i-1],y[i-1],x[i],y[i],fill="green")    #create the new pen segment
        
    
        color = ["#"+''.join([choice('ABCDEF0123456789') for i in range(6)])]  #choose a random color (random hex value)
        line.append(canv.create_line(x[amount-1],y[amount-1],x[amount],y[amount], fill=color))  #draw a line between the end of the pen, and the ends last location
        x[amount] = x[amount-1]                                                #update the end of the pen
        y[amount] = y[amount-1]
        line.append(canv.create_oval(x[amount-1]-3,y[amount-1]-3,x[amount-1]+3,y[amount-1]+3, outline=color))  #draw a circle at the end of the pen
        canv.update()                #update the window so the user can see the change
        sleep(0.025)                 #sleep for a tick to let other stuff happen in the OS
    for s in line: canv.delete(s)    #delete all of the connecting lines and circles
    for l in pen: canv.delete(l)     #delete the old pen

#once the window has been closed, clean up all of the resources and close the window.
win.destroy()
menator01 likes this post
Reply


Messages In This Thread
Python 100 line Challenge - by codingCat - May-27-2022, 02:14 PM
RE: Python 100 line Challenge - by codingCat - May-27-2022, 02:19 PM
RE: Python 100 line Challenge - by Coricoco_fr - May-29-2022, 06:06 PM
RE: Python 100 line Challenge - by codingCat - Jun-01-2022, 02:04 PM
RE: Python 100 line Challenge - by codingCat - May-28-2022, 07:09 PM
RE: Python 100 line Challenge - by Larz60+ - May-28-2022, 09:56 PM
RE: Python 100 line Challenge - by codingCat - May-29-2022, 12:02 AM
RE: Python 100 line Challenge - by menator01 - Jun-04-2022, 10:25 AM
RE: Python 100 line Challenge - by menator01 - Jun-08-2022, 09:52 AM
RE: Python 100 line Challenge - by Coricoco_fr - Jun-20-2022, 07:18 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python 25 Line Challenge codingCat 34 8,337 May-18-2022, 07:17 PM
Last Post: codingCat
  Zen Python Challenge ichabod801 3 4,158 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