Бесконечный цикл в Python (tkinter)
Я пишу игру на Python 3 с помощью библиотеки tkinter. В игре мне нужно реализовать систему автоматического заработка. К примеру я купил улучшение, и каждую секунду мне капает определенное количество монет.
Я пробовал это сделать с помощью while True в функции, где при нажатии на эту кнопку срабатывает функция этого самого улучшения, но при нажатии на кнопку у меня просто зависала игра и выключалась. Позже попробовал сделать бесконечный цикл с помощью этого же метода (while True) в самом конце кода, он просто не срабатывал, как будто этого цикла и нету. Ещё пробовал сделать функцию с кодом, который должен был быть в цикле, и я так же в конце кода написал while True, но теперь в цикле была одна строчка - функция с нужным кодом. Это тоже не помогло.
Теперь хочу попросить помощи с циклом. Есть ли ещё способы сделать бесконечные циклы без while True, или как исправить эту ошибку. Буду благодарен.
Вот код (код не самый хороший, я новичок в программировании):
from tkinter import *
import time
# глобальные переменные
clicks = 0
put, pat = 1, 0
damages = 1
dps_marker = 0
more_enemy1, more_enemy2, more_enemy3 = 0, 0, 0
more_dps_enemy1, more_dps_enemy2 = 0, 0
price_enemy_one, price_enemy_two, price_enemy_three = 100, 350, 800
price_dps_enemy_one, price_dps_enemy_two = 2, 1000
def money_click():
global clicks
global put
clicks += put
plusmoney = 'MONEY: {}'.format(clicks)
money.configure(text=plusmoney)
def enemy1():
global put, clicks, damages, more_enemy1, price_enemy_one
if clicks >= price_enemy_one:
more_enemy1 += 1
put += 7
damages = put
plusdamage = 'DAMAGE: {}'.format(damages)
damage.configure(text=plusdamage)
plusmast = '{}'.format(more_enemy1)
enemyone.configure(text=plusmast)
clicks -= price_enemy_one
minus_dollars = 'MONEY: {}'.format(clicks)
money.configure(text=minus_dollars)
result_percent = price_enemy_one - (price_enemy_one // 100 * 94)
price_enemy_one += result_percent
priceplus = 'PRICE: {}'.format(price_enemy_one)
enemy1price.configure(text=priceplus)
def enemy2():
global put, clicks, damages, more_enemy2, price_enemy_two
if clicks >= price_enemy_two:
more_enemy2 += 1
put += 5
damages = put
plusdamage = 'DAMAGE: {}'.format(damages)
damage.configure(text=plusdamage)
plusmast = '{}'.format(more_enemy2)
enemytwo.configure(text=plusmast)
clicks -= price_enemy_two
minus_dollars = 'MONEY: {}'.format(clicks)
money.configure(text=minus_dollars)
result_percent = price_enemy_two - (price_enemy_two // 100 * 94)
price_enemy_two += result_percent
priceplus = 'PRICE: {}'.format(price_enemy_two)
enemy2price.configure(text=priceplus)
def enemy3():
global put, clicks, damages, more_enemy3, price_enemy_three
if clicks >= price_enemy_three:
more_enemy3 += 1
put += 10
damages = put
plusdamage = 'DAMAGE: {}'.format(damages)
damage.configure(text=plusdamage)
plusmast = '{}'.format(more_enemy3)
enemythree.configure(text=plusmast)
clicks -= price_enemy_three
minus_dollars = 'MONEY: {}'.format(clicks)
money.configure(text=minus_dollars)
result_percent = price_enemy_three - (price_enemy_three // 100 * 94)
price_enemy_three += result_percent
priceplus = 'PRICE: {}'.format(price_enemy_three)
enemy3price.configure(text=priceplus)
def dpsenemy1():
global dps_marker, pat, clicks, more_dps_enemy1, price_dps_enemy_one, money
if clicks >= price_dps_enemy_one:
more_dps_enemy1 += 1
pat += 3
dps_marker = pat
plusdamage = 'DPS: {}'.format(dps_marker)
dps.configure(text=plusdamage)
plusmast = '{}'.format(more_dps_enemy1)
dps_enemy_one.configure(text=plusmast)
clicks -= price_dps_enemy_one
minus_dollars = 'MONEY: {}'.format(clicks)
money.configure(text=minus_dollars)
result_percent = price_dps_enemy_one - (price_dps_enemy_one // 100 * 94)
price_dps_enemy_one += result_percent
priceplus = 'PRICE: {}'.format(price_dps_enemy_one)
dpsenemy1price.configure(text=priceplus)
def dpsenemy2():
pass
wind = Tk()
imagemonster = PhotoImage(file='Image\\eepp.png')
imageenemyone = PhotoImage(file='Image\\ENEMY1.png')
imageenemytwo = PhotoImage(file='Image\\ENEMY2.png')
imageenemythree = PhotoImage(file='Image\\ENEMY3.png')
imagedpsenemyone = PhotoImage(file='Image\\DPSENEMY1.png')
imagedpsenemytwo = PhotoImage(file='Image\\DPSENEMY2.png')
WIDTH, HEIGHT = 700, 510
POS_X, POS_Y = wind.winfo_screenwidth() // 2 - WIDTH // 2, wind.winfo_screenheight() // 2 - HEIGHT // 2
wind.geometry(f"{WIDTH}x{HEIGHT}+{POS_X}+{POS_Y}")
wind.resizable(False, False)
wind.title('ExpMasClick')
wind.config(bg='#FFA07A')
money = Label(wind, text='MONEY: 0 ', bg='#FFA06A', font=('segoe print', 14, 'bold'))
money.place(x=5, y=0)
damage = Label(wind, text='DAMAGE: 1 ', bg='#FFA06A', font=('segoe print', 13, 'bold'))
damage.place(x=5, y=43)
dps = Label(wind, text='DPS: 0', bg='#FFA06A', font=('segoe print', 13, 'bold'))
dps.place(x=5, y=86)
enemy1price = Label(wind, text='PRICE: 100', bg='#FFA06A', font=('segoe print', 13, 'bold'))
enemy1price.place(x=70, y=320)
enemy2price = Label(wind, text='PRICE: 350', bg='#FFA06A', font=('segoe print', 13, 'bold'))
enemy2price.place(x=295, y=320)
enemy3price = Label(wind, text='PRICE: 800 ', bg='#FFA06A', font=('segoe print', 13, 'bold'))
enemy3price.place(x=520, y=320)
dpsenemy1price = Label(wind, text='PRICE: 200', bg='#FFA06A', font=('segoe print', 13, 'bold'))
dpsenemy1price.place(x=150, y=465)
dpsenemy2price = Label(wind, text='PRICE: 1000', bg='#FFA06A', font=('segoe print', 13, 'bold'))
dpsenemy2price.place(x=440, y=465)
monster = Button(wind, image=imagemonster, bd=0, bg='#FFA07A', activebackground='#FFA07A',
command=money_click)
monster.place(x=250, y=5)
enemyone = Button(wind, text='0', image=imageenemyone, bd=4, bg='#FFA05A', padx=6, activebackground='#FFA07A',
compound='bottom', font=('segoe print', 15, 'bold'), command=enemy1)
enemyone.place(x=40, y=215)
enemytwo = Button(wind, text='0', image=imageenemytwo, bd=4, bg='#FFA05A', padx=6, activebackground='#FFA07A',
compound='bottom', font=('segoe print', 15, 'bold'), command=enemy2)
enemytwo.place(x=265, y=215)
enemythree = Button(wind, text='0', image=imageenemythree, bd=4, bg='#FFA05A', padx=6, activebackground='#FFA07A',
compound='bottom', font=('segoe print', 15, 'bold'), command=enemy3)
enemythree.place(x=490, y=215)
dps_enemy_one = Button(wind, text='0', image=imagedpsenemyone, bd=4, bg='#FFA03A', padx=6, activebackground='#FFA07A',
compound='bottom', font=('segoe print', 15, 'bold'), command=dpsenemy1)
dps_enemy_one.place(x=85, y=360)
dps_enemy_two = Button(wind, text='0', image=imagedpsenemytwo, bd=4, bg='#FFA03A', padx=6, activebackground='#FFA07A',
compound='bottom', font=('segoe print', 15, 'bold'), command=dpsenemy2)
dps_enemy_two.place(x=375, y=360)
ex = Button(wind, text='QUIT', bd=4, bg='#FFA05A', activebackground='#FFA07A', padx='10',
font=('segoe print', 9, 'bold'))
ex['command'] = quit
ex.place(x=630, y=5)
wind.mainloop()
1
Ответы (1 шт):
Проблема решена с помощью метода after()
Вот измененный код с решенной задачей:
from tkinter import *
clicks = 0
put, pat = 1, 0
damages = 1
dps_marker = 0
more_enemy1, more_enemy2, more_enemy3 = 0, 0, 0
more_dps_enemy1, more_dps_enemy2 = 0, 0
price_enemy_one, price_enemy_two, price_enemy_three = 100, 350, 800
price_dps_enemy_one, price_dps_enemy_two = 2, 1000
def money_click():
global clicks
global put
clicks += put
plusmoney = 'MONEY: {}'.format(clicks)
money.configure(text=plusmoney)
def enemy1():
global put, clicks, damages, more_enemy1, price_enemy_one
if clicks >= price_enemy_one:
more_enemy1 += 1
put += 3
damages = put
plusdamage = 'DAMAGE: {}'.format(damages)
damage.configure(text=plusdamage)
plusmast = '{}'.format(more_enemy1)
enemyone.configure(text=plusmast)
clicks -= price_enemy_one
minus_dollars = 'MONEY: {}'.format(clicks)
money.configure(text=minus_dollars)
result_percent = price_enemy_one - (price_enemy_one // 100 * 94)
price_enemy_one += result_percent
priceplus = 'PRICE: {}'.format(price_enemy_one)
enemy1price.configure(text=priceplus)
def enemy2():
global put, clicks, damages, more_enemy2, price_enemy_two
if clicks >= price_enemy_two:
more_enemy2 += 1
put += 5
damages = put
plusdamage = 'DAMAGE: {}'.format(damages)
damage.configure(text=plusdamage)
plusmast = '{}'.format(more_enemy2)
enemytwo.configure(text=plusmast)
clicks -= price_enemy_two
minus_dollars = 'MONEY: {}'.format(clicks)
money.configure(text=minus_dollars)
result_percent = price_enemy_two - (price_enemy_two // 100 * 94)
price_enemy_two += result_percent
priceplus = 'PRICE: {}'.format(price_enemy_two)
enemy2price.configure(text=priceplus)
def enemy3():
global put, clicks, damages, more_enemy3, price_enemy_three
if clicks >= price_enemy_three:
more_enemy3 += 1
put += 10
damages = put
plusdamage = 'DAMAGE: {}'.format(damages)
damage.configure(text=plusdamage)
plusmast = '{}'.format(more_enemy3)
enemythree.configure(text=plusmast)
clicks -= price_enemy_three
minus_dollars = 'MONEY: {}'.format(clicks)
money.configure(text=minus_dollars)
result_percent = price_enemy_three - (price_enemy_three // 100 * 94)
price_enemy_three += result_percent
priceplus = 'PRICE: {}'.format(price_enemy_three)
enemy3price.configure(text=priceplus)
def dpsenemy1():
global dps_marker, pat, clicks, more_dps_enemy1, price_dps_enemy_one, money
if clicks >= price_dps_enemy_one:
more_dps_enemy1 += 1
pat += 3
dps_marker = pat
plusdamage = 'DPS: {}'.format(dps_marker)
dps.configure(text=plusdamage)
plusmast = '{}'.format(more_dps_enemy1)
dps_enemy_one.configure(text=plusmast)
clicks -= price_dps_enemy_one
minus_dollars = 'MONEY: {}'.format(clicks)
money.configure(text=minus_dollars)
result_percent = price_dps_enemy_one - (price_dps_enemy_one // 100 * 94)
price_dps_enemy_one += result_percent
priceplus = 'PRICE: {}'.format(price_dps_enemy_one)
dpsenemy1price.configure(text=priceplus)
def recursion():
global clicks, pat, money
clicks += pat
plusmone = 'MONEY: {}'.format(clicks)
money.configure(text=plusmone)
wind.after(1000, recursion)
def dpsenemy2():
pass
wind = Tk()
imagemonster = PhotoImage(file='Image\\eepp.png')
imageenemyone = PhotoImage(file='Image\\ENEMY1.png')
imageenemytwo = PhotoImage(file='Image\\ENEMY2.png')
imageenemythree = PhotoImage(file='Image\\ENEMY3.png')
imagedpsenemyone = PhotoImage(file='Image\\DPSENEMY1.png')
imagedpsenemytwo = PhotoImage(file='Image\\DPSENEMY2.png')
WIDTH, HEIGHT = 700, 550
POS_X, POS_Y = wind.winfo_screenwidth() // 2 - WIDTH // 2, wind.winfo_screenheight() // 2 - HEIGHT // 2
wind.geometry(f"{WIDTH}x{HEIGHT}+{POS_X}+{POS_Y}")
wind.resizable(False, False)
wind.title('ExpMasClick')
wind.config(bg='#FFA07A')
money = Label(wind, text='MONEY: 0 ', bg='#FFA06A', font=('segoe print', 14, 'bold'))
money.place(x=5, y=0)
damage = Label(wind, text='DAMAGE: 1 ', bg='#FFA06A', font=('segoe print', 13, 'bold'))
damage.place(x=5, y=43)
dps = Label(wind, text='DPS: 0', bg='#FFA06A', font=('segoe print', 13, 'bold'))
dps.place(x=5, y=86)
enemy1price = Label(wind, text='PRICE: 100', bg='#FFA06A', font=('segoe print', 13, 'bold'))
enemy1price.place(x=70, y=360)
enemy2price = Label(wind, text='PRICE: 350', bg='#FFA06A', font=('segoe print', 13, 'bold'))
enemy2price.place(x=295, y=360)
enemy3price = Label(wind, text='PRICE: 800 ', bg='#FFA06A', font=('segoe print', 13, 'bold'))
enemy3price.place(x=520, y=360)
dpsenemy1price = Label(wind, text='PRICE: 200', bg='#FFA06A', font=('segoe print', 13, 'bold'))
dpsenemy1price.place(x=150, y=505)
dpsenemy2price = Label(wind, text='PRICE: 1000', bg='#FFA06A', font=('segoe print', 13, 'bold'))
dpsenemy2price.place(x=440, y=505)
monster = Button(wind, image=imagemonster, bd=0, bg='#FFA07A', activebackground='#FFA07A',
command=money_click)
monster.place(x=250, y=5)
enemyone = Button(wind, text='0', image=imageenemyone, bd=4, bg='#FFA05A', padx=6, activebackground='#FFA07A',
compound='bottom', font=('segoe print', 15, 'bold'), command=enemy1)
enemyone.place(x=40, y=255)
enemytwo = Button(wind, text='0', image=imageenemytwo, bd=4, bg='#FFA05A', padx=6, activebackground='#FFA07A',
compound='bottom', font=('segoe print', 15, 'bold'), command=enemy2)
enemytwo.place(x=265, y=255)
enemythree = Button(wind, text='0', image=imageenemythree, bd=4, bg='#FFA05A', padx=6, activebackground='#FFA07A',
compound='bottom', font=('segoe print', 15, 'bold'), command=enemy3)
enemythree.place(x=490, y=255)
dps_enemy_one = Button(wind, text='0', image=imagedpsenemyone, bd=4, bg='#FFA03A', padx=6, activebackground='#FFA07A',
compound='bottom', font=('segoe print', 15, 'bold'), command=dpsenemy1)
dps_enemy_one.place(x=85, y=400)
dps_enemy_two = Button(wind, text='0', image=imagedpsenemytwo, bd=4, bg='#FFA03A', padx=6, activebackground='#FFA07A',
compound='bottom', font=('segoe print', 15, 'bold'), command=dpsenemy2)
dps_enemy_two.place(x=375, y=400)
ex = Button(wind, text='QUIT', bd=4, bg='#FFA05A', activebackground='#FFA07A', padx='10',
font=('segoe print', 9, 'bold'))
ex['command'] = quit
ex.place(x=630, y=5)
recursion()
wind.mainloop()