-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHangmanGame.py
181 lines (150 loc) · 5.11 KB
/
HangmanGame.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
import pygame
from pygame import mixer
import math
import random
import json # Import Wordlist
import os
# word data loading ...
data = json.load(open('Word_list.json'))
Wordlist = data['data'] # parse data
# song data loading
mixer.pre_init(44100, 16, 1, 512)
mixer.init()
BASE_DIR = os.getcwd()
SOUND_EFFECTS = {
"wrong": mixer.Sound(os.path.join(BASE_DIR, "musics/wrong.wav")),
"correct": mixer.Sound(os.path.join(BASE_DIR, "musics/correct.wav")),
"won": mixer.Sound(os.path.join(BASE_DIR, "musics/won.wav")),
"background": mixer.Sound(os.path.join(BASE_DIR, "musics/background.wav")),
"background2": mixer.Sound(os.path.join(BASE_DIR, "musics/background2.mp3")),
"lost": mixer.Sound(os.path.join(BASE_DIR, "musics/lost.mp3")),
}
# Initialize the game
pygame.init()
width, height = 800, 600
win = pygame.display.set_mode((width, height))
pygame.display.set_caption("Hangman Game")
LOGO = "hang_icon.png"
pygame.display.set_icon(pygame.image.load(LOGO))
# font
Menu_Font = pygame.font.SysFont('timesnewroman', 60)
Letter_Font = pygame.font.SysFont('timesnewroman', 30)
Guessed_Font = pygame.font.SysFont('timesnewroman', 40)
Message_Font = pygame.font.SysFont('timesnewroman', 50)
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 180, 0)
RED = (255, 0, 0)
# Load Images
Images = []
for i in range(7):
Image = pygame.image.load("hangman" + str(i) + ".png")
Images.append(Image)
# button variables
RADIUS = 20
GAP = 15
letters = []
startx = round((width - (RADIUS * 2 + GAP) * 13) / 2)
starty = 400
A = 65
for i in range(26):
x = startx + GAP * 2 + ((RADIUS * 2 + GAP) * (i % 13))
y = starty + ((i // 13) * (RADIUS * 2 + GAP))
letters.append([x, y, chr(A + i), True])
# Word
# Choose randomly a word from the Wordlist
def get_valid_word(Wordlist):
word = random.choice(Wordlist) # randomly chooses something from the list
while '-' in word or ' ' in word:
word = random.choice(Wordlist)
return word.upper()
word = get_valid_word(Wordlist)
guessed = []
# game status
hangman_status = 0
# draw
def draw():
win.fill(WHITE)
# SOUND_EFFECTS.get("background").play()
# draw word
display_word = ""
for letter in word:
if letter in guessed:
display_word += letter + " "
else:
display_word += "_ "
text = Guessed_Font.render(display_word, True, GREEN)
win.blit(text, [330, 200])
# draw buttons
for letter in letters:
x, y, ltr, visible = letter
if visible:
pygame.draw.circle(win, BLACK, [x, y], RADIUS, 2)
text = Letter_Font.render(ltr, True, BLACK)
win.blit(text, [x - text.get_width() / 2, y - text.get_height() / 2])
win.blit(Images[hangman_status], [70, 50])
pygame.display.update()
def display_window(Result):
if Result:
message = "YOU WON!"
win.fill(WHITE)
text = Message_Font.render(message, True, GREEN)
win.blit(text, [(width - text.get_width()) / 2, (height - text.get_height()) / 2])
pygame.display.update()
pygame.time.delay(5000)
else:
message = "YOU LOST!"
win.fill(BLACK)
text = Message_Font.render(message, True, RED)
win.blit(text, [(width - text.get_width()) / 2, (height - text.get_height()) / 2])
pygame.display.update()
pygame.time.delay(5000)
# Main Loop
def main_loop():
# Game Status
global hangman_status
FPS = 60
clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
m_x, m_y = pygame.mouse.get_pos()
for letter in letters:
x, y, ltr, visible = letter
if visible:
dis = math.sqrt((x - m_x) ** 2 + (y - m_y) ** 2)
if dis <= RADIUS:
letter[3] = False
guessed.append(ltr)
if ltr in word:
SOUND_EFFECTS.get("correct").play(maxtime=300)
if ltr not in word:
SOUND_EFFECTS.get("wrong").play(maxtime=400)
hangman_status += 1
draw()
won = True
for letter in word:
if letter not in guessed:
won = False
if won:
pygame.time.delay(500)
SOUND_EFFECTS.get("won").play(maxtime=5000)
pygame.time.delay(1000)
Result = True
display_window(Result)
break
if hangman_status == 6:
pygame.time.delay(300)
SOUND_EFFECTS.get("lost").play(maxtime=3500)
pygame.time.delay(1000)
Result = False
display_window(Result)
break
# run game
main_loop()
pygame.quit()