0

I have a circle that I want to move around a map, I am in the early stages, but I've looked in many places and can't figure out why the circle isn't moving. my code is

import Tkinter as Tkinter

class gameScreen:
    def moveup(self, event):
        self.canvas.move(self.char, -100, 0)
        self.canvas.focus(self.char)
        self.canvas.update()

    def __init__(self, master):

        self.master = master
        master.title("Game")

        master.resizable(width=False, height=False)
        self.img = tkinter.PhotoImage(file = "platformer.gif")
        self.canvas = tkinter.Canvas(master, width=self.img.width(),
                                             height=self.img.height())

        self.canvas.pack(expand="YES",fill="both")

        self.canvas.create_image(0, 0,anchor="nw", image = self.img)
        self.char = tkinter.PhotoImage(file = "hero.gif")
        self.canvas.create_oval(0, 0, 50, 50, fill="red")
        self.x = 0
        self.y = 0
        master.bind("<Up>", self.moveup)        

root = tkinter.Tk()
my_gui = gameScreen(root)
root.mainloop()`

1 Answer 1

1

You have to give the move method an id or tag for an object on the canvas. The id is returned when you create the canvas item.

class gameScreen:
    def moveup(self, event):
        ...
        self.canvas.move(self.canvas_item, -100, 0)
        ...

    def __init__(self, master):
        ...
        self.canvas_item = self.canvas.create_oval(...)
        ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.