我剛剛開始使用python,我正在嘗試製作一個小型射箭遊戲。然而,在這一點上它會產生一個錯誤:d = math.sqrt(x * x + y * y)(即新點與圓的原始中心之間的距離)關於爲什麼這不起作用的任何想法?python中的簡單射箭遊戲
def archery():
win = GraphWin("Archery Game", 500,500)
win.setCoords(-50, -50, 50, 50)
circle1 = Circle(Point(0,0), 40)
circle1.setFill("white")
circle1.draw(win)
circle2 = Circle(Point(0,0), 35)
circle2.setFill("black")
circle2.draw(win)
circle3 = Circle(Point(0,0), 30)
circle3.setFill("blue")
circle3.draw(win)
circle4 = Circle(Point(0,0), 25)
circle4.setFill("red")
circle4.draw(win)
circle5 = Circle(Point(0,0), 20)
circle5.setFill("yellow")
circle5.draw(win)
score = 0
for i in range(5):
p = win.getMouse()
p.draw(win)
x = p.getX
y = p.getY
d = math.sqrt(x*x + y*y)
if 40 >= d > 35:
score = score + 1
elif 35 >= d > 30:
score = score + 3
elif 30 >= d > 25:
score = score + 5
elif 25 >= d > 20:
score = score + 7
elif 20 >= d >= 0:
score = score + 9
else:
score = score + 0
print("Your current score is:", score)
win.getMouse()
win.close()
你得到了什麼錯誤?你輸入數學嗎? – 2011-02-24 17:48:27
小提示:您可以用'**'在Python中提出一個數字。 – Skurmedel 2011-02-24 17:49:04
它看起來像p.getX和p.getY應該是「p.getX()」和「p.getY()」。我假設你試圖將getX和getY函數相乘並相加,而不是這些函數返回的數字。 – stderr 2011-02-24 17:50:13