在python中简单的射箭游戏

时间:2011-02-24 17:45:53

标签: python

我刚刚开始使用python,我正在尝试制作一个小射箭游戏。但是,它会在此时产生错误:d = math.sqrt(x * x + y * y)(即新点与cirlce原始中心之间的距离)有关为什么不起作用的任何想法?

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()

2 个答案:

答案 0 :(得分:4)

    x = p.getX
    y = p.getY

将返回函数getXgetY,而不是执行它。正如Mike Steder所说,试试getX(),这应该返回一个值。

答案 1 :(得分:1)

首先,您可能需要这样做:

x = p.getX()
y = p.getY()

即。调用函数并使用返回值,而不是使用函数本身。

其次,您可以将math.sqrt(x*x + y*y)调用更改为:

d = math.hypot(x, y)