Python Turtle Graphics 冻结且无响应

问题描述 投票:0回答:1

这段代码运行了 4-5 次没有出现问题,但现在就不再出现问题了。每当我运行该文件时,Python Turtle Graphics 的窗口就会停止响应(冻结)并最终崩溃。

import random
import turtle

import package

running = True

words = ['aardvark', 'camel', 'baboon']
found_letters = []
hangman = package.Man(turtle.Turtle())

word = str(random.choice(words))
listed_word = []

for character in word:
    listed_word.append(character)


blanks = ''
lives = 6

for character in word:
    blanks += '_'

print(blanks)

while running:
    guess = str(input(f"Enter your letter: ")).lower()

    if len(guess) == 1:
        if guess in listed_word:
            for index, letter in enumerate(listed_word):
                if letter == guess:
                    blanks_list = list(blanks)

                    blanks_list[index] = guess
                    blanks = ''.join(blanks_list)

            print(blanks)

            if blanks == word:
                print("You win!")
                turtle.done()
                running = False
        else:
            if lives != 0:
                lives -= 1
                hangman.check(lives)

            if lives > 0:
                print(f"You have {lives} lives left.")
            else:
                print("Game over!")
                turtle.done()
                running = False
    else:
        pass

我不确定问题是在循环内调用检查函数,还是初始化类。

这是带有“Man”类的“package”文件。

import turtle


class Man:
    def __init__(self, turtle_class: turtle.Turtle):
        self.pen = turtle_class
        self.pen.penup()

        self.pen.goto(0, 100)

    def circle(self):
        self.pen.pendown()

        self.pen.circle(30)

        self.pen.penup()

    def body(self):
        self.pen.pendown()
        self.pen.right(90)

        self.pen.forward(60)
        self.pen.penup()

    def left_leg(self):
        self.pen.pendown()
        self.pen.left(30)

        self.pen.forward(60)
        self.pen.penup()

        self.pen.back(60)
        self.pen.right(30)

    def right_leg(self):
        self.pen.pendown()
        self.pen.right(30)

        self.pen.forward(60)
        self.pen.penup()

        self.pen.back(60)
        self.pen.left(30)

    def left_arm(self):
        self.pen.left(180)
        self.pen.forward(46.6666666667)

        self.pen.left(90)
        self.pen.left(30)

        self.pen.pendown()
        self.pen.forward(46.6666666667)

        self.pen.penup()

        self.pen.left(180)
        self.pen.forward(46.6666666667)

        self.pen.left(30)

    def right_arm(self):
        self.pen.right(90)

        self.pen.pendown()
        self.pen.forward(46.6666666667)

        self.pen.penup()

    def check(self, value):
        if value == 5:
            self.circle()
        elif value == 4:
            self.body()
        elif value == 3:
            self.left_leg()
        elif value == 2:
            self.right_leg()
        elif value == 1:
            self.left_arm()

检查函数是主文件中唯一调用的函数。

我确实注意到,每当我在用该类制作的“包”文件中尝试它时,它都会在初始化后自动关闭,因为我没有对它执行任何操作。

但是,在主文件中,每当我初始化我的类(顶部名为hangman的变量)时,即使我不将其放入循环中或对其执行任何操作,它也会保留下来。 (测试期间)

当我删除与类和“包”文件相关的所有内容时,游戏运行得很好。

python crash turtle-graphics freeze python-turtle
1个回答
0
投票

我通过使用

turtle.bye()
而不是
turtle.done()
解决了这个问题。

这成功终止了循环。

这是循环的(新)代码:

while running:
    guess = str(input(f"Enter your letter: ")).lower()

    if len(guess) == 1:
        if guess in listed_word:
            for index, letter in enumerate(listed_word):
                if letter == guess:
                    blanks_list = list(blanks)

                    blanks_list[index] = guess
                    blanks = ''.join(blanks_list)

            print(blanks)

            if blanks == word:
                print("You win!")
                turtle.bye()
                running = False
        else:
            if lives != 0:
                lives -= 1
                hangman.check(lives)

            if lives > 0:
                print(f"You have {lives} lives left.")
            else:
                print("Game over!")
                turtle.bye()
                running = False
    else:
        pass
© www.soinside.com 2019 - 2024. All rights reserved.