【发布时间】:2020-09-11 17:01:37
【问题描述】:
我正在用python制作一个简单的面向对象的弹丸模拟程序。 我只是在使用 Turtle 和 Math 模块。 问题是当我尝试简单地移动我的弹丸时(作为积分某些方程之前的测试)它不会移动。
import turtle
from turtle import Turtle
import math
window = turtle.Screen()
window.title("Projectile")
window.bgcolor("black")
window.setup(width=800, height=800)
window.tracer(0)
def drawLine():
line = turtle.Turtle()
line.penup()
line.pencolor("white")
line.pensize(10)
line.goto(-400, -300)
line.pendown()
line.forward(800)
line.penup()
line.ht()
class Projectile:
def __init__(self, x, y, color, shape, a, b):
self.turtle = turtle.Turtle()
self.turtle.goto(x, y)
self.turtle.color(color)
self.turtle.shape(shape)
self.turtle.shapesize(a, b)
def launch(self, x, y):
self.turtle.penup()
self.turtle.setx(self.turtle.xcor() + x)
self.turtle.sety(self.turtle.ycor() + y)
running = True
while running:
window.update()
drawLine()
projectileOne = Projectile(-290, -290, "red", "circle", 1, 1)
projectileOne.launch(25, 25)
这应该可以移动我的乌龟,不是吗?
self.turtle.setx(self.turtle.xcor() + x)
self.turtle.sety(self.turtle.ycor() + y)
我不明白发生了什么。为什么弹丸不动?它只是移动 (25, 25) 并停止。
代码运行后出现的错误:
Traceback (most recent call last):
File "C:\Users\HP\Desktop\Python projects\test\test.py", line 39, in <module>
drawLine()
File "C:\Users\HP\Desktop\Python projects\test\test.py", line 12, in drawLine
line = turtle.Turtle()
File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 3813, in __init__
RawTurtle.__init__(self, Turtle._screen,
File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 2557, in __init__
self._update()
File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 2660, in _update
self._update_data()
File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 2646, in _update_data
self.screen._incrementudc()
File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 1292, in _incrementudc
raise Terminator
turtle.Terminator
[Finished in 7.4s]
如果我从我得到的代码中完全删除 drawLine():
Traceback (most recent call last):
File "C:\Users\HP\Desktop\Python projects\test\test.py", line 40, in <module>
projectileOne = Projectile(-290, -290, "red", "circle", 1, 1)
File "C:\Users\HP\Desktop\Python projects\test\test.py", line 24, in __init__
self.turtle = turtle.Turtle()
File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 3813, in __init__
RawTurtle.__init__(self, Turtle._screen,
File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 2557, in __init__
self._update()
File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 2660, in _update
self._update_data()
File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 2646, in _update_data
self.screen._incrementudc()
File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 1292, in _incrementudc
raise Terminator
turtle.Terminator
[Finished in 5.2s]
【问题讨论】:
-
你一遍又一遍地调用唯一的绘图函数
drawLine()。它一直在画同一条线。launch方法永远不会放下笔。 -
我从主循环中完全删除了
drawLine(),但它仍然不起作用。我用关闭程序后遇到的错误更新了我的问题。something = turtle.Turtle()有问题吗?
标签: python python-3.x turtle-graphics