i created a game where u can shoot jump and there is a “circle” chasing you but when the player jumps and shoots the bullet (the yellow line) stops and when he moves while the bullet moves both of them slow down
and u cant move in the start while the “circle” is chasing you
i tried to make different threads for the bullet and the jump and move but it didnt work i am not good at that i wanted that the bullet would be able to move at the same speed when the player is moving and be able to move at all when he is jumping
code:
import time
import threading
food_cords: dict = {}
foodcords: dict = {}
l = 0
wind = Tk()
wind.geometry("1600x850")
global canvas
canvas = Canvas(wind, width=1600, height=850, )
canvas.pack()
canvas.create_line(0, 700, 1600, 700, fill="black", width=5, tag="line")
canvas.create_line(0, 600, 500, 600, fill="black", width=5, tag="line2")
scor = 0
class Food():
for i in range(0, 10):
x_postion: int = i * 200
global parent
parent = Canvas(wind, width=30, height=30)
parent.place(x=x_postion, y=660)
oval = parent.create_oval(0, 0, 30, 30, fill="yellow", tag="foood")
parent.update()
food_cords[i] = [x_postion, parent]
bg = PhotoImage(file="python2.png")
mybg = canvas.create_image(0, -580, image=bg, anchor=NW)
label = Label(wind, image=bg)
label.place(x=0, y=580)
def checkCords():
temp = None
print(f"Label X: {Food.label.winfo_x()} / Label Y: {Food.label.winfo_y()}")
for i in food_cords:
lis = food_cords[i]
if lis[0] == Food.label.winfo_x():
print(f"remove food @ X:{lis[0]}")
lis[1].delete("all")
temp = i
wind.update()
global scor
scor = scor + 10
print(scor)
scoreee = Label(wind, text=scor, font=("arial", 25)).place(x=40, y=60)
if temp != None:
food_cords.pop(temp)
def move_right(event):
if 1450 <= Food.label.winfo_x():
print("hi21")
return
else:
Food.label.place(x=Food.label.winfo_x() + 10, y=Food.label.winfo_y())
x1 = Food.label.winfo_x()
print(x1)
wind.update()
if Food.label.winfo_x() >= 1400:
Food.label.place(x=0)
#hzom()
#zombie()
for i in range(0, 10):
x_postion: int = i * 200
parent = Canvas(wind, width=30, height=30)
parent.place(x=x_postion, y=660)
oval = parent.create_oval(0, 0, 30, 30, fill="yellow", tag="foood")
parent.update()
food_cords[i] = [x_postion, parent]
#zombie()
if list3[0] <= Food.label.winfo_x():
zombie()
checkCords()
def move_down(event):
Food.label.place(x=Food.label.winfo_x(), y=Food.label.winfo_y() + 10)
def move_up(event):
if 580 == Food.label.winfo_y() :
print("hi321")
Food.label.place(x=Food.label.winfo_x(), y=Food.label.winfo_y() - 200)
wind.update()
if 475 <= Food.label.winfo_y() <= 485 and 0 <= Food.label.winfo_x() <= 500:
Food.label.place(x=Food.label.winfo_x(), y=Food.label.winfo_y() - 200)
wind.update()
if 579 >= Food.label.winfo_y():
while 579 >= Food.label.winfo_y():
print(Food.label.winfo_y())
print(Food.label.winfo_x())
time.sleep(1/100000)
Food.label.place(y=Food.label.winfo_y() + 2)
wind.update()
if 475 <= Food.label.winfo_y() <= 485:
if 0 <= Food.label.winfo_x() <= 500:
while 475 <= Food.label.winfo_y() <= 485 and 0 <= Food.label.winfo_x() <= 500:
print("hi322222221")
wind.update()
def move_left(event):
if 0 >= Food.label.winfo_x():
return
print("hi1")
Food.label.place(x=Food.label.winfo_x() - 10, y=Food.label.winfo_y())
x1 = Food.label.winfo_x()
print(x1)
wind.update()
checkCords()
zombie()
list2=[0,0,0,0]
def shoot(event):
if scor < 200:
global way
way = canvas.create_line(Food.label.winfo_x() + 20, Food.label.winfo_y() + 40, Food.label.winfo_x() + 65,
Food.label.winfo_y() + 40, fill="yellow", width=5, tags=("line1"))
print(Food.label.winfo_x()+5)
print("ggffedg")
print(canvas.coords(way))
global list2
list2 = canvas.coords(way)
print(list2[1])
if list2[2] <= 1450 and scor < 200:
while list2[2] <= 1450:
print(list2[2])
print("d")
print("hellllo")
canvas.coords(way,list2[0] +100,list2[1],list2[2]+100,list2[3])
list2[0] = list2[0] + 0.05
list2[1] = list2[1]
list2[2] = list2[2] + 0.05
list2[3] = list2[3]
print(list2)
if list3[0] >= list2[2]>list3[0]-1:
print("f")
global l
l = 1
parent1.delete(w)
parent1.destroy()
list3[0] = 213213123
wind.update()
if list2[2] >= 1450:
canvas.itemconfig(way, fill='white')
if scor > 200:
print("132")
way = canvas.create_line(Food.label.winfo_x() + 20, Food.label.winfo_y() + 40, Food.label.winfo_x() + 65,
Food.label.winfo_y() + 40, fill="blue", width=5, tags=("line1"))
list2 = canvas.coords(way)
if list2[2] <= 1450:
while list2[2] <= 1450:
print("hellllo")
canvas.coords(way, list2[0] + 100, list2[1], list2[2] + 100, list2[3])
list2[0] = list2[0] + 0.05
list2[1] = list2[1]
list2[2] = list2[2] + 0.05
list2[3] = list2[3]
print(list2)
wind.update()
if list2[2] >= 1450:
canvas.itemconfig(way, fill='white')
l = 0
def hzom():
global parent1
parent1 = Canvas(wind, width=50, height=50)
global oval12
global w
w = parent1.create_oval(0, 0, 50, 50, fill="blue", tags=("oval12",))
global list3
list3 = [1300, 640]
parent1.place(x=list3[0], y=list3[1])
print("d")
def zombie():
if list3[0] >= Food.label.winfo_x():
while list3[0] >= Food.label.winfo_x():
if l == 1:
break
list3[0] = list3[0] - 0.05
parent1.place(x=list3[0], y=list3[1])
#print(list3[0])
#print(list3[1])
wind.update()
if list3[0] <= Food.label.winfo_x():
while list3[0] <= Food.label.winfo_x():
if l ==1:
break
#print(list3[0])
#print(list3[1])
list3[0] = list3[0] + 0.05
parent1.place(x=list3[0],y=list3[1])
wind.update()
print(Food.label.winfo_y())
wind.bind("<e>", shoot)
hzom()
Food()
zombie()
if wind.bind("<d>", move_right):
x = threading.Thread(target=move_right(event=wind.bind("<d>")))
x.start()
if wind.bind("<a>", move_left):
pass
if wind.bind("<q>", move_up):
pass
wind.mainloop()