猫史档案馆


【Python作品分享】【去太空,趣编程】太阳系代码

用户:Larry0601Larry0601查看:0 回复:1 评论:0 创建时间:2021-05-05T21:48:33


【作品展示】

center_image

 

【作品介绍】

这是一个太阳系的模拟示意图

 

【作品源代码】

import sys
import math
import pygame
from pygame.locals import *
WHITE = (255, 255, 255)
SILVER = (192, 192, 192)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
SandyBrown = (244, 1喵, 96)
PaleGodenrod = (238, 232, 170)
PaleVioletRed = (219, 112, 147)
Thistle = (216, 191, 216)
size = width, height = 800, 600
screen = pygame.display.set_mode(size)
pygame.display.set_caption("太阳系")
# 创建时钟(控制游戏循环频率)
clock = pygame.time.Clock()
# 定义三个空列表
pos_v = pos_e = pos_mm = []
# 地球、月球等行星转过的角度
roll_v = roll_e = roll_m = 0
roll_3 = roll_4 = roll_5 = roll_6 = roll_7 = roll_8 = 0
# 太阳的位置(中心)
position = size[0] // 2, size[1] // 2

keep_going = True  # 定义一个变量来表示是否执行循环


while keep_going:
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            # 如果触发了退出事件就改写keep_going,不再执行无限循环
            keep_going = False
    screen.fill(BLACK)
    # 画地球
    roll_e += 0.0001  # 假设地球每帧公转 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))

    # 地球的轨迹线
    pos_e.append((pos_e_x, pos_e_y))
    if len(pos_e) > 255:
        pos_e.pop(0)

    # 其他几个行星
    roll_3 += 0.0003
    pos_3_x = int(size[0] // 2 + size[1] // 3.5 * math.sin(roll_3))
    pos_3_y = int(size[1] // 2 + size[1] // 3.5 * math.cos(roll_3))

    roll_4 += 0.0004
    pos_4_x = int(size[0] // 2 + size[1] // 4 * math.sin(roll_4))
    pos_4_y = int(size[1] // 2 + size[1] // 4 * math.cos(roll_4))

    roll_5 += 0.0005
    pos_5_x = int(size[0] // 2 + size[1] // 5 * math.sin(roll_5))
    pos_5_y = int(size[1] // 2 + size[1] // 5 * math.cos(roll_5))

    roll_6 += 0.0006
    pos_6_x = int(size[0] // 2 + size[1] // 2.5 * math.sin(roll_6))
    pos_6_y = int(size[1] // 2 + size[1] // 2.5 * math.cos(roll_6))

    roll_7 += 0.0007
    pos_7_x = int(size[0] // 2 + size[1] // 4.5 * math.sin(roll_7))
    pos_7_y = int(size[1] // 2 + size[1] // 4.5 * math.cos(roll_7))

    roll_8 += 0.0008
    pos_8_x = int(size[0] // 2 + size[1] // 5.5 * math.sin(roll_8))
    pos_8_y = int(size[1] // 2 + size[1] // 5.5 * math.cos(roll_8))
    pygame.draw.circle(screen, YELLOW, position, 60, 0)
    pygame.draw.circle(screen, BLUE, (pos_e_x, pos_e_y), 15, 0)
    for i in range(len(pos_e)):
        pygame.draw.circle(screen, SILVER, pos_e[i], 1, 0)
    pygame.draw.circle(screen, GREEN, (pos_3_x, pos_3_y), 20, 0)
    pygame.draw.circle(screen, SandyBrown, (pos_4_x, pos_4_y), 20, 0)
    pygame.draw.circle(screen, PaleGodenrod, (pos_5_x, pos_5_y), 20, 0)
    pygame.draw.circle(screen, PaleVioletRed, (pos_6_x, pos_6_y), 20, 0)
    pygame.draw.circle(screen, Thistle, (pos_7_x, pos_7_y), 20, 0)
    pygame.draw.circle(screen, WHITE, (pos_8_x, pos_8_y), 20, 0)
    pygame.display.update()  # 更新界面

pygame.quit()  # 结束游戏

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
Larry0601Larry0601

center_image

点赞0


评论