双轴 Bbox 故障
Bbox malfunction on both axis
我正在测试一个 Tkinter 项目,该项目将在软件中有一些小游戏(有点像 Mario paint)。对于这个测试,我有 2 个盒子,一个是红色的,另一个是蓝色的,但是当它在红色盒子旁边或里面时,它会变成绿色。
这是代码。
from tkinter import *
from threading import Timer
window = Tk()
window.title("Canvas Test")
x = 30
y = 30
Test = Canvas(window, width = 1000, height = 1000)
Test.config(bg = "white")
Test.pack()
Move = Test.create_rectangle(x, y, x + 40, y + 40, fill = "blue")
Hitd = Test.create_rectangle(x, y, x + 40, y + 40, fill = "red")
Move = Test.create_rectangle(x, y, x + 30, y + 30, fill = "blue")
Test.update()
w = False
a2 = False
s = False
d = False
def KeyDown(event):
print("K Down")
global x
global y
global Move
global w
global a2
global s
global d
if event.char == "w" or event.char == "W":
w = True
elif event.char == "s" or event.char == "S":
s = True
if event.char == "a" or event.char == "A":
a2 = True
elif event.char == "d" or event.char == "D":
d = True
def KeyUp(event):
print("K Up")
global w
global a2
global s
global d
if event.char == "w" or event.char == "W":
w = False
elif event.char == "s" or event.char == "S":
s = False
if event.char == "a" or event.char == "A":
a2 = False
elif event.char == "d" or event.char == "D":
d = False
def MoveCheck():
global Move
global w
global a2
global s
global d
if w == True:
Test.move(Move, 0, -10) #name, x mod, y mod
elif s == True:
Test.move(Move, 0, 10)
if a2 == True:
Test.move(Move, -10, 0)
elif d == True:
Test.move(Move, 10, 0)
a = Test.bbox(Move)
b = Test.bbox(Hitd)
if b[0] in range(a[0],a[2]) or b[2] in range(a[0],a[2]) and b[1] in range(a[1],a[3]) or b[3] in range(a[1],a[3]):
Test.itemconfig(Move, fill = "green")
else:
Test.itemconfig(Move, fill = "blue")
t = Timer(0.03, MoveCheck)
t.start()
MoveCheck()
window.bind("<Key>", KeyDown)
window.bind("<KeyRelease>", KeyUp)
window.mainloop()
如果您与方块对齐,然后向下或向上移动 1(或 left/right,具体取决于您在哪个轴上进行测试),那么您会看到方块无论如何都会变成绿色。我对使用 bbox 非常陌生,所以这可能只是我的疏忽。
您可以使用 Canvas.find_overlapping()
测试移动矩形是否与其他对象重叠。
a = Test.bbox(Move)
#b = Test.bbox(Hitd)
if Hitd in Test.find_overlapping(*a):
Test.itemconfig(Move, fill = "green")
else:
Test.itemconfig(Move, fill = "blue")
具有其他更改的完整工作代码
after()
而不是 Thread
lower_case_names
对于变量 (PEP 8 -- Style Guide for Python Code)
- 函数中的参数
=
周围没有空格 (PEP 8 -- Style Guide for Python Code)
- 没有
import *
(PEP 8 -- Style Guide for Python Code)
- 主要部分之前的所有函数(和类)
- 有意义的变量名(
canvas
而不是 Test
)
。
import tkinter as tk
# --- functions ---
def key_down(event):
global speed_x
global speed_y
print("K Down")
char = event.char.lower()
if char == "w":
speed_y = -10
elif char == "s":
speed_y = 10
elif char == "a":
speed_x = -10
elif char == "d":
speed_x = 10
def key_up(event):
global speed_x
global speed_y
print("K Up")
char = event.char.lower()
if char in ("w", "s"):
speed_y = 0
elif char in ("a", "d"):
speed_x = 0
def move_check():
canvas.move(player, speed_x, speed_y) #name, x mod, y mod
player_bbox = canvas.bbox(player)
if enemy in canvas.find_overlapping(*player_bbox):
canvas.itemconfig(player, fill="green")
else:
canvas.itemconfig(player, fill="blue")
window.after(30, move_check)
# --- main ---
window = tk.Tk()
window.title("Canvas Test")
x = 30
y = 30
speed_x = 0
speed_y = 0
canvas = tk.Canvas(window, width=1000, height=1000, bg="white")
canvas.pack()
enemy = canvas.create_rectangle(x, y, x+40, y+40, fill="red")
player = canvas.create_rectangle(x, y, x+30, y+30, fill="blue")
move_check()
window.bind("<Key>", key_down)
window.bind("<KeyRelease>", key_up)
window.mainloop()
我正在测试一个 Tkinter 项目,该项目将在软件中有一些小游戏(有点像 Mario paint)。对于这个测试,我有 2 个盒子,一个是红色的,另一个是蓝色的,但是当它在红色盒子旁边或里面时,它会变成绿色。
这是代码。
from tkinter import *
from threading import Timer
window = Tk()
window.title("Canvas Test")
x = 30
y = 30
Test = Canvas(window, width = 1000, height = 1000)
Test.config(bg = "white")
Test.pack()
Move = Test.create_rectangle(x, y, x + 40, y + 40, fill = "blue")
Hitd = Test.create_rectangle(x, y, x + 40, y + 40, fill = "red")
Move = Test.create_rectangle(x, y, x + 30, y + 30, fill = "blue")
Test.update()
w = False
a2 = False
s = False
d = False
def KeyDown(event):
print("K Down")
global x
global y
global Move
global w
global a2
global s
global d
if event.char == "w" or event.char == "W":
w = True
elif event.char == "s" or event.char == "S":
s = True
if event.char == "a" or event.char == "A":
a2 = True
elif event.char == "d" or event.char == "D":
d = True
def KeyUp(event):
print("K Up")
global w
global a2
global s
global d
if event.char == "w" or event.char == "W":
w = False
elif event.char == "s" or event.char == "S":
s = False
if event.char == "a" or event.char == "A":
a2 = False
elif event.char == "d" or event.char == "D":
d = False
def MoveCheck():
global Move
global w
global a2
global s
global d
if w == True:
Test.move(Move, 0, -10) #name, x mod, y mod
elif s == True:
Test.move(Move, 0, 10)
if a2 == True:
Test.move(Move, -10, 0)
elif d == True:
Test.move(Move, 10, 0)
a = Test.bbox(Move)
b = Test.bbox(Hitd)
if b[0] in range(a[0],a[2]) or b[2] in range(a[0],a[2]) and b[1] in range(a[1],a[3]) or b[3] in range(a[1],a[3]):
Test.itemconfig(Move, fill = "green")
else:
Test.itemconfig(Move, fill = "blue")
t = Timer(0.03, MoveCheck)
t.start()
MoveCheck()
window.bind("<Key>", KeyDown)
window.bind("<KeyRelease>", KeyUp)
window.mainloop()
如果您与方块对齐,然后向下或向上移动 1(或 left/right,具体取决于您在哪个轴上进行测试),那么您会看到方块无论如何都会变成绿色。我对使用 bbox 非常陌生,所以这可能只是我的疏忽。
您可以使用 Canvas.find_overlapping() 测试移动矩形是否与其他对象重叠。
a = Test.bbox(Move)
#b = Test.bbox(Hitd)
if Hitd in Test.find_overlapping(*a):
Test.itemconfig(Move, fill = "green")
else:
Test.itemconfig(Move, fill = "blue")
具有其他更改的完整工作代码
after()
而不是Thread
lower_case_names
对于变量 (PEP 8 -- Style Guide for Python Code)- 函数中的参数
=
周围没有空格 (PEP 8 -- Style Guide for Python Code) - 没有
import *
(PEP 8 -- Style Guide for Python Code) - 主要部分之前的所有函数(和类)
- 有意义的变量名(
canvas
而不是Test
)
。
import tkinter as tk
# --- functions ---
def key_down(event):
global speed_x
global speed_y
print("K Down")
char = event.char.lower()
if char == "w":
speed_y = -10
elif char == "s":
speed_y = 10
elif char == "a":
speed_x = -10
elif char == "d":
speed_x = 10
def key_up(event):
global speed_x
global speed_y
print("K Up")
char = event.char.lower()
if char in ("w", "s"):
speed_y = 0
elif char in ("a", "d"):
speed_x = 0
def move_check():
canvas.move(player, speed_x, speed_y) #name, x mod, y mod
player_bbox = canvas.bbox(player)
if enemy in canvas.find_overlapping(*player_bbox):
canvas.itemconfig(player, fill="green")
else:
canvas.itemconfig(player, fill="blue")
window.after(30, move_check)
# --- main ---
window = tk.Tk()
window.title("Canvas Test")
x = 30
y = 30
speed_x = 0
speed_y = 0
canvas = tk.Canvas(window, width=1000, height=1000, bg="white")
canvas.pack()
enemy = canvas.create_rectangle(x, y, x+40, y+40, fill="red")
player = canvas.create_rectangle(x, y, x+30, y+30, fill="blue")
move_check()
window.bind("<Key>", key_down)
window.bind("<KeyRelease>", key_up)
window.mainloop()