1

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()
ggorlen
  • 44,755
  • 7
  • 76
  • 106
247Anxiety
  • 13
  • 3
  • Does this answer your question? [Python Turtle.Terminator even after using exitonclick()](https://stackoverflow.com/questions/45534458/python-turtle-terminator-even-after-using-exitonclick) (in this case, `.bye()`, but same issue--once you close the window you can't call more turtle methods) – ggorlen Sep 21 '22 at 03:01
  • Yes that helps a lot with it, thank you. I think I was getting that error because I did try a few of those codes and would have had the wrong ones/too many/wrong place or trying to call a window again etc. Both answers have been really helpful - currently doing some more work on the code and it's running a lot better with no errors now (except the ones I create haha). – 247Anxiety Sep 21 '22 at 04:40

1 Answers1

2

I'm surprised this works at all. You exit turtle (turtle.bye()) shortly after starting but continue to call turtle methods. You import tkinter but use turtle in a standalone fashion instead of an embedded fashion necessary under tkinter. You import turtle twice, two different ways, which is always a warning sign for disaster. You invoke the (effectively) no-op turtle.tracer() multiple times. You have variables and functions with the same name.

Below is my rework of your code. I don't believe you need tkinter nor pynput, as everything you need is already in turtle. My advice is you focus on getting your basic game working first and not start out making text banners and the like:

from turtle import Screen, Turtle
from time import sleep

WIDTH, HEIGHT = 800, 800

BOLD_FONT = ('Arial', 20, 'bold')
NORMAL_FONT = ('Arial', 20, 'normal')

# Hangman Body Parts:

def create_head():
    head = Turtle()
    head.hideturtle()
    head.speed('fastest')
    head.color('#ff9966')
    head.shape('circle')
    head.shapesize(2.5)
    head.penup()
    head.setpos(-160, 155)
    head.showturtle()

    return head

def create_body():
    body = Turtle()
    body.hideturtle()
    body.speed('fastest')
    body.color('#6600ff')
    body.shape('square')
    body.left(90)
    body.shapesize(1.5, 3.5, 4.5)
    body.penup()
    body.setpos(-160, 90)
    body.showturtle()

    return body

def create_left_arm():
    left_arm = Turtle()
    left_arm.hideturtle()
    left_arm.speed('fastest')
    left_arm.color('#ff9966')
    left_arm.shape('square')
    left_arm.shapesize(stretch_wid=0.5, stretch_len=2.5)
    left_arm.left(40)
    left_arm.penup()
    left_arm.setpos(-189, 104)
    left_arm.showturtle()

    return left_arm

def create_right_arm():
    right_arm = Turtle()
    right_arm.hideturtle()
    right_arm.speed('fastest')
    right_arm.color('#ff9966')
    right_arm.shape('square')
    right_arm.shapesize(stretch_wid=0.5, stretch_len=2.5)
    right_arm.left(140)
    right_arm.penup()
    right_arm.setpos(-129, 102)
    right_arm.showturtle()

    return right_arm

def create_left_leg():
    left_leg = Turtle()
    left_leg.hideturtle()
    left_leg.speed('fastest')
    left_leg.color('#ff9966')
    left_leg.shape('square')
    left_leg.shapesize(stretch_len=0.7, stretch_wid=1.7)
    left_leg.penup()
    left_leg.setpos(-170, 35.6)
    left_leg.showturtle()

    return left_leg

def create_right_leg():
    right_leg = Turtle()
    right_leg.hideturtle()
    right_leg.speed(0)
    right_leg.color('#ff9966')
    right_leg.shape('square')
    right_leg.shapesize(stretch_len=0.7, stretch_wid=1.7)
    right_leg.penup()
    right_leg.setpos(-150, 35.6)
    right_leg.showturtle()

    return right_leg

# Clear Hangman

def game_clear():
    hangman_body.clear()
    hangman_head.clear()
    hangman_left_arm.clear()
    hangman_right_arm.clear()
    hangman_left_leg.clear()
    hangman_right_leg.clear()

def game_lose():
    marker.write('Better Luck Next Time!', align='center', font=BOLD_FONT)

def game_win():
    marker.write('Congratulations!', align='center', font=BOLD_FONT)

# Game Screen
screen = Screen()
screen.setup(WIDTH, HEIGHT)
screen.bgcolor('#dfe4db')
screen.title("Hangman by Name")
screen.onkey(screen.bye, 'Escape')
screen.listen()

# Game Greeting Text

marker = Turtle()
marker.hideturtle()
marker.penup()
marker.sety(40)
marker.write('Welcome to Hangman', align='center', font=BOLD_FONT)
marker.home()
marker.write("Press 'ESC' to quit at any time", align='center', font=NORMAL_FONT)

panel = Turtle()
panel.hideturtle()
panel.speed('fastest')
panel.shape('square')
panel.penup()

# Hangman Top Panel Stockhold
top_panel = panel.clone()
top_panel.color('#4f371b')
top_panel.setpos(-170, 260)
top_panel.shapesize(stretch_len=20)
top_panel.showturtle()

# Hangman Left Side Panel Stockhold
left_panel = panel.clone()
left_panel.color('#4f371b')
left_panel.setpos(-380, 20)
left_panel.shapesize(stretch_len=1, stretch_wid=25)
left_panel.showturtle()

# Hangman Rope
rope = panel.clone()
rope.color('#4f371b')
rope.setpos(-160, 199)
rope.shapesize(stretch_len=0.5, stretch_wid=5)
rope.showturtle()

# Hangman Base Stockhold
base_panel_ground = panel.clone()
base_panel_ground.color('#1c5721')
base_panel_ground.setpos(390, -290)
base_panel_ground.shapesize(stretch_len=600, stretch_wid=10)
base_panel_ground.showturtle()

# Storing the Words and Letters:

hangman_head = create_head()
hangman_body = create_body()
hangman_left_arm = create_left_arm()
hangman_right_arm = create_right_arm()
hangman_left_leg = create_left_leg()
hangman_right_leg = create_right_leg()

sleep(1)
marker.clear()

screen.mainloop()
cdlane
  • 40,441
  • 5
  • 32
  • 81
  • Hi Cdlane, Thanks for your advice and for reworking the code. I'll be sure to take your advice in and work on those things first, and avoid the issues you pointed out to me. Very appreciated, it all helps me to learn more and to do it the right way. Thanks again! – 247Anxiety Sep 20 '22 at 22:05