Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Clicker Game

Tags:

python

tkinter

I have come up with some code that I am using for a clicker game, kinda like Cookie Clicker.

from tkinter import *
import time

master = Tk()

def uiPrint():
    info()
    print("")
    print(click)
    blankLine()

def info():
    print("Double click purchases need 50 clicks!")
    print("Auto clicker purchases need 75 clicks!")

info()

click = 0
mult = 1
dcp1 = 0

def blankLine():
    for i in range(20):
        print("")

def purchaseDoubleClicksCommand():
    global click
    global mult
    if click < 5:
        print("Not enough clicks!")
        blankLine()
    elif click >= 5:
        mult = mult*2
        click = click - 5
        print("Double Clicks Purchased!")
        blankLine()


def purchaseAutoClickerCommand():
    global click
    if click < 7:
        print("Not enough clicks!")
        blankLine()
    elif click >= 7:
        click = click - 7
        print("Auto clicker purchased!")
        while True:
            click = click + 1
            time.sleep(1)


def buttonCommand():
    global click
    global mult
    click += 1*(mult)
    uiPrint()

    if click == 100:
        print('''Achievement Unlocked: Junior Clicker!
        BONUS 100 clicks!''')
        click += 100

    elif click == 400:
        print ('''Achievement Unlocked: Little Ninja Clicks!
        BONUS 200!''')
        click += 300

    elif click == 1500:
        print ('''Achievement Unlocked: Click Ninja Master!
        QUAD CLICKS!''')
        mult = mult * 4

    elif click == 3000:
        print ('''Achievement Unlocked:  Jackie Chan Style!
        8 TIMES THE CLICKS!''')
        mult = mult * 8

mainClickButton = Button(master, text="Click!", command = buttonCommand)
mainClickButton.pack()

purchaseDoubleClickButton = Button(master, text="Purchase Double Clicks", command = purchaseDoubleClicksCommand)
purchaseDoubleClickButton.pack()

purchaseAutoClickerButton = Button(master, text="Purchase Auto Clicker", command = purchaseAutoClickerCommand)
purchaseAutoClickerButton.pack()

master.title("Clicker! v0.0.6")
master.geometry("%sx%s+%s+%s" % (200,70,512,512))
mainloop()

This is the code that I have so far. I am trying to add an Auto Clicker that you can buy through a button. I have found one other post that is about this, but the solution for that uses PyMouse which 1.) I can't get installed (Trust me I've tried everything) and 2.) Don't want to imitate user input.

So this is the code in question.

def purchaseAutoClickerCommand():
    global click
    if click < 7:
        print("Not enough clicks!")
        blankLine()
    elif click >= 7:
        click = click - 7
        print("Auto clicker purchased!")
        while True:
            click = click + 1
            time.sleep(1)

And the code in charge of the button

purchaseAutoClickerButton = Button(master, text="Purchase Auto Clicker", command = purchaseAutoClickerCommand)
purchaseAutoClickerButton.pack()

When I hit the button that says "Purchase Auto Clicker", not only do I not get the number of clicks to increase by one per second, but the entire app crashes.

So my main question is, how can I have the number of clicks increase by one per second by the game itself (without the user needing to keep the mouse on the "Click!" button), and how can I have the Auto Clickers stack when more than one are bought (plus I would like to not have the program crash as soon as I try to buy it).

Editing this for Scratso to see:

This is what I have changed to the code

click = 0
mult = 1
dcp1 = 0
autoclickers = 0

def blankLine():
    for i in range(20):
        print("")

def purchaseDoubleClicksCommand():
    global click
    global mult
    if click < 5:
        print("Not enough clicks!")
        blankLine()
    elif click >= 5:
        mult = mult*2
        click = click - 5
        print("Double Clicks Purchased!")
        blankLine()


def purchaseAutoClickerCommand():
    global click
    global autoclickers
    if click < 7:
        print("Not enough clicks!")
        blankLine()
    elif click >= 7:
        autoclickers += 1 # add an autoclicker
        click = click - 7
        print("Auto clicker purchased!")

And at the end of the code I have added

while True:
    mainClickButton = Button(master, text="Click!", command = buttonCommand)
    mainClickButton.pack()

    purchaseDoubleClickButton = Button(master, text="Purchase Double Clicks", command = purchaseDoubleClicksCommand)
    purchaseDoubleClickButton.pack()

    purchaseAutoClickerButton = Button(master, text="Purchase Auto Clicker", command = purchaseAutoClickerCommand)
    purchaseAutoClickerButton.pack()

    master.title("Clicker! v0.0.6")
    master.geometry("%sx%s+%s+%s" % (200,70,512,512))
    mainloop()

    for autoclicker in range(autoclickers):
        click += 1
    time.sleep(1)
like image 889
Nolan Porter Avatar asked Apr 15 '26 03:04

Nolan Porter


1 Answers

The problem with using sleep() in a tkinter application is that it messes with the way the GUI is updated. Instead, call after() on the root tkinter object to tell it to execute the given command (function) after the given number of milliseconds have elapsed. This after() call will be placed within the function itself, so that, after calling this function normally, it will be called again one second later.

autoclickers=0 # start autoclickers at 0

def purchaseAutoClickerCommand():
    global click
    global autoclickers # declare global
    if click < 7:
        print("Not enough clicks!")
        blankLine()
    else:
        click -= 7 # pay for an autoclicker
        print("Auto clicker purchased!")
        autoclickers += 1 # receive an autoclicker

def autoclick():
    global master
    global click
    global autoclickers
    click += autoclickers # get clicks from autoclickers
    master.after(1000, autoclick) # do this again 1 second later

autoclick() # start benefiting from all existing autoclickers
like image 88
TigerhawkT3 Avatar answered Apr 17 '26 01:04

TigerhawkT3



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!