Как добавлять 1 к переменной каждую секунду в модуле черепахи - PullRequest
0 голосов
/ 07 мая 2020

Я пытаюсь добавить 1 щелчок каждую секунду, но в большинстве ответов в Интернете используется time.sleep, который не работает с моим кодом, а некоторые используют многопоточность, но они тоже не работают. Я хочу, чтобы clicks_per_second добавлялись к кликам каждую секунду. Это простая игра-кликер ie Cook. Я пробовал многопоточность, но не уверен, правильно ли я все сделал.

    #Clicker

import turtle
import time
import threading


# Adding the screen
wn = turtle.Screen()
wn.title("Clicker")
wn.bgcolor("black")
wn.setup(width=1080, height=690)
wn.tracer(0)

# Variables
neededclicks1 = 5
neededclicks2 = 11

textcolor = 'white'

clicks = 0
clicks_per_sec = 0
clickvalue = 1

# Adding turtles
clicksneeded = turtle.Turtle()
clicksneeded.speed(0)
clicksneeded.color(textcolor)
clicksneeded.penup()
clicksneeded.hideturtle()
clicksneeded.goto(-25, -100)
clicksneeded.write("Clicks needed to upgrade: 5                                                                Clicks needed to upgrade: 11", align='center', font=('Arial', 15, 'normal'))

totalclicks = turtle.Turtle()
totalclicks.speed(0)
totalclicks.color(textcolor)
totalclicks.penup()
totalclicks.hideturtle()
totalclicks.goto(0, 260)
totalclicks.write("Clicks: 0", align='center', font=('Arial', 24, "normal"))

ClicksPerSec = turtle.Turtle()
ClicksPerSec.speed(0)
ClicksPerSec.color(textcolor)
ClicksPerSec.penup()
ClicksPerSec.hideturtle()
ClicksPerSec.goto(0, 200)
ClicksPerSec.write("Clicks Each Second: 0", align='center', font=('Arial', 24, 'normal'))

ClickHere = turtle.Turtle()
ClickHere.speed(0)
ClickHere.color(textcolor)
ClickHere.penup()
ClickHere.hideturtle()
ClickHere.goto(0, -200)
ClickHere.write("CLICK ANYWHERE", align='center', font=('Arial', 35, 'normal'))

Upgrademan = turtle.Turtle()
Upgrademan.speed(0)
Upgrademan.color(textcolor)
Upgrademan.penup()
Upgrademan.hideturtle()
Upgrademan.goto(0, +100)
Upgrademan.write("Press E to upgrade click value.                          Press Q to upgrade clicks per second.", align='center', font=('Arial', 20, 'normal'))

SaQ = turtle.Turtle()
SaQ.speed(0)
SaQ.color(textcolor)
SaQ.penup()
SaQ.hideturtle()
SaQ.goto(-450, 325)
SaQ.write("Press Esc to save and quit", align='center', font=('Arial', 10, 'normal'))

# Making functions 
def click(uselessparameter1, uselessparameter1point5):
    global clickvalue
    global clicks
    clicks += clickvalue
    totalclicks.clear()
    totalclicks.write("Clicks: {}".format(str(clicks)), align='center', font=('Arial', 24, "normal"))
def upgradeValue():
    global clickvalue
    global neededclicks1
    global clicks
    if clicks >= neededclicks1:
        clicks -= neededclicks1
        neededclicks1 *= 2
        clickvalue += 2
        totalclicks.clear()
        totalclicks.write("Clicks: {}".format(str(clicks)), align='center', font=('Arial', 24, 'normal'))
        clicksneeded.clear()
        clicksneeded.write("Clicks needed to upgrade: {}                                                                Clicks needed to upgrade: {}".format(neededclicks1, neededclicks2), align='center', font=('Arial', 15, 'normal'))

    else:
        Upgrademan.clear()
        Upgrademan.write('Not enough clicks', align='center', font=('Arial', 15, 'normal'))
        time.sleep(0.2)
        Upgrademan.clear()
        Upgrademan.write("Press E to upgrade click value.                            Press Q to upgrade clicks per second.", align='center', font=('Arial', 20, 'normal'))

def upgradeClicksPerSec():
    global clicks_per_sec
    global clicks
    global neededclicks2
    if clicks >= neededclicks2:
        clicks_per_sec += 1
        clicks -= neededclicks2
        neededclicks2 *= 2
        totalclicks.clear()
        clicksneeded.clear()
        ClicksPerSec.clear()
        totalclicks.write("Clicks: {}".format(str(clicks)), align='center', font=('Arial', 24, 'normal'))
        clicksneeded.write("Clicks needed to upgrade: {}                                                                Clicks needed to upgrade: {}".format(neededclicks1, neededclicks2), align='center', font=('Arial', 15, 'normal'))
        ClicksPerSec.write("Clicks Each Second: {}".format(clicks_per_sec), align='center', font=('Arial', 24, 'normal'))
    else:
        Upgrademan.clear()
        Upgrademan.write('Not enough clicks', align='center', font=('Arial', 15, 'normal'))
        time.sleep(0.2)
        Upgrademan.clear()
        Upgrademan.write("Press E to upgrade click value.                            Press Q to upgrade clicks per second.", align='center', font=('Arial', 20, 'normal'))

def addingclicks():
    global totalclicks
    global clicks
    global clicks_per_sec
    clicks += clicks_per_sec
    totalclicks.clear()
    totalclicks.write("Clicks: {}".format(str(clicks)), align='center', font=('Arial', 24, "normal"))
    time.sleep(1)

wn.listen()
turtle.onscreenclick(click)
turtle.onkey(upgradeValue, 'e')
wn.onkey(upgradeClicksPerSec, 'q')

wn.mainloop()

1 Ответ

1 голос
/ 07 мая 2020

Первая первая вещь , на которую я хотел бы обратить внимание, это собственное событие ontimer() черепахи. Ниже я переписал ваш код для использования события таймера, хотя я до сих пор не совсем уверен, что делает эта программа:

from turtle import Screen, Turtle
from time import sleep

# Constants
TEXT_COLOR = 'white'

TINY_FONT = ('Arial', 10, 'normal')
SMALL_FONT = ('Arial', 15, 'normal')
MEDIUM_FONT = ('Arial', 20, 'normal')
LARGE_FONT = ('Arial', 24, 'normal')
HUGE_FONT = ('Arial', 35, 'normal')

# Variables
needed_clicks_1 = 5
needed_clicks_2 = 11

clicks = 0
clicks_per_sec = 0
click_value = 1

# Functions
def click(x, y):
    global clicks

    clicks += click_value
    total_clicks.clear()
    total_clicks.write("Clicks: {}".format(clicks), align='center', font=LARGE_FONT)

def upgradeValue():
    global click_value
    global needed_clicks_1
    global clicks

    if clicks >= needed_clicks_1:
        clicks -= needed_clicks_1
        needed_clicks_1 *= 2
        click_value += 2

        total_clicks.clear()
        total_clicks.write("Clicks: {}".format(clicks), align='center', font=LARGE_FONT)

        clicks_needed.clear()
        clicks_needed.write("Clicks needed to upgrade: {}                                                                Clicks needed to upgrade: {}".format(needed_clicks_1, needed_clicks_2), align='center', font=SMALL_FONT)

    else:
        upgrade_man.clear()
        upgrade_man.write('Not enough clicks', align='center', font=SMALL_FONT)
        sleep(0.2)
        upgrade_man.clear()
        upgrade_man.write("Press E to upgrade click value.                            Press Q to upgrade clicks per second.", align='center', font=MEDIUM_FONT)

def upgradeClicks_Per_Sec():
    global clicks_per_sec
    global clicks
    global needed_clicks_2

    if clicks >= needed_clicks_2:
        clicks_per_sec += 1
        clicks -= needed_clicks_2
        needed_clicks_2 *= 2

        total_clicks.clear()
        total_clicks.write("Clicks: {}".format(str(clicks)), align='center', font=LARGE_FONT)

        clicks_needed.clear()
        clicks_needed.write("Clicks needed to upgrade: {}                                                                Clicks needed to upgrade: {}".format(needed_clicks_1, needed_clicks_2), align='center', font=SMALL_FONT)

        Clicks_Per_Sec.clear()
        Clicks_Per_Sec.write("Clicks Each Second: {}".format(clicks_per_sec), align='center', font=LARGE_FONT)
    else:
        upgrade_man.clear()
        upgrade_man.write('Not enough clicks', align='center', font=SMALL_FONT)
        sleep(0.2)
        upgrade_man.clear()
        upgrade_man.write("Press E to upgrade click value.                            Press Q to upgrade clicks per second.", align='center', font=MEDIUM_FONT)

def adding_clicks():
    global clicks

    clicks += clicks_per_sec
    total_clicks.clear()
    total_clicks.write("Clicks: {}".format(clicks), align='center', font=LARGE_FONT)

    screen.ontimer(adding_clicks, 1000)

# Adding the screen
screen = Screen()
screen.title("Clicker")
screen.bgcolor("black")
screen.setup(width=1080, height=690)

# Adding turtles
clicks_needed = Turtle()
clicks_needed.hideturtle()
clicks_needed.color(TEXT_COLOR)
clicks_needed.penup()
clicks_needed.goto(-25, -100)
clicks_needed.write("Clicks needed to upgrade: 5                                                                Clicks needed to upgrade: 11", align='center', font=SMALL_FONT)

total_clicks = Turtle()
total_clicks.hideturtle()
total_clicks.color(TEXT_COLOR)
total_clicks.penup()
total_clicks.goto(0, 260)
total_clicks.write("Clicks: 0", align='center', font=LARGE_FONT)

Clicks_Per_Sec = Turtle()  # to avoid clashing with clicks_per_sec
Clicks_Per_Sec.hideturtle()
Clicks_Per_Sec.color(TEXT_COLOR)
Clicks_Per_Sec.penup()
Clicks_Per_Sec.goto(0, 200)
Clicks_Per_Sec.write("Clicks Each Second: 0", align='center', font=LARGE_FONT)

click_here = Turtle()
click_here.hideturtle()
click_here.color(TEXT_COLOR)
click_here.penup()
click_here.goto(0, -200)
click_here.write("CLICK ANYWHERE", align='center', font=HUGE_FONT)

upgrade_man = Turtle()
upgrade_man.hideturtle()
upgrade_man.color(TEXT_COLOR)
upgrade_man.penup()
upgrade_man.goto(0, 100)
upgrade_man.write("Press E to upgrade click value.                          Press Q to upgrade clicks per second.", align='center', font=MEDIUM_FONT)

save_and_quit = Turtle()
save_and_quit.hideturtle()
save_and_quit.color(TEXT_COLOR)
save_and_quit.penup()
save_and_quit.goto(-450, 325)
save_and_quit.write("Press Esc to save and quit", align='center', font=TINY_FONT)

screen.onscreenclick(click)
screen.onkey(upgradeValue, 'e')
screen.onkey(upgradeClicks_Per_Sec, 'q')
screen.listen()

adding_clicks()

screen.mainloop()

Хотя time.sleep() обычно не используется вместе с событиями таймера, Я удалил 1-секундный сон, но сохранил другие ваши короткие сны, поскольку они используются только для временного отображения текста перед его стиранием.

...