又到每日分享Python小技巧的時候了,我真是太開心了。今天給大家分享的是模擬太陽系行星運轉。聽起來就很不錯,效果出來的時候也是很不錯的,讓我們一起期待。 1、準備材料 首先我們需要準備這樣一些材料 宇宙背景圖 背景透明的行星圖 2:編寫代碼 代碼分塊詳解 導入需要的模塊 ###Python學習交流 ...
又到每日分享Python小技巧的時候了,我真是太開心了。今天給大家分享的是模擬太陽系行星運轉。聽起來就很不錯,效果出來的時候也是很不錯的,讓我們一起期待。
1、準備材料
首先我們需要準備這樣一些材料
宇宙背景圖
背景透明的行星圖
2:編寫代碼
代碼分塊詳解
導入需要的模塊
###Python學習交流Q群:906715085### import pygame import sys import math from pygame.locals import *
定義視窗大小、標題名稱、字體設置、創建時鐘
size = width, height = 1206, 780 screen = pygame.display.set_mode(size) pygame.display.set_caption(太陽系行星運轉示意圖) myfont = pygame.font.Font(None,60) clock = pygame.time.Clock()
宇宙背景初始化
background = pygame.image.load(r 這裡填背景圖片本地路徑 )
screen.blit(background, (0, 0))
右側文字及星球顯示
###Python學習交流Q群:906715085## textImage = myfont.render(Solar System, True, (255, 255, 0)) # 太陽系 screen.blit(textImage, (100, 100)) my_font = pygame.font.SysFont(arial, 15) text_surface = my_font.render(Sun, True, (255, 0, 0), (0, 0, 0)) # 太陽 screen.blit(text_surface, (1020, 30)) sun = pygame.image.load(rF:\solar-system\image\sun_bg.png) screen.blit(pygame.transform.scale(sun, (27, 27)), (1090, 25)) my_font = pygame.font.SysFont(arial, 15) text_surface = my_font.render(Mercury, True, (255, 0, 0), (0, 0, 0)) # 水星 screen.blit(text_surface, (1020, 70)) my_font = pygame.font.SysFont(arial, 15) Mercury = pygame.image.load(rF:\solar-system\image\mercury_bg.png) screen.blit(pygame.transform.scale(Mercury, (27, 27)), (1090, 65)) text_surface = my_font.render(Venus, True, (255, 0, 0), (0, 0, 0)) # 金星 screen.blit(text_surface, (1020, 110)) my_font = pygame.font.SysFont(arial, 15) spark = pygame.image.load(rF:\solar-system\image\spark_bg.png) screen.blit(pygame.transform.scale(spark, (27, 27)), (1090, 105)) text_surface = my_font.render(Earth, True, (255, 0, 0), (0, 0, 0)) # 地球 screen.blit(text_surface, (1020, 150)) my_font = pygame.font.SysFont(arial, 15) earth = pygame.image.load(rF:\solar-system\image\earth_min_bg.png) screen.blit(pygame.transform.scale(earth, (27, 27)), (1090, 145)) text_surface = my_font.render(Moon, True, (255, 0, 0), (0, 0, 0)) # 月球 screen.blit(text_surface, (1020, 190)) my_font = pygame.font.SysFont(arial, 15) moon = pygame.image.load(rF:\solar-system\image\mercury_bg.png) screen.blit(pygame.transform.scale(moon, (27, 27)), (1090, 185)) text_surface = my_font.render(Mars, True, (255, 0, 0), (0, 0, 0)) # 火星 screen.blit(text_surface, (1020, 230)) Mars = pygame.image.load(rF:\solar-system\image\venus_bg.png) screen.blit(pygame.transform.scale(Mars, (27, 27)), (1090, 225)) my_font = pygame.font.SysFont(arial, 15) text_surface = my_font.render(Jupiter, True, (255, 0, 0), (0, 0, 0)) # 木星 screen.blit(text_surface, (1020, 270)) Jupiter = pygame.image.load(rF:\solar-system\image\jupiter_min_bg.png) screen.blit(pygame.transform.scale(Jupiter, (27, 27)), (1090, 265)) my_font = pygame.font.SysFont(arial, 15) text_surface = my_font.render(Saturn, True, (255, 0, 0), (0, 0, 0)) # 土星 screen.blit(text_surface, (1020, 300)) Saturn = pygame.image.load(rF:\solar-system\image\saturn_bg.png) screen.blit(pygame.transform.scale(Saturn, (30, 30)), (1090, 305)) my_font = pygame.font.SysFont(arial, 15) text_surface = my_font.render(Uranus, True, (255, 0, 0), (0, 0, 0)) # 天王星 screen.blit(text_surface, (1020, 340)) Uranus = pygame.image.load(rF:\solar-system\image\uranus_bg.png) screen.blit(pygame.transform.scale(Uranus, (27, 27)), (1090, 345)) my_font = pygame.font.SysFont(arial, 15) text_surface = my_font.render(Neptune, True, (255, 0, 0), (0, 0, 0)) # 海王星 screen.blit(text_surface, (1020, 380)) Neptune = pygame.image.load(rF:\solar-system\image\neptune_bg.png) screen.blit(pygame.transform.scale(Neptune, (27, 27)), (1090, 385))
設定太陽初始位置
sun = pygame.image.load(rF:/solar-system/image/sun_bg.png)
screen.blit(pygame.transform.scale(sun, (170, 170)), (527,307))
實現各個星球繞著太陽轉
# 水星 roll_3 += 0.077 # 每幀公轉pi pos_3_x = int(size[0] // 2 + size[1] // 8 * math.sin(roll_3)) pos_3_y = int(size[1] // 2 + size[1] // 8 * math.cos(roll_3)) mercury = pygame.image.load(rF:\solar-system\image\mercury_bg.png) screen.blit(pygame.transform.scale(mercury, (8, 8)), (pos_3_x, pos_3_y))
#金星
roll_2 += 0.069 # 每幀公轉pi pos_2_x = int(size[0] // 2 + size[1] // 7 * math.sin(roll_2)) pos_2_y = int(size[1] // 2 + size[1] // 7 * math.cos(roll_2)) venus = pygame.image.load(rF:\solar-system\image\venus_bg.png) screen.blit(pygame.transform.scale(venus, (10, 10)), (pos_2_x, pos_2_y))
# 地球
roll_e += 0.060 # 每幀公轉pi pos_e_x = int(size[0] // 2 + size[1] // 6 * math.sin(roll_e)) pos_e_y = int(size[1] // 2 + size[1] // 6 * math.cos(roll_e)) earth = pygame.image.load(rF:\solar-system\image\earth_min_bg.png) screen.blit(pygame.transform.scale(earth, (15, 15)), (pos_e_x, pos_e_y))
# 火星
roll_4 += 0.053 # 每幀公轉pi pos_4_x = int(size[0] // 2 + size[1] // 5 * math.sin(roll_4)) pos_4_y = int(size[1] // 2 + size[1] // 5 * math.cos(roll_4)) venus = pygame.image.load(rF:\solar-system\image\venus_bg.png) screen.blit(pygame.transform.scale(venus, (13, 13)), (pos_4_x, pos_4_y))
# 木星
roll_5 += 0.045 # 每幀公轉pi pos_5_x = int(size[0] // 2 + size[1] // 4 * math.sin(roll_5)) pos_5_y = int(size[1] // 2 + size[1] // 4 * math.cos(roll_5)) mouth = pygame.image.load(rF:\solar-system\image\jupiter_bg.png) screen.blit(pygame.transform.scale(mouth, (70, 70)), (pos_5_x, pos_5_y))
# 土星
roll_6 += 0.037 # 每幀公轉pi pos_6_x = int(size[0] // 2 + size[1] // 3.5 * math.sin(roll_6)) pos_6_y = int(size[1] // 2 + size[1] // 3.5 * math.cos(roll_6)) saturn = pygame.image.load(rF:\solar-system\image\saturn_bg.png) screen.blit(pygame.transform.scale(saturn, (50, 50)), (pos_6_x, pos_6_y))
# 天王星
roll_7 += 0.031 # 每幀公轉pi pos_7_x = int(size[0] // 2 + size[1] // 2.7 * math.sin(roll_7)) pos_7_y = int(size[1] // 2 + size[1] // 2.7 * math.cos(roll_7)) uranus = pygame.image.load(rF:\solar-system\image\uranus_bg.png) screen.blit(pygame.transform.scale(uranus, (45, 45)), (pos_7_x, pos_7_y))
# 海王星
roll_8 += 0.025 # 每幀公轉pi pos_8_x = int(size[0] // 2 + size[1] // 2 * math.sin(roll_8)) pos_8_y = int(size[1] // 2 + size[1] // 2 * math.cos(roll_8)) neptune = pygame.image.load(rF:\solar-system\image\neptune_bg.png) screen.blit(pygame.transform.scale(neptune, (37, 37)), (pos_8_x, pos_8_y))
實現月亮繞著地球轉
roll_m += 0.2 # 每幀公轉pi pos_m_x = int(pos_e_x + size[1] // 50 * math.sin(roll_m)) pos_m_y = int(pos_e_y + size[1] // 50 * math.cos(roll_m)) mouth = pygame.image.load(rF:\solar-system\image\mercury_bg.png) screen.blit(pygame.transform.scale(mouth, (6, 6)), (pos_m_x, pos_m_y))
如果你很有情調的話,還可以在迴圈體之前加一段代碼,配曲music
註:此處音樂力求達到巨集偉效果,因此選的是 Victory.mp3
#添加背景音樂 pygame.mixer.music.load('F:/music.mp3') pygame.mixer.music.play(-1, 40) pygame.mixer.music.set_volume(0.5)
總觀整體代碼
#導入模塊 import pygame import sys import math from pygame.locals import * #初始化 pygame.init()
#定義視窗大小、標題名稱、字體設置、創建時鐘(可以控制游戲迴圈頻率)等
size = width, height = 1206, 780 screen = pygame.display.set_mode(size) pygame.display.set_caption(太陽系行星運轉示意圖) myfont = pygame.font.Font(None,60) clock = pygame.time.Clock()
#定義三個空列表 pos_e = pos_mm = [] #地球和月球等其他行星的公轉過的角度 roll_e = roll_m = 0 roll_2 = roll_3 = roll_4 = roll_5 = roll_6 = roll_7 = roll_8 = 0 #添加背景音樂 pygame.mixer.music.load('F:/music.mp3') pygame.mixer.music.play(-1, 40) pygame.mixer.music.set_volume(0.5) #迴圈,達到萬事萬物永不停息的目的 while True: for event in pygame.event.get(): if event.type == QUIT: sys.exit()
# 宇宙
background = pygame.image.load(rE:/3.PNG)
screen.blit(background, (0, 0))
# 顯示文字及星球
textImage = myfont.render(Solar System, True, (255, 255, 0)) # 太陽系 screen.blit(textImage, (100, 100)) my_font = pygame.font.SysFont(arial, 15) text_surface = my_font.render(Sun, True, (255, 0, 0), (0, 0, 0)) # 太陽 screen.blit(text_surface, (1020, 30)) sun = pygame.image.load(rF:\solar-system\image\sun_bg.png) screen.blit(pygame.transform.scale(sun, (27, 27)), (1090, 25)) my_font = pygame.font.SysFont(arial, 15) text_surface = my_font.render(Mercury, True, (255, 0, 0), (0, 0, 0)) # 水星 screen.blit(text_surface, (1020, 70)) my_font = pygame.font.SysFont(arial, 15) Mercury = pygame.image.load(rF:\solar-system\image\mercury_bg.png) screen.blit(pygame.transform.scale(Mercury, (27, 27)), (1090, 65)) text_surface = my_font.render(Venus, True, (255, 0, 0), (0, 0, 0)) # 金星 screen.blit(text_surface, (1020, 110)) my_font = pygame.font.SysFont(arial, 15) spark = pygame.image.load(rF:\solar-system\image\spark_bg.png) screen.blit(pygame.transform.scale(spark, (27, 27)), (1090, 105)) text_surface = my_font.render(Earth, True, (255, 0, 0), (0, 0, 0)) # 地球 screen.blit(text_surface, (1020, 150)) my_font = pygame.font.SysFont(arial, 15) earth = pygame.image.load(rF:\solar-system\image\earth_min_bg.png) screen.blit(pygame.transform.scale(earth, (27, 27)), (1090, 145)) text_surface = my_font.render(Moon, True, (255, 0, 0), (0, 0, 0)) # 月球 screen.blit(text_surface, (1020, 190)) my_font = pygame.font.SysFont(arial, 15) moon = pygame.image.load(rF:\solar-system\image\mercury_bg.png) screen.blit(pygame.transform.scale(moon, (27, 27)), (1090, 185)) text_surface = my_font.render(Mars, True, (255, 0, 0), (0, 0, 0)) # 火星 screen.blit(text_surface, (1020, 230)) Mars = pygame.image.load(rF:\solar-system\image\venus_bg.png) screen.blit(pygame.transform.scale(Mars, (27, 27)), (1090, 225)) my_font = pygame.font.SysFont(arial, 15) text_surface = my_font.render(Jupiter, True, (255, 0, 0), (0, 0, 0)) # 木星 screen.blit(text_surface, (1020, 270)) Jupiter = pygame.image.load(rF:\solar-system\image\jupiter_min_bg.png) screen.blit(pygame.transform.scale(Jupiter, (27, 27)), (1090, 265)) my_font = pygame.font.SysFont(arial, 15) text_surface = my_font.render(Saturn, True, (255, 0, 0), (0, 0, 0)) # 土星 screen.blit(text_surface, (1020, 300)) Saturn = pygame.image.load(rF:\solar-system\image\saturn_bg.png) screen.blit(pygame.transform.scale(Saturn, (30, 30)), (1090, 305)) my_font = pygame.font.SysFont(arial, 15) text_surface = my_font.render(Uranus, True, (255, 0, 0), (0, 0, 0)) # 天王星 screen.blit(text_surface, (1020, 340)) Uranus = pygame.image.load(rF:\solar-system\image\uranus_bg.png) screen.blit(pygame.transform.scale(Uranus, (27, 27)), (1090, 345)) my_font = pygame.font.SysFont(arial, 15) text_surface = my_font.render(Neptune, True, (255, 0, 0), (0, 0, 0)) # 海王星 screen.blit(text_surface, (1020, 380)) Neptune = pygame.image.load(rF:\solar-system\image\neptune_bg.png) screen.blit(pygame.transform.scale(Neptune, (27, 27)), (1090, 385)) # 太陽 sun = pygame.image.load(rF:/solar-system/image/sun_bg.png) screen.blit(pygame.transform.scale(sun, (170, 170)), (527,307)) # 水星 roll_3 += 0.077 # 每幀公轉pi pos_3_x = int(size[0] // 2 + size[1] // 8 * math.sin(roll_3)) pos_3_y = int(size[1] // 2 + size[1] // 8 * math.cos(roll_3)) mercury = pygame.image.load(rF:\solar-system\image\mercury_bg.png) screen.blit(pygame.transform.scale(mercury, (8, 8)), (pos_3_x, pos_3_y)) # 金星 roll_2 += 0.069 # 每幀公轉pi pos_2_x = int(size[0] // 2 + size[1] // 7 * math.sin(roll_2)) pos_2_y = int(size[1] // 2 + size[1] // 7 * math.cos(roll_2)) venus = pygame.image.load(rF:\solar-system\image\venus_bg.png) screen.blit(pygame.transform.scale(venus, (10, 10)), (pos_2_x, pos_2_y)) # 地球 roll_e += 0.060 # 每幀公轉pi pos_e_x = int(size[0] // 2 + size[1] // 6 * math.sin(roll_e)) pos_e_y = int(size[1] // 2 + size[1] // 6 * math.cos(roll_e)) earth = pygame.image.load(rF:\solar-system\image\earth_min_bg.png) screen.blit(pygame.transform.scale(earth, (15, 15)), (pos_e_x, pos_e_y)) # 月球 roll_m += 0.2 # 每幀公轉pi pos_m_x = int(pos_e_x + size[1] // 50 * math.sin(roll_m)) pos_m_y = int(pos_e_y + size[1] // 50 * math.cos(roll_m)) mouth = pygame.image.load(rF:\solar-system\image\mercury_bg.png) screen.blit(pygame.transform.scale(mouth, (6, 6)), (pos_m_x, pos_m_y)) # 火星 roll_4 += 0.053 # 每幀公轉pi pos_4_x = int(size[0] // 2 + size[1] // 5 * math.sin(roll_4)) pos_4_y = int(size[1] // 2 + size[1] // 5 * math.cos(roll_4)) venus = pygame.image.load(rF:\solar-system\image\venus_bg.png) screen.blit(pygame.transform.scale(venus, (13, 13)), (pos_4_x, pos_4_y)) # 木星 roll_5 += 0.045 # 每幀公轉pi pos_5_x = int(size[0] // 2 + size[1] // 4 * math.sin(roll_5)) pos_5_y = int(size[1] // 2 + size[1] // 4 * math.cos(roll_5)) mouth = pygame.image.load(rF:\solar-system\image\jupiter_bg.png) screen.blit(pygame.transform.scale(mouth, (70, 70)), (pos_5_x, pos_5_y)) # 土星 roll_6 += 0.037 # 每幀公轉pi pos_6_x = int(size[0] // 2 + size[1] // 3.5 * math.sin(roll_6)) pos_6_y = int(size[1] // 2 + size[1] // 3.5 * math.cos(roll_6)) saturn = pygame.image.load(rF:\solar-system\image\saturn_bg.png) screen.blit(pygame.transform.scale(saturn, (50, 50)), (pos_6_x, pos_6_y)) # 天王星 roll_7 += 0.031 # 每幀公轉pi pos_7_x = int(size[0] // 2 + size[1] // 2.7 * math.sin(roll_7)) pos_7_y = int(size[1] // 2 + size[1] // 2.7 * math.cos(roll_7)) uranus = pygame.image.load(rF:\solar-system\image\uranus_bg.png) screen.blit(pygame.transform.scale(uranus, (45, 45)), (pos_7_x, pos_7_y)) # 海王星 roll_8 += 0.025 # 每幀公轉pi pos_8_x = int(size[0] // 2 + size[1] // 2 * math.sin(roll_8)) pos_8_y = int(size[1] // 2 + size[1] // 2 * math.cos(roll_8)) neptune = pygame.image.load(rF:\solar-system\image\neptune_bg.png) screen.blit(pygame.transform.scale(neptune, (37, 37)), (pos_8_x, pos_8_y)) # 刷新 pygame.display.flip() # 數值越大刷新越快,小球運動越快 clock.tick(50)
最後
到這裡就沒有了,150行代碼,不多不少剛剛好。其實還是挺多的,密密麻麻的代碼先給自己點一個贊。喜歡的點贊,不懂的記得點贊收藏。下一章見。