切换代码编辑器后Python Turtle库抛出Terminator错误
问题
在replit.com上编写的弓箭小游戏代码运行正常,但导入Python 3.9.6版本的VS Code后触发错误。
游戏代码
import math import turtle import time wn = turtle.Screen() wn.title("Tiro al blanco") wn.bgcolor("black") wn.setup(width = 800, height = 600) wn.tracer(0) #parametros optimos: vi = 14.5, th = 45 vi = 14.5 th = 45 segs = 0 score = 0 #dibujar target target = turtle.Turtle() target.speed(0) #Velocidad de animacion, no de movimiento target.shape("circle") target.color("white") target.shapesize(stretch_wid = 3, stretch_len = 3) target.penup() ##Para que no trace línea de movimiento target.goto(350,0) ##posición inicial target2 = turtle.Turtle() target2.speed(0) #Velocidad de animacion, no de movimiento target2.shape("circle") target2.color("red") target2.shapesize(stretch_wid = 2, stretch_len = 2) target2.penup() ##Para que no trace línea de movimiento target2.goto(350,0) ##posición inicial target3 = turtle.Turtle() target3.speed(0) #Velocidad de animacion, no de movimiento target3.shape("circle") target3.color("white") target3.shapesize(stretch_wid = 1, stretch_len = 1) target3.penup() ##Para que no trace línea de movimiento target3.goto(350,0) ##posición inicial target4 = turtle.Turtle() target4.speed(0) #Velocidad de animacion, no de movimiento target4.shape("circle") target4.color("red") target4.shapesize(stretch_wid = 0.5, stretch_len = 0.5) target4.penup() ##Para que no trace línea de movimiento target4.goto(350,0) ##posición inicial #dibujar arco bow = turtle.Turtle() bow.speed(0) #Velocidad de animacion, no de movimiento bow.shape(turtle.circle(20,180)) bow.color("brown") bow.shapesize(stretch_wid = 5, stretch_len = 1) bow.penup() ##Para que no trace línea de movimiento bow.goto(-350,0) ##posición inicial #dibujar flecha arrow = turtle.Turtle() arrow.speed(0) #Velocidad de animacion, no de movimiento arrow.shape("arrow") arrow.color("cyan") arrow.penup() ##Para que no trace línea de movimiento arrow.goto(-350,0) ##posición inicial arrow.velx = vi*math.cos(math.radians(th)) arrow.vely = vi*math.sin(math.radians(th)) #scoreboard pen = turtle.Turtle() pen.speed(0) pen.color("white") pen.penup() pen.hideturtle() pen.goto(0, 260) pen.write("Score: 0", align = "center", font=("Courier", 24, "normal")) #parametros pen = turtle.Turtle() pen.speed(0) pen.color("white") pen.penup() pen.hideturtle() pen.goto(0, -260) pen.write("Velocidad:{}, Inclinacion: {} ".format(vi, th), align = "center", font=("Courier", 24, "normal")) while True: wn.update() segs +=1 arrow.vely = vi*math.sin(math.radians(th)) - 0.3*(segs) if arrow.ycor() < -100: arrow.vely = 0 arrow.velx = 0 if arrow.ycor() > 275: arrow.vely = 0 arrow.velx = 0 if arrow.xcor() > 335: arrow.vely = 0 arrow.velx = 0 if arrow.xcor() >= 330: if arrow.xcor() <= 350: if arrow.ycor() >= -30: if arrow.ycor() <= 30: arrow.vely = 0 arrow.velx = 0 score +=1 pen.clear() time.sleep(3) arrow.goto(-350,0) arrow.setx(arrow.xcor() + arrow.velx) arrow.sety(arrow.ycor() + arrow.vely)
报错信息
DEPRECATION WARNING: The system version of Tk is deprecated and may be removed in a future release. Please don't rely on it. Set TK_SILENCE_DEPRECATION=1 to suppress this warning. Traceback (most recent call last): File "/Users/nicolasderbez/Desktop/tiroalblanco.py", line 21, in <module> target = turtle.Turtle() File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/turtle.py", line 3814, in __init__ RawTurtle.__init__(self, Turtle._screen, File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/turtle.py", line 2558, in __init__ self._update() File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/turtle.py", line 2661, in _update self._update_data() File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/turtle.py", line 2647, in _update_data self.screen._incrementudc() File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/turtle.py", line 1293, in _incrementudc raise Terminator turtle.Terminator
是否因版本差异需要重新编写代码?
解决方案
不需要完全重写代码,问题主要出在Tkinter环境和代码细节上,按以下步骤修复:
屏蔽Tk版本警告
在代码最顶部添加环境变量设置:import os os.environ['TK_SILENCE_DEPRECATION'] = '1'修复弓的形状设置错误
turtle.circle()是绘图方法,不能直接作为shape()参数,替换为预定义形状或自定义形状:# 替换原bow.shape行 bow.shape("square") # 若要半圆形状,可改为: # wn.register_shape("bow", ((-20,0), (-20,-20), (20,-20), (20,0))) # bow.shape("bow")替换死循环避免线程冲突
原while True会阻塞turtle事件循环,改用ontimer和mainloop:# 替换原while循环 def game_loop(): global segs, score wn.update() segs +=1 arrow.vely = vi*math.sin(math.radians(th)) - 0.3*(segs) if arrow.ycor() < -100: arrow.vely = 0 arrow.velx = 0 if arrow.ycor() > 275: arrow.vely = 0 arrow.velx = 0 if arrow.xcor() > 335: arrow.vely = 0 arrow.velx = 0 hit = False if 330 <= arrow.xcor() <= 350 and -30 <= arrow.ycor() <=30: arrow.vely = 0 arrow.velx = 0 score +=1 score_pen.clear() score_pen.write(f"Score: {score}", align = "center", font=("Courier", 24, "normal")) # 用ontimer替代time.sleep避免阻塞 def reset_arrow(): arrow.goto(-350,0) global segs segs = 0 arrow.velx = vi*math.cos(math.radians(th)) arrow.vely = vi*math.sin(math.radians(th)) wn.ontimer(reset_arrow, 3000) hit = True if not hit: arrow.setx(arrow.xcor() + arrow.velx) arrow.sety(arrow.ycor() + arrow.vely) wn.ontimer(game_loop, 10)修复pen变量覆盖问题
原代码两次用pen = turtle.Turtle()导致变量覆盖,需分别命名:# scoreboard score_pen = turtle.Turtle() score_pen.speed(0) score_pen.color("white") score_pen.penup() score_pen.hideturtle() score_pen.goto(0, 260) score_pen.write("Score: 0", align = "center", font=("Courier", 24, "normal")) #parametros param_pen = turtle.Turtle() param_pen.speed(0) param_pen.color("white") param_pen.penup() param_pen.hideturtle() param_pen.goto(0, -260) param_pen.write("Velocidad:{}, Inclinacion: {} ".format(vi, th), align = "center", font=("Courier", 24, "normal"))添加循环启动与主循环
在代码末尾添加:game_loop() wn.mainloop()
内容的提问来源于stack exchange,提问作者Nicolás Derbez
相关产品推荐
相关产品推荐

