python的pygame的3d星空和太陽-地球-月亮的模擬運動代碼即分析

1.說明:

1.1 推薦指數:★★★★★

1.2 理由:上次matplotlib製作太陽-地球-月亮模擬大家很喜歡,這次比上次高級一些,加入3d星空運動,還可以加入背景音樂,所以推薦指數5個星。

1.3 利用python的相關知識和pygame的動畫設計,逐步代碼分析,最後有簡潔的完整代碼。慢慢看,一看就會,通俗易懂。

1.4 推薦:python3(python3.8)、pygame和微軟vscode編輯器。

1.5 我們生活在宇宙中,人類是很渺小,病毒才是人類的共同敵人。好好熱愛生活,注意身體,愛護和珍惜身邊的人,為祖國目前已經戰勝疫情而驕傲,也祝世界其他國家早日戰勝疫情。最後繼續聽從終南山、李蘭娟、張文宏等教授的建議,做好防護,防止輸入性疫情復發,人人有責。

1.6 有點長,慢慢品讀,適合收藏和轉發,謝謝大家喜歡。


python的pygame的3d星空和太陽-地球-月亮的模擬運動代碼即分析

人類很渺小,宇宙很美

2.本次高級一點點的效果圖:


python的pygame的3d星空和太陽-地球-月亮的模擬運動代碼即分析

3.代碼分析和註釋:

3.1 第1步:

<code>#---第1步---導出模塊---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math/<code>

3.2 第2步:

<code>#---第2步---pygame初始化,窗口大小和標題設置
pygame.init()
pygame.display.set_caption("太陽-地球-月亮模擬3d星空")
#這個是打開的窗口的大小
width,height = 1800,1400 #設置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕掛起/<code>

3.3 第3步:

<code>#---第3步---相關定義---
#顏色定義,顏色可以的單獨定義,在pygame中顏色定義可以使如下
#列表法、元組法、直接法,還有引入rgb法,就是不能十六進制法
white = (255,255,255)
black=0,0,0
planse=[65,105,225]

#定義x和y的屏幕座標,用於地球和月球的位置設置
screenCenterx = width//2 -1
screenCentery = height//2 -1

#設置背景音效---可要可不要,自己弄一個音樂,放在根目錄下或者指定位置引出即可
pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)/<code>

3.4 第4步:

<code>#---第4步---星星類的初始化定義---
class Star(object): #類
def __init__(self,x,y,speed): #定義座標和速度
self.x = x
self.y = y
self.speed = speed/<code>

3.5 第5步:

<code>#---第5步---三大星球定義:sun,earth,moon---
class Ball():
#球的半徑,顏色和每次轉的角速度
def __init__(self,r,color,speed):
self.image = pygame.Surface((2*r,2*r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#繞著x,y點以radius為半徑旋轉
def move(self,x,y,radius):
#乘以多少1~5,那麼地球離太陽的距離就遠些,相應的月球離地球也遠些,3比較合適
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定義畫圖函數
def draw(self):
screen.blit(self.image,self.rect)/<code>

===========================================

第6步:請注意,兩種方法,簡單遊戲不需要name和main

===========================================

3.6 第6步:

<code>#---第6步---運行定義
#def run(): #這是註釋掉def run的代碼,整體向左移動一格
#地球和月亮的球顏色和大小的設置
#65,105,225=品藍色;0,0,255=藍色
#50是地球大小,10是月球大小
#1和5是對應的速度
#earth = Ball(50,(65,105,225),1) #兩種顏色法,故意註釋掉
earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)

#畫星星,隨機按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一幀畫上一些星星
#x和y不但是座標,還是畫布大小的設置
#初始化生成的星星個數2000,可自己調節
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))

clock = pygame.time.Clock() #Clock對象

#while循環
while True:
#退出設置,建議固定
for event in pygame.event.get():
if event.type == QUIT:

#return
exit()

#在循環啟動後仍能隨機生成小星星,不要的畫也可以,就是不能繼續生成從右到左的後續小星星
y = float(randint(0,height))
x = float(randint(0,width))
speed = float(randint(10,300))
star = Star(x,y,speed)
stars.append(star)

time_passed = clock.tick()
time_passed_seconds = time_passed/1000 #單位毫秒,需轉換

#屏幕背景顏色
#screen.fill((0,0,0))
screen.fill(black)

for star in stars: #繪製所有星星
new_x = star.x - time_passed_seconds*star.speed
#畫星星
pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y))
star.x = new_x

#畫個太陽在中間,255,125,64=肉色,大小100
pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100)
#150是地球與太陽的距離,也是半徑
earth.move(screenCenterx,screenCentery,150)
earth.draw()
#50是月球與地球的距離,也是半徑
moon.move(earth.rect.centerx,earth.rect.centery,50)
moon.draw()

#pygame.display.flip() #可要可不要,由於雙緩衝的原因,需要將整個display的surface對象更新到屏幕上去
#刷新速度,60~600,數值越大刷新越快,速度也越快
clock.tick(60)

#屏幕更新,必須要,否則無畫面
pygame.display.update()

#---單文件可要可不要,但是也是有道理的,我以前講過
# 如果不要,註釋掉,那麼def run就可以刪除,其下面的代碼塊整體向左相應的定格試試
#這是註釋掉def run的代碼
#if __name__ == "__main__":
#run()
/<code>
  1. 簡單動畫或遊戲,是不需要用if __name__ == "__main__":,但是推薦使用,我為了講解代碼的區別和分析採用去掉的,而且簡單動畫或遊戲沒關係,複雜一點的或者大型動畫或遊戲,是需要的。
  1. 簡潔的完整代碼如下:
<code>
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math

pygame.init()
pygame.display.set_caption("太陽-地球-月亮模擬3d星空")
width,height = 1800,1400 #設置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕掛起

white = (255,255,255)
black=0,0,0

planse=[65,105,225]

screenCenterx = width//2 -1
screenCentery = height//2 -1

pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play(-1,0.0)

class Star(object): #類
def __init__(self,x,y,speed): #定義座標和速度
self.x = x
self.y = y
self.speed = speed

class Ball():
#球的半徑,顏色和每次轉的角速度
def __init__(self,r,color,speed):
self.image = pygame.Surface((2*r,2*r))
pygame.draw.circle(self.image,color,(r,r),r)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.speed= speed
self.angle = 0
#繞著x,y點以radius為半徑旋轉
def move(self,x,y,radius):
#乘以多少1~5,那麼地球離太陽的距離就遠些,相應的月球離地球也遠些,3比較合適
self.rect.centerx = int(x + radius * math.cos(math.radians(self.angle))*3)
self.rect.centery = int(y + radius * math.sin(math.radians(self.angle))*3)
self.angle = self.angle + self.speed
#定義畫圖函數
def draw(self):
screen.blit(self.image,self.rect)

earth = Ball(50,planse,1)
moon = Ball(10,(255,255,200),5)

#畫星星,隨機按x和y的大小生成,存入stars的列表中
stars=[]
for n in range(2000): #在第一幀畫上一些星星
#x和y不但是座標,還是畫布大小的設置

#初始化生成的星星個數2000,可自己調節
x = float(randint(0,width))
y = float(randint(0,height))
speed = float(randint(10,300))
stars.append(Star(x,y,speed))

clock = pygame.time.Clock() #Clock對象

#while循環
while True:
#退出設置,建議固定
for event in pygame.event.get():
if event.type == QUIT:
#return
exit()

#在循環啟動後仍能隨機生成小星星,不要的畫也可以,就是不能繼續生成從右到左的後續小星星
y = float(randint(0,height))
x = float(randint(0,width))
speed = float(randint(10,300))
star = Star(x,y,speed)
stars.append(star)

time_passed = clock.tick()
time_passed_seconds = time_passed/1000 #單位毫秒,需轉換

#屏幕背景顏色
#screen.fill((0,0,0))
screen.fill(black)

for star in stars: #繪製所有星星
new_x = star.x - time_passed_seconds*star.speed
#畫星星
pygame.draw.aaline(screen,white,(new_x,star.y),(star.x+1,star.y))
star.x = new_x

#畫個太陽在中間,255,125,64=肉色,大小100
pygame.draw.circle(screen,(255,125,64),(screenCenterx,screenCentery),100)
#150是地球與太陽的距離,也是半徑

earth.move(screenCenterx,screenCentery,150)
earth.draw()
#50是月球與地球的距離,也是半徑
moon.move(earth.rect.centerx,earth.rect.centery,50)
moon.draw()

clock.tick(60)
#屏幕更新,必須要,否則無畫面
pygame.display.update()
/<code>


分享到:


相關文章: