'python rotating object disfunction
I have been working on my final project, where we are supposed to create a tank game in tkinter. I have done the movement, however i´ve been struggling with the rotation of my tank. Yesterday, I manually calculated all the x and y coordinates for rotation and it works, but for some reason, when i rotate the tank for example to left, back to normal(till here it works) and then to left again the tank just completely messes up. Can´t think of reason why this is happening. Would appreciate some help. Here is my code :
from tkinter import *
x=0
y=0
poloha="hore"
class Tank:
def __init__(self, delta) -> None:
"""""
x=input("Enter position of tank(x):")
y=input("Enter position of tank(y):")
"""""
self.t1 = plocha.create_rectangle(int(x)+50,int(y)+375,int(x)+100,int(y)+425)
self.t2 = plocha.create_rectangle(int(x)+45,int(y)+370,int(x)+50,int(y)+430)
self.t3 = plocha.create_rectangle(int(x)+100,int(y)+370,int(x)+105,int(y)+430)
self.t4 = plocha.create_rectangle(int(x)+72.5,int(y)+350,int(x)+77.5,int(y)+400)
self.delta = delta
#Movement
def left(event):
plocha.move(event.t1, -event.delta, 0)
plocha.move(event.t2, -event.delta, 0)
plocha.move(event.t3, -event.delta, 0)
plocha.move(event.t4, -event.delta, 0)
def right(event):
plocha.move(event.t1, +event.delta, 0)
plocha.move(event.t2, +event.delta, 0)
plocha.move(event.t3, +event.delta, 0)
plocha.move(event.t4, +event.delta, 0)
def up(event):
x1, y1, x2, y2 = plocha.coords(event.t4)
if y1 - 1 > 0:
plocha.move(event.t1, 0, -event.delta)
plocha.move(event.t2, 0, -event.delta)
plocha.move(event.t3, 0, -event.delta)
plocha.move(event.t4, 0, -event.delta)
else:
pass
def down(event):
x1, y1, x2, y2 = plocha.coords(event.t4)
if y2 + 1 < 500:
plocha.move(event.t1, 0, +event.delta)
plocha.move(event.t2, 0, +event.delta)
plocha.move(event.t3, 0, +event.delta)
plocha.move(event.t4, 0, +event.delta)
else:
pass
def down_turn(event):
global poloha
poloha="dole"
x1, y1, x2, y2 = plocha.coords(event.t4)
plocha.coords(event.t4, x1, y1+55, x2 , y2+55)
plocha.update()
def up_turn(event):
global poloha
if poloha=="vpravo":
x1, y1, x2, y2 = plocha.coords(event.t2)
plocha.coords(event.t2, x1, y1, x2 - 60, y2 + 55)
x1, y1, x2, y2 = plocha.coords(event.t3)
plocha.coords(event.t3, x1+55, y1-55 , x2-5 , y2)
x1, y1, x2, y2 = plocha.coords(event.t4)
plocha.coords(event.t4, x1 - 5, y1 - 50, x2 - 50, y2 - 5)
plocha.update()
if poloha=="vlavo":
x1, y1, x2, y2 = plocha.coords(event.t2)
plocha.coords(event.t2, x1 + 60, y1 - 55, x2 - 5, y2)
x1, y1, x2, y2 = plocha.coords(event.t3)
plocha.coords(event.t3, x1 + 5, y1, x2 - 60, y2 + 55)
x1, y1, x2, y2 = plocha.coords(event.t4)
plocha.coords(event.t4, x1+50, y1 - 50, x2-5, y2 +5)
plocha.update()
if poloha=="dole":
x1, y1, x2, y2 = plocha.coords(event.t4)
plocha.coords(event.t4, x1, y1 - 55, x2, y2 - 55)
plocha.update()
def right_turn(event):
global poloha
up_turn(event)
poloha="vpravo"
x1,y1,x2,y2=plocha.coords(event.t2)
plocha.coords(event.t2,x1,y1,x2+60,y2-55)
x1, y1, x2, y2 = plocha.coords(event.t3)
plocha.coords(event.t3, x1+10, y1+60, x2 -60, y2-5)
x1, y1, x2, y2 = plocha.coords(event.t4)
plocha.coords(event.t4, x1+5,y1+50,x2+50,y2-5)
plocha.update()
def left_turn(event):
global poloha
up_turn(event)
poloha="vlavo"
x1,y1,x2,y2=plocha.coords(event.t2)
plocha.coords(event.t2,x1+60,y1+55,x2-5,y2)
x1, y1, x2, y2 = plocha.coords(event.t3)
plocha.coords(event.t3, x1+5, y1,x2-60, y2-55)
x1,y1,x2,y2=plocha.coords(event.t4)
plocha.coords(event.t4, x1+5,y1+50,x2-50,y2-5)
plocha.update()
master = Tk()
master.title("WOT")
plocha = Canvas(master, width=800, height=500)
global t1,t2,t3,t4 # later delete
tank = Tank(5)
delta = 5
#Movement
plocha.bind("<w>", lambda event: up(tank))
plocha.bind("<a>", lambda event: left(tank))
plocha.bind("<s>", lambda event: down(tank))
plocha.bind("<d>", lambda event: right(tank))
plocha.bind("<Down>", lambda event: down_turn(tank))
plocha.bind("<Up>", lambda event: up_turn(tank))
plocha.bind("<Right>", lambda event: right_turn(tank))
plocha.bind("<Left>", lambda event: left_turn(tank))
plocha.focus_set()
plocha.pack()
plocha.mainloop()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
