I'm very new to Python, I'm using Python 3.10 and have been practicing a lot the past few weeks. Overview: I'm creating Hangman using Turtle, Pynput, Tkinter, and Time modules.
Issues: On my work computer, the file runs fine up until trying to implement a second 'listen' bind to add the option to start the game - it works using the escape key to exit the game window. The issue is the error 'not in main loop', I'm pretty new to this and I found that I need to have main loop on the end on my file, this fixed the first issue where I was trying to bind escape to the quit function.
Fast forward to the main issue - on my personal laptop the code does not want to work at all, the window opens and the welcome greeting shows up, but then the program crashes and throws this error "Exception has occurred: Terminator exception: no description" relating directly to line 33 which is "game_menu = turtle.Turtle()" I feel like it will throw this for other things as well.
This was not happening on my work computer, and it all loaded properly until I was trying to fix the second key bind issue to add a bind to start the game (where I would create the meat of game somehow, with the letters and words etc).
I have tried moving the code around to try and put the greeting and menu into functions, I've tried just moving the code to different places to see if it would work in the While True loop. The while True is pretty confusing and I might be missing something that needs to be in it.
In any case, this is the code below, if anyone can please tell me what I';m doing wrong or why it won't work on my laptop but will on my other computer that would be great. (Using Visual Studio Code).
Thanks so much.
START CODE:
# Modules
from tkinter import *
import turtle
from turtle import *
import time
from pynput import keyboard
# Game Screen
width = 800
height = 800
game_window = turtle.Screen()
game_window.setup(width, height)
game_window.bgcolor('#dfe4db')
game_window.title("Hangman by Name")
# Game Greeting Text
welcome_greeting = turtle.Turtle()
welcome_greeting.color('black')
welcome_greeting.hideturtle()
welcome_greeting.penup()
welcome_greeting.setpos(-70, 0)
welcome_greeting.pendown()
welcome_greeting.write('Welcome to Hangman', move=True,
align='left', font=('Arial', 20, 'normal'))
time.sleep(1)
turtle.bye()
# Game Menu
game_menu = turtle.Turtle()
game_menu.color('black')
game_menu.shape('square')
game_menu.hideturtle()
game_menu.penup()
game_menu.setpos(-70, 0)
game_menu.write("Press 'Up Arrow' to start game\nPress 'ESC' to quit",
align='center', font=('Arial', 20, 'bold'))
if keyboard.Key.esc:
turtle.bye()
# Hangman Top Panel Stockhold
top_panel = turtle.Turtle()
turtle.tracer()
top_panel.speed(0)
top_panel.hideturtle()
top_panel.color('#4f371b')
top_panel.shape('square')
top_panel.penup()
top_panel.showturtle()
top_panel.setpos(-170, 260)
top_panel.shapesize(stretch_len=20)
# Hangman Left Side Panel Stockhold
left_panel = turtle.Turtle()
turtle.tracer()
left_panel.speed(0)
left_panel.hideturtle()
left_panel.color('#4f371b')
left_panel.shape('square')
left_panel.penup()
left_panel.setpos(-380, 20)
left_panel.left(90)
left_panel.showturtle()
left_panel.shapesize(stretch_len=25)
# Hangman Rope
rope = turtle.Turtle()
turtle.tracer()
rope.speed(0)
rope.hideturtle()
rope.color('#4f371b')
rope.shape('square')
rope.right(90)
rope.shapesize(stretch_len=5, stretch_wid=.5)
rope.penup()
rope.setpos(-160, 199)
rope.showturtle()
# Hangman Base Stockhold
base_panel_ground = turtle.Turtle()
turtle.tracer()
base_panel_ground.speed(0)
base_panel_ground.hideturtle()
base_panel_ground.color('#1c5721')
base_panel_ground.shape('square')
base_panel_ground.shapesize(stretch_len=600, stretch_wid=10)
base_panel_ground.penup()
base_panel_ground.setpos(390, -290)
base_panel_ground.showturtle()
# Hangman Body Parts:
# Head
def hangman_head():
hangman_head = turtle.Turtle()
hangman_head.speed(0)
hangman_head.color('#ff9966')
hangman_head.shape('circle')
hangman_head.shapesize(2.5, 2.5, 2.5)
hangman_head.penup()
hangman_head.setpos(-160, 155)
hangman_head.pendown()
# Body
def hangman_body():
hangman_body = turtle.Turtle()
hangman_body.speed(0)
hangman_body.color('#6600ff')
hangman_body.shape('square')
hangman_body.left(90)
hangman_body.shapesize(stretch_len=5)
hangman_body.shapesize(1.5, 3.5, 4.5)
hangman_body.penup()
hangman_body.setpos(-160, 90)
hangman_body.pendown()
# Left Arm
def hangman_left_arm():
hangman_left_arm = turtle.Turtle()
hangman_left_arm.speed(0)
hangman_left_arm.color('#ff9966')
hangman_left_arm.shape('square')
hangman_left_arm.shapesize(1.5, .5, 1.5)
hangman_left_arm.shapesize(stretch_wid=.5, stretch_len=2.5)
hangman_left_arm.left(40)
hangman_left_arm.penup()
hangman_left_arm.setpos(-189, 104)
# Right Arm
def hangman_right_arm():
hangman_right_arm = turtle.Turtle()
hangman_right_arm.speed(0)
hangman_right_arm.color('#ff9966')
hangman_right_arm.shape('square')
hangman_right_arm.shapesize(1.5, .5, 1.5)
hangman_right_arm.shapesize(stretch_wid=.5, stretch_len=2.5)
hangman_right_arm.left(140)
hangman_right_arm.penup()
hangman_right_arm.setpos(-129, 102)
# Left Leg
def hangman_left_leg():
hangman_left_leg = turtle.Turtle()
hangman_left_leg.speed(0)
hangman_left_leg.color('#ff9966')
hangman_left_leg.shape('square')
hangman_left_leg.shapesize(stretch_len=1.7, stretch_wid=.7)
hangman_left_leg.right(90)
hangman_left_leg.penup()
hangman_left_leg.setpos(-170, 35.6)
# Right Leg
def hangman_right_leg():
hangman_right_leg = turtle.Turtle()
hangman_right_leg.speed(0)
hangman_right_leg.color('#ff9966')
hangman_right_leg.shape('square')
hangman_right_leg.shapesize(stretch_len=1.7, stretch_wid=.7)
hangman_right_leg.right(90)
hangman_right_leg.penup()
hangman_right_leg.setpos(-150, 35.6)
# Clear Hangman
# Clear Head
def clear_head():
hangman_head.clear()
# Clear Body
def clear_hangman_body():
hangman_body.clear()
# Clear Left Arm
def clear_left_arm():
hangman_left_arm.clear()
# Clear Right Arm
def clear_right_arm():
hangman_right_arm.clear()
# Clear Left Leg
def clear_left_leg():
hangman_left_arm.clear()
# Clear Right Leg
def clear_right_leg():
hangman_right_leg.clear()
# Lose Game
def lose_game():
you_lose = turtle.Turtle()
you_lose.color('black')
you_lose.hideturtle()
you_lose.penup()
you_lose.setpos(-70, 340)
you_lose.write(
'Better Luck Next Time!'
'(No Hangmen were harmed in the making of this game)',
align='center', font=('Arial', 20, 'bold'))
# Clear Game
def game_clear():
clear_hangman_body()
clear_head()
clear_left_arm()
clear_right_arm()
clear_left_leg()
clear_right_leg()
# Win Game
def game_win():
you_win = turtle.Turtle()
you_win.color('black')
you_win.hideturtle()
you_win.penup()
you_win.setpos(-70, 340)
you_win.write(
'Congratulations! (No Hangmen were harmed in the making of this game)',
align='center', font=('Arial', 20, 'bold'))
# Storing the Words and Letters:
# Stored Letters For Display
word_display = []
# Printing the Word to be Guessed
def print_word():
pass
# Categories:
# Topics
topics = {1: "Animals", 2: "Music",
3: "Elements", 4: "Mythology", 5: "Countries"}
# Topic Words
dataset = {
"Animals": [
"FELINE", "CANINE", "SNAKE", "TORTOISE", "TURTLE", "OWL",
"SHARK", "FISH", "ANT EATER", "MOLLUSK", "SEAGULL", "ELEPHANT",
"ECHIDNA", "KOOKOOBURRA", "KANGAROO", "PYTHON"],
"Music": [
"CLASSICAL", "ROCK N ROLL", "JAZZ", "BLUES", "COUNTRY",
"ELECTRONIC", "CONTEMPORARY", "HIP HOP", "RAP", "POP",
"METAL", "PUNK ROCK", "ALTERNATIVE"],
"Elements": [
"HYDROGEN", "HELIUM", "LITHIUM", "BORON", "CARBON", "OXYGEN",
"NITROGEN", "MAGNESIUM", "ARGON", "SULFUR", "CHLORINE", "CALCIUM",
"IRON", "COPPER", "ZINC", "BROMINE", "INDIUM", "IODINE", "TERBIUM",
"IRIDIUM", "MERCURY", "ASTATINE", "URANIUM"],
"Mythology": [
"ODIN", "THOR", "LOKI", "HEL", "FORSETI", "YMIR", "FREYR",
"FREYJA", "FENRIR", "SKOLL", "ANGRBODA", "SKADI", "SEKHMET",
"ISIS", "HORUS", "SETH", "NEPHTHYS", "OSIRIS", "THOTH", "ANUBIS",
"BASTET", "ZEUS", "APOLLO", "ARTERMIS", "SELENE", "HERA", "APHRODITE",
"HAPHAESTUS", "ARES", "POSEIDON", "HERMES", "HADES"],
"Countries": [
"AUSTRALIA", "AMERICA", "MADAGASCAR", "EGYPT", "AFRICA", "CANADA",
"RUSSIA", "UKRAINE", "SINGAPORE", "SOUTH KOREA", "INDIA",
"NEW ZEALAND", "BRAZIL", "COLUMBIA", "THAILAND", "CHINA", "JAPAN",
"GERMANY", "HOLLAND", "ENGLAND", "SCOTLAND", "IRELAND", "GREECE"]}
# Main Game
while True:
game_window.tracer(0)
game_menu()
def quit_game():
game_window.bye()
def on_press(key):
if key == keyboard.Key.esc:
return False
else:
quit_game()
with keyboard.Listener(on_press=on_press) as listener:
listener.join()
quit_game()
game_window.mainloop()