在画布上移动对象?

2024-10-17 08:32:19 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使类中的对象在画布上移动?下面是一些代码,它只对形状有效,但对对象无效。我不擅长类和对象。如果你能帮助我,我将不胜感激

from tkinter import *

root = Tk()
sirka = root.winfo_screenwidth()
vyska = root.winfo_screenheight()


canvas = Canvas(root , height = vyska,width = sirka,bg= "gray")
canvas.place(x = -2,y = -2)

root.attributes("-fullscreen",True)

ex = Button(root,text = 'exit',command =root.destroy)
ex.place(x =sirka - 27,y =0)

x1 = 400
y1 = 200





class stickman(object):
    def __init__(self,head,body):
        self.head = head
        self.body = body


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



def move(event):
    global x1, y1  
    if event.char == "a":
         canvas.move(man, -10, 0)
    elif event.char == "d":
        canvas.move(man, 10, 0)
    elif event.char == "w":
        canvas.move(man, 0, -10)
    elif event.char == "s":
        canvas.move(man, 0, 10)



root.bind("<Key>", move)

mainloop()

Tags: 对象selfeventmovebodyrootheadcanvas
1条回答
网友
1楼 · 发布于 2024-10-17 08:32:19

你必须分开移动每个元素

 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_xman.spee_yroot.after()100ms运行一次update_game

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

编辑:正如Bryan Oakley在评论中提到的-您还可以将同一标记指定给许多对象,并使用此标记移动所有对象

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)

相关问题 更多 >