|
| 1 | +import pygame |
| 2 | +from pygame.locals import * |
| 3 | +import random |
| 4 | +from string import ascii_letters |
| 5 | + |
| 6 | +pygame.init() |
| 7 | +pygame.font.init() |
| 8 | + |
| 9 | +screen = pygame.display.set_mode((400, 500)) |
| 10 | +pygame.display.set_caption("Hangman") |
| 11 | + |
| 12 | +class Hangman(): |
| 13 | + def __init__(self): |
| 14 | + with open("./words.txt", "r") as file: |
| 15 | + # picks secret word |
| 16 | + words = file.read().split("\n") |
| 17 | + self.secret_word = random.choice(words) |
| 18 | + # passing secret word's length for making letter blanks |
| 19 | + self.guessed_word = "*" * len(self.secret_word) |
| 20 | + self.wrong_guesses = [] |
| 21 | + self.wrong_guess_count = 0 |
| 22 | + self.taking_guess = True |
| 23 | + self.running = True |
| 24 | + |
| 25 | + self.background_color = (155, 120, 70) |
| 26 | + self.gallow_color = (0,0,0) |
| 27 | + self.body_color = (255,253,175) |
| 28 | + |
| 29 | + self.font = pygame.font.SysFont("Courier New", 20) |
| 30 | + self.FPS = pygame.time.Clock() |
| 31 | + |
| 32 | + |
| 33 | + # draws the gallow |
| 34 | + def _gallow(self): |
| 35 | + stand = pygame.draw.rect(screen, self.gallow_color, pygame.Rect(75, 280, 120, 10)) |
| 36 | + body = pygame.draw.rect(screen, self.gallow_color, pygame.Rect(128, 40, 10, 240)) |
| 37 | + hanger = pygame.draw.rect(screen, self.gallow_color, pygame.Rect(128, 40, 80, 10)) |
| 38 | + rope = pygame.draw.rect(screen, self.gallow_color, pygame.Rect(205, 40,10, 30)) |
| 39 | + |
| 40 | + |
| 41 | + # draw man's body parts for every wrong guess |
| 42 | + def _man_pieces(self): |
| 43 | + if self.wrong_guess_count == 1: |
| 44 | + head = pygame.draw.circle(screen, self.body_color, [210, 85], 20, 0) |
| 45 | + elif self.wrong_guess_count == 2: |
| 46 | + body = pygame.draw.rect(screen, self.body_color, pygame.Rect(206, 105, 8, 45)) |
| 47 | + elif self.wrong_guess_count == 3: |
| 48 | + r_arm = pygame.draw.line(screen, self.body_color, [183, 149], [200, 107], 6) |
| 49 | + elif self.wrong_guess_count == 4: |
| 50 | + l_arm = pygame.draw.line(screen, self.body_color, [231, 149], [218, 107], 6), |
| 51 | + elif self.wrong_guess_count == 5: |
| 52 | + r_leg = pygame.draw.line(screen, self.body_color, [189, 198], [208, 148], 6), |
| 53 | + elif self.wrong_guess_count == 6: |
| 54 | + l_leg = pygame.draw.line(screen, self.body_color, [224, 198], [210, 148], 6) |
| 55 | + |
| 56 | + |
| 57 | + def _right_guess(self, guess_letter): |
| 58 | + index_positions = [index for index, item in enumerate(self.secret_word) if item == guess_letter] |
| 59 | + for i in index_positions: |
| 60 | + self.guessed_word = self.guessed_word[0:i] + guess_letter + self.guessed_word[i+1:] |
| 61 | + # stacks a layer of color on guessed word to hide multiple guessed_word stack |
| 62 | + screen.fill(pygame.Color(self.background_color), (10, 370, 390, 20)) |
| 63 | + |
| 64 | + |
| 65 | + def _wrong_guess(self, guess_letter): |
| 66 | + self.wrong_guesses.append(guess_letter) |
| 67 | + self.wrong_guess_count += 1 |
| 68 | + self._man_pieces() |
| 69 | + |
| 70 | + |
| 71 | + def _guess_taker(self, guess_letter): |
| 72 | + if guess_letter in ascii_letters: |
| 73 | + if guess_letter in self.secret_word and guess_letter not in self.guessed_word: |
| 74 | + self._right_guess(guess_letter) |
| 75 | + elif guess_letter not in self.secret_word and guess_letter not in self.wrong_guesses: |
| 76 | + self._wrong_guess(guess_letter) |
| 77 | + |
| 78 | + |
| 79 | + def _message(self): |
| 80 | + # win situation |
| 81 | + if self.guessed_word == self.secret_word: |
| 82 | + self.taking_guess = False |
| 83 | + screen.fill(pygame.Color(0,0,79), (40, 218, 320, 30)) |
| 84 | + message = self.font.render("YOU WIN!!", True, (255,235,0)) |
| 85 | + screen.blit(message,(152,224)) |
| 86 | + |
| 87 | + # lose situation |
| 88 | + elif self.wrong_guess_count == 6: |
| 89 | + self.taking_guess = False |
| 90 | + screen.fill(pygame.Color("grey"), (40, 218, 320, 30)) |
| 91 | + message = self.font.render("GAME OVER YOU LOSE!!", True, (150,0,10)) |
| 92 | + screen.blit(message,(78,224)) |
| 93 | + # shows the secret word if the player lose |
| 94 | + word = self.font.render(f"secret word: {self.secret_word}", True, (255,255,255)) |
| 95 | + screen.blit(word,(10,300)) |
| 96 | + |
| 97 | + # removes the instruction message if not taking guesses anymore |
| 98 | + if not self.taking_guess: |
| 99 | + screen.fill(pygame.Color(self.background_color), (35, 460, 390, 20)) |
| 100 | + |
| 101 | + |
| 102 | + def main(self): |
| 103 | + # game's main components (no need to update) |
| 104 | + screen.fill(self.background_color) |
| 105 | + self._gallow() |
| 106 | + instructions = self.font.render('Press any key to take Guess', True, (9,255,78)) |
| 107 | + screen.blit(instructions,(35,460)) |
| 108 | + |
| 109 | + while self.running: |
| 110 | + # shows the guessed word in the game window |
| 111 | + guessed_word = self.font.render(f"guessed word: {self.guessed_word}", True, (0,0,138)) |
| 112 | + screen.blit(guessed_word,(10,370)) |
| 113 | + # shows the wrong guesses in the game window |
| 114 | + wrong_guesses = self.font.render(f"wrong guesses: {' '.join(map(str, self.wrong_guesses))}", True, (125,0,0)) |
| 115 | + screen.blit(wrong_guesses,(10,420)) |
| 116 | + |
| 117 | + # checking game state |
| 118 | + self._message() |
| 119 | + |
| 120 | + for self.event in pygame.event.get(): |
| 121 | + if self.event.type == pygame.QUIT: |
| 122 | + self.running = False |
| 123 | + |
| 124 | + # manages keys pressed |
| 125 | + elif self.event.type == pygame.KEYDOWN: |
| 126 | + if self.taking_guess: |
| 127 | + self._guess_taker(self.event.unicode) |
| 128 | + |
| 129 | + pygame.display.flip() |
| 130 | + self.FPS.tick(60) |
| 131 | + |
| 132 | + pygame.quit() |
| 133 | + |
| 134 | + |
| 135 | +if __name__ =="__main__": |
| 136 | + h = Hangman() |
| 137 | + h.main() |
0 commit comments