简体   繁体   English

graphics.py中的TypeError

[英]TypeError in graphics.py

I'm trying to make a rudimentary cookie clicker: every time you click the cookie, a counter on the bottom of it goes up by one. 我正在尝试制作一个基本的cookie单击器:每次单击cookie时,它底部的一个计数器都会增加一个。 whenever I click the cookie, I get this wonderful error: 每当我单击Cookie时,都会收到以下错误提示:

TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'

I've figured out that something in my code is probably giving a return value of `None' for my program to spit out this error but I can't seem to figure out what. 我已经发现我的代码中的某些内容可能会给程序提供一个“无”的返回值,以消除该错误,但是我似乎无法弄清楚是什么。

# imports and variables

win = GraphWin('Cookie Clik', 500, 600)

# the cookie itself (center at 250, 250) and displaying the counter

if math.sqrt((win.mouseX - 250)^2 + (win.mouseY - 250)^2) < 200: # <- here's where I get an error
    # add one to the counter

I'm basically using pythag to determine how far away the mouse is from a point (the center of the circle) and if it's less than the radius, add one to the counter 我基本上是用pythag来确定鼠标与一个点(圆心)的距离,如果它小于半径,则将其添加到计数器中

First, in this expression: 首先,在此表达式中:

sqrt((win.mouseX - 250)^2 + (win.mouseY - 250)^2)

^ isn't exponentiation , it's xor ! ^不是 ,是xor You want ** . 你要** Next, there is no: 接下来,没有:

win.mouseX

instead you call win.getMouse() which returns a Point instance. 而是调用win.getMouse()返回一个Point实例。 Here's a rough sketch of what you're trying to do: 这是您要执行的操作的粗略草图:

from graphics import *
from math import sqrt

RADIUS = 20

win = GraphWin('Cookie Clik', 500, 600)

location = Point(250, 300)

cookie = Circle(location, RADIUS)
cookie.draw(win)

counter = 0

text = Text(Point(300, 250), counter)
text.draw(win)

while True:
    point = win.getMouse()

    if sqrt((point.getX() - location.getX()) ** 2 + (point.getY() - location.getY()) ** 2) < RADIUS:
        counter += 1

        text.setText(counter)

The next thing to add would be somewhere to click to get out of the program! 接下来要添加的内容是单击以退出该程序!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM