Skip to content

Commit b120695

Browse files
authored
Add files via upload
1 parent 4eb0370 commit b120695

11 files changed

+212
-0
lines changed

Hangman Game/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Hangman-Python
2+
A simple hangman game made with python and pygame. This was made as a beginner pygame project in grade 11 (2017).
3+
4+
# Requirements
5+
- Python 3.x
6+
- pygame
7+
8+
# Run in GitPod
9+
10+
You can also run Hnagman in Gitpod, a free online dev environment for GitHub:
11+
12+
If you're intersted in a paid subscription with GitPod use the coupon code: **TECHWITHTIM19**
13+
14+
[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/techwithtim/Hangman/blob/master/hangman.py)

Hangman Game/hangman.py

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
#########################################################
2+
## File Name: hangman.py ##
3+
## Description: Starter for Hangman project - ICS3U ##
4+
#########################################################
5+
import pygame
6+
import random
7+
8+
pygame.init()
9+
winHeight = 480
10+
winWidth = 700
11+
win=pygame.display.set_mode((winWidth,winHeight))
12+
#---------------------------------------#
13+
# initialize global variables/constants #
14+
#---------------------------------------#
15+
BLACK = (0,0, 0)
16+
WHITE = (255,255,255)
17+
RED = (255,0, 0)
18+
GREEN = (0,255,0)
19+
BLUE = (0,0,255)
20+
LIGHT_BLUE = (102,255,255)
21+
22+
btn_font = pygame.font.SysFont("arial", 20)
23+
guess_font = pygame.font.SysFont("monospace", 24)
24+
lost_font = pygame.font.SysFont('arial', 45)
25+
word = ''
26+
buttons = []
27+
guessed = []
28+
hangmanPics = [pygame.image.load('hangman0.png'), pygame.image.load('hangman1.png'), pygame.image.load('hangman2.png'), pygame.image.load('hangman3.png'), pygame.image.load('hangman4.png'), pygame.image.load('hangman5.png'), pygame.image.load('hangman6.png')]
29+
30+
limbs = 0
31+
32+
33+
def redraw_game_window():
34+
global guessed
35+
global hangmanPics
36+
global limbs
37+
win.fill(GREEN)
38+
# Buttons
39+
for i in range(len(buttons)):
40+
if buttons[i][4]:
41+
pygame.draw.circle(win, BLACK, (buttons[i][1], buttons[i][2]), buttons[i][3])
42+
pygame.draw.circle(win, buttons[i][0], (buttons[i][1], buttons[i][2]), buttons[i][3] - 2
43+
)
44+
label = btn_font.render(chr(buttons[i][5]), 1, BLACK)
45+
win.blit(label, (buttons[i][1] - (label.get_width() / 2), buttons[i][2] - (label.get_height() / 2)))
46+
47+
spaced = spacedOut(word, guessed)
48+
label1 = guess_font.render(spaced, 1, BLACK)
49+
rect = label1.get_rect()
50+
length = rect[2]
51+
52+
win.blit(label1,(winWidth/2 - length/2, 400))
53+
54+
pic = hangmanPics[limbs]
55+
win.blit(pic, (winWidth/2 - pic.get_width()/2 + 20, 150))
56+
pygame.display.update()
57+
58+
59+
def randomWord():
60+
file = open('words.txt')
61+
f = file.readlines()
62+
i = random.randrange(0, len(f) - 1)
63+
64+
return f[i][:-1]
65+
66+
67+
def hang(guess):
68+
global word
69+
if guess.lower() not in word.lower():
70+
return True
71+
else:
72+
return False
73+
74+
75+
def spacedOut(word, guessed=[]):
76+
spacedWord = ''
77+
guessedLetters = guessed
78+
for x in range(len(word)):
79+
if word[x] != ' ':
80+
spacedWord += '_ '
81+
for i in range(len(guessedLetters)):
82+
if word[x].upper() == guessedLetters[i]:
83+
spacedWord = spacedWord[:-2]
84+
spacedWord += word[x].upper() + ' '
85+
elif word[x] == ' ':
86+
spacedWord += ' '
87+
return spacedWord
88+
89+
90+
def buttonHit(x, y):
91+
for i in range(len(buttons)):
92+
if x < buttons[i][1] + 20 and x > buttons[i][1] - 20:
93+
if y < buttons[i][2] + 20 and y > buttons[i][2] - 20:
94+
return buttons[i][5]
95+
return None
96+
97+
98+
def end(winner=False):
99+
global limbs
100+
lostTxt = 'You Lost, press any key to play again...'
101+
winTxt = 'WINNER!, press any key to play again...'
102+
redraw_game_window()
103+
pygame.time.delay(1000)
104+
win.fill(GREEN)
105+
106+
if winner == True:
107+
label = lost_font.render(winTxt, 1, BLACK)
108+
else:
109+
label = lost_font.render(lostTxt, 1, BLACK)
110+
111+
wordTxt = lost_font.render(word.upper(), 1, BLACK)
112+
wordWas = lost_font.render('The phrase was: ', 1, BLACK)
113+
114+
win.blit(wordTxt, (winWidth/2 - wordTxt.get_width()/2, 295))
115+
win.blit(wordWas, (winWidth/2 - wordWas.get_width()/2, 245))
116+
win.blit(label, (winWidth / 2 - label.get_width() / 2, 140))
117+
pygame.display.update()
118+
again = True
119+
while again:
120+
for event in pygame.event.get():
121+
if event.type == pygame.QUIT:
122+
pygame.quit()
123+
if event.type == pygame.KEYDOWN:
124+
again = False
125+
reset()
126+
127+
128+
def reset():
129+
global limbs
130+
global guessed
131+
global buttons
132+
global word
133+
for i in range(len(buttons)):
134+
buttons[i][4] = True
135+
136+
limbs = 0
137+
guessed = []
138+
word = randomWord()
139+
140+
#MAINLINE
141+
142+
143+
# Setup buttons
144+
increase = round(winWidth / 13)
145+
for i in range(26):
146+
if i < 13:
147+
y = 40
148+
x = 25 + (increase * i)
149+
else:
150+
x = 25 + (increase * (i - 13))
151+
y = 85
152+
buttons.append([LIGHT_BLUE, x, y, 20, True, 65 + i])
153+
# buttons.append([color, x_pos, y_pos, radius, visible, char])
154+
155+
word = randomWord()
156+
inPlay = True
157+
158+
while inPlay:
159+
redraw_game_window()
160+
pygame.time.delay(10)
161+
162+
for event in pygame.event.get():
163+
if event.type == pygame.QUIT:
164+
inPlay = False
165+
if event.type == pygame.KEYDOWN:
166+
if event.key == pygame.K_ESCAPE:
167+
inPlay = False
168+
if event.type == pygame.MOUSEBUTTONDOWN:
169+
clickPos = pygame.mouse.get_pos()
170+
letter = buttonHit(clickPos[0], clickPos[1])
171+
if letter != None:
172+
guessed.append(chr(letter))
173+
buttons[letter - 65][4] = False
174+
if hang(chr(letter)):
175+
if limbs != 5:
176+
limbs += 1
177+
else:
178+
end()
179+
else:
180+
print(spacedOut(word, guessed))
181+
if spacedOut(word, guessed).count('_') == 0:
182+
end(True)
183+
184+
pygame.quit()
185+
186+
# always quit pygame when done!

Hangman Game/hangman0.png

28.1 KB
Loading

Hangman Game/hangman1.png

29.3 KB
Loading

Hangman Game/hangman2.png

29.4 KB
Loading

Hangman Game/hangman3.png

30.4 KB
Loading

Hangman Game/hangman4.png

30.4 KB
Loading

Hangman Game/hangman5.png

30.8 KB
Loading

Hangman Game/hangman6.png

31.1 KB
Loading

Hangman Game/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pygame

Hangman Game/words.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
the big bang
2+
the pillow feels soft
3+
odd one out
4+
press conference
5+
show me the money
6+
time to go home
7+
that was easy
8+
hangman is cool
9+
zoologist
10+
quadruplets
11+
the sky is blue

0 commit comments

Comments
 (0)