Computers Board Project
Computers Board Project
2023-24
Hangman Game
# Assigning all the primary/secondary colors to a dictionary to use more practically colors
= {"black":(0,0,0), "darkgray":(70,70,70), "gray":(128,128,128), "lightgray":(200,200,200),
"white":(255,255,255), "red":(255,0,0),
"darkred":(128,0,0),"green":(0,255,0),"darkgreen":(0,128,0), "blue":(0,0,255),
"navy":(0,0,128), "darkblue":(0,0,128),
"yellow":(255,255,0), "gold":(255,215,0), "orange":(255,165,0),
"lilac":(229,204,255),"lightblue":(135,206,250),"teal":(0,128,128),
"cyan":(0,255,255), "purple":(150,0,150), "pink":(238,130,238), "brown":(139,69,19),
"lightbrown":(222,184,135),"lightgreen":(144,238,144),
"turquoise":(64,224,208),"beige":(245,245,220),"honeydew":(240,255,240),"lavender":(230,
230,250),"crimson":(220,20,60)}
3:pg.image.load("imgs/v3.png"),4:pg.image.load("imgs/v4.png"),5:pg.image.load("imgs/v5.p
ng"),6:pg.image.load("imgs/v6.png")}
alphabet = list(string.ascii_uppercase)
class Button(object):
def __init__(self, color, pos, width, height, letter, active = False, type = 1, size = 40):
self.type = type
self.active = active
self.clicked = False
self.rollOver = False
self.size = size
self.font = pg.font.SysFont(None, self.size)
self.color = color
self.letter = letter
self.pos = pos
self.width = width
self.height = height
self.subsurface = pg.Surface((self.width, self.height))
self.subsurface.fill(self.color)
self.text = self.font.render(self.letter, True, colors["white"])
letters = []
j=0
for number, letter in enumerate(alphabet):
if number > 12:
number = number - 13
j=1
letters.append(Button(colors["gray"], (70+number*90,140+j*60), 50, 50, letter))
chbuttons = []
chbuttons.append(Button(colors["gray"], (30, 400), 90,40, "SPORTS", False, 2, 30))
chbuttons.append(Button(colors["gray"], (120, 400), 90,40, "CITIES", True, 2, 30))
errorCount = 0
#SPORTS
sports = open("wordsEN.txt","r")
# CITIES
cities = open("wordsTR.txt","r")
guessed = []
lw = 40
ls = 10
needRestart = False
winCount = 0
pointCount = 0
spaceCount = 0
for letter in word_list[currentWord]:
if letter == " ":
spaceCount += 1
sports_content = sports.read()
cities_content = cities.read()
print(len(sports_content))
print(len(cities_content))
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
if event.type==pg.MOUSEMOTION:
for button in letters:
currentRect = button.subsurface.get_rect(topleft = (button.pos[0], button.pos[1]))
if currentRect.collidepoint(pg.mouse.get_pos()):
button.rollOver = True
else:
button.rollOver = False
if event.type == pg.MOUSEBUTTONDOWN:
if event.button == 1:
for button in letters:
if button.rollOver is True and button.clicked is False:
button.clicked = True
guessed.append(button.letter)
noError = False
for letter in currentchoice[currentWord]:
if button.letter == letter:
noError = True
if errorCount < 6 and not noError:
errorCount += 1
for button in chbuttons:
currentRectLang = button.subsurface.get_rect(topleft = (button.pos[0],
button.pos[1]))
if currentRectLang.collidepoint(pg.mouse.get_pos()):
button.active = True
if button.letter == "English":
currentchoice = cities
else:
currentchoice = sports
currentIndex = chbuttons.index(button)
for subbutton in chbuttons
if chbuttons.index(subbutton) =currentIndex:
pass
else:
subbutton.active = False
needRestart = True
screen.fill(colors["white"])
screen.blit(notesArea, (0,0))
screen.blit(buttonArea, (0, 700))
pg.display.update
errorCount = 0
winCount = 0
letter.clicked = False
pg.time.wait(1000)
if totalShown == len(currentchoice[currentWord]) - spaceCount: # IF IT'S A WIN CONDITION
screen.blit(win_text, (380, 380))
pg.display.update()
pg.time.wait(1000)
guessed.clear()
winCount+=1
spaceCount = 0
Game Win
Screen
Game Lose
Screen
1. www.wikipedia.com
2. www.freecodecamp.com
3. www.devdocs.io
4. www.youtube.com