-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_hangman.py
More file actions
145 lines (113 loc) · 4.21 KB
/
game_hangman.py
File metadata and controls
145 lines (113 loc) · 4.21 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
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
##############################################################################
# The Hangman "Game"
##############################################################################
import tkinter
import random
import time
width, height = 640, 480
canvas = tkinter.Canvas(width=width, height=height)
canvas.pack()
# GRAPHICS
def draw_gallows():
x, y = width / 2, height / 2 + 150
gnd = (x - 60, y, x + 60, y)
frame = (x, y, x, y - 250,
x, y - 250, x + 140, y - 250,
x + 140, y - 250, x + 140, y - 200)
canvas.create_line(gnd, width=10)
canvas.create_line(frame, width=5)
def draw_hangman():
x, y, r = width / 2, height / 2 + 150, 20
width_man = 3
head = (x + 140 - r, y - 200, x + 140 + r, y - 200 + 2 * r)
torso = (x + 140, y - 200 + 2 * r, x + 140, y - 200 + 2 * r + 80)
larm = (x + 140, y - 200 + 2 * r + 20, x + 110, y - 200 + 2 * r + 50)
rarm = (x + 140, y - 200 + 2 * r + 20, x + 170, y - 200 + 2 * r + 50)
lleg = (x + 140, y - 200 + 2 * r + 80, x + 110, y - 200 + 2 * r + 110)
rleg = (x + 140, y - 200 + 2 * r + 80, x + 170, y - 200 + 2 * r + 110)
head = canvas.create_oval(head, width=width_man, state=tkinter.HIDDEN)
torso = canvas.create_oval(torso, width=width_man, state=tkinter.HIDDEN)
larm = canvas.create_line(larm, width=width_man, state=tkinter.HIDDEN)
rarm = canvas.create_line(rarm, width=width_man, state=tkinter.HIDDEN)
lleg = canvas.create_line(lleg, width=width_man, state=tkinter.HIDDEN)
rleg = canvas.create_line(rleg, width=width_man, state=tkinter.HIDDEN)
return head, torso, larm, rarm, lleg, rleg
def draw_letters(letters):
start = width / 2 - 40 * len(letters) / 2
letter_ids = {letter: [] for letter in letters if letter != " "}
for letter in letters:
if letter == " ":
start += 40
continue
canvas.create_line(start + 5, 100, start + 35, 100, width=2)
idx = canvas.create_text(start + 20, 75, text=letter.upper(),
font="arial 30", state=tkinter.HIDDEN)
letter_ids[letter].append(idx)
start += 40
return letter_ids
def update_letters(letter_ids, already_guessed):
for letter, ids in letter_ids.items():
if letter in already_guessed:
for idx in ids:
canvas.itemconfig(idx, state=tkinter.NORMAL)
def update_hangman(wrong_guesses, hangman_ids):
for i in range(0, wrong_guesses):
canvas.itemconfig(hangman_ids[i], state=tkinter.NORMAL)
# GAME LOGIC
def load_word():
with open("data/words.txt", "r") as fp:
word_list = fp.read().splitlines()
random_word = random.choice(word_list)
return random_word
def good_guess(letter):
already_guessed.append(letter)
update_letters(letter_ids, already_guessed)
def bad_guess(letter):
global wrong_guesses
already_guessed.append(letter)
wrong_guesses += 1
update_hangman(wrong_guesses, hangman_ids)
def is_winner(letter_ids, already_guessed):
print("letter_ids {}".format(letter_ids))
print("already {} ".format(already_guessed))
for letter in letter_ids:
print("letter {}".format(letter))
if letter not in already_guessed:
print("letter IF {}".format(letter))
return False
return True
def check_game_state():
global game_state
if is_winner(letter_ids, already_guessed):
game_state = "WIN"
elif wrong_guesses > 5:
game_state = "LOSS"
def game_over(state):
if state == "WIN":
canvas.create_text(320, 240, text="NICE YOU WIN",
font="arial 60", fill="RED")
else:
canvas.create_text(320, 240, text="GAME OVER",
font="arial 60", fill="RED")
canvas.update()
time.sleep(3)
# INIT GLOBAL VARIABLES
draw_gallows()
hangman_ids = draw_hangman()
letter_ids = draw_letters(load_word())
game_state = "RUNNING"
already_guessed = []
wrong_guesses = 0
# MAIN LOOP
while game_state == "RUNNING":
guess = input("guess letter: ")
if guess in already_guessed:
continue
elif guess in letter_ids:
good_guess(guess)
else:
bad_guess(guess)
check_game_state()
canvas.update()
game_over(game_state)
# canvas.mainloop()