I am just posting this in hopes of someone pointing out any ways I could improve my hangman game.
I know there are properly a lot of silly issues and unnecessary parts to this, but it works and now I am lost at how to shrink the code down or how to tidy it up a little bit.
from graphics import graphics
import random
class Hangman:
# Word lists
options = {"movies": {0 : "titanic",
1 : "seven",
2 : "deadpool"}}
playing = False
guessed_letters = []
word = ""
show_letters = None
previous_words = []
attempts = 7
mode = None
def setup():
while True:
user_input = input("Please select the game mode you would like to play?").lower()
Hangman.mode = user_input
# Checks to see if the mode the player selected exists
if user_input in Hangman.options:
# Picks a random word from the selected mode list
Hangman.word = random.choice(Hangman.options[Hangman.mode])
# Changes the show letter variable to _ times letters in the
# chosen word
Hangman.show_letters = list("_" * len(Hangman.word))
# Starts the game loop
Hangman.playing = True
Hangman.game_loop()
break
# If the selected mode does not exist
else:
print("[{}] mode does not exist! Try a different mode".format(user_input))
# This section is to check if the player has any attempts left
def check_attempts():
# If the player has more than 0 attempts
if Hangman.attempts > 0:
# Shows the player graphics of the hangman
print(graphics[Hangman.attempts])
# If the player has used all of there attempts
else:
print(graphics[Hangman.attempts])
print("\n\nYou lost!\n")
print("The word was: {}".format(Hangman.word))
Hangman.playing = False
# Remove 1 attempt after each incorrect guess
Hangman.attempts -= 1
# Check if the player guessed the correct word
def check_winner():
while True:
# If show_letters does not contain any _ you win
if "_" not in Hangman.show_letters:
print("\n\Congratulations. You have won!")
# Ask the player if they would like to play again
user_input = input("Would you like to play again? [yes/no]").lower()
if user_input == "yes":
Hangman.reset()
break
elif user_input == "no":
print("Thank you for playing!")
Hangman.playing = False
Hangman.setting_up = False
break
# Check for a valid answer
else:
print("That was an invalid answer!")
# If they haven't won yet break this loop
else:
break
# Inform the player they have already tried the letter they just typed
# Show the player the letters they have already guessed
def already_guessed():
print(" ".join(Hangman.guessed_letters))
def game_loop():
# Used to show the selected word. For testing use only
print(Hangman.word)
# Start the game loop if playing = True
while Hangman.playing == True:
# Winner check
Hangman.check_winner()
# Show the word as _*letters in selected word
# Or h_ll_o if the player has got some letters correct
print("\nWord:")
print("".join(Hangman.show_letters), "\n")
# Get the players guess
guess = input("Guess a letter: ").lower()
# If the player types the whole selected word they win
if guess == Hangman.word:
Hangman.show_letters = guess
Hangman.check_winner()
# Check if the players guess is a letter and only 1 letter not more
if guess.isalpha() and len(guess) < 2:
# Let the player know they has guessed that letter already
if guess in Hangman.guessed_letters:
Hangman.already_guessed()
# Let the player know their guess was incorrect
elif guess not in Hangman.word:
print("\n{} is incorrect!".format(guess))
Hangman.check_attempts()
# Update the shown letters if the player guesses a correct letter
else:
# Check if the guessed letter is in word
# The position of the guessed letter in word
# The letter that has been guessed
for position, letter in enumerate(Hangman.word):
# If it is in word change the shown letters to show
# The new letter in the correct position
if letter == guess:
Hangman.show_letters[position] = letter
print("\n{} is correct!".format(guess))
# If the guessed letter isn't in guessed letters already add
# it
if guess not in Hangman.guessed_letters:
Hangman.guessed_letters.append(guess)
else:
print("You can only guess letter's and 1 at a time!")
# Start the entire program
Hangman.setup()
I know it is a lot of code but I wasn't sure how else to show it all and get help. this is my first game I have finished making. It all works but I have remove some bits to make it shorter to post and it is indented 4 spaces in idle. I haven't used any tabs because I have read they can cause issues. But i have used the idle indent button a few times