简体   繁体   中英

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. whenever I click the cookie, I get this wonderful error:

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

First, in this expression:

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

^ isn't exponentiation , it's xor ! You want ** . Next, there is no:

win.mouseX

instead you call win.getMouse() which returns a Point instance. 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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