gpt4 book ai didi

Python Turtle 对象不移动

转载 作者:行者123 更新时间:2023-12-03 17:00:17 25 4
gpt4 key购买 nike

我正在用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)

这应该会移动我的 turtle ,不是吗?
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 这样的事件驱动环境中没有位置,它可能会阻塞事件。 (就像“窗口正在关闭”事件。)相反,使用计时器事件来保持运行。

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

enter image description here

关于Python Turtle 对象不移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61995939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com