from graphics import*
import time
import random
def main():
numx=random.randint(10,700)
wn=GraphWin("AK",700,700)
wn.setBackground("white")
msg=Text(Point(25,30),"Score")
msg.setSize(12)
msg.setTextColor('blue')
msg.draw(wn)
inch=Entry(Point(60,30),2)
inch.setFill('white')
inch.draw(wn)
sqrg=Rectangle(Point(330,650),Point(430,665))
sqrg.setFill("red")
sqrg.draw(wn)
blx=Circle(Point(numx,80),20)
blx.setFill("blue")
blx.draw(wn)
xval=10
yval=0
wn.getMouse()
for i in range(150):
sqrg.move(xval,yval)
symbl=wn.checkKey()
if symbl=="Right":
xval=10
yval=0
if symbl=="Left":
xval=-10
yval=0
time.sleep(0.08)
blx.move(0,20)
main()
我很困惑,我的教授很困惑,对于一个检测到碰撞得分会提高的项目,我需要这样做。
答案 0 :(得分:0)
您的半径是20。在循环内部,只需测试sqrg和blx之间的欧几里得距离是否在20以内。
答案 1 :(得分:0)
以下是根据您的代码精简的示例。它测量两个运动对象的中心之间的距离,以确定是否发生碰撞。如果您设法将球击中广场,则该球应向上弹跳:
from random import randint
from time import sleep
from graphics import *
def distance(p1, p2):
return ((p2.x - p1.x) ** 2 + (p2.y - p1.y) ** 2) ** 0.5
wn = GraphWin("AK", 700, 700)
sqrg = Rectangle(Point(325, 625), Point(375, 675))
sqrg.setFill("red")
sqrg.draw(wn)
numx = randint(10, 700)
blx = Circle(Point(numx, 80), 20)
blx.setFill("blue")
blx.draw(wn)
xval, yval = 10, 0
bheading = 1
wn.getMouse()
for i in range(150):
sqrg.move(xval, yval)
if distance(blx.getCenter(), sqrg.getCenter()) < 25:
bheading *= -1
symbl = wn.checkKey()
if symbl == "Right":
xval = 10
elif symbl == "Left":
xval = -10
sleep(0.1)
blx.move(0, bheading * 20)
Cleary本身并不是可行的游戏,而是碰撞检测的演示。