首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Find_overlapping &冲突和Zelle图形(python) -想要一些反馈,请

Find_overlapping &冲突和Zelle图形(python) -想要一些反馈,请
EN

Stack Overflow用户
提问于 2022-07-30 14:45:58
回答 1查看 62关注 0票数 0

我正在寻找一种使用Zelle和find_overlapping创建冲突检测的不同方法。我相信重叠显示一个对象的边界框与另一个对象的边界框(元组)接触的位置?我希望能得到一些关于这段代码的反馈。现在,物体在它断裂之前要经过矩形,但我认为这个想法是有价值的。这只是一个测试,需要更多的微调。

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

发布于 2022-07-30 16:24:00

我看不出什么问题:

  1. 您首先检查碰撞,然后移动到新位置,然后检查是否停止游戏-但是您应该在检查碰撞后停止游戏而不移动到新位置。或者你应该先移动到新的位置,然后检查碰撞并检查是否停止游戏。

update()

  1. 您移动dx = 20,但墙壁只有宽度10,它可以跳过它-您应该使用更小的dx和使用更大的值在
  2. 中。

  1. 检查墙壁是否与任何其他对象重叠,但是如果您将有许多墙壁,则检查圆圈是否与任何其他对象重叠会更简单。

  1. 当圆圈重叠时,你必须把它移回来-如果你向右移动,你应该设置圆圈的右边框而不是矩形的左边框。

最小的工作示例与更改1,2,3,但没有4,因为它将需要更多的更改。

代码语言:javascript
复制
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()`
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73176557

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档