0

I'm trying to create a simple clicker style game. But lately I've been having some issues. In a previous question of mine I asked how can I solve a screen = getscreen() error. That question was answered but the same day I got a new error.

When I try to click the button I get this traceback error:

TypeError: clicking() takes exactly 0 arguments (2 given) at <unknown>`. 

It links back to the definition of clicking().

Part of code in which clicking() is defined:

def clicking():
  if distance( button.pos() ) < 2:
    BUTTON_CLICKS = BUTTON_CLICKS + 1

The whole code:

import time
import turtle

screen = turtle.Screen()
image_BUTTON = "Button.png"
image_BUTTON_CLICKS = "Button_clicks.png"
image_UPGRADEBG = "UPGRADEBG.png"

button = turtle.Turtle()
BUTTON_CLICKS = 0
BUTTON_CLICKS1 = turtle.Turtle()
BUTTON_CLICKS2 = turtle.Turtle()
upgrade = turtle.Turtle()
upgrade1 = turtle.Turtle()
upgrade2 = turtle.Turtle()
upgrade3 = turtle.Turtle()
upgrade4 = turtle.Turtle()
upgrade5 = turtle.Turtle()

screen.addshape(image_BUTTON)
button.penup()
button.speed(0)
button.left(90)
button.shape(image_BUTTON)
button.goto(0, 0)

BUTTON_CLICKS1.speed(0)
BUTTON_CLICKS1.penup()
BUTTON_CLICKS1.hideturtle()
BUTTON_CLICKS1.goto(-65, 170)
BUTTON_CLICKS1.write("Button   clicks: %d" % BUTTON_CLICKS, font=("Bebas", 14, "bold"))

upgrade.speed(0)
upgrade.penup()
upgrade.hideturtle()
upgrade.goto(110, -190)
upgrade.write("Upgrades", font=("Bebas", 13, "bold"))

def clicking():
  if distance( button.pos() ) < 2:
    BUTTON_CLICKS = BUTTON_CLICKS + 1

screen = turtle.getscreen()
screen.onclick( clicking )

Note: I'm creating this game in trinket.io

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
righteous
  • 35
  • 10

2 Answers2

1

onclick accepts a function as an argument and calls it using two other arguments, the coordinates of the point clicked.

From the documentation:

turtle.onclick(fun, btn=1, add=None)¶

Parameters: fun – a function with two arguments which will be called with the coordinates of the clicked point on the canvas

(emphasis mine)

As a result, when you supply a function (clicking()) which doesn't take any arguments, a TypeError is going to get raised because onclick will call it with two arguments.

Add two arguments to your function to remove the TypeError, what you do with these, is up to you to decide.

def clicking(x, y):
  if distance( button.pos() ) < 2:
    BUTTON_CLICKS = BUTTON_CLICKS + 1
Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
  • Hah, I don't get that error anymore. But I get a `NameError: name 'distance' is not defined on line 41`. (Sorry for being so dumb) I'm clearly missing something? – righteous Jan 20 '16 at 18:19
  • You haven't defined `distance`, why have added it there? – Dimitris Fasarakis Hilliard Jan 20 '16 at 18:21
  • I'm learning through an internet course. I didn't see a definition in the example there. So I thought I didn't need one. How do I define it? – righteous Jan 20 '16 at 18:22
  • Well, it's a function (because it is called) you need to define a new function with `def distance(button_position): # added logic` that implements something. I don't know what that something is, the internet course **should** provide it. – Dimitris Fasarakis Hilliard Jan 20 '16 at 18:24
  • It doesn't. And it's kind of abandoned so I won't get a response for sure. Can you help me a bit on this one? How do I make the button click. I'm so lost. – righteous Jan 20 '16 at 18:26
  • The comments here don't really allow such explanation. A working example can be found [here](http://stackoverflow.com/questions/15893236/python-3-0-using-turtle-onclick) or an interactive guide like [this](http://interactivepython.org/runestone/static/thinkcspy/PythonTurtle/OurFirstTurtleProgram.html) one. Seriously though, google these things and try them out in the interpreter; `turtle` is a pretty popular library so resources are plentyfull. :-) – Dimitris Fasarakis Hilliard Jan 20 '16 at 18:29
  • 1
    Thanks for you help, Jim. I'll try to find the solution. – righteous Jan 20 '16 at 18:31
0

Quoting official docs (emphasis mine):

turtle.onclick(fun, btn=1, add=None)

Parameters:

fun – a function with two arguments which will be called with the coordinates of the clicked point on the canvas

num – number of the mouse-button, defaults to 1 (left mouse button)

add – True or False – if True, a new binding will be added, otherwise it will replace a former binding Bind fun to mouse-click events on this turtle.

Your function takes 0 arguments, and docs specifies that it should accept exactly two arguments.

Change your function signature to:

def clicking(x, y):
    pass  # your code goes here 
Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93