如何解决如何在python中为乌龟添加物理
我正在用 python 创建一个游戏,您可以四处移动并尝试通过与硬币碰撞来收集积分。我想为它添加 2D 物理以使其逼真。有没有办法做到这一点?谢谢
解决方法
模拟的本质在于它们与现实不同。在进行模拟时,我们创建了对象行为的数学模型。让我们考虑模拟重力。我们定义一个球,然后随着时间的流逝移动球的位置。牛顿通过 x=gt^2
模拟了球的下落。因此,我们必须定义球朝向地面的加速度。 in this video 提供了更多有关模拟的信息。
您可以使用此代码来模拟重力:
yvel=5*(time()-start_t)
time()
来自哪里from time import time
这是如何做到的: 解释在代码里面。
#Importing all the modules
import turtle
from turtle import *
import math
#Creating the screen
screen=Screen()
#Creating the turtle
vector1=Turtle("classic")
vector1.speed(-1)
vector1.penup()
#Declaring all the needed variables
Vx=0
Vy=0
V=0
A=0
#Starting the while loop
while True:
#Updating the screen for better preformence
screen.update()
#Carculating the velocity
V=math.sqrt(Vx**2+Vy**2)
#Carculating the angle
if Vx!=0:
A=math.degrees(math.atan(Vy/Vx))
if Vy<0 and Vx<0 or Vy>0 and Vx<0:
A=A-180
elif Vy <0:
A=270
else:
A=90
#Moving the turtle
vector1.seth(A)
vector1.fd(V)
#Changing the values of the velocities
Vy-=0.5
if Vx>0:
Vx-=0.2
elif Vx<0:
Vx+=0.2
如有任何问题,请追问!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。