【问题标题】:Python Turtle object not movingPython Turtle 对象不移动
【发布时间】: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


【解决方案1】:

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

running = True
while running:

像这样的无限循环在乌龟这样的事件驱动环境中没有位置,它可能会阻塞事件。 (就像“窗口正在关闭”事件。)相反,使用计时器事件来保持运行。

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

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()

【讨论】:

  • 谢谢,这正是我想要的!
猜你喜欢
  • 1970-01-01
  • 2018-10-16
  • 1970-01-01
  • 2016-10-03
  • 2021-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多