如何解决python乌龟模块中的推力和加速度有问题
我正在尝试编写一艘在放开“w”后保持其动力的船。但是,我也希望它在放开“w”后逐渐变慢。我得到了一艘不错的船,但有时当我在向某个方向前进后按“w”时,船就会飞起来。有什么办法可以增加最高速度或让它变慢吗?
self.thrust = 0.0
self.acceleration = 0.0001
self.dx += math.cos(math.radians(self.heading)) * self.thrust
self.dy += math.sin(math.radians(self.heading)) * self.thrust
def accelerate(self):
self.thrust += self.acceleration
def decelerate(self):
self.thrust = 0
解决方法
如果您希望它在不按“w”时逐渐减速,那么您可能希望它以相同的速度或更快的速度运行。此代码将使其以相同的速度运行。这应该适用于任何 Python IDE。
假设你的船是一只乌龟。
import turtle
Win = turtle.screen()
Boat = turtle.Turtle()
如果你想让它看起来也像一艘船,你可以这样做:
screen.adshape(Saved Document Direction)
Boat.shape(Saved Document Direction)
否则,您可以创建自己的形状。 现在,我们希望它以恒定速度运行,同时按下“w”: 所以:
def Constant_Speed():
Boat.speed(Speed You Want it to Go)
Win.listen()
Win.onkeypress(Constant_Speed,"w")
否则,它应该会变慢。
Initial_Speed = Initial Speed
Boat.speed(Initial_Speed)
Initial_Speed *= Fraction of what you want it to be in/decimal
所以,如果你用变量替换,这应该可以工作:
import turtle
Win = turtle.screen()
Boat = turtle.Turtle()
screen.adshape(Saved Document Direction)
Boat.shape(Saved Document Direction)
def Constant_Speed():
Boat.speed(Speed You Want it to Go)
Win.listen()
Win.onkeypress(Constant_Speed,"w")
Initial_Speed = Initial Speed
Boat.speed(Initial_Speed)
Initial_Speed *= Fraction of what you want it to be in/decimal and must be less than 1
但是如果你想让它跑得更快,而不是匀速怎么办?
然后转到 def Constant_Speed()
部分。
您可以重命名它:def Go_Faster()
现在,代码:
Speed = Initial_Speed
Boat.speed(Speed)
Speed *= Times you want to increase speed by
还有: 将最后一行更改为:
Initial_Speed2 = Initial Speed * Fraction of what you want it to be in/decimal and must be less than 1
用你的变量。 现在,这应该有效:
import turtle
Win = turtle.screen()
Boat = turtle.Turtle()
screen.adshape(Saved Document Direction)
Boat.shape(Saved Document Direction)
def Go_Faster():
Speed = Initial_Speed()
Boat.speed(Speed)
Speed *= Times you want to increase by
Win.listen()
Win.onkeypress(Constant_Speed,"w")
Initial_Speed = Initial Speed
Boat.speed(Initial_Speed)
Initial_Speed2 = Initial Speed * Fraction of what you want it to be in/decimal and must be less than 1
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。