我正在寻找一种使用Zelle和find_overlapping创建冲突检测的不同方法。我相信重叠显示一个对象的边界框与另一个对象的边界框(元组)接触的位置?我希望能得到一些关于这段代码的反馈。现在,物体在它断裂之前要经过矩形,但我认为这个想法是有价值的。这只是一个测试,需要更多的微调。
from graphics import *
win = GraphWin("game loop", 500, 500, autoflush=False)
win.master.attributes('-topmost', True)
x=20
y=20
dx=20
play=0
cir1=Circle(Point(x,y),10)
cir1.setFill("red")
cir1.draw(win)
x1=300
x2=310
y1=0
y2=50
rect1=Rectangle(Point(x1,y1),Point(x2,y2))
rect1.setFill("blue")
rect1.draw(win)
xwidth=win.getWidth()
while play==0:
for i in range(100):
xposition = cir1.getCenter().getX()
test1=win.find_overlapping(x1,y1,x2,y2)
print("This is the length of test1:",len(test1))
print(test1)
if xposition+1>=xwidth or xposition-1<=0:
dx=-dx
cir1.move(dx, 0)
update(15)
if len(test1)==2:
print("overlap")
play=1
break
print("game over")
win.mainloop()发布于 2022-07-30 16:24:00
我看不出什么问题:
update()
dx = 20,但墙壁只有宽度10,它可以跳过它-您应该使用更小的dx和使用更大的值在最小的工作示例与更改1,2,3,但没有4,因为它将需要更多的更改。
from graphics import *
win = GraphWin("game loop", 500, 500, autoflush=False)
win.master.attributes('-topmost', True)
x = 200
y = 20
dx = -5
play = True
cirle = Circle(Point(x, y), 10)
cirle.setFill("red")
cirle.draw(win)
x1 = 300
x2 = 310
y1 = 0
y2 = 50
rect1 = Rectangle(Point(x1, y1), Point(x2, y2))
rect1.setFill("blue")
rect1.draw(win)
xwidth = win.getWidth()
while play:
for i in range(100):
# move circle to new position
cirle.move(dx, 0)
# get circle's new position
p1 = cirle.getP1()
p2 = cirle.getP2()
# check circle's collision in new position
overlaps = win.find_overlapping(p1.x, p1.y, p2.x, p2.y)
print("test:", len(overlaps), overlaps)
if len(overlaps) > 1:
print("overlap")
play = False
break
# check circle's collision with window's boders
if p2.x >= xwidth or p1.x <= 0:
dx = -dx
update(50)
print("game over")
#win.mainloop() # no need if you use `update()`https://stackoverflow.com/questions/73176557
复制相似问题