Python利用3D引擎做一個(gè)太陽系行星模擬器
這次,我們再來用Ursina引擎來做一個(gè)太陽系行星模擬器吧!
想要了解Ursina 3D引擎的基本使用方法的話,查看我的另一篇文章:詳解Python 3D引擎Ursina如何繪制立體圖形
這一次,我們要實(shí)現(xiàn)的效果如下


首先,送上本次需要用到的資源
Earth.jpg

Jupiter.jpg

Mars.jpg

Mercury.jpg

Neptune.jpg

Saturn.jpg

Sun.jpg

Uranus.jpg

Venus.jpg

現(xiàn)在,就開始寫代碼吧!
首先,導(dǎo)入我們需要的模塊,導(dǎo)入3D引擎ursina,數(shù)學(xué)庫math,ursina中自帶的第一人稱,sys,random隨機(jī)庫
from ursina import * from math import * from ursina.prefabs.first_person_controller import FirstPersonController import sys import random as rd
然后,創(chuàng)建app
app=Ursina()
將窗口設(shè)置為全屏,并設(shè)置背景顏色
window.fullscreen=True window.color=color.black
定義一個(gè)列表,來儲存生成的星
planets=[]
引入所有星球的材質(zhì)
sun_texture=load_texture("texture/Sun.png")
mercury_texture=load_texture("texture/Mercury.png")
venus_texture=load_texture("texture/Venus.png")
earth_texture=load_texture("texture/Earth.png")
mars_texture=load_texture("texture/Mars.png")
jupiter_texture=load_texture("texture/Jupiter.png")
saturn_texture=load_texture("texture/Saturn.png")
uranus_texture=load_texture("texture/Uranus.png")
neptune_texture=load_texture("texture/Neptune.png")
創(chuàng)建一個(gè)類Planet,繼承自實(shí)體Entity,傳入_type是星的類型,pos是位置,scale是縮放
angle:每次更新的時(shí)候行星圍繞太陽轉(zhuǎn)的弧度
fastMode的值為1或0,表示是否讓行星圍繞太陽公轉(zhuǎn)速度增加到200倍
rotation:星球傾斜度,這里我們隨機(jī)生成
rotspeed:星球自轉(zhuǎn)的速度
rotMode:表示沿著xyz軸的其中一條進(jìn)行旋轉(zhuǎn),自動(dòng)選擇
_type存儲星球類型
texture則是材質(zhì),通過eval獲得該變量
然后進(jìn)行超類的初始化,model是sphere,也就是球體形狀,texture表示貼圖,color顏色設(shè)置為white,position傳入坐標(biāo)
定義turn方法,傳入angle,只要不是太陽,就進(jìn)行自轉(zhuǎn)公轉(zhuǎn)操作,如果是快速模式,則速度增加到200倍,然后計(jì)算得出新的xy坐標(biāo),并用exec進(jìn)行自傳操作
最后定義input方法,接受用戶輸入,注意,這里方法名必須用input,因?yàn)樗窍到y(tǒng)自動(dòng)調(diào)用的,它總會(huì)向其傳入一個(gè)參數(shù),為按下的按鍵名字,我們就進(jìn)行判斷,如果按下回車,則進(jìn)行快速模式和普通模式間的切換
class Planet(Entity):
def __init__(self,_type,pos,scale=2):
self.angle=rd.uniform(0.0005,0.01)
self.fastMode=0
self.rotation=(rd.randint(0,360) for i in range(3))
self.rotspeed=rd.uniform(0.25,1.5)
self.rotMode=rd.choice(["x","y","z"])
self._type=_type
texture=eval(f"{_type}_texture")
super().__init__(model="sphere",
scale=scale,
texture=texture,
color=color.white,
position=pos)
def turn(self,angle):
if self._type!="sun":
if self.fastMode:
angle*=200
self.x=self.x*cos(radians(angle))-self.y*sin(radians(angle))
self.y=self.x*sin(radians(angle))+self.y*cos(radians(angle))
exec(f"self.rotation_{self.rotMode}+=self.rotspeed")
def input(self,key):
if key=="enter":
self.fastMode=1-self.fastMode接下來,我們定義Player類,繼承自FirstPersonController
為什么不直接用FirstPersonController呢?
因?yàn)閡rsina自帶的FirstPersonController自帶重力,我們這里只是作為第一人稱的視角使用,不需要重力,然后還有一些功能我們不需要用到,所以我們就寫一個(gè)類繼承下來,然后重寫它的一部分代碼即可。首先,引入全局變量planets,超類初始化,視野設(shè)置為90,將初始位置設(shè)置為地球的位置,重力(gravity)設(shè)置為0,表示沒有重力,vspeed表示上升下降時(shí)的速度,speed表示水平方向移動(dòng)的速度,mouse_sensitivity是鼠標(biāo)靈敏度,需要用Vec2的形式,注意,上面除了vspeed變量可以自己命名,其它的都不可以修改。接下來,重寫input,只接收esc按鍵的信息,當(dāng)我們按下esc時(shí),如果鼠標(biāo)為鎖定,則釋放,如果已經(jīng)釋放,則退出程序。然后創(chuàng)建_update方法,這里我們不重寫ursina自動(dòng)調(diào)用的update方法,因?yàn)橄到y(tǒng)代碼里面,update方法還有很多操作,如果我們要重寫的話,可能還要加上把系統(tǒng)代碼復(fù)制過來,代碼過于繁瑣,這里我們自己定義一個(gè)名字,在接下來會(huì)講到的代碼中自己調(diào)用它,在該方法中,監(jiān)聽鼠標(biāo)左鍵、左shift和空格的事件,空格原本是跳躍,這里我們設(shè)置為上升,系統(tǒng)代碼是在input中接收空格鍵的信息的,我們已經(jīng)重寫過了,所以這里不會(huì)觸發(fā)系統(tǒng)代碼的跳躍方法。
這里講一下input和update中進(jìn)行按鍵事件監(jiān)聽操作的不同,input每次只接收一個(gè)按鍵,而且,如果我們一個(gè)按鍵一直按下,它不會(huì)一直觸發(fā),只會(huì)觸發(fā)一次,然后等到該按鍵釋放,才會(huì)重新對該按鍵進(jìn)行監(jiān)聽;update相當(dāng)于主循環(huán),在任何于ursina有關(guān)的地方(比如繼承自Entity、Button這樣的類,或者是主程序)寫update方法,ursina都會(huì)進(jìn)行自動(dòng)調(diào)用,我們不需要手動(dòng)調(diào)用它,在update方法中監(jiān)聽事件,我們用到了held_keys,不難發(fā)現(xiàn),held_keys有多個(gè)元素,只要按下就為True,所以每次運(yùn)行到這里,只要按鍵按下,就執(zhí)行,而input傳入的key本身就是一個(gè)元素,所以只有一個(gè),我們按下esc的操作不能連續(xù)調(diào)用,所以用input,其它移動(dòng)玩家的代碼時(shí)可以重復(fù)執(zhí)行的,所以寫在update(應(yīng)該說是用held_keys)中。
class Player(FirstPersonController):
def __init__(self):
global planets
super().__init__()
camera.fov=90
self.position=planets[3].position
self.gravity=0
self.vspeed=2
self.speed=600
self.mouse_sensitivity=Vec2(160,160)
self.on_enable()
def input(self,key):
if key=="escape":
if mouse.locked:
self.on_disable()
else:
sys.exit()
def _update(self):
if held_keys["left mouse"]:
self.on_enable()
if held_keys["left shift"]:
self.y-=self.vspeed
if held_keys["space"]:
self.y+=self.vspeed然后在主程序中寫update方法,并在其中調(diào)用我們剛剛寫的player中的_update方法,再對星球進(jìn)行自轉(zhuǎn)公轉(zhuǎn)操作
def update():
global planets,player
for planet in planets:
planet.turn(planet.angle)
player._update()
接下來,我們定義兩個(gè)列表,分別表示星球名稱和星球的大小,其實(shí)在實(shí)際的大小比例中,和這個(gè)相差很多,如果地球是1,太陽則大約為130000,木星和圖形分別為1500多和700多,這樣相差太大,做在程序里看起來很不尋常,所以我們這里對大多數(shù)星球的大小進(jìn)行放大縮小,把它們大小的相差拉近點(diǎn)。然后遍歷并繪制,每顆星球的間隔為前一個(gè)的10倍
ps=["sun","mercury","venus","earth","mars","jupiter","saturn","uranus","neptune"]
cp=[200,15,35,42,20,160,145,90,80]
x,y,z=0,0,0
for i,p in enumerate(ps):
newPlanet=Planet(p,(x,y,z),cp[i])
planets.append(newPlanet)
x+=cp[i]*10
最后實(shí)例化player,并運(yùn)行app
player=Player()
if __name__ == '__main__':
app.run()
然后就能實(shí)現(xiàn)文章前面展示的效果啦~


最后,附上代碼
from ursina import *
from math import *
from ursina.prefabs.first_person_controller import FirstPersonController
import sys
import random as rd
app=Ursina()
window.fullscreen=True
window.color=color.black
planets=[]
class Planet(Entity):
def __init__(self,_type,pos,scale=2):
self.angle=rd.uniform(0.0005,0.01)
self.fastMode=0
self.rotation=(rd.randint(0,360) for i in range(3))
self.rotspeed=rd.uniform(0.25,1.5)
self.rotMode=rd.choice(["x","y","z"])
self._type=_type
texture=eval(f"{_type}_texture")
super().__init__(model="sphere",
scale=scale,
texture=texture,
color=color.white,
position=pos)
def turn(self,angle):
if self._type!="sun":
if self.fastMode:
angle*=200
self.x=self.x*cos(radians(angle))-self.y*sin(radians(angle))
self.y=self.x*sin(radians(angle))+self.y*cos(radians(angle))
exec(f"self.rotation_{self.rotMode}+=self.rotspeed")
def input(self,key):
if key=="enter":
self.fastMode=1-self.fastMode
class Player(FirstPersonController):
def __init__(self):
global planets
super().__init__()
camera.fov=90
self.position=planets[3].position
self.gravity=0
self.vspeed=2
self.speed=600
self.mouse_sensitivity=Vec2(160,160)
self.on_enable()
def input(self,key):
if key=="escape":
if mouse.locked:
self.on_disable()
else:
sys.exit()
def _update(self):
if held_keys["left mouse"]:
self.on_enable()
if held_keys["left shift"]:
self.y-=self.vspeed
if held_keys["space"]:
self.y+=self.vspeed
def update():
global planets,player
for planet in planets:
planet.turn(planet.angle)
player._update()
sun_texture=load_texture("texture/Sun.png")
mercury_texture=load_texture("texture/Mercury.png")
venus_texture=load_texture("texture/Venus.png")
earth_texture=load_texture("texture/Earth.png")
mars_texture=load_texture("texture/Mars.png")
jupiter_texture=load_texture("texture/Jupiter.png")
saturn_texture=load_texture("texture/Saturn.png")
uranus_texture=load_texture("texture/Uranus.png")
neptune_texture=load_texture("texture/Neptune.png")
ps=["sun","mercury","venus","earth","mars","jupiter","saturn","uranus","neptune"]
cp=[200,15,35,42,20,160,145,90,80]
x,y,z=0,0,0
for i,p in enumerate(ps):
newPlanet=Planet(p,(x,y,z),cp[i])
planets.append(newPlanet)
x+=cp[i]*10
player=Player()
if __name__ == '__main__':
app.run()以上就是Python利用3D引擎做一個(gè)太陽系行星模擬器的詳細(xì)內(nèi)容,更多關(guān)于Python太陽系行星模擬器的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python隊(duì)列Queue實(shí)現(xiàn)詳解
這篇文章主要介紹了Python隊(duì)列Queue實(shí)現(xiàn)詳解,隊(duì)列是一種列表,隊(duì)列用于存儲按順序排列的數(shù)據(jù),隊(duì)列是一種先進(jìn)先出的數(shù)據(jù)結(jié)構(gòu),不同的是隊(duì)列只能在隊(duì)尾插入元素,在隊(duì)首刪除元素,需要的朋友可以參考下2023-07-07
python 密碼學(xué)示例——理解哈希(Hash)算法
這篇文章主要介紹了哈希(Hash)算法的相關(guān)資料,幫助大家更好的利用python處理密碼,感興趣的朋友可以了解下2020-09-09
python關(guān)于倒排列的知識點(diǎn)總結(jié)
在本篇文章里小編給大家分享的是一篇關(guān)于python關(guān)于倒排列的知識點(diǎn)總結(jié),有需要的朋友們可以參考下。2020-10-10
Kmeans均值聚類算法原理以及Python如何實(shí)現(xiàn)
這個(gè)算法中文名為k均值聚類算法,首先我們在二維的特殊條件下討論其實(shí)現(xiàn)的過程,方便大家理解。2020-09-09
在win64上使用bypy進(jìn)行百度網(wǎng)盤文件上傳功能
這篇文章主要介紹了在win64上使用bypy進(jìn)行百度網(wǎng)盤文件上傳功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01

