Python Forum
Clicker game counter help - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: Clicker game counter help (/thread-3121.html)



Clicker game counter help - JohnNo - Apr-30-2017

You guys helped me alot, you made my life easy with your help and code. So i ask again. I am trying to create clicker game where you click on some button and you get +1 score but I am trying to create this in tkinter without pygame. I know it can be done, but I dont know how to get the score work. I got some code for my counter from someone, it is working console prints out 1 2 3 4... but I dont know how to make a label, that shows the value and i need to store it somewhere like
 a = //That value// 
so I can later use it in my shop like if a is = 20 he can buy something that will change how much points he gets, so like if a = 20 he buys that and now the score is like this 2 4 6.... and global value of the counter so if I change the value to 3 the counter will count like this 3 6 9....So I ask if someone here can help me make simple counter/adder to label and store the value globaly so i can refer to it anywhere, if it is possible I dont understand __init__ and self., so if someone can do it without it, it would be really great, but i can do with __init__ but for me it is very hard i am learing python and still i dont know what that is, even if I look on multiple sites that explained it, still I dont know.
Here is the counter but I dont know how to get the value global and refer to it anywhere. 
import tkinter as tk
class Counter(object):
    def __init__(self, start: int = 0):
        self.value = start
    def add(self) -> int:
        if self.value < 900:
            self.value += 1
        print(self.value)
        return self.value
clicks = Counter()
root = tk.Tk()
tk.Button(root, text="click me!", command=clicks.add).pack()
root.mainloop()



RE: Clicker game counter help - JohnNo - Apr-30-2017

from tkinter import *

root = Tk()
root.geometry("200x200")
root.title("My Clicker Game")

global counter
counter = 0

def nClick():
    global counter
    counter += 1
    mLabel.config(text = counter)

mButton1 = Button(text = counter, command = nClick, fg = "darkgreen", bg = "white")
mButton1.pack()
mButton2 = Button(text = "Goodbye", command = root.destroy, fg = "darkblue", bg = "white")
mButton2.pack()
mLabel = Label()
mLabel.pack()

root.mainloop()
I need something like if counter is 20 then program will do something but I dont know how to do that I have tried this.
if counter == 20:
   root.destroy
but that does not work so I ask if someone can help


RE: Clicker game counter help - nilamo - May-02-2017

(Apr-30-2017, 09:24 AM)JohnNo Wrote: if counter == 20:
   root.destroy
but that does not work so I ask if someone can help
Have you tried calling the function, instead of just referring to it? ie: root.destroy()

I don't think using global variables is a good idea. It solves the immediate issue while making the next thing more complicated.