SaveText.Ru

тамагочи
  1. import tkinter as tk
  2. from tkinter import ttk
  3.  
  4. from my_class import Pet
  5.  
  6. import time
  7.  
  8. def start_game(animal):
  9.     display.destroy()
  10.     display_game = tk.Tk()
  11.  
  12.     label = tk.Label(display_game, text=f'Имя {animal.name} nВозраст: {animal.age} nПол: {animal.gender}')
  13.     label.grid(row=0, column=0)
  14.  
  15.     label_money = tk.Label(display_game, text=f'Всего денег {animal.money}')
  16.     label_money.grid(row=1, column=0)
  17.  
  18.     btn_money = ttk.Button(display_game, text='Пойти на работу', command=lambda: work(animal))
  19.     btn_money.grid(row=2, column=0)
  20.  
  21.     label_test = tk.Label(display_game, bg='black', height=20, width=6)
  22.     label_test2 = tk.Label(display_game, bg='black', height=20, width=6)
  23.     label_test3 = tk.Label(display_game, bg='black', height=20, width=6)
  24.  
  25.     label_test .grid(row=1, column=1, sticky=tk.S)
  26.     label_test2.grid(row=1, column=2, sticky=tk.S)
  27.     label_test3.grid(row=1, column=3, sticky=tk.S)
  28.  
  29.     label_hp = tk.Label(display_game, text=animal.hp, bg='red', height=percent(animal.hp), width=6)
  30.     label_en = tk.Label(display_game, text=animal.energy, bg='red', height=percent(animal.energy), width=6)
  31.     label_mood = tk.Label(display_game, text=animal.mood, bg='red', height=percent(animal.mood), width=6)
  32.     label_hp.grid(row=1, column=1, sticky=tk.S)
  33.     label_en.grid(row=1, column=2, sticky=tk.S)
  34.     label_mood.grid(row=1, column=3, sticky=tk.S)
  35.  
  36.     btn_hp = ttk.Button(display_game, text='Покормить', command=lambda: buff(animal, 'Покормить'))
  37.     btn_hp.grid(row=2, column=1)
  38.     btn_en = ttk.Button(display_game, text='Дать отдохнуть', command=lambda: buff(animal, 'Отдохнуть'))
  39.     btn_en.grid(row=2, column=2)
  40.     btn_mood = ttk.Button(display_game, text='Поиграть', command=lambda: buff(animal, 'Поиграть'))
  41.     btn_mood.grid(row=2, column=3)
  42.  
  43.     label_hp_text = tk.Label(display_game, text='Голод')
  44.     label_hp_text.grid(row=0, column=1)
  45.     label_en_text = tk.Label(display_game, text='Энергия')
  46.     label_en_text.grid(row=0, column=2)
  47.     label_mood_text = tk.Label(display_game, text='Настроение')
  48.     label_mood_text.grid(row=0, column=3)
  49.  
  50.     def work(animal):
  51.         btn_hp.configure(state='disable')
  52.         btn_en.configure(state='disable')
  53.         btn_mood.configure(state='disable')
  54.         btn_money.configure(state='disable')
  55.         display_game.after(5000, lambda: btn_hp.configure(state='enable'))
  56.         display_game.after(5000, lambda: btn_en.configure(state='enable'))
  57.         display_game.after(5000, lambda: btn_mood.configure(state='enable'))
  58.         display_game.after(5000, lambda: btn_money.configure(state='enable'))
  59.         animal.money += 200
  60.         label_money.configure(text=f'Всего денег {animal.money}')
  61.  
  62.     def game_har(animal):
  63.         if animal.hp > 0:
  64.             animal.hp -= 10
  65.             label_hp.configure(height=percent(animal.hp), text=animal.hp)
  66.             display_game.after(2000, lambda: game_har(animal))
  67.         else:
  68.             finish()
  69.  
  70.     display_game.after(2000, lambda: game_har(animal))
  71.  
  72.     def game_en(animal):
  73.         if animal.energy > 0:
  74.             animal.energy -= 10
  75.             label_en.configure(height=percent(animal.energy), text=animal.energy)
  76.             display_game.after(2500, lambda: game_en(animal))
  77.         else:
  78.             animal.hp -= 10
  79.             label_hp.configure(height=percent(animal.hp), text=animal.hp)
  80.         display_game.after(2500, lambda: game_en(animal))
  81.  
  82.     display_game.after(2500, lambda: game_en(animal))
  83.  
  84.     def game_mood(animal):
  85.         if animal.mood > 0:
  86.             animal.mood -= 10
  87.             label_mood.configure(height=percent(animal.mood), text=animal.mood)
  88.             display_game.after(3000, lambda: game_mood(animal))
  89.  
  90.     display_game.after(3000, lambda: game_mood(animal))
  91.  
  92.  
  93.  
  94.     def buff(animal, arg):
  95.  
  96.         if arg == 'Покормить' and animal.hp < 100 and animal.money >= 50:
  97.             animal.hp += 30
  98.             animal.money -= 50
  99.             label_money.configure(text=f'Всего денег {animal.money}')
  100.             label_hp.configure(height=percent(animal.hp), text=animal.hp)
  101.  
  102.         elif arg == 'Отдохнуть' and animal.energy < 100:
  103.             animal.energy += 40
  104.             if animal.energy > 100:
  105.                 animal.energy = 100
  106.             btn_mood.configure(state='disabled')
  107.             btn_en.configure(state='disabled')
  108.             btn_hp.configure(state='disabled')
  109.             btn_money.configure(state='disabled')
  110.  
  111.             display_game.after(4000, lambda: btn_hp.configure(state='enable'))
  112.             display_game.after(4000, lambda: btn_en.configure(state='enable'))
  113.             display_game.after(4000, lambda: btn_mood.configure(state='enable'))
  114.             display_game.after(4000, lambda: btn_money.configure(state='enable'))
  115.  
  116.             label_en.configure(height=percent(animal.energy), text=animal.energy)
  117.  
  118.         elif arg == 'Поиграть' and animal.mood < 100 and animal.energy >= 10 and animal.hp >=5:
  119.             animal.mood += 20
  120.             if animal.mood > 100:
  121.                 animal.mood = 100
  122.             animal.energy -= 10
  123.             animal.hp -= 5
  124.  
  125.             label_mood.configure(height=percent(animal.mood), text=animal.mood)
  126.             label_en.configure(height=percent(animal.energy), text=animal.energy)
  127.             label_hp.configure(height=percent(animal.hp), text=animal.hp)
  128.  
  129.  
  130.     def finish():
  131.         display_game.destroy()
  132.         animal.time = round(time.monotonic())
  133.         game_over = tk.Tk()
  134.         label_game_over = tk.Label(game_over, text=f'Время жизни {animal.time}', font=('Times New Roman', 120))
  135.         label_game_over.grid(row=0, column=0)
  136.  
  137. def percent(score):
  138.     perc = score * 0.2
  139.     if perc > 20:
  140.         perc = 20
  141.     return round(perc)
  142.  
  143.  
  144. def game():
  145.     global display
  146.     display = tk.Tk()
  147.     display.geometry('300x300')
  148.  
  149.     btn1 = ttk.Button(display, text='Начать новую игру', command=lambda: new_char())
  150.     btn1.place(x=70, y=120)
  151.  
  152.     btn2 = ttk.Button(display, text='Продолжить игру')
  153.     btn2.place(relx=0.25, rely=0.5)
  154.     display.mainloop()
  155.  
  156. def new_char():
  157.     char_okno = tk.Toplevel()
  158.  
  159.     label1 = tk.Label(char_okno, text='Имя питомца')
  160.     label2 = tk.Label(char_okno, text='Возраст питомца')
  161.     label3 = tk.Label(char_okno, text='Пол питомца')
  162.  
  163.     label1.grid(row=0,column=0)
  164.     label2.grid(row=1, column=0)
  165.     label3.grid(row=2, column=0)
  166.  
  167.     field1 = tk.Entry(char_okno)
  168.     field2 = tk.Entry(char_okno)
  169.     field3 = tk.Entry(char_okno)
  170.  
  171.     field1.grid(row=0, column=1)
  172.     field2.grid(row=1, column=1)
  173.     field3.grid(row=2, column=1)
  174.  
  175.     button = ttk.Button(char_okno, text='Создать питомца',
  176.                         command=lambda: pet_create(field1.get(),
  177.                                                    field2.get(),
  178.                                                    field3.get()))
  179.     button.grid(row=3, column=0)
  180.  
  181.     char_okno.mainloop()
  182.  
  183.  
  184. def pet_create(name, age, gender):
  185.     cat = Pet(name, age, gender)
  186.     print(cat.__dict__)
  187.     start_game(cat)
  188.  
  189.  
  190. if __name__ == '__main__':
  191.     game()

Share with your friends:

Print