自动移动形状?Python3.5Tkinter
在python中,我目前正在使用tkinter制作一个“游戏”,但它已决定不起作用。我想做的是让一个矩形可以通过鼠标移动,而另一个矩形可以在玩家不做任何事情的情况下连续上下移动。这是我的密码:自动移动形状?Python3.5Tkinter,python,canvas,tkinter,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_rec
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()
从tkinter导入*
导入时间
root=Tk()
根标题(“游戏”)
根几何(“800x800”)
def motion():
canvas.delete(全部)
a=画布。创建_矩形(event.x-50、event.y-50、event.x+50、event.y+50、fill='red')
def motion2():
b=画布。创建一个矩形(10100100,10,fill='blue')
y=-15
x=0
睡眠时间(0.025)
画布移动(b,x,-y)
canvas.update()
画布=画布(根,宽=1000,高=5000,背景为白色)
canvas.bind(“,运动)
canvas.pack(pady=0)
mainloop()
我希望这个问题能很快得到解决。在过去的几天里,我一直在研究这个问题,但仍然没有答案。感谢您的时间:)-Jake您可以使用
root.after(毫秒,函数名)
定期运行函数,它可以使用canvas.move(对象id,偏移x,偏移y)
自动移动对象
您可以使用canvas.coords(object_id,x1,y1,x2,y2)
使用鼠标位置设置新位置<代码>绑定用一个参数执行函数(对象事件
),因此函数必须接收此参数
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()
编辑:在画布上移动
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()
使用
root.after(毫秒,函数名)
自动移动对象-并删除time.sleep()
。要移动对象,您不必删除对象并再次创建-您有canvas.coords(object\u id,…)
和canvas.move(object\u id,…)
。更好地阅读文档:即,我的意思是不断地上下移动,这意味着它永远不会停止。谢谢你的回答。要停止它或改变方向,你必须使用变量,如move\u up=True
和一些if/else
来检查何时改变方向。我如何使其连续?请参阅回答中的新示例另一个示例-移动画布
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()
#!/usr/bin/env python3
import tkinter as tk
# --- constants --- (UPPER_CASE names)
DISPLAY_WIDHT = 800
DISPLAY_HEIGHT = 600
# --- classes --- (CamelCase names)
#class Player():
# pass
#class BlueEnemy():
# pass
# --- functions --- (lower_case names)
def move_a(event):
# don't move if gama paused
if not game_paused:
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
# don't move if gama paused
if not game_paused:
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 >= DISPLAY_HEIGHT:
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 >= DISPLAY_WIDHT:
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)
def pause(event):
global game_paused
# change True/False
game_paused = not game_paused
if game_paused:
# center text on canvas
canvas.coords(text_pause, DISPLAY_WIDHT//2, DISPLAY_HEIGHT//2)
else:
# move text somewhere outside canvas
canvas.coords(text_pause, -1000, -1000)
# --- main --- (lower_case names)
# init
root = tk.Tk()
# key `p` pause game
game_paused = False
root.bind('p', pause)
# create canvas
canvas = tk.Canvas(root, width=DISPLAY_WIDHT, height=DISPLAY_HEIGHT)
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()
# create text somewhere outside canvas - so it will be "invisible"
text_pause = canvas.create_text(-1000, -1000, text="PAUSED", font=(50,))
# start program
root.mainloop()