|
| 1 | + |
| 2 | +from tkinter import * |
| 3 | +import os, sys |
| 4 | +from random import randint |
| 5 | + |
| 6 | + |
| 7 | +class Main(): |
| 8 | + |
| 9 | + """The gallows game: |
| 10 | + - player choose the letter and if the word includes this letter player get 1 point (if: 0 < points < 7) |
| 11 | + - elif get -1 point (if: 0 < points < 7) |
| 12 | + - else game is over""" |
| 13 | + |
| 14 | + def __init__(self): |
| 15 | + |
| 16 | + """Create windows and settings""" |
| 17 | + |
| 18 | + self.root = Tk() |
| 19 | + self.WDIR = os.getcwd() |
| 20 | + |
| 21 | + self.BG = '#000000' |
| 22 | + self.FG = '#ffffff' |
| 23 | + self.FONT = 'Courier 20' |
| 24 | + |
| 25 | + self.answer = [] |
| 26 | + self.question = 'Play' |
| 27 | + |
| 28 | + self.questL = None |
| 29 | + self.ansL = None |
| 30 | + self.wordL = None |
| 31 | + self.gallows = None |
| 32 | + |
| 33 | + self.points = 1 |
| 34 | + self.dictionary = None |
| 35 | + self.word = None |
| 36 | + self.users_word = None |
| 37 | + self.result = None |
| 38 | + |
| 39 | + self.root.attributes('-fullscreen', True) |
| 40 | + self.root.config(background=self.BG, cursor='none') |
| 41 | + |
| 42 | + self.GUI() |
| 43 | + |
| 44 | + |
| 45 | + def _show_gallows(self): |
| 46 | + |
| 47 | + """Return gallows stage by .txt file in game data""" |
| 48 | + |
| 49 | + with open(os.path.join(self.WDIR, 'game', 'stages', f'{self.points}.txt'), 'r', encoding='utf-8') as f: |
| 50 | + return f.read() |
| 51 | + |
| 52 | + |
| 53 | + def _random_word(self): |
| 54 | + |
| 55 | + """Gets a random word from the current dictionary""" |
| 56 | + |
| 57 | + with open(os.path.join(self.WDIR, 'game', 'dicts', self.dictionary), 'r', encoding='utf-8') as f: |
| 58 | + lines = f.readlines() |
| 59 | + return lines[randint(1, len(lines)-1)][0:-1] |
| 60 | + |
| 61 | + |
| 62 | + def _Gameplay(self): |
| 63 | + |
| 64 | + """Gameplay process: questions-answers (word guessing)""" |
| 65 | + |
| 66 | + def create_widgets(): |
| 67 | + |
| 68 | + self.questL['text'] = '>>> Guess letter or write full word: ' |
| 69 | + self.question = 'Game' |
| 70 | + |
| 71 | + self.ansL.place(relx=.37,rely=.02) |
| 72 | + |
| 73 | + w = Label(self.root, |
| 74 | + text=self.users_word, |
| 75 | + background=self.BG, |
| 76 | + foreground=self.FG, |
| 77 | + font=self.FONT, |
| 78 | + pady=20) |
| 79 | + |
| 80 | + w.pack() |
| 81 | + w.place(relx=0.02,rely=0.75) |
| 82 | + |
| 83 | + return w |
| 84 | + |
| 85 | + self.word = self._random_word().lower() |
| 86 | + self.users_word = ['_' for i in self.word] |
| 87 | + |
| 88 | + self.wordL = create_widgets() |
| 89 | + |
| 90 | + |
| 91 | + def GUI(self): |
| 92 | + |
| 93 | + """Main loop of draw labels and choice the level hard""" |
| 94 | + |
| 95 | + def inp(event): |
| 96 | + |
| 97 | + """Control user's input: |
| 98 | + Esc - exit, |
| 99 | + Backspace - delete symbol, |
| 100 | + [a-zA-z0-9] - append symbol, |
| 101 | + Enter - Do something""" |
| 102 | + |
| 103 | + textAns = ''.join(self.answer).lower() |
| 104 | + |
| 105 | + # Service keys: |
| 106 | + if event.keysym == 'Escape': |
| 107 | + sys.exit() |
| 108 | + |
| 109 | + elif event.keysym == 'BackSpace': |
| 110 | + del self.answer[-1] |
| 111 | + self.ansL['text'] = ''.join(self.answer) |
| 112 | + return |
| 113 | + |
| 114 | + elif event.keysym == 'Return': |
| 115 | + |
| 116 | + if self.question == 'Play': |
| 117 | + |
| 118 | + if textAns == 'y': |
| 119 | + |
| 120 | + self.answer.clear() |
| 121 | + self.ansL['text'] = '_' |
| 122 | + self.ansL.place(relx=.56,rely=.02) |
| 123 | + |
| 124 | + self.question = 'Difficulty' |
| 125 | + self.questL['text'] = '>>> Choose difficulty (e - Easy, m - Medium, h - Hard): ' |
| 126 | + |
| 127 | + elif textAns == 'n': |
| 128 | + sys.exit() |
| 129 | + |
| 130 | + else: |
| 131 | + self.answer.clear() |
| 132 | + self.ansL['text'] = '_' |
| 133 | + return |
| 134 | + |
| 135 | + elif self.question == 'Difficulty': |
| 136 | + |
| 137 | + if textAns == 'e': |
| 138 | + self.dictionary = 'easy.txt' |
| 139 | + self._Gameplay() |
| 140 | + |
| 141 | + elif textAns == 'm': |
| 142 | + self.dictionary = 'medium.txt' |
| 143 | + self._Gameplay() |
| 144 | + |
| 145 | + elif textAns == 'h': |
| 146 | + self.dictionary = 'hard.txt' |
| 147 | + self._Gameplay() |
| 148 | + |
| 149 | + self.answer.clear() |
| 150 | + self.ansL['text'] = '_' |
| 151 | + |
| 152 | + elif self.question == 'Game': |
| 153 | + |
| 154 | + if (self.word.count(textAns) == 0) or (self.users_word.count(textAns) > 0): |
| 155 | + |
| 156 | + if self.points == 6: |
| 157 | + self.result = False |
| 158 | + |
| 159 | + self.points += 1 |
| 160 | + |
| 161 | + elif self.word == textAns: |
| 162 | + self.result = True |
| 163 | + |
| 164 | + else: |
| 165 | + if self.points > 1: |
| 166 | + self.points -= 1 |
| 167 | + for n,i in enumerate(self.word): |
| 168 | + if self.word[n] == textAns: |
| 169 | + self.users_word[n] = textAns |
| 170 | + |
| 171 | + self.answer.clear() |
| 172 | + self.gallows['text'] = self._show_gallows() |
| 173 | + |
| 174 | + self.ansL['text'] = '_' |
| 175 | + self.wordL['text'] = self.users_word |
| 176 | + |
| 177 | + if ''.join(self.users_word) == self.word: |
| 178 | + self.result = True |
| 179 | + |
| 180 | + # If won: |
| 181 | + if self.result: |
| 182 | + |
| 183 | + self.questL['text'] = f'>>> You won! This word is {self.word}!' |
| 184 | + self.ansL.destroy() |
| 185 | + self.ansL.after(2000, self.__init__) |
| 186 | + |
| 187 | + # If lose: |
| 188 | + elif self.result is False: |
| 189 | + |
| 190 | + self.questL['text'] = f'>>> You won! This word is {self.word}...' |
| 191 | + self.ansL.destroy() |
| 192 | + self.ansL.after(2000, self.__init__) |
| 193 | + |
| 194 | + |
| 195 | + # An other: |
| 196 | + if len(event.keysym) == 1: |
| 197 | + self.answer.append(event.keysym) |
| 198 | + self.ansL['text'] = ''.join(self.answer) |
| 199 | + |
| 200 | + |
| 201 | + def create_widgets(): |
| 202 | + |
| 203 | + """Create a question, an invisible text form and show gallows""" |
| 204 | + |
| 205 | + q = Label(self.root, |
| 206 | + text='>>> Wanna play a game (Y/n): ', |
| 207 | + background=self.BG, |
| 208 | + foreground=self.FG, |
| 209 | + font=self.FONT, |
| 210 | + pady=20) |
| 211 | + |
| 212 | + q.pack() |
| 213 | + q.place(relx=0,rely=0) |
| 214 | + |
| 215 | + a = Label(self.root, |
| 216 | + text='_', |
| 217 | + background=self.BG, |
| 218 | + foreground=self.FG, |
| 219 | + font=self.FONT) |
| 220 | + |
| 221 | + a.pack() |
| 222 | + a.place(relx=.29,rely=.02) |
| 223 | + |
| 224 | + g = Label(self.root, |
| 225 | + text=self._show_gallows(), |
| 226 | + background=self.BG, |
| 227 | + foreground=self.FG, |
| 228 | + font=self.FONT) |
| 229 | + |
| 230 | + g.pack() |
| 231 | + g.place(relx=0,rely=.06) |
| 232 | + |
| 233 | + return q, a, g |
| 234 | + |
| 235 | + self.questL, self.ansL, self.gallows = create_widgets() |
| 236 | + self.root.bind("<Key>", inp) |
| 237 | + |
| 238 | + |
| 239 | +if __name__ == '__main__': |
| 240 | + main = Main() |
| 241 | + main.root.mainloop() |
0 commit comments