-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
62 lines (51 loc) · 1.77 KB
/
game.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
import random
from draw import hangman_drawings
class Hangman:
INITIAL_HP = 7
def __init__(self, list_of_words):
self.list_of_words = list_of_words
self.letters = []
self.hp = self.INITIAL_HP
def play(self):
self.choose_word()
self.draw_board()
while True:
letter = self.provide_letter()
self.check_letter(letter)
self.draw_board()
self.draw_man()
print(f"Sprawdzone litery: {self.letters}\n")
if self.hp == 0:
print(f"Przegrałeś. Poszukiwane słowo to: {self.word}")
break
if "_" not in self.board:
print("Wygrałeś!")
break
def choose_word(self):
self.word = random.choice(self.list_of_words).upper()
def draw_board(self):
self.board = []
for letter in self.word:
if letter in self.letters:
self.board.append(letter)
else:
self.board.append("_")
print(" ".join(self.board))
def check_letter(self, letter):
self.letters.append(letter)
if letter not in self.word:
self.hp -= 1
def provide_letter(self):
letter = input("Wprowadź literę: ").upper()
while True:
if not letter.isalpha() or len(letter) != 1:
letter = input(f"Proszę wprowadź JEDNĄ LITERERĘ: ").upper()
elif letter in self.letters:
letter = input(
f"'{letter}' już sprawdzałeś! Proszę wprowadź inną literę: "
).upper()
else:
return letter
def draw_man(self):
if self.hp != self.INITIAL_HP:
print(hangman_drawings[self.hp])