├── .gitpod.yml ├── README.md ├── hangman.py ├── hangman0.png ├── hangman1.png ├── hangman2.png ├── hangman3.png ├── hangman4.png ├── hangman5.png ├── hangman6.png ├── requirements.txt └── words.txt /.gitpod.yml: -------------------------------------------------------------------------------- 1 | image: gitpod/workspace-full-vnc 2 | ports: 3 | - port: 5900 4 | onOpen: ignore 5 | - port: 6080 6 | onOpen: open-preview 7 | tasks: 8 | - init: pip3 install -r requirements.txt 9 | command: python3 hangman.py 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 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 | # 💻 Launch Your Software Development Career Today! 9 | 10 | 🎓 **No degree? No problem!** My program equips you with everything you need to break into tech and land an entry-level software development role. 11 | 12 | 🚀 **Why Join?** 13 | - 💼 **$70k+ starting salary potential** 14 | - 🕐 **Self-paced:** Complete on your own time 15 | - 🤑 **Affordable:** Low risk compared to expensive bootcamps or degrees 16 | - 🎯 **45,000+ job openings** in the market 17 | 18 | 👉 **[Start your journey today!](https://techwithtim.net/dev)** 19 | No experience needed—just your determination. Future-proof your career and unlock six-figure potential like many of our students have! 20 | -------------------------------------------------------------------------------- /hangman.py: -------------------------------------------------------------------------------- 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! 187 | -------------------------------------------------------------------------------- /hangman0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techwithtim/Hangman/69ca8f3f30560a4a8d997800773a40d2011b0d01/hangman0.png -------------------------------------------------------------------------------- /hangman1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techwithtim/Hangman/69ca8f3f30560a4a8d997800773a40d2011b0d01/hangman1.png -------------------------------------------------------------------------------- /hangman2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techwithtim/Hangman/69ca8f3f30560a4a8d997800773a40d2011b0d01/hangman2.png -------------------------------------------------------------------------------- /hangman3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techwithtim/Hangman/69ca8f3f30560a4a8d997800773a40d2011b0d01/hangman3.png -------------------------------------------------------------------------------- /hangman4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techwithtim/Hangman/69ca8f3f30560a4a8d997800773a40d2011b0d01/hangman4.png -------------------------------------------------------------------------------- /hangman5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techwithtim/Hangman/69ca8f3f30560a4a8d997800773a40d2011b0d01/hangman5.png -------------------------------------------------------------------------------- /hangman6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techwithtim/Hangman/69ca8f3f30560a4a8d997800773a40d2011b0d01/hangman6.png -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | pygame 2 | -------------------------------------------------------------------------------- /words.txt: -------------------------------------------------------------------------------- 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 --------------------------------------------------------------------------------