-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.py
205 lines (169 loc) · 6.65 KB
/
project.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from tkinter import *
import random
from tkinter import messagebox
# Function to set number and lives
def set_number(level):
hearts = 0
number = 0
heading_text = ""
if (level == "easy"):
heading_text = " 1-20"
hearts = 4
number = random.randint(1,20)
elif (level == "medium"):
heading_text = " 1-50"
hearts = 5
number = random.randint(1,50)
else:
heading_text = " 1-100"
hearts = 7
number = random.randint(1,100)
return heading_text,hearts, number
# Forth page function
def forth_page():
def yes_action():
root.destroy()
first_page()
def no_action():
root.destroy()
exit()
root = Tk()
root.title("Guess number game")
root.geometry("400x500")
root['background']='#856ff8'
root.minsize(400,500)
root.maxsize(400,500)
greeting_text = Label(root, text="Do to want to play again?", font= ('Helvetica 20 bold'), bg= '#856ff8', fg='#18122B')
greeting_text.pack()
greeting_text.place(y=50, relx= 0.5, anchor='center')
btn_yes = Button(root, text = 'Yes', command= lambda: yes_action(),
font= ('Helvetica 10 normal'), bg="#443C68", fg="#ffffff")
btn_yes.pack()
btn_yes.place(y = 120, width=80, relx= 0.5, anchor='center')
btn_no = Button(root, text = 'No', command= lambda: no_action(),
font= ('Helvetica 10 normal'), bg="#cc3549")
btn_no.pack()
btn_no.place(y = 155, width=80, relx= 0.5, anchor='center')
root.mainloop()
# Forth page function
def third_page(level, player_name):
HEARTS = []
heading_text,hearts, number = set_number(level)
HEARTS.append(hearts)
def write_to_file(condition):
file = open("log.txt","a")
text = player_name + " " + condition + '\n'
file.write(text)
file.close()
def submit():
inp = inputtxt.get(1.0, "end-1c")
result = play_game(inp)
if(HEARTS[0] <= 0):
messagebox.showerror("Sorry", "You lost! Number was "+ str(number))
write_to_file("lost")
root.destroy()
forth_page()
if(result == 1):
messagebox.showinfo("Congrats", "You won!")
write_to_file("won")
root.destroy()
forth_page()
elif(result == 2):
messagebox.showerror("Wrong answer", "Go down!")
else:
messagebox.showerror("Wrong answer", "Go up!")
def play_game(inp):
if (int(inp) == number):
return 1
HEARTS[0] -= 1
hearts_text['text'] = str(HEARTS[0]) # This line update lives of user after guess a number wrong
if(int(inp) > int(number)):
return 2
else:
return 3
root = Tk()
root.title("Guess number game")
root.geometry("400x500")
root['background']='#856ff8'
root.minsize(400,500)
root.maxsize(400,500)
greeting_text = "Hi " + player_name
greeting_text = Label(root, text=greeting_text, font= ('Helvetica 20 bold'), bg= '#856ff8', fg='#18122B')
greeting_text.pack()
greeting_text.place(y=50, relx= 0.5, anchor='center')
text2 = Label(root, text='Choose a number between' + heading_text, font= ('Helvetica 12 normal'),
bg= '#856ff8', fg='#18122B')
text2.pack()
text2.place(y=90, relx= 0.5, anchor='center')
emoji = Label(root, text="\u2764\ufe0f", font= ('Helvetica 12 normal'), bg= '#856ff8', fg='#18122B')
emoji.pack()
emoji.place(anchor='nw')
hearts_text = Label(root, text=str(hearts), font= ('Helvetica 12 normal'), bg= '#856ff8', fg='#18122B')
hearts_text.pack()
hearts_text.place(x = 30, anchor='nw')
inputtxt = Text(root,height = 2,width = 20)
inputtxt.pack()
inputtxt.place(y = 150, relx= 0.5, anchor='center')
btn_easy = Button(root, text = 'Submit', command= lambda: submit(),
font= ('Helvetica 10 normal'), bg="#443C68", fg="#ffffff")
btn_easy.pack()
btn_easy.place(y = 200, width=80, relx= 0.5, anchor='center')
root.mainloop()
# Second page function
def second_page(palyer_name):
def game_window(level):
root.destroy()
third_page(level, palyer_name)
root = Tk()
root.title("Guess number game")
root.geometry("400x500")
root['background']='#856ff8'
root.minsize(400,500)
root.maxsize(400,500)
greeting_text_ = "Hi " + palyer_name
greeting_text = Label(root, text=greeting_text_, font= ('Helvetica 20 bold'), bg= '#856ff8', fg='#18122B')
greeting_text.pack()
greeting_text.place(y=50, relx= 0.5, anchor='center')
level_text = Label(root, text='Choose level of difficulty', font= ('Helvetica 12 normal'), bg= '#856ff8', fg='#18122B')
level_text.pack()
level_text.place(y=90, relx= 0.5, anchor='center')
btn_easy = Button(root, text = 'Easy',command=lambda: game_window("easy"),
font= ('Helvetica 10 normal'), bg="#443C68", fg="#ffffff")
btn_medium = Button(root, text = 'Medium',command=lambda: game_window("medium"),
font= ('Helvetica 10 normal'), bg="#443C68", fg="#ffffff")
btn_hard = Button(root, text = 'Hard',command=lambda: game_window("hard"),
font= ('Helvetica 10 normal'), bg="#443C68", fg="#ffffff")
btn_easy.pack()
btn_easy.place(y = 150, width=80, relx= 0.5, anchor='center')
btn_medium.pack()
btn_medium.place(y = 185, width=80, relx= 0.5, anchor='center')
btn_hard.pack()
btn_hard.place(y = 220, width=80, relx= 0.5, anchor='center')
root.mainloop()
# First page
def first_page():
def submit():
inp = inputtxt.get(1.0, "end-1c")
root.destroy()
second_page(inp)
root = Tk()
root.title("Guess number game")
root.geometry("400x500")
root['background']='#856ff8'
root.minsize(400,500)
root.maxsize(400,500)
greeting_text = Label(root, text='Welcome to my game', font= ('Helvetica 20 bold'), bg= '#856ff8', fg='#18122B')
greeting_text.pack()
greeting_text.place(y=50, relx= 0.5, anchor='center')
level_text = Label(root, text='Enter your name', font= ('Helvetica 12 normal'), bg= '#856ff8', fg='#18122B')
level_text.pack()
level_text.place(y=90, relx= 0.5, anchor='center')
inputtxt = Text(root,height = 2,width = 20)
inputtxt.pack()
inputtxt.place(y = 150, relx= 0.5, anchor='center')
btn_easy = Button(root, text = 'Submit',command=lambda: submit(),
font= ('Helvetica 10 normal'), bg="#443C68", fg="#ffffff")
btn_easy.pack()
btn_easy.place(y = 200, width=80, relx= 0.5, anchor='center')
root.mainloop()
first_page()