Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added __pycache__/hangman.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/hangmanOtimizado.cpython-311.pyc
Binary file not shown.
32 changes: 16 additions & 16 deletions hangman.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
lost_font = pygame.font.SysFont('arial', 45)
word = ''
buttons = []
guessed = []
guessed = set()
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
Expand Down Expand Up @@ -72,18 +72,16 @@ def hang(guess):
return False


def spacedOut(word, guessed=[]):
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] == ' ':
for ch in word:
if ch ==' ':
spacedWord += ' '
elif ch.upper() in guessed:
spacedWord += ch.upper() + ' '
else:
spacedWord += '_ '
return spacedWord


Expand Down Expand Up @@ -118,8 +116,10 @@ def end(winner=False):
again = True
while again:
for event in pygame.event.get():
if event.type == pygame.QUIT:
if event.type == pygame.QUIT:
pygame.quit()
#add quit para não dar erro ao encerrar o programa
quit()
if event.type == pygame.KEYDOWN:
again = False
reset()
Expand All @@ -134,7 +134,7 @@ def reset():
buttons[i][4] = True

limbs = 0
guessed = []
guessed = set()
word = randomWord()

#MAINLINE
Expand Down Expand Up @@ -168,8 +168,8 @@ def reset():
if event.type == pygame.MOUSEBUTTONDOWN:
clickPos = pygame.mouse.get_pos()
letter = buttonHit(clickPos[0], clickPos[1])
if letter != None:
guessed.append(chr(letter))
if letter != None and chr(letter) not in guessed:
guessed.add(chr(letter))
buttons[letter - 65][4] = False
if hang(chr(letter)):
if limbs != 5:
Expand All @@ -180,7 +180,7 @@ def reset():
print(spacedOut(word, guessed))
if spacedOut(word, guessed).count('_') == 0:
end(True)

pygame.quit()

# always quit pygame when done!
# always quit pygame when done!