-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguess-the-number.py
148 lines (116 loc) · 4.14 KB
/
guess-the-number.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import tkinter as tk
from tkinter.ttk import *
import random
import os
# for taskbar icon
basedir = os.path.dirname(__file__)
try:
from ctypes import windll # Only exists on Windows.
myappid = "mycompany.myproduct.subproduct.version"
windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
except ImportError:
pass
root = tk.Tk()
# root['background'] = '#856ff8' # ? Debug
root.title("Number Guessing game")
root.geometry('270x480')
root.resizable(0,0)
img = tk.PhotoImage(file="icon.png")
root.iconphoto(False, img)
MAX_LENGHT = 0
RANDOM_NUMBER = 0
GUESS = 0
ROUND = 0
TRIES = 5
WINS = 0
WIN_STREAK = 0
GATE_PASSAGE_FOR_STREAK = 0
def GET_MAX_LENGHT():
global RANDOM_NUMBER
global ROUND
global TRIES
global GATE_PASSAGE_FOR_STREAK
global WIN_STREAK
if GATE_PASSAGE_FOR_STREAK == 0:
WIN_STREAK = 0
else:
GATE_PASSAGE_FOR_STREAK = 0
TRIES = 5
streak_label.config(text=f"Win streak: {WIN_STREAK}")
guesses_left.config(text=f"Guesses left: {TRIES}")
higher_or_lower.forget()
SUBMIT_GUESSED_NUMBER.pack()
higher_or_lower.config(text="")
higher_or_lower.pack()
MAX_LENGHT = textbox.get(1.0, "end-1c")
max_number_label.config(text=f"Maximum lenght: {int(MAX_LENGHT)}")
ROUND += 1
round_label.config(text=f"Round: {ROUND}")
RANDOM_NUMBER = random.randint(1, int(MAX_LENGHT))
print(f"Random number is: {RANDOM_NUMBER}")
print(f"Round is: {ROUND}")
def GUESS_NUMBER():
global TRIES
global WINS
global WIN_STREAK
global GATE_PASSAGE_FOR_STREAK
GUESS = int(guess_textbox.get(1.0, "end-1c"))
if GUESS > RANDOM_NUMBER:
higher_or_lower.config(text="The number is less than guessed")
TRIES -= 1
guesses_left.config(text=f"Guesses left: {TRIES}")
GAME_OVER()
if GUESS < RANDOM_NUMBER:
higher_or_lower.config(text="The number is more than guessed")
TRIES -= 1
guesses_left.config(text=f"Guesses left: {TRIES}")
GAME_OVER()
if GUESS == RANDOM_NUMBER:
higher_or_lower.config(text="Congrats! You guessed the right number")
WINS += 1
WIN_STREAK += 1
GATE_PASSAGE_FOR_STREAK = 1
streak_label.config(text=f"Win streak: {WIN_STREAK}")
wins_label.config(text=f"Wins: {WINS}")
def GAME_OVER():
global GATE_PASSAGE_FOR_STREAK
global TRIES
global WIN_STREAK
print(GATE_PASSAGE_FOR_STREAK)
if TRIES <= 0:
higher_or_lower.config(text="You lost! Try another round")
SUBMIT_GUESSED_NUMBER.forget()
# Top
input_max_container = tk.Frame(root)
input_max_container.pack(anchor=tk.CENTER, padx=20, pady=20)
max_number_label = tk.Label(input_max_container, text="Choose the max number:")
max_number_label.pack(pady=(10, 0))
textbox = tk.Text(input_max_container, height=1, width=15)
textbox.pack(pady=(2, 10))
SUBMIT_MAX_NUMBER = tk.Button(
input_max_container, text="New Game", command=GET_MAX_LENGHT)
SUBMIT_MAX_NUMBER.pack()
# Middle
center_submit_container = tk.Frame(root)
center_submit_container.pack(anchor=tk.CENTER, padx=20, pady=20)
guess_submit_label = tk.Label(center_submit_container, text="Your guess: ")
guess_submit_label.pack()
guess_textbox = tk.Text(center_submit_container, height=1, width=15)
guess_textbox.pack(pady=(2, 10))
SUBMIT_GUESSED_NUMBER = tk.Button(
center_submit_container, text="Submit guess", command=GUESS_NUMBER)
SUBMIT_GUESSED_NUMBER.pack()
higher_or_lower = tk.Label(center_submit_container)
higher_or_lower.pack()
# Bottom
game_stats_container = tk.Frame(root)
game_stats_container.pack(anchor=tk.CENTER, side="bottom", padx=20, pady=20)
guesses_left = tk.Label(game_stats_container, text=f"Guesses left: {TRIES}")
guesses_left.pack()
round_label = tk.Label(game_stats_container, text=f"Round: {ROUND}")
round_label.pack()
wins_label = tk.Label(game_stats_container, text=f"Wins: {WINS}")
wins_label.pack()
streak_label = tk.Label(game_stats_container, text=f"Win streak: {WIN_STREAK}")
streak_label.pack()
root.mainloop()