Python编写贪吃蛇游戏调用turtle.update()触发Terminator错误如何解决
报错原因及代码问题
turtle.Terminator报错本质是窗口销毁后依然在调用turtle相关的更新方法,除此之外你的代码还存在多处语法、逻辑错误,具体问题如下:
- 未初始化
high_score变量,首次吃到食物判断得分时会直接报错 - 边界碰撞处理时,遍历身体段的代码写错了调用对象:
segments.goto(1000,1000)应该改为单个segment.goto(1000,1000),列表没有goto方法 - 自碰撞处理时,清空身体段的代码写错:
segment.clear()应该改为segments.clear(),错误调用了单段对象的方法清空列表 - 主循环逻辑缩进错误:身体段位置跟随、蛇头移动、自碰撞检测的代码全部缩进在了「吃到食物」的判断分支内,只有吃到食物蛇才会移动,逻辑完全错误
- 方向变量大小写不一致:初始方向是小写
"stop",碰撞后赋值为大写"Stop",导致移动函数无法正确识别停止状态 - 没有补充吃到食物后重置食物位置的逻辑,食物会一直停在原地
- 无限
while True循环没有处理窗口关闭事件,手动关闭窗口后循环还在执行wn.update(),触发Terminator异常
修复后可正常运行的代码
import turtle import time import random delay = 0.1 score = 0 high_score = 0 # 初始化最高分变量 wn = turtle.Screen() wn.title("Snake Game") wn.bgcolor("blue") wn.setup(width=600 ,height=600) wn.tracer(0) # 蛇头 head = turtle.Turtle() head.shape("square") head.color("white") head.penup() head.goto(0,0) head.direction = "stop" # 食物 food = turtle.Turtle() colors = random.choice(['red','green','black']) shapes = random.choice(['square','triangle','circle']) food.speed(0) food.shape(shapes) food.color(colors) food.penup() food.goto(0,100) # 得分面板 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")) def goup(): if head.direction !="down": head.direction = "up" def godown(): if head.direction !="up": head.direction ="down" def goleft(): if head.direction !="right": head.direction ="left" def goright(): 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) wn.listen() wn.onkeypress(goup,"w") wn.onkeypress(godown,"s") wn.onkeypress(goleft,"a") wn.onkeypress(goright,"d") segments = [] # 主游戏循环,增加异常捕获处理窗口关闭 try: while True: wn.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" # 统一方向变量小写 for segment in segments: segment.goto(1000,1000) # 修改为单个segment调用方法 segments.clear() score = 0 delay = 0.1 pen.clear() pen.write(f"Score : {score} High 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) # 重置食物位置 # 添加身体段 new_segment = turtle.Turtle() new_segment.speed(0) new_segment.shape("square") new_segment.color("orange") new_segment.penup() segments.append(new_segment) delay -= 0.001 score += 10 if score > high_score: high_score = score pen.clear() pen.write(f"Score : {score} High Score : {high_score} ",align="center", font=("candara",24,"bold")) # 身体段位置跟随 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() # 移动蛇头 # 自碰撞检测 for segment in segments: if segment.distance(head) < 20: time.sleep(1) head.goto(0,0) head.direction = "stop" # 统一方向变量小写 for segment in segments: segment.goto(1000,1000) segments.clear() # 清空身体段列表 score = 0 delay = 0.1 pen.clear() pen.write(f"Score : {score} High Score : {high_score} ",align="center", font=("candara",24,"bold")) time.sleep(delay) except turtle.Terminator: # 捕获窗口关闭的异常,正常退出程序 pass
内容的提问来源于stack exchange,提问作者TobiDzN
相关产品推荐
相关产品推荐

