0

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()
1
  • local versus global variables. I would actually recommend using a class instead, inhereting from tk.Frame() and make everything class variables. fe: look at this link to see how to do this... Commented Mar 17, 2022 at 21:31

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thanks a lot actually I have changed my code a lot. I´ve figured out that there is no point in using those globals.
0

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()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.