Timerhandle = window.after(20,onTimer) #start the game loop #- Main Program - Executes as soon as you hit run -Ĭanvas = tkinter.Canvas(window, width=winWid, height=winHei, bg="white") #Random color - This is a helper function If ballY winHei-balldiameter: spdY = spdY * -1 #left or right? reverse directionsĬords(ballShape, (ballX, ballY, ballX+balldiameter, ballY+balldiameter)) If dist winWid-balldiameter: spdX = spdX * -1 #top or bottom? reverse directions Timerhandle = window.after(frameDelay,onTimer) #fill update rest of this frame with a delayīallShape.append(canvas.create_oval(newX,newY, newX+balldiameter,newY+balldiameter, outline=color, fill=color))įor i in range(0,ballCnt): #for each ballīallX = ballX + spdX #update its positionįor j in range(i+1,ballCnt): #check for colision between other ballsĭist = math.sqrt(( (ballX+(balldiameter/2)) - (ballX+(balldiameter/2)))**2 + ( (ballY+(balldiameter/2)) - (ballY+(balldiameter/2)))**2) MoveTheBalls() #update the position of the balls ![]() Window.title("FD:" + str(frameDelay) + " - " + str(ballCnt)) # frame goes up, the delay between the frames goes downĮlapsedTime = round((time.perf_counter() - frameStart)* 1000) #time from start of frame until nowįrameDelay = frameRate - elapsedTime #delay for this frame is the frame size, minus how long it took to process this frameĪddTimer = time.perf_counter() #update the add time ![]() #An animation frame is the work being done to draw/update a frame, Global timerhandle, frameStart, frameDelay, addTimer #- Initialize Globla Variables -įrameRate = 20 #length of a frame (include delay) in millescondsįrameDelay = 0 #the delay at the end of the current frameįrameStart = 0 #when the current frame started In addition to being open to general comments, I am looking for suggestions to make the animation smoother and less at the whims of "after" and background event processing. ![]() "After" isn't really to tool for the job. The end result works, but I am dissatisfied with the pacing on the game loop (onTimer). With a newbies eyes in mind I have tried to keep to a limited tool kit avoid pygame and threading and sticking to tkinter (included with most installations) to make the code as clear and as easy to understand as possible. This is a little animation demo that I wrote to show off the basics of a game loop in Python. The goal of each frame of the animation to be the same length as the one before it, regardless of the amount of processing that takes place as the frame is being built.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |