自动移动形状? Python 3.5 Tkinter

时间:2017-01-21 10:59:33

标签: python canvas tkinter

在python中,我正在制作一款游戏'使用tkinter,但它决定不工作。我想要做的是让一个矩形可以通过鼠标移动,另一个矩形可以在没有玩家做任何事情的情况下连续上下移动。这是我的代码:

from tkinter import *
import time
root = Tk()
root.title("Game")
root.geometry("800x800")

def motion():
    canvas.delete(ALL)
    a = canvas.create_rectangle(event.x-50, event.y-50, event.x+50, event.y+50, fill='red')

def motion2():
    b = canvas.create_rectangle(10, 100, 100, 10, fill='blue')
    y = -15
    x = 0
    time.sleep(0.025)
    canvas.move(b, x, -y)
    canvas.update()

canvas = Canvas(root, width=1000, height=5000, bg='white')
canvas.bind("<Motion>", motion)
canvas.pack(pady=0)

mainloop()

我希望很快就能解决这个问题。我过去几天一直在研究这个问题,但仍然没有答案。谢谢你的时间:) -Jake

1 个答案:

答案 0 :(得分:1)

您可以使用root.after(milliseconds, function_name)定期运行功能,并可以使用canvas.move(object_id, offset_x, offset_y)自动移动对象。

您可以使用canvas.coords(object_id, x1, y1, x2, y2)使用鼠标位置设置新位置。 bind使用一个参数(对象event)执行函数,因此函数必须接收此参数。

import tkinter as tk

# --- functions ---

def move_a(event):
    canvas.coords(a, event.x-50, event.y-50, event.x+50, event.y+50)

def move_b():
    canvas.move(b, 1, 0)
    # move again after 25ms (0.025s)
    root.after(25, move_b)

# --- main ---

# init
root = tk.Tk()

# create canvas
canvas = tk.Canvas(root)
canvas.pack()

# create objects
a = canvas.create_rectangle(0, 0, 100, 100, fill='red')
b = canvas.create_rectangle(0, 0, 100, 100, fill='blue')

# start moving `a` with mouse
canvas.bind("<Motion>", move_a)

# start moving `b` automatically
move_b()

# start program
root.mainloop()

编辑要上下移动,您必须使用speed变量,这样您就可以将其从speed更改为-speed并再次更改为{{ 1}}。您也可以使用speed来检查当前的方向(或者您可以使用move_down = True/False来检查方向,但speed对人们来说更具可读性)

b_move_down

编辑:在画布上移动

import tkinter as tk

# --- functions ---

def move_a(event):
    canvas.coords(a, event.x-50, event.y-50, event.x+50, event.y+50)

def move_b():
    # inform function to use external/global variable 
    # because we use `=` to change its value
    global b_speed
    global b_move_down

    canvas.move(b, 0, b_speed)

    # get current position        
    x1, y1, x2, y2 = canvas.coords(b)

    # check if you have to change direction
    #if b_speed > 0:
    if b_move_down:
        # if it reachs bottom
        if y2 > 300:
            # change direction
            #b_move_down = False
            b_move_down = not b_move_down
            b_speed = -b_speed
    else:
        # if it reachs top
        if y1 < 0:
            # change direction
            #b_move_down = True
            b_move_down = not b_move_down
            b_speed = -b_speed

    # move again after 25 ms (0.025s)
    root.after(25, move_b)

# --- main ---

# init
root = tk.Tk()

# create canvas
canvas = tk.Canvas(root, width=500, height=300)
canvas.pack()

# create objects
a = canvas.create_rectangle(0, 0, 100, 100, fill='red')
b = canvas.create_rectangle(0, 0, 100, 100, fill='blue')
# create global variables
b_move_down = True
b_speed = 5

# start moving `a` with mouse
canvas.bind("<Motion>", move_a)

# start moving `b` automatically
move_b()

# start program
root.mainloop()

编辑:最后一个示例 - 键import tkinter as tk # --- functions --- def move_a(event): canvas.coords(a, event.x-50, event.y-50, event.x+50, event.y+50) def move_b(): # inform function to use external/global variable # because we use `=` to change its value global b_speed_x global b_speed_y global b_direction canvas.move(b, b_speed_x, b_speed_y) # get current position x1, y1, x2, y2 = canvas.coords(b) if b_direction == 'down': if y2 >= 300: b_direction = 'right' b_speed_x = 5 b_speed_y = 0 elif b_direction == 'up': if y1 <= 0: b_direction = 'left' b_speed_x = -5 b_speed_y = 0 elif b_direction == 'right': if x2 >= 500: b_direction = 'up' b_speed_x = 0 b_speed_y = -5 elif b_direction == 'left': if x1 <= 0: b_direction = 'down' b_speed_x = 0 b_speed_y = 5 # move again after 25 ms (0.025s) root.after(25, move_b) # --- main --- # init root = tk.Tk() # create canvas canvas = tk.Canvas(root, width=500, height=300) canvas.pack() # create objects a = canvas.create_rectangle(0, 0, 100, 100, fill='red') b = canvas.create_rectangle(0, 0, 100, 100, fill='blue') # create global variables b_direction = 'down' b_speed_x = 0 b_speed_y = 5 # start moving `a` with mouse canvas.bind("<Motion>", move_a) # start moving `b` automatically move_b() # start program root.mainloop() 暂停游戏

p