Hangman
Hangman
pygame.init()
winHeight = 480
winWidth = 700
win=pygame.display.set_mode((winWidth,winHeight))
#---------------------------------------#
# initialize global variables/constants #
#---------------------------------------#
BLACK = (0,0, 0)
WHITE = (255,255,255)
RED = (255,0, 0)
GREEN = (0,255,0)
BLUE = (0,0,255)
LIGHT_BLUE = (102,255,255)
limbs = 0
def redraw_game_window():
global guessed
global hangmanPics
global limbs
win.fill(GREEN)
# Buttons
for i in range(len(buttons)):
if buttons[i][4]:
pygame.draw.circle(win, BLACK, (buttons[i][1], buttons[i][2]),
buttons[i][3])
pygame.draw.circle(win, buttons[i][0], (buttons[i][1], buttons[i][2]),
buttons[i][3] - 2
)
label = btn_font.render(chr(buttons[i][5]), 1, BLACK)
win.blit(label, (buttons[i][1] - (label.get_width() / 2), buttons[i][2]
- (label.get_height() / 2)))
def randomWord():
file = open('words.txt')
f = file.readlines()
i = random.randrange(0, len(f) - 1)
return f[i][:-1]
def hang(guess):
global word
if guess.lower() not in word.lower():
return True
else:
return False
def end(winner=False):
global limbs
lostTxt = 'You Lost, press any key to play again...'
winTxt = 'WINNER!, press any key to play again...'
redraw_game_window()
pygame.time.delay(1000)
win.fill(GREEN)
if winner == True:
label = lost_font.render(winTxt, 1, BLACK)
else:
label = lost_font.render(lostTxt, 1, BLACK)
def reset():
global limbs
global guessed
global buttons
global word
for i in range(len(buttons)):
buttons[i][4] = True
limbs = 0
guessed = []
word = randomWord()
#MAINLINE
# Setup buttons
increase = round(winWidth / 13)
for i in range(26):
if i < 13:
y = 40
x = 25 + (increase * i)
else:
x = 25 + (increase * (i - 13))
y = 85
buttons.append([LIGHT_BLUE, x, y, 20, True, 65 + i])
# buttons.append([color, x_pos, y_pos, radius, visible, char])
word = randomWord()
inPlay = True
while inPlay:
redraw_game_window()
pygame.time.delay(10)
pygame.quit()