0

With my code I make a rocket that is supposed to, when I click space, launch itself, unfortunately, the while loop inside of the mov_r function causes lots of lag. And just brings up the spinning beachball of death for a few minutes until it stops and the rocket is gone. Which means that you can't actually see the rocket moving, which is not what I want to happen. I want to be able to see the rocket moving. I have tried adjusting the time it sleeps and the amount the rocket moves each time but none of it works. Here is the code:

from tkinter import *
import random, time
tk = Tk()
tk.wm_attributes('-topmost', 1)
canvas = Canvas(tk, width=1000, height=1000, bd=0)
canvas.pack()  
def move_r(event):
    if event.keysym == 'space': 
        global y2
        global y3
        a = 0
        b = 1000
        while a <b:
            y2-=1
            y3-=1
            time.sleep(.1)
            a+=1 

x2=460
y2=940
x3=570
y3=900
r1=None
num = 0
num1= 4
tk.bind('<KeyPress-space>', move_r)
while num < num1:
    background = canvas.create_rectangle(0, 0, 1000, 1000, fill = 'red') 
    base=canvas.create_rectangle(0, 1000, 1000, 900, fill='#86592d')
    def rocket():
        rocketbase=canvas.create_rectangle(x2+50, y2-50, x3-50, y3-90, fill ='blue')
        rrocketfin=canvas.create_rectangle(x2+60, y2-50, x3-40, y3-20, fill ='#e69100')
        lrocketfin=canvas.create_rectangle(x2+40, y2-50, x3-60, y3-20, fill ='#e69100')
        mrocketfin=canvas.create_rectangle(x2+54, y2-50, x3-53, y3-20, fill ='#e69100')
        lrockethead=canvas.create_rectangle(x2+45, y2-125, x3-60, y3-70, fill ='#e69100')
        rrockethead=canvas.create_rectangle(x2+60, y2-125, x3-45, y3-70, fill ='#e69100')
    rocket()  
    tk.update()
tk.mainloop()

If anyone can tell me what to do to get rid of the lag please help, thanks. Also please don't ask why i use x2 and x3 and y2 and y3 it's a long story.

Maximus
  • 71
  • 9
  • What is the purpose of the loop? – cdarke Apr 24 '16 at 20:48
  • To move the rocket up the screen by its self – Maximus Apr 24 '16 at 20:49
  • and does that work? I think you are expecting multithreading where there is none. – cdarke Apr 24 '16 at 20:50
  • yes, but it causes lots of lag, and then jumps of my screen, so it looks like it disappears even though i want it to be "animated" – Maximus Apr 24 '16 at 20:52
  • I wouldn't use tkinter for animation, it's not a very robust framework – Natecat Apr 24 '16 at 20:59
  • But it isn't really animation, its just that the lag is getting in the way of the game working, thats why i need a fix for the lag, not the animation – Maximus Apr 24 '16 at 21:02
  • What happens if you remove sleep or decrease it like .001? – alpert Apr 24 '16 at 21:18
  • Also see [here](http://stackoverflow.com/questions/10214701/run-an-infinite-loop-in-the-backgroung-in-tkinter). – TigerhawkT3 Apr 24 '16 at 21:22
  • Basically, you're doing an event loop incorrectly. Instead of a `while` loop with `time.sleep`, use `after`. Oh, and instead of creating seven rectangles as fast as the CPU can handle, try _moving_ them on a reasonable timer with, again, `after()`. – TigerhawkT3 Apr 24 '16 at 21:23
  • 2
    So your telling me to use after instead of while – Maximus Apr 24 '16 at 22:25
  • @Natecat: tkinter is perfectly fine for simple animations as learning exercises. It can animate perhaps a few hundred items before it bogs down. when done properly. – Bryan Oakley Apr 24 '16 at 23:45

0 Answers0