Вы должны переместить каждый элемент отдельно
canvas.move(man.head, -10, 0)
canvas.move(man.body, -10, 0)
Или создать метод в классе
def move(self, x, y):
canvas.move(self.head, x, y)
canvas.move(self.body, x, y)
, а затем вы можете использовать
man.move(-10, 0)
#from tkinter import * # PEP8: `import *` is not preferred
import tkinter as tk
# --- classes --- (CamelCaseNames)
class Stickman:
def __init__(self, head, body): # PEP8: spaces after comma
self.head = head
self.body = body
def move(self, x, y):
canvas.move(self.head, x, y)
canvas.move(self.body, x, y)
# --- functions ---
def move(event):
if event.char == "a":
man.move(-10, 0)
elif event.char == "d":
man.move(10, 0)
elif event.char == "w":
man.move(0, -10)
elif event.char == "s":
man.move(0, 10)
# --- main ---
root = tk.Tk()
sirka = root.winfo_screenwidth()
vyska = root.winfo_screenheight()
canvas = tk.Canvas(root, height=vyska, width=sirka, bg="gray") # PEP8: formatting
canvas.place(x=-2, y=-2) # PEP8: formatting
root.attributes("-fullscreen", True)
ex = tk.Button(root, text='exit', command=root.destroy)
ex.place(x=sirka-27, y=0)
x1 = 400
y1 = 200
man = Stickman(canvas.create_oval(x1, y1, x1+20, y1+20, fill='black'), canvas.create_rectangle(x1-10, y1+20, x1+30, y1+60, fill='red'))
root.bind("<Key>", move)
root.mainloop()
РЕДАКТИРОВАНИЕ: Для более плавного перемещения <KeyPress>
и <KeyRelease>
для изменения man.speed_x
, man.spee_y
и root.after()
для запуска update_game
каждые 100ms
#from tkinter import * # PEP8: `import *` is not preferred
import tkinter as tk
# --- classes --- (CamelCaseNames)
class Stickman:
def __init__(self, head, body): # PEP8: spaces after comma
self.head = head
self.body = body
self.speed_x = 0
self.speed_y = 0
def move(self, x, y):
canvas.move(self.head, x, y)
canvas.move(self.body, x, y)
# --- functions ---
def on_press(event):
if event.char == "a":
man.speed_x -= 10
elif event.char == "d":
man.speed_x += 10
elif event.char == "w":
man.speed_y -= 10
elif event.char == "s":
man.speed_y += 10
def on_release(event):
if event.char == "a":
man.speed_x -= -10
elif event.char == "d":
man.speed_x += -10
elif event.char == "w":
man.speed_y -= -10
elif event.char == "s":
man.speed_y += -10
def update_game():
man.move(man.speed_x, man.speed_y)
root.after(50, update_game) # update again after 100ms
# --- main ---
root = tk.Tk()
sirka = root.winfo_screenwidth()
vyska = root.winfo_screenheight()
canvas = tk.Canvas(root, height=vyska, width=sirka, bg="gray") # PEP8: formatting
canvas.place(x=-2, y=-2) # PEP8: formatting
root.attributes("-fullscreen", True)
ex = tk.Button(root, text='exit', command=root.destroy)
ex.place(x=sirka-27, y=0)
x1 = 400
y1 = 200
man = Stickman(canvas.create_oval(x1, y1, x1+20, y1+20, fill='black'), canvas.create_rectangle(x1-10, y1+20, x1+30, y1+60, fill='red'))
root.bind("<KeyPress>", on_press)
root.bind("<KeyRelease>", on_release)
update_game() # update first time
root.mainloop()
РЕДАКТИРОВАТЬ: , как упоминал Брайан Оукли в комментарии - вы также можете назначить один и тот же тег многим объектам и использовать этот тег для перемещения всех объектов.
class Stickman:
def __init__(self, head, body):
self.head = head
self.body = body
canvas.itemconfig(head, tag='man') # tag=('man', 'man_head')
canvas.itemconfig(body, tag='man') # tag=('man', 'man_body')
def move(self, x, y):
canvas.move('man', x, y)