Python 乌龟对象不动

Python Turtle object not moving

我正在 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]

关闭程序后出现的错误与您的绘图问题没有直接关系。它们是由这种逻辑引起的:

running = True
while running:

像这样的无限循环在像 turtle 这样的事件驱动环境中没有立足之地,它可能会阻塞事件。 (如 "window is closing" 事件。)而是使用计时器事件来保持 运行.

下面是对您的代码的重写,它使用计时器事件来发射几个(无重力)射弹,这些射弹将继续飞行,直到它们到达 window 的顶部。希望这会为您提供一个构建模拟的框架:

from turtle import Screen, Turtle

class Projectile(Turtle):
    def __init__(self, position, color, shape, width, length):
        super().__init__(shape=shape)

        self.color('white', color)
        self.shapesize(width, length)
        self.penup()
        self.setposition(position)
        self.pendown()
        self.pensize(4)

        self.flying = False
        screen.update()

    def launch(self, angle):
        self.setheading(angle)
        self.flying = True
        self.fly()

    def fly(self):
        if self.flying:
            self.forward(3)

            if self.ycor() > screen.window_height()/2:
                self.flying = False

            screen.ontimer(self.fly, 50)

        screen.update()

screen = Screen()
screen.setup(width=800, height=800)
screen.title("Projectile")
screen.bgcolor('black')
screen.tracer(0)

projectileOne = Projectile((-290, -290), 'red', 'triangle', 1, 2)
projectileTwo = Projectile((290, -290), 'green', 'circle', 1, 1)

projectileOne.launch(75)
projectileTwo.launch(130)

screen.mainloop()