Skip to content

Commit

Permalink
GUI Version of Hangman (#7)
Browse files Browse the repository at this point in the history
* GUI Version of Hangman

* Increased Words
  • Loading branch information
ambushneupane authored Oct 10, 2022
1 parent b2f8a5d commit 4aa305d
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 0 deletions.
Binary file added hangman-GUI/hangman0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added hangman-GUI/hangman1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added hangman-GUI/hangman2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added hangman-GUI/hangman3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added hangman-GUI/hangman4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added hangman-GUI/hangman5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added hangman-GUI/hangman6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
128 changes: 128 additions & 0 deletions hangman-GUI/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import pygame
import math
import random
from wordsStore import words

# Setup Display
pygame.init()
WIDTH, HEIGHT = 800, 500
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Hangman Game")

# colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)

# Button Variables
RADIUS = 20
GAP_SIZE = 15
letters = []
startX = round((WIDTH - (RADIUS * 2 + GAP_SIZE) * 13) / 2) # Value =42

# startX= round((WIDTH-((RADIUS*2*13)+(GAP_SIZE*12)))/2) # value = 50
startY = 400

A = 65
for i in range(26):
x = startX + GAP_SIZE * 2 + ((RADIUS * 2 + GAP_SIZE) * (i % 13))
y = startY + ((i // 13) * (GAP_SIZE + RADIUS * 2))
letters.append([x, y, chr(A + i), True])

# Fonts

LETTER_FONT = pygame.font.SysFont("comicsans", 40)
WORD_FONT = pygame.font.SysFont("comicsans", 60)
TITLE_FONT=pygame.font.SysFont("comicsans", 80)

# Loading Images
images = []
for i in range(7):
image = pygame.image.load(f'hangman{i}.png')
images.append(image)

# Game Variables
hangman_status = 0

# word="FOOD"
word = random.choice(words)
guessed = []

# SETUP game Loop
FPS = 60
clock = pygame.time.Clock()
running = True


def draw():
screen.fill(WHITE)
#Draw title
text= TITLE_FONT.render("SIMPLE HANGMAN GAME",1,RED)
screen.blit(text,(WIDTH/2-text.get_width()/2,20))

# draw word
display_word = ""
for letter in word:
if letter in guessed:
display_word += letter + " "
else:
display_word += "_ "

text = WORD_FONT.render(display_word, 1, BLACK)
screen.blit(text, (400, 200))


# draw Buttons
for letter in letters:
x, y, ltr, visible = letter
if visible:
pygame.draw.circle(screen, BLACK, (x, y), RADIUS, 3)
text = LETTER_FONT.render(ltr, True, BLACK)
# screen.blit(text,(x-10,y-14)) # this one works too
screen.blit(text, (x - int(text.get_width() / 2), y - int(text.get_height() / 2)))

screen.blit(images[hangman_status], (150, 100))
pygame.display.update()

def display_message(message): #DRY concept lol
screen.fill(WHITE)
text= WORD_FONT.render(message,1,BLACK)
screen.blit(text,(int(WIDTH/2-text.get_width()/2),int(HEIGHT/2-text.get_height()/2)))
pygame.display.update()
pygame.time.delay(2000)

while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

if event.type == pygame.MOUSEBUTTONDOWN:
m_x, m_y = pygame.mouse.get_pos()
# print(m_x,m_y)
# print(RADIUS)
for letter in letters:
x, y, ltr, visible = letter
if visible:
distance = math.sqrt((x - m_x) ** 2 + (y - m_y) ** 2) # Distance formula
if distance < RADIUS:
letter[3] = False
guessed.append(ltr)
if ltr not in word:
hangman_status += 1
draw()
won = True
for letter in word:
if letter not in guessed:
won = False
break

draw()
if won:
display_message("YOU WON!!!!")
break
if hangman_status == 6:
display_message("You Lost :(")
break

pygame.quit()
3 changes: 3 additions & 0 deletions hangman-GUI/wordsStore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

words=["Developer","Ambush","Reddit","Food","PYGAME","PYthon","hacking","reading","fast","mango","coding","books","mother","Grand","Evening","Music","Love","Happy","Agony","Visual","request","realm","Information","Laugh","Calender","Promo","Speaker","Base","Nose","Eyes","Sugar","Couch","Coach","Lovely","Mirror","Clear","King","Name","Mouse"]
words=[word.upper() for word in words]

0 comments on commit 4aa305d

Please sign in to comment.