我试着用Zelle使我的圆圈从矩形反弹图形.py. 一旦圆从矩形反弹出去,我希望它继续随机移动。这是我目前为止的代码,它正在工作! 我也知道,每一个圆图形技术上都可以使用最小的正方形的点来进行碰撞,但我做这件事有困难。在
from graphics import *
import random
def delay(d):
for i in range(d):
for i in range(50):
pass
#-------------------------------------------------
def main():
win=GraphWin("Moving Circle",500,400)
win.setBackground('white')
pt= Point(100,200)
cir=Circle(pt,30)
#changes the color of the circle for each game
r = random.randrange(256)
b = random.randrange(256)
g = random.randrange(256)
color = color_rgb(r, g, b)
cir.setFill(color)
cir.draw(win)
#rectangle
rec = Rectangle(Point(450,450), Point(275, 425))
rec.draw(win)
rec.setFill('black')
#-------------------------------------------------
pt5 = Point(250,30)
instruct1=Text(pt5, "click multiple times to start(rectangle can take multiple clicks to move)")
instruct1.setTextColor('black')
instruct1.draw(win)
#-------------------------------------------------
p=cir.getCenter()
p2=win.getMouse()
dx=1
dy=1
keepGoing=True
while keepGoing:
d = 100
delay(d)
cir.move(dx,dy)
p=cir.getCenter()
p2=win.checkMouse()
instruct1.setText("")
#rectanlge
isClicked= win.checkMouse()
if isClicked:
rp = isClicked
rc = rec.getCenter()
rdx = rp.getX() - rc.getX()
rdy = rp.getY() - rc.getY()
rec.move(rdx,rdy)
#circle
if((p.getX()-30)<=0.0) or ((p.getX()+30)>=500):
dx= -dx
if((p.getY()-30)<=0.0) or ((p.getY()+30)>=400):
dy=-dy
p3=win.checkMouse()
main()
我在考虑另一个想法,我们可以考虑一个围绕矩形的圆,而不是一个围绕圆的正方形。对我来说,问题是我们不仅需要检测碰撞,还需要知道从另一个物体移开的方法。它不仅仅是
True
和False
,而是(dx, dy)
类型的结果。在显然,一个围绕矩形的圆太粗糙了,但是假设它是由许多较小的圆组成的矩形,我们测量圆的中心到中心的距离来检测命中:
击中一个中心(绿色)矩形圆圈意味着反转大圆的垂直方向。只在末端(红色)的圆圈上击中意味着与大圆的水平方向相反。我们可以检测到这两种类型的撞击并完全扭转大圈。在
下面是我对您的代码进行的修改,我还修复了您的多次单击问题,并进行了许多样式更改:
不是完美的,但是有一些东西可以玩,可能插入一个更好的
intersects()
实现。在相关问题 更多 >
编程相关推荐