[英]TypeError in graphics.py
我正在尝试制作一个基本的cookie单击器:每次单击cookie时,它底部的一个计数器都会增加一个。 每当我单击Cookie时,都会收到以下错误提示:
TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'
我已经发现我的代码中的某些内容可能会给程序提供一个“无”的返回值,以消除该错误,但是我似乎无法弄清楚是什么。
# 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
我基本上是用pythag来确定鼠标与一个点(圆心)的距离,如果它小于半径,则将其添加到计数器中
首先,在此表达式中:
sqrt((win.mouseX - 250)^2 + (win.mouseY - 250)^2)
^
不是幂 ,是xor ! 你要**
。 接下来,没有:
win.mouseX
而是调用win.getMouse()
返回一个Point
实例。 这是您要执行的操作的粗略草图:
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)
接下来要添加的内容是单击以退出该程序!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.