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.