'TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType' , and many different errors
I am working on my final project, where we are supposed to create a tank game and right now we are just making the frame of program. I want to move the tank with keys across the tkinter area, however i have tried many things and still i am getting many different errors. Also, when the program atleast started, the tank wouldn´t move at all. Would appreciate some help. Here is my code :
master=Tk()
master.title("WOT")
plocha=Canvas(master,width=800,height=500)
plocha.pack()
x = None
y = None
t1 = None
t2 = None
t3 = None
t4 = None
def tank():
t1=plocha.create_rectangle(int(x)+50,int(y)+375,int(x)+100,int(y)+425)
t2=plocha.create_rectangle(int(x)+45,int(y)+370,int(x)+50,int(y)+430)
t3=plocha.create_rectangle(int(x)+100,int(y)+370,int(x)+105,int(y)+430)
t4=plocha.create_rectangle(int(x)+72.5,int(y)+350,int(x)+77.5,int(y)+400)
def pozicia():
x=input("Zadaj pozíciu tanku(x):")
y=input("Zadaj pozíciu tanku(y):")
tank()
def vlavo(event):
x=-10
y=0
plocha.move(t1,x,y)
plocha.move(t2,x,y)
plocha.move(t3,x,y)
plocha.move(t4,x,y)
plocha.update()
def vpravo(event):
x=10
y=0
plocha.move(t1,x,y)
plocha.move(t2,x,y)
plocha.move(t3,x,y)
plocha.move(t4,x,y)
plocha.update()
def hore(event):
x=0
y=-40
plocha.move(t1,x,y)
plocha.move(t2,x,y)
plocha.move(t3,x,y)
plocha.move(t4,x,y)
plocha.update()
def dole(event):
x=0
y=10
plocha.move(t1,x,y)
plocha.move(t2,x,y)
plocha.move(t3,x,y)
plocha.move(t4,x,y)
plocha.update()
plocha.bind("<w>",hore)
plocha.bind("<a>",dole)
plocha.bind("<s>",vlavo)
plocha.bind("<d>",vpravo)
pozicia()
plocha.mainloop()
Solution 1:[1]
Here:
def pozicia():
x=input("Zadaj pozíciu tanku(x):")
y=input("Zadaj pozíciu tanku(y):")
tank()
You assign the result of the call to input() to x, but this is a local variable x, not the global x you set with x = None.
You could do this:
def pozicia():
global x, y
x = input("Zadaj pozíciu tanku(x):")
y = input("Zadaj pozíciu tanku(y):")
tank()
But it's generally better not to use globals like this and instead pass values around using function parameters.
For example
def pozicia():
x = input("Zadaj pozíciu tanku(x):")
y = input("Zadaj pozíciu tanku(y):")
return x, y
x, y = pozicia()
tank(x, y)
Or:
def pozicia():
x = input("Zadaj pozíciu tanku(x):")
y = input("Zadaj pozíciu tanku(y):")
tank(x, y)
If you don't need x and y outside the function.
As @edoakse recommends, using classes to keep data together and defining functions as methods on those classes might be an even better way of doing things, but that's both a matter of need, style and preference.
Solution 2:[2]
Actually I have figured it out. Here is how my code looks like now :
x=0
y=0
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
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):
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)
def down(event):
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)
master = Tk()
master.title("WOT")
plocha = Canvas(master, width=800, height=500)
global t1, t2, t3, t4 # later delete
tank = Tank(5)
delta = 5
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.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 |
|---|---|
| Solution 1 | Grismar |
| Solution 2 | KotPali |
