From 25a87746088c3b9d97ff83951744b5ff7980cbb0 Mon Sep 17 00:00:00 2001 From: Jilesh980 Date: Sun, 26 Nov 2023 15:53:14 -0500 Subject: [PATCH] updated code with play again --- hangman.py | 161 ++++++++++------------------------------------------- 1 file changed, 29 insertions(+), 132 deletions(-) diff --git a/hangman.py b/hangman.py index b47fd06..8f1db9a 100644 --- a/hangman.py +++ b/hangman.py @@ -8,151 +8,47 @@ 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) - -btn_font = pygame.font.SysFont("arial", 20) -guess_font = pygame.font.SysFont("monospace", 24) -lost_font = pygame.font.SysFont('arial', 45) -word = '' -buttons = [] -guessed = [] -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')] - -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))) - - spaced = spacedOut(word, guessed) - label1 = guess_font.render(spaced, 1, BLACK) - rect = label1.get_rect() - length = rect[2] - - win.blit(label1,(winWidth/2 - length/2, 400)) - - pic = hangmanPics[limbs] - win.blit(pic, (winWidth/2 - pic.get_width()/2 + 20, 150)) - pygame.display.update() +win = pygame.display.set_mode((winWidth, winHeight)) -def randomWord(): - file = open('words.txt') - f = file.readlines() - i = random.randrange(0, len(f) - 1) +# Add the following function for the game-over screen - return f[i][:-1] - - -def hang(guess): - global word - if guess.lower() not in word.lower(): - return True - else: - return False - - -def spacedOut(word, guessed=[]): - spacedWord = '' - guessedLetters = guessed - for x in range(len(word)): - if word[x] != ' ': - spacedWord += '_ ' - for i in range(len(guessedLetters)): - if word[x].upper() == guessedLetters[i]: - spacedWord = spacedWord[:-2] - spacedWord += word[x].upper() + ' ' - elif word[x] == ' ': - spacedWord += ' ' - return spacedWord - - -def buttonHit(x, y): - for i in range(len(buttons)): - if x < buttons[i][1] + 20 and x > buttons[i][1] - 20: - if y < buttons[i][2] + 20 and y > buttons[i][2] - 20: - return buttons[i][5] - return None - - -def end(winner=False): +def game_over_screen(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) + global guessed + global buttons + global word + win.fill(GREEN) + pygame.time.delay(1000) - if winner == True: - label = lost_font.render(winTxt, 1, BLACK) + if winner: + message = 'WINNER!' else: - label = lost_font.render(lostTxt, 1, BLACK) + message = 'You Lost' - wordTxt = lost_font.render(word.upper(), 1, BLACK) - wordWas = lost_font.render('The phrase was: ', 1, BLACK) + label = lost_font.render(message, 1, BLACK) + word_txt = lost_font.render(f'The phrase was: {word.upper()}', 1, BLACK) + play_again_btn = btn_font.render('Play Again', 1, BLACK) - win.blit(wordTxt, (winWidth/2 - wordTxt.get_width()/2, 295)) - win.blit(wordWas, (winWidth/2 - wordWas.get_width()/2, 245)) + win.blit(word_txt, (winWidth / 2 - word_txt.get_width() / 2, 295)) win.blit(label, (winWidth / 2 - label.get_width() / 2, 140)) + win.blit(play_again_btn, (winWidth / 2 - play_again_btn.get_width() / 2, 350)) pygame.display.update() - again = True - while again: + + # Wait for user input to play again + waiting_for_input = True + play_again_btn_rect = play_again_btn.get_rect(topleft=(winWidth / 2 - play_again_btn.get_width() / 2, 350)) + while waiting_for_input: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() - if event.type == pygame.KEYDOWN: - again = False + if event.type == pygame.KEYDOWN or (event.type == pygame.MOUSEBUTTONDOWN and play_again_btn_rect.collidepoint(event.pos)): + waiting_for_input = False reset() -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: @@ -166,21 +62,22 @@ def reset(): if event.key == pygame.K_ESCAPE: inPlay = False if event.type == pygame.MOUSEBUTTONDOWN: - clickPos = pygame.mouse.get_pos() - letter = buttonHit(clickPos[0], clickPos[1]) - if letter != None: + click_pos = pygame.mouse.get_pos() + letter = buttonHit(click_pos[0], click_pos[1]) + if letter is not None: guessed.append(chr(letter)) buttons[letter - 65][4] = False if hang(chr(letter)): if limbs != 5: limbs += 1 else: - end() + game_over_screen() else: print(spacedOut(word, guessed)) if spacedOut(word, guessed).count('_') == 0: - end(True) + game_over_screen(True) pygame.quit() + # always quit pygame when done!