前言
本文的文字及圖片來源于網路,僅供學習、交流使用,不具有任何商業用途,如有問題請及時聯系我們以作處理,
PS:如有需要Python學習資料的小伙伴可以點擊下方鏈接自行獲取
Python免費學習資料、代碼以及交流解答點擊即可加入
1. 代碼實作
需要安裝依賴包:pygame
篇幅原因,這里僅展示部分代碼,
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
# 背景顏色為黑色
screen.fill(BLACK)
# 畫太陽
pygame.draw.circle(screen, YELLOW, position, 60, 0)
# 畫地球
roll_e += 0.01 # 假設地球每幀公轉 0.01 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))
pygame.draw.circle(screen, BLUE, (pos_e_x, pos_e_y), 15, 0)
# 地球的軌跡線
pos_e.append((pos_e_x, pos_e_y))
if len(pos_e) > 255:
pos_e.pop(0)
for i in range(len(pos_e)):
pygame.draw.circle(screen, SILVER, pos_e[i], 1, 0)
# 畫月球
roll_m += 0.1
pos_m_x = int(pos_e_x + size[1] // 20 * math.sin(roll_m))
pos_m_y = int(pos_e_y + size[1] // 20 * math.cos(roll_m))
pygame.draw.circle(screen, WHITE, (pos_m_x, pos_m_y), 8, 0)
# 月球的軌跡線
pos_mm.append((pos_m_x, pos_m_y))
if len(pos_mm) > 255:
pos_mm.pop(0)
for i in range(len(pos_mm)):
pygame.draw.circle(screen, SILVER, pos_mm[i], 1, 0)
# 畫金星
roll_v += 0.015
pos_v_x = int(size[0] // 2 + size[1] // 3 * math.sin(roll_v))
pos_v_y = int(size[1] // 2 + size[1] // 3 * math.cos(roll_v))
pygame.draw.circle(screen, RED, (pos_v_x, pos_v_y), 20, 0)
# 金星的軌跡線
pos_v.append((pos_v_x, pos_v_y))
if len(pos_v) > 255:
pos_v.pop(0)
for i in range(len(pos_v)):
pygame.draw.circle(screen, SILVER, pos_v[i], 1, 0)
# 重繪
pygame.display.flip()
# 數值越大重繪越快,小球運動越快
clock.tick(40)
2. 效果圖
看著這公轉的太陽,是不是很炫酷,
總結
關注公眾號回復【210305】領取原始碼
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/266606.html
標籤:Python
