-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame-with-ui.py
More file actions
114 lines (87 loc) · 2.51 KB
/
game-with-ui.py
File metadata and controls
114 lines (87 loc) · 2.51 KB
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
from tkinter import *
import random
window = Tk()
window.title("Guess The Number Game")
window.configure(bg="#0b1220")
window.geometry("600x500")
welcome = Frame(window, bg = "black")
welcome.pack(fill="both", expand=True)
top_spacer = Frame(welcome, bg="black")
top_spacer.pack(fill="both", expand=True)
label1 = Label(welcome, text = "Welcome to guessing game!",
bg = "black",
fg = "#4cc9ff",
font = ("Arial", 25, "bold"))
label1.pack()
label2 = Label(welcome, text = "Press start button",
bg = "black",
fg = "#bdebff",
font = ("Arial", 15, "bold"))
label2.pack()
num = None
chances = 10
def start():
global num
num = random.randint(1,50)
chances = 10
result_label.config(text="You have 10 chances.")
entry.delete(0, END)
welcome.pack_forget()
title_frame.pack(fill = "x")
button = Button(welcome, text = "start", font = ("Arial", 20),
bg = "#1C4D8D",
fg = "white" ,
command = start)
button.pack()
bottom_spacer = Frame(welcome, bg="black")
bottom_spacer.pack(fill="both", expand=True)
title_frame = Frame(window, bg="#0b1220")
label = Label(
title_frame,
text="Guess number between 1-50!",
font= ("Arial", 25, "bold"),
fg="#4cc9ff",
bg="#0b1220")
label.pack()
entry = Entry(
title_frame,
font=("Arial", 15),
bg="#BDE8F5",
fg="black",
width=15,
justify="center"
)
entry.pack(pady=5)
result_label = Label(
title_frame,
text="",
bg="#0b1220",
fg="white",
font=("Arial", 12)
)
result_label.pack(pady=5)
def game():
global num, chances
guess = entry.get()
if not guess.isdigit():
result_label.config(text="Enter a valid number.")
return
user_guess = int(guess)
if user_guess == num:
result_label.config(text = "You gussed it right, You WON! ")
return
elif user_guess<num:
chances -= 1
result_label.config(text =f"Your guess is too low, chances left: {chances}")
elif user_guess > num:
chances -= 1
result_label.config(text = f"Your guess is too high, chances left: {chances}")
if chances <= 0 and user_guess!= num:
result_label.config(text =f"You lost all chances, number was: {num}")
entry.delete(0, END)
button2 = Button(title_frame , text = "Check",
bg = "#79C9C5",
fg = "white",
command = game)
button2.pack(pady = 10)
window.mainloop()