我们如何得到一个工作围绕Python引发终结者错误的屏幕?



朋友们,我很感激你们的帮助。我不是python专家,只是在帮我侄子做他的项目。

我们在更新屏幕时得到raise终结者错误。

我们在Google和StackOverflow上搜索了类似错误的查询- "raise Terminator error…"但似乎在得到答案方面没有进展。

代码如下:

import turtle  
import time
import random

delay = 0.1


score = 0


high_score = 0






t=turtle.Screen()


t.title("Snake Xenzia")


t.bgcolor("skyblue")


t.setup(width=600, height=600)


t.tracer(0)


# Head of snake


head = turtle.Turtle()


head.shape("square")


head.color("white")


head.penup()


head.goto(0, 0)


head.direction = "Stop"


# Food


food = turtle.Turtle()


colors = random.choice(['red', 'green', 'blue'])


shapes = random.choice(['square', 'triangle', 'circle'])


food.speed(0)


food.shape(shapes)


food.color(colors)


food.penup()


food.goto(0, 100)


# Adding Segments


pen = turtle.Turtle()


pen.speed(0)


pen.shape("square")


pen.color("white")


pen.penup()


pen.hideturtle()


pen.goto(0, 250)


pen.write("Score : 0  High Score : 0", align="center",


font=("candara", 24, "bold"))






# Coding Moving


def wkey():


if head.direction != "down":


head.direction = "up"




def skey():


if head.direction != "up":


head.direction = "down"




def akey():


if head.direction != "right":


head.direction = "left"




def dkey():


if head.direction != "left":


head.direction = "right"




def move():


if head.direction == "up":


y = head.ycor()


head.sety(y+20)


if head.direction == "down":


y = head.ycor()


head.sety(y-20)


if head.direction == "left":


x = head.xcor()


head.setx(x-20)


if head.direction == "right":


x = head.xcor()


head.setx(x+20)







t.listen()


t.onkeypress(wkey, "w")


t.onkeypress(skey, "s")


t.onkeypress(akey, "a")


t.onkeypress(dkey, "d")


segments = []






# Main Gameplay


while True:


t.update()


if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
time.sleep(1)
head.goto(0, 0)
head.direction = "Stop"
colors = random.choice(['red', 'blue', 'green'])
shapes = random.choice(['square', 'circle'])


for segment in segments:
segment.goto(1000, 1000)


segments.clear()
score = 0
delay = 0.1


pen.clear()


pen.write("Score : {} High Score : {} ".format(


score, high_score), align="center", font=("candara", 24, "bold"))


if head.distance(food) < 20:


x = random.randint(-270, 270)
y = random.randint(-270, 270)


food.goto(x, y)


# Adding segment
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("orange")  # tail colour
new_segment.penup()
segments.append(new_segment)


delay -= 0.001
score += 10


if score > high_score:
high_score = score


pen.clear()
pen.write("Score : {} High Score : {} ".format(score, high_score), align="center", font=("candara", 24, "bold"))


# Collision


for index in range(len(segments)-1, 0, -1):
x = segments[index-1].xcor()
y = segments[index-1].ycor()
segments[index].goto(x, y)


if len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x, y)


move()


# Check for head collision with the body segments
for segment in segments:
if segment.distance(head) < 20:
time.sleep(1)
head.goto(0, 0)
head.direction = "stop"


colors = random.choice(['red', 'blue', 'green'])
shapes = random.choice(['square', 'circle'])


for segment in segments:
segment.goto(1000, 1000)


segment.clear()
score = 0
delay = 0.1


pen.clear()
pen.write("Score : {} High Score : {} ".format(score, high_score), align="center", font=("candara", 24, "bold"))

time.sleep(delay)

t.mainloop()

任何帮助都会很感激。谢谢。

我无法重现您描述的错误。然而,有一些问题的代码,一旦解决可能解决您的问题。

最明显的问题是while True:循环,它在事件驱动的环境中没有位置,比如turtle,以及从未到达的mainloop()调用。我们应该用ontimer()事件来代替。

另一个问题是您的"stop""Stop"令牌有两个不同的字母案例。这不是一个迫在眉睫的问题,但很容易成为一个问题。

在没有绘制任何内容的段上调用segment.clear()。这看起来像一个副本&在之前的循环中粘贴segments.clear()的工件

您在不实际使用colorsshapes时设置它们。你的墙碰撞和身体碰撞代码几乎是相同的,可以合并成一个。

下面是我对你的代码的修改。看看这些改变是否对你有意义,是否能解决你的问题:

from turtle import Screen, Turtle
from random import choice, randint
SCORE_FONT = ('candara', 24, 'bold')
# Moving
def wkey():
if head.direction != 'down':
head.direction = 'up'
def skey():
if head.direction != 'up':
head.direction = 'down'
def akey():
if head.direction != 'right':
head.direction = 'left'
def dkey():
if head.direction != 'left':
head.direction = 'right'
def move():
x, y = head.position()
if head.direction == 'up':
head.sety(y + 20)
elif head.direction == 'down':
head.sety(y - 20)
elif head.direction == 'left':
head.setx(x - 20)
elif head.direction == 'right':
head.setx(x + 20)
if segments:
for index in range(len(segments) - 1, 0, -1):
previous = segments[index-1]
segments[index].goto(previous.position())
segments[0].goto(x, y)
# Main Gameplay
delay = 0.1
score = 0
high_score = 0
def play():
global high_score, score, delay
# Check for head collision with wall or own body segments
if not (-290 < head.xcor() < 290 and -290 < head.ycor() < 290) or any(head.distance(segment) < 20 for segment in segments):
head.home()
head.direction = 'stop'
for segment in segments:
segment.hideturtle()
segments.clear()
score = 0
delay = 0.1
pen.clear()
pen.write("Score : {} High Score : {} ".format(score, high_score), align='center', font=SCORE_FONT)
elif head.distance(food) < 20:
shape = choice(['square', 'triangle', 'circle'])
color = choice(['red', 'green', 'blue'])
x, y = randint(-270, 270), randint(-270, 270)
food.shape(shape)
food.color(color)
food.goto(x, y)
# Adding segment
new_segment = head.clone()
new_segment.color('orange')  # tail colour
segments.append(new_segment)
delay -= 0.001
score += 10
if score > high_score:
high_score = score
pen.clear()
pen.write("Score : {} High Score : {} ".format(score, high_score), align='center', font=SCORE_FONT)
move()
screen.update()
screen.ontimer(play, int(delay * 1000))
screen = Screen()
screen.title("Snake Xenzia")
screen.bgcolor('skyblue')
screen.setup(width=600, height=600)
screen.tracer(False)
# Head of snake
head = Turtle()
head.shape('square')
head.color('white')
head.penup()
head.direction = 'stop'  # user-defined property
# Food
color = choice(['red', 'green', 'blue'])
shape = choice(['square', 'triangle', 'circle'])
food = Turtle()
food.shape(shape)
food.color(color)
food.penup()
food.sety(100)
# Scoring
pen = Turtle()
pen.hideturtle()
pen.color('white')
pen.penup()
pen.sety(250)
pen.write("Score : 0  High Score : 0", align='center', font=SCORE_FONT)
segments = []
screen.onkeypress(wkey, 'w')
screen.onkeypress(skey, 's')
screen.onkeypress(akey, 'a')
screen.onkeypress(dkey, 'd')
screen.listen()
play()
screen.mainloop()

请注意,一旦你关闭了tracer(),调用speed()方法什么也不做,因为我们正在运行。

最新更新