感謝分享原文-http://bjbsair.com/2020-04-03/tech-info/30000.htmlhtml
1.說明:python
1.1 推薦指數:★★★★★app
1.2 理由:上次matplotlib製做太陽-地球-月亮模擬你們很喜歡,此次比上次高級一些,加入3d星空運動,還能夠加入背景音樂,因此推薦指數5個星。dom
1.3 利用python的相關知識和pygame的動畫設計,逐步代碼分析,最後有簡潔的完整代碼。慢慢看,一看就會,通俗易懂。編輯器
1.4 推薦:python3(python3.8)、pygame和微軟vscode編輯器。函數
1.5 咱們生活在宇宙中,人類是很眇小,病毒纔是人類的共同敵人。好好熱愛生活,注意身體,愛護和珍惜身邊的人,爲祖國目前已經打敗疫情而驕傲,也祝世界其餘國家早日打敗疫情。最後繼續遵從終南山、李蘭娟、張文宏等教授的建議,作好防禦,防止輸入性疫情復發,人人有責。動畫
1.6 有點長,慢慢品讀,適合收藏和轉發,謝謝你們喜歡。設計
人類很眇小,宇宙很美3d
2.本次高級一點點的效果圖:code
3.代碼分析和註釋:
3.1 第1步:
#---第1步---導出模塊--- import pygame from pygame.locals import* from sys import exit from random import randint import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和標題設置 pygame.init() pygame.display.set_caption("太陽-地球-月亮模擬3d星空") #這個是打開的窗口的大小 width,height = 1800,1400 #設置屏幕分辨率 screen = pygame.display.set_mode((width,height),0,32) #屏幕掛起
3.3 第3步:
#---第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)
3.4 第4步:
#---第4步---星星類的初始化定義--- class Star(object): #類 def __init__(self,x,y,speed): #定義座標和速度 self.x = x self.y = y self.speed = speed
3.5 第5步:
#---第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)
===========================================
第6步:請注意,兩種方法,簡單遊戲不須要name和main
===========================================
3.6 第6步:
#---第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()
-
簡單動畫或遊戲,是不須要用if name == "main":,可是推薦使用,我爲了講解代碼的區別和分析採用去掉的,並且簡單動畫或遊戲不要緊,複雜一點的或者大型動畫或遊戲,是須要的。
-
簡潔的完整代碼以下:
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() ```感謝分享原文-http://bjbsair.com/2020-04-03/tech-info/30000.html 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星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/dfic-imagehandler/f8965599-1ab8-4666-a6b5-d1d5e3d7145f) 人類很眇小,宇宙很美 2.本次高級一點點的效果圖: ![python的pygame的3d星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/pgc-image/80ec146cc6754510ac6671d84ace3678) 3.代碼分析和註釋: 3.1 第1步:
#---第1步---導出模塊---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和標題設置
pygame.init()
pygame.display.set_caption("太陽-地球-月亮模擬3d星空")
#這個是打開的窗口的大小
width,height = 1800,1400 #設置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕掛起
3.3 第3步:
#---第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)
3.4 第4步:
#---第4步---星星類的初始化定義---
class Star(object): #類
def init(self,x,y,speed): #定義座標和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定義:sun,earth,moon---
class Ball():
#球的半徑,顏色和每次轉的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
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)
=========================================== 第6步:請注意,兩種方法,簡單遊戲不須要name和main =========================================== 3.6 第6步:
#---第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()
4. 簡單動畫或遊戲,是不須要用if __name__ == "__main__":,可是推薦使用,我爲了講解代碼的區別和分析採用去掉的,並且簡單動畫或遊戲不要緊,複雜一點的或者大型動畫或遊戲,是須要的。 5. 簡潔的完整代碼以下:
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((2r,2r))
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()
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星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/dfic-imagehandler/f8965599-1ab8-4666-a6b5-d1d5e3d7145f) 人類很眇小,宇宙很美 2.本次高級一點點的效果圖: ![python的pygame的3d星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/pgc-image/80ec146cc6754510ac6671d84ace3678) 3.代碼分析和註釋: 3.1 第1步:
#---第1步---導出模塊---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和標題設置
pygame.init()
pygame.display.set_caption("太陽-地球-月亮模擬3d星空")
#這個是打開的窗口的大小
width,height = 1800,1400 #設置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕掛起
3.3 第3步:
#---第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)
3.4 第4步:
#---第4步---星星類的初始化定義---
class Star(object): #類
def init(self,x,y,speed): #定義座標和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定義:sun,earth,moon---
class Ball():
#球的半徑,顏色和每次轉的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
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)
=========================================== 第6步:請注意,兩種方法,簡單遊戲不須要name和main =========================================== 3.6 第6步:
#---第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()
4. 簡單動畫或遊戲,是不須要用if __name__ == "__main__":,可是推薦使用,我爲了講解代碼的區別和分析採用去掉的,並且簡單動畫或遊戲不要緊,複雜一點的或者大型動畫或遊戲,是須要的。 5. 簡潔的完整代碼以下:
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((2r,2r))
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()
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星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/dfic-imagehandler/f8965599-1ab8-4666-a6b5-d1d5e3d7145f) 人類很眇小,宇宙很美 2.本次高級一點點的效果圖: ![python的pygame的3d星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/pgc-image/80ec146cc6754510ac6671d84ace3678) 3.代碼分析和註釋: 3.1 第1步:
#---第1步---導出模塊---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和標題設置
pygame.init()
pygame.display.set_caption("太陽-地球-月亮模擬3d星空")
#這個是打開的窗口的大小
width,height = 1800,1400 #設置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕掛起
3.3 第3步:
#---第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)
3.4 第4步:
#---第4步---星星類的初始化定義---
class Star(object): #類
def init(self,x,y,speed): #定義座標和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定義:sun,earth,moon---
class Ball():
#球的半徑,顏色和每次轉的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
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)
=========================================== 第6步:請注意,兩種方法,簡單遊戲不須要name和main =========================================== 3.6 第6步:
#---第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()
4. 簡單動畫或遊戲,是不須要用if __name__ == "__main__":,可是推薦使用,我爲了講解代碼的區別和分析採用去掉的,並且簡單動畫或遊戲不要緊,複雜一點的或者大型動畫或遊戲,是須要的。 5. 簡潔的完整代碼以下:
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((2r,2r))
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()
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星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/dfic-imagehandler/f8965599-1ab8-4666-a6b5-d1d5e3d7145f) 人類很眇小,宇宙很美 2.本次高級一點點的效果圖: ![python的pygame的3d星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/pgc-image/80ec146cc6754510ac6671d84ace3678) 3.代碼分析和註釋: 3.1 第1步:
#---第1步---導出模塊---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和標題設置
pygame.init()
pygame.display.set_caption("太陽-地球-月亮模擬3d星空")
#這個是打開的窗口的大小
width,height = 1800,1400 #設置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕掛起
3.3 第3步:
#---第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)
3.4 第4步:
#---第4步---星星類的初始化定義---
class Star(object): #類
def init(self,x,y,speed): #定義座標和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定義:sun,earth,moon---
class Ball():
#球的半徑,顏色和每次轉的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
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)
=========================================== 第6步:請注意,兩種方法,簡單遊戲不須要name和main =========================================== 3.6 第6步:
#---第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()
4. 簡單動畫或遊戲,是不須要用if __name__ == "__main__":,可是推薦使用,我爲了講解代碼的區別和分析採用去掉的,並且簡單動畫或遊戲不要緊,複雜一點的或者大型動畫或遊戲,是須要的。 5. 簡潔的完整代碼以下:
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((2r,2r))
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()
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星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/dfic-imagehandler/f8965599-1ab8-4666-a6b5-d1d5e3d7145f) 人類很眇小,宇宙很美 2.本次高級一點點的效果圖: ![python的pygame的3d星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/pgc-image/80ec146cc6754510ac6671d84ace3678) 3.代碼分析和註釋: 3.1 第1步:
#---第1步---導出模塊---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和標題設置
pygame.init()
pygame.display.set_caption("太陽-地球-月亮模擬3d星空")
#這個是打開的窗口的大小
width,height = 1800,1400 #設置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕掛起
3.3 第3步:
#---第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)
3.4 第4步:
#---第4步---星星類的初始化定義---
class Star(object): #類
def init(self,x,y,speed): #定義座標和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定義:sun,earth,moon---
class Ball():
#球的半徑,顏色和每次轉的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
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)
=========================================== 第6步:請注意,兩種方法,簡單遊戲不須要name和main =========================================== 3.6 第6步:
#---第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()
4. 簡單動畫或遊戲,是不須要用if __name__ == "__main__":,可是推薦使用,我爲了講解代碼的區別和分析採用去掉的,並且簡單動畫或遊戲不要緊,複雜一點的或者大型動畫或遊戲,是須要的。 5. 簡潔的完整代碼以下:
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((2r,2r))
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()
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星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/dfic-imagehandler/f8965599-1ab8-4666-a6b5-d1d5e3d7145f) 人類很眇小,宇宙很美 2.本次高級一點點的效果圖: ![python的pygame的3d星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/pgc-image/80ec146cc6754510ac6671d84ace3678) 3.代碼分析和註釋: 3.1 第1步:
#---第1步---導出模塊---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和標題設置
pygame.init()
pygame.display.set_caption("太陽-地球-月亮模擬3d星空")
#這個是打開的窗口的大小
width,height = 1800,1400 #設置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕掛起
3.3 第3步:
#---第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)
3.4 第4步:
#---第4步---星星類的初始化定義---
class Star(object): #類
def init(self,x,y,speed): #定義座標和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定義:sun,earth,moon---
class Ball():
#球的半徑,顏色和每次轉的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
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)
=========================================== 第6步:請注意,兩種方法,簡單遊戲不須要name和main =========================================== 3.6 第6步:
#---第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()
4. 簡單動畫或遊戲,是不須要用if __name__ == "__main__":,可是推薦使用,我爲了講解代碼的區別和分析採用去掉的,並且簡單動畫或遊戲不要緊,複雜一點的或者大型動畫或遊戲,是須要的。 5. 簡潔的完整代碼以下:
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((2r,2r))
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()
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星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/dfic-imagehandler/f8965599-1ab8-4666-a6b5-d1d5e3d7145f) 人類很眇小,宇宙很美 2.本次高級一點點的效果圖: ![python的pygame的3d星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/pgc-image/80ec146cc6754510ac6671d84ace3678) 3.代碼分析和註釋: 3.1 第1步:
#---第1步---導出模塊---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和標題設置
pygame.init()
pygame.display.set_caption("太陽-地球-月亮模擬3d星空")
#這個是打開的窗口的大小
width,height = 1800,1400 #設置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕掛起
3.3 第3步:
#---第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)
3.4 第4步:
#---第4步---星星類的初始化定義---
class Star(object): #類
def init(self,x,y,speed): #定義座標和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定義:sun,earth,moon---
class Ball():
#球的半徑,顏色和每次轉的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
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)
=========================================== 第6步:請注意,兩種方法,簡單遊戲不須要name和main =========================================== 3.6 第6步:
#---第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()
4. 簡單動畫或遊戲,是不須要用if __name__ == "__main__":,可是推薦使用,我爲了講解代碼的區別和分析採用去掉的,並且簡單動畫或遊戲不要緊,複雜一點的或者大型動畫或遊戲,是須要的。 5. 簡潔的完整代碼以下:
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((2r,2r))
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()
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星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/dfic-imagehandler/f8965599-1ab8-4666-a6b5-d1d5e3d7145f) 人類很眇小,宇宙很美 2.本次高級一點點的效果圖: ![python的pygame的3d星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/pgc-image/80ec146cc6754510ac6671d84ace3678) 3.代碼分析和註釋: 3.1 第1步:
#---第1步---導出模塊---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和標題設置
pygame.init()
pygame.display.set_caption("太陽-地球-月亮模擬3d星空")
#這個是打開的窗口的大小
width,height = 1800,1400 #設置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕掛起
3.3 第3步:
#---第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)
3.4 第4步:
#---第4步---星星類的初始化定義---
class Star(object): #類
def init(self,x,y,speed): #定義座標和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定義:sun,earth,moon---
class Ball():
#球的半徑,顏色和每次轉的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
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)
=========================================== 第6步:請注意,兩種方法,簡單遊戲不須要name和main =========================================== 3.6 第6步:
#---第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()
4. 簡單動畫或遊戲,是不須要用if __name__ == "__main__":,可是推薦使用,我爲了講解代碼的區別和分析採用去掉的,並且簡單動畫或遊戲不要緊,複雜一點的或者大型動畫或遊戲,是須要的。 5. 簡潔的完整代碼以下:
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((2r,2r))
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()
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星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/dfic-imagehandler/f8965599-1ab8-4666-a6b5-d1d5e3d7145f) 人類很眇小,宇宙很美 2.本次高級一點點的效果圖: ![python的pygame的3d星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/pgc-image/80ec146cc6754510ac6671d84ace3678) 3.代碼分析和註釋: 3.1 第1步:
#---第1步---導出模塊---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和標題設置
pygame.init()
pygame.display.set_caption("太陽-地球-月亮模擬3d星空")
#這個是打開的窗口的大小
width,height = 1800,1400 #設置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕掛起
3.3 第3步:
#---第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)
3.4 第4步:
#---第4步---星星類的初始化定義---
class Star(object): #類
def init(self,x,y,speed): #定義座標和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定義:sun,earth,moon---
class Ball():
#球的半徑,顏色和每次轉的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
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)
=========================================== 第6步:請注意,兩種方法,簡單遊戲不須要name和main =========================================== 3.6 第6步:
#---第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()
4. 簡單動畫或遊戲,是不須要用if __name__ == "__main__":,可是推薦使用,我爲了講解代碼的區別和分析採用去掉的,並且簡單動畫或遊戲不要緊,複雜一點的或者大型動畫或遊戲,是須要的。 5. 簡潔的完整代碼以下:
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((2r,2r))
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()
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星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/dfic-imagehandler/f8965599-1ab8-4666-a6b5-d1d5e3d7145f) 人類很眇小,宇宙很美 2.本次高級一點點的效果圖: ![python的pygame的3d星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/pgc-image/80ec146cc6754510ac6671d84ace3678) 3.代碼分析和註釋: 3.1 第1步:
#---第1步---導出模塊---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和標題設置
pygame.init()
pygame.display.set_caption("太陽-地球-月亮模擬3d星空")
#這個是打開的窗口的大小
width,height = 1800,1400 #設置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕掛起
3.3 第3步:
#---第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)
3.4 第4步:
#---第4步---星星類的初始化定義---
class Star(object): #類
def init(self,x,y,speed): #定義座標和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定義:sun,earth,moon---
class Ball():
#球的半徑,顏色和每次轉的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
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)
=========================================== 第6步:請注意,兩種方法,簡單遊戲不須要name和main =========================================== 3.6 第6步:
#---第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()
4. 簡單動畫或遊戲,是不須要用if __name__ == "__main__":,可是推薦使用,我爲了講解代碼的區別和分析採用去掉的,並且簡單動畫或遊戲不要緊,複雜一點的或者大型動畫或遊戲,是須要的。 5. 簡潔的完整代碼以下:
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((2r,2r))
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()
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星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/dfic-imagehandler/f8965599-1ab8-4666-a6b5-d1d5e3d7145f) 人類很眇小,宇宙很美 2.本次高級一點點的效果圖: ![python的pygame的3d星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/pgc-image/80ec146cc6754510ac6671d84ace3678) 3.代碼分析和註釋: 3.1 第1步:
#---第1步---導出模塊---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和標題設置
pygame.init()
pygame.display.set_caption("太陽-地球-月亮模擬3d星空")
#這個是打開的窗口的大小
width,height = 1800,1400 #設置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕掛起
3.3 第3步:
#---第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)
3.4 第4步:
#---第4步---星星類的初始化定義---
class Star(object): #類
def init(self,x,y,speed): #定義座標和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定義:sun,earth,moon---
class Ball():
#球的半徑,顏色和每次轉的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
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)
=========================================== 第6步:請注意,兩種方法,簡單遊戲不須要name和main =========================================== 3.6 第6步:
#---第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()
4. 簡單動畫或遊戲,是不須要用if __name__ == "__main__":,可是推薦使用,我爲了講解代碼的區別和分析採用去掉的,並且簡單動畫或遊戲不要緊,複雜一點的或者大型動畫或遊戲,是須要的。 5. 簡潔的完整代碼以下:
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((2r,2r))
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()
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星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/dfic-imagehandler/f8965599-1ab8-4666-a6b5-d1d5e3d7145f) 人類很眇小,宇宙很美 2.本次高級一點點的效果圖: ![python的pygame的3d星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/pgc-image/80ec146cc6754510ac6671d84ace3678) 3.代碼分析和註釋: 3.1 第1步:
#---第1步---導出模塊---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和標題設置
pygame.init()
pygame.display.set_caption("太陽-地球-月亮模擬3d星空")
#這個是打開的窗口的大小
width,height = 1800,1400 #設置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕掛起
3.3 第3步:
#---第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)
3.4 第4步:
#---第4步---星星類的初始化定義---
class Star(object): #類
def init(self,x,y,speed): #定義座標和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定義:sun,earth,moon---
class Ball():
#球的半徑,顏色和每次轉的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
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)
=========================================== 第6步:請注意,兩種方法,簡單遊戲不須要name和main =========================================== 3.6 第6步:
#---第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()
4. 簡單動畫或遊戲,是不須要用if __name__ == "__main__":,可是推薦使用,我爲了講解代碼的區別和分析採用去掉的,並且簡單動畫或遊戲不要緊,複雜一點的或者大型動畫或遊戲,是須要的。 5. 簡潔的完整代碼以下:
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((2r,2r))
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()
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星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/dfic-imagehandler/f8965599-1ab8-4666-a6b5-d1d5e3d7145f) 人類很眇小,宇宙很美 2.本次高級一點點的效果圖: ![python的pygame的3d星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/pgc-image/80ec146cc6754510ac6671d84ace3678) 3.代碼分析和註釋: 3.1 第1步:
#---第1步---導出模塊---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和標題設置
pygame.init()
pygame.display.set_caption("太陽-地球-月亮模擬3d星空")
#這個是打開的窗口的大小
width,height = 1800,1400 #設置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕掛起
3.3 第3步:
#---第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)
3.4 第4步:
#---第4步---星星類的初始化定義---
class Star(object): #類
def init(self,x,y,speed): #定義座標和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定義:sun,earth,moon---
class Ball():
#球的半徑,顏色和每次轉的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
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)
=========================================== 第6步:請注意,兩種方法,簡單遊戲不須要name和main =========================================== 3.6 第6步:
#---第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()
4. 簡單動畫或遊戲,是不須要用if __name__ == "__main__":,可是推薦使用,我爲了講解代碼的區別和分析採用去掉的,並且簡單動畫或遊戲不要緊,複雜一點的或者大型動畫或遊戲,是須要的。 5. 簡潔的完整代碼以下:
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((2r,2r))
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()
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星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/dfic-imagehandler/f8965599-1ab8-4666-a6b5-d1d5e3d7145f) 人類很眇小,宇宙很美 2.本次高級一點點的效果圖: ![python的pygame的3d星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/pgc-image/80ec146cc6754510ac6671d84ace3678) 3.代碼分析和註釋: 3.1 第1步:
#---第1步---導出模塊---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和標題設置
pygame.init()
pygame.display.set_caption("太陽-地球-月亮模擬3d星空")
#這個是打開的窗口的大小
width,height = 1800,1400 #設置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕掛起
3.3 第3步:
#---第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)
3.4 第4步:
#---第4步---星星類的初始化定義---
class Star(object): #類
def init(self,x,y,speed): #定義座標和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定義:sun,earth,moon---
class Ball():
#球的半徑,顏色和每次轉的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
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)
=========================================== 第6步:請注意,兩種方法,簡單遊戲不須要name和main =========================================== 3.6 第6步:
#---第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()
4. 簡單動畫或遊戲,是不須要用if __name__ == "__main__":,可是推薦使用,我爲了講解代碼的區別和分析採用去掉的,並且簡單動畫或遊戲不要緊,複雜一點的或者大型動畫或遊戲,是須要的。 5. 簡潔的完整代碼以下:
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((2r,2r))
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()
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星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/dfic-imagehandler/f8965599-1ab8-4666-a6b5-d1d5e3d7145f) 人類很眇小,宇宙很美 2.本次高級一點點的效果圖: ![python的pygame的3d星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/pgc-image/80ec146cc6754510ac6671d84ace3678) 3.代碼分析和註釋: 3.1 第1步:
#---第1步---導出模塊---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和標題設置
pygame.init()
pygame.display.set_caption("太陽-地球-月亮模擬3d星空")
#這個是打開的窗口的大小
width,height = 1800,1400 #設置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕掛起
3.3 第3步:
#---第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)
3.4 第4步:
#---第4步---星星類的初始化定義---
class Star(object): #類
def init(self,x,y,speed): #定義座標和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定義:sun,earth,moon---
class Ball():
#球的半徑,顏色和每次轉的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
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)
=========================================== 第6步:請注意,兩種方法,簡單遊戲不須要name和main =========================================== 3.6 第6步:
#---第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()
4. 簡單動畫或遊戲,是不須要用if __name__ == "__main__":,可是推薦使用,我爲了講解代碼的區別和分析採用去掉的,並且簡單動畫或遊戲不要緊,複雜一點的或者大型動畫或遊戲,是須要的。 5. 簡潔的完整代碼以下:
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((2r,2r))
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()
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星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/dfic-imagehandler/f8965599-1ab8-4666-a6b5-d1d5e3d7145f) 人類很眇小,宇宙很美 2.本次高級一點點的效果圖: ![python的pygame的3d星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/pgc-image/80ec146cc6754510ac6671d84ace3678) 3.代碼分析和註釋: 3.1 第1步:
#---第1步---導出模塊---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和標題設置
pygame.init()
pygame.display.set_caption("太陽-地球-月亮模擬3d星空")
#這個是打開的窗口的大小
width,height = 1800,1400 #設置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕掛起
3.3 第3步:
#---第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)
3.4 第4步:
#---第4步---星星類的初始化定義---
class Star(object): #類
def init(self,x,y,speed): #定義座標和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定義:sun,earth,moon---
class Ball():
#球的半徑,顏色和每次轉的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
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)
=========================================== 第6步:請注意,兩種方法,簡單遊戲不須要name和main =========================================== 3.6 第6步:
#---第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()
4. 簡單動畫或遊戲,是不須要用if __name__ == "__main__":,可是推薦使用,我爲了講解代碼的區別和分析採用去掉的,並且簡單動畫或遊戲不要緊,複雜一點的或者大型動畫或遊戲,是須要的。 5. 簡潔的完整代碼以下:
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((2r,2r))
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()
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星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/dfic-imagehandler/f8965599-1ab8-4666-a6b5-d1d5e3d7145f) 人類很眇小,宇宙很美 2.本次高級一點點的效果圖: ![python的pygame的3d星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/pgc-image/80ec146cc6754510ac6671d84ace3678) 3.代碼分析和註釋: 3.1 第1步:
#---第1步---導出模塊---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和標題設置
pygame.init()
pygame.display.set_caption("太陽-地球-月亮模擬3d星空")
#這個是打開的窗口的大小
width,height = 1800,1400 #設置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕掛起
3.3 第3步:
#---第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)
3.4 第4步:
#---第4步---星星類的初始化定義---
class Star(object): #類
def init(self,x,y,speed): #定義座標和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定義:sun,earth,moon---
class Ball():
#球的半徑,顏色和每次轉的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
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)
=========================================== 第6步:請注意,兩種方法,簡單遊戲不須要name和main =========================================== 3.6 第6步:
#---第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()
4. 簡單動畫或遊戲,是不須要用if __name__ == "__main__":,可是推薦使用,我爲了講解代碼的區別和分析採用去掉的,並且簡單動畫或遊戲不要緊,複雜一點的或者大型動畫或遊戲,是須要的。 5. 簡潔的完整代碼以下:
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((2r,2r))
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()
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星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/dfic-imagehandler/f8965599-1ab8-4666-a6b5-d1d5e3d7145f) 人類很眇小,宇宙很美 2.本次高級一點點的效果圖: ![python的pygame的3d星空和太陽-地球-月亮的模擬運動代碼即分析](http://p1.pstatp.com/large/pgc-image/80ec146cc6754510ac6671d84ace3678) 3.代碼分析和註釋: 3.1 第1步:
#---第1步---導出模塊---
import pygame
from pygame.locals import*
from sys import exit
from random import randint
import math
3.2 第2步:
#---第2步---pygame初始化,窗口大小和標題設置
pygame.init()
pygame.display.set_caption("太陽-地球-月亮模擬3d星空")
#這個是打開的窗口的大小
width,height = 1800,1400 #設置屏幕分辨率
screen = pygame.display.set_mode((width,height),0,32) #屏幕掛起
3.3 第3步:
#---第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)
3.4 第4步:
#---第4步---星星類的初始化定義---
class Star(object): #類
def init(self,x,y,speed): #定義座標和速度
self.x = x
self.y = y
self.speed = speed
3.5 第5步:
#---第5步---三大星球定義:sun,earth,moon---
class Ball():
#球的半徑,顏色和每次轉的角速度
def init(self,r,color,speed):
self.image = pygame.Surface((2r,2r))
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)
=========================================== 第6步:請注意,兩種方法,簡單遊戲不須要name和main =========================================== 3.6 第6步:
#---第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()
4. 簡單動畫或遊戲,是不須要用if __name__ == "__main__":,可是推薦使用,我爲了講解代碼的區別和分析採用去掉的,並且簡單動畫或遊戲不要緊,複雜一點的或者大型動畫或遊戲,是須要的。 5. 簡潔的完整代碼以下:
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((2r,2r))
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()