-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.pyw
240 lines (161 loc) · 5.69 KB
/
main.pyw
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
from tkinter import *
import os, sys
from random import randint
class Main():
"""The gallows game:
- player choose the letter and if the word includes this letter player get 1 point (if: 0 < points < 7)
- elif get -1 point (if: 0 < points < 7)
- else game is over"""
def __init__(self):
"""Create windows and settings"""
self.root = Tk()
self.WDIR = os.getcwd()
self.BG = '#000000'
self.FG = '#ffffff'
self.FONT = 'Courier 20'
self.answer = []
self.question = 'Play'
self.questL = None
self.ansL = None
self.wordL = None
self.gallows = None
self.points = 1
self.dictionary = None
self.word = None
self.users_word = None
self.result = None
self.root.attributes('-fullscreen', True)
self.root.config(background=self.BG, cursor='none')
self.GUI()
def _show_gallows(self):
"""Return gallows stage by .txt file in game data"""
with open(os.path.join(self.WDIR, 'game', 'stages', f'{self.points}.txt'), 'r', encoding='utf-8') as f:
return f.read()
def _random_word(self):
"""Gets a random word from the current dictionary"""
with open(os.path.join(self.WDIR, 'game', 'dicts', self.dictionary), 'r', encoding='utf-8') as f:
lines = f.readlines()
return lines[randint(1, len(lines)-1)][0:-1]
def _Gameplay(self):
"""Gameplay process: questions-answers (word guessing)"""
def create_widgets():
self.questL['text'] = '>>> Guess letter or write full word: '
self.question = 'Game'
self.ansL.place(relx=.37,rely=.02)
w = Label(self.root,
text=self.users_word,
background=self.BG,
foreground=self.FG,
font=self.FONT,
pady=20)
w.pack()
w.place(relx=0.02,rely=0.75)
return w
self.word = self._random_word().lower()
self.users_word = ['_' for i in self.word]
self.wordL = create_widgets()
def GUI(self):
"""Main loop of draw labels and choice the level hard"""
def inp(event):
"""Control user's input:
Esc - exit,
Backspace - delete symbol,
[a-zA-z0-9] - append symbol,
Enter - Do something"""
textAns = ''.join(self.answer).lower()
# Service keys:
if event.keysym == 'Escape':
sys.exit()
elif event.keysym == 'BackSpace':
del self.answer[-1]
self.ansL['text'] = ''.join(self.answer)
return
elif event.keysym == 'Return':
if self.question == 'Play':
if textAns == 'y':
self.answer.clear()
self.ansL['text'] = '_'
self.ansL.place(relx=.56,rely=.02)
self.question = 'Difficulty'
self.questL['text'] = '>>> Choose difficulty (e - Easy, m - Medium, h - Hard): '
elif textAns == 'n':
sys.exit()
else:
self.answer.clear()
self.ansL['text'] = '_'
return
elif self.question == 'Difficulty':
if textAns == 'e':
self.dictionary = 'easy.txt'
self._Gameplay()
elif textAns == 'm':
self.dictionary = 'medium.txt'
self._Gameplay()
elif textAns == 'h':
self.dictionary = 'hard.txt'
self._Gameplay()
self.answer.clear()
self.ansL['text'] = '_'
elif self.question == 'Game':
if (self.word.count(textAns) == 0) or (self.users_word.count(textAns) > 0):
if self.points == 6:
self.result = False
self.points += 1
elif self.word == textAns:
self.result = True
else:
if self.points > 1:
self.points -= 1
for n,i in enumerate(self.word):
if self.word[n] == textAns:
self.users_word[n] = textAns
self.answer.clear()
self.gallows['text'] = self._show_gallows()
self.ansL['text'] = '_'
self.wordL['text'] = self.users_word
if ''.join(self.users_word) == self.word:
self.result = True
# If won:
if self.result:
self.questL['text'] = f'>>> You won! This word is {self.word}!'
self.ansL.destroy()
self.ansL.after(2000, self.__init__)
# If lose:
elif self.result is False:
self.questL['text'] = f'>>> You won! This word is {self.word}...'
self.ansL.destroy()
self.ansL.after(2000, self.__init__)
# An other:
if len(event.keysym) == 1:
self.answer.append(event.keysym)
self.ansL['text'] = ''.join(self.answer)
def create_widgets():
"""Create a question, an invisible text form and show gallows"""
q = Label(self.root,
text='>>> Wanna play a game (Y/n): ',
background=self.BG,
foreground=self.FG,
font=self.FONT,
pady=20)
q.pack()
q.place(relx=0,rely=0)
a = Label(self.root,
text='_',
background=self.BG,
foreground=self.FG,
font=self.FONT)
a.pack()
a.place(relx=.29,rely=.02)
g = Label(self.root,
text=self._show_gallows(),
background=self.BG,
foreground=self.FG,
font=self.FONT)
g.pack()
g.place(relx=0,rely=.06)
return q, a, g
self.questL, self.ansL, self.gallows = create_widgets()
self.root.bind("<Key>", inp)
if __name__ == '__main__':
main = Main()
main.root.mainloop()