前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >星辰秘典:揭示Python项目的宇宙奥秘——宇宙星空模拟器(改)

星辰秘典:揭示Python项目的宇宙奥秘——宇宙星空模拟器(改)

作者头像
命运之光
发布2024-03-20 11:27:24
740
发布2024-03-20 11:27:24
举报

前言:你好,欢迎来到我的博客。我是一个热爱编程的人,特别喜欢用Python这门语言来创造一些有趣的图形项目。在这篇博客中,我将和你分享一些我用Python写的小的图形项目,包括它们的原理,代码和效果。我希望你能从中学到一些有用的知识,也能感受到编程的乐趣。如果你对我的项目有任何问题或建议,欢迎在评论区留言,我会尽快回复你。让我们开始吧!

项目名称: 宇宙星空模拟器

简介

宇宙星空模拟器是一个用Python和Pygame库创建的小型项目,它可以模拟宇宙中的星星、星系和星云,并展现出美丽的星空效果。通过这个模拟器,你可以欣赏到宇宙中的无尽星辰,感受宇宙的浩瀚和神秘。

特色功能

星星的随机生成: 项目通过随机生成星星的位置、大小、颜色和透明度,使得每个星星都具有独特的特点,营造出绚丽多彩的星空效果。

连线效果

星星之间会随机生成连线,形成星星之间的关联,让整个星空更加生动有趣。

爆炸效果: 当点击鼠标左键时,选中的星星将会爆炸,并产生出多个粒子效果,增加了交互和视觉上的动感。

星云效果

项目中还包含了星云效果,随机生成星云的位置、大小和颜色,为星空增添了神秘的氛围。

使用说明

运行代码: 需要安装Python和Pygame库,然后运行代码即可打开宇宙星空模拟器。

鼠标交互: 可以使用鼠标左键点击星星,触发爆炸效果。

自定义设置: 可以根据自己的需求修改代码中的参数,例如星星数量、连线概率和帧率等,来调整星空的效果和动画速度。

项目展示
动态图展示
图片展示
项目源代码
代码语言:javascript
复制
import pygame
import random
import math

# 初始化pygame
pygame.init()

# 显示窗口的宽度和高度
width, height = 800, 600

# 创建一个窗口
screen = pygame.display.set_mode((width, height))

# 设置窗口标题
pygame.display.set_caption("宇宙星空模拟器")

# 定义星星类
class Star:
    def __init__(self):
        self.x = random.randint(0, width)
        self.y = random.randint(0, height)
        self.size = random.randint(1, 3)
        self.color = (random.randint(100, 255), random.randint(100, 255), random.randint(100, 255))
        self.alpha = random.randint(50, 255)

    def draw(self):
        surface = pygame.Surface((self.size * 2, self.size * 2), pygame.SRCALPHA)
        surface.set_alpha(self.alpha)
        pygame.draw.circle(surface, self.color, (self.size, self.size), self.size)
        screen.blit(surface, (self.x - self.size, self.y - self.size))

    def draw_line(self, other_star):
        pygame.draw.line(screen, self.color, (self.x, self.y), (other_star.x, other_star.y))

    def explode(self):
        explosion_particles = []
        for _ in range(20):
            dx = random.uniform(-1, 1)
            dy = random.uniform(-1, 1)
            speed = random.uniform(1, 5)
            particle = ExplosionParticle(self.x, self.y, dx, dy, speed)
            explosion_particles.append(particle)
        return explosion_particles

# 定义爆炸粒子类
class ExplosionParticle:
    def __init__(self, x, y, dx, dy, speed):
        self.x = x
        self.y = y
        self.dx = dx
        self.dy = dy
        self.speed = speed
        self.size = random.randint(1, 3)
        self.color = (random.randint(100, 255), random.randint(100, 255), random.randint(100, 255))

    def move(self):
        self.x += self.dx * self.speed
        self.y += self.dy * self.speed

    def draw(self):
        pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.size)

# 定义星云类
class Cloud:
    def __init__(self):
        self.x = random.randint(0, width)
        self.y = random.randint(0, height)
        self.size = random.randint(20, 50)
        self.color = (random.randint(100, 200), random.randint(100, 200), random.randint(100, 200))
        self.alpha = random.randint(50, 150)

    def draw(self):
        surface = pygame.Surface((self.size, self.size), pygame.SRCALPHA)
        surface.set_alpha(self.alpha)
        pygame.draw.circle(surface, self.color, (self.size // 2, self.size // 2), self.size // 2)
        screen.blit(surface, (self.x - self.size // 2, self.y - self.size // 2))

# 定义星系类
class Galaxy:
    def __init__(self, center_x, center_y, num_stars, radius):
        self.center_x = center_x
        self.center_y = center_y
        self.num_stars = num_stars
        self.radius = radius
        self.stars = []

        for _ in range(self.num_stars):
            angle = random.uniform(0, 2 * math.pi)
            dist = random.uniform(0, self.radius)
            x = self.center_x + dist * math.cos(angle)
            y = self.center_y + dist * math.sin(angle)
            size = random.randint(1, 3)
            color = (random.randint(100, 255), random.randint(100, 255), random.randint(100, 255))
            alpha = random.randint(50, 255)
            self.stars.append((x, y, size, color, alpha))

    def draw(self):
        for star in self.stars:
            x, y, size, color, alpha = star
            surface = pygame.Surface((size * 2, size * 2), pygame.SRCALPHA)
            surface.set_alpha(alpha)
            pygame.draw.circle(surface, color, (size, size), size)
            screen.blit(surface, (x - size, y - size))

# 创建星星列表
stars = []
for _ in range(200):
    stars.append(Star())

# 创建星云列表
clouds = []
for _ in range(10):
    clouds.append(Cloud())

# 创建星系列表
galaxies = []
for _ in range(3):
    x = random.randint(0, width)
    y = random.randint(0, height)
    num_stars = random.randint(50, 100)
    radius = random.randint(50, 100)
    galaxies.append(Galaxy(x, y, num_stars, radius))

# 创建爆炸粒子列表
explosion_particles = []

# 游戏循环
running = True
clock = pygame.time.Clock()
while running:
    # 处理退出事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:  # 鼠标左键点击
                mouse_pos = pygame.mouse.get_pos()
                for star in stars:
                    if math.sqrt((star.x - mouse_pos[0]) ** 2 + (star.y - mouse_pos[1]) ** 2) < star.size:
                        explosion_particles += star.explode()
                        stars.remove(star)

    # 填充背景色
    screen.fill((0, 0, 0))

    # 更新和绘制星云
    for cloud in clouds:
        cloud.draw()

    # 更新和绘制星系
    for galaxy in galaxies:
        galaxy.draw()

    # 更新和绘制星星
    for star in stars:
        star.draw()

    # 更新和绘制爆炸粒子
    for particle in explosion_particles:
        particle.move()
        particle.draw()

    # 绘制星星之间的连线
    for i, star in enumerate(stars):
        for other_star in stars[i + 1:]:
            if random.random() < 0.0005:  # 调整连线的概率
                star.draw_line(other_star)
                pygame.time.delay(1)  # 添加延迟

    # 限制帧率
    clock.tick(30)

    # 更新屏幕显示
    pygame.display.flip()

# 退出游戏
pygame.quit()
如何运行项目(超简单)

在运行上述代码之前,你需要确保你的环境中已经安装了Pygame依赖项:

Pygame:一个用于开发游戏的Python库。你可以使用以下命令通过pip安装Pygame

如果没有安装用以下方法进行安装

1.win+r打开命令行窗口
2.在窗口中复制粘贴下面内容

使用国内的镜像源:将pip的默认源替换为国内的镜像源可以加快下载速度。你可以使用以下命令来更换pip的源:

下载:Pygame:一个用于游戏开发的Python库,用于创建游戏界面和处理用户输入。

代码语言:javascript
复制
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pygame

下载:numpy:一个用于数值计算和数组操作的Python库。

代码语言:javascript
复制
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple numpy
打开pycharm
step1
step2
复制粘贴源代码运行
项目总结

宇宙星空模拟器是一个基于Python和Pygame库的小型项目,旨在模拟宇宙中的星星、星系和星云,呈现出绚丽多彩的星空效果。通过这个模拟器,用户可以欣赏到宇宙的浩瀚与神秘,感受到宇宙中无尽星辰的美妙。

在项目中,使用随机生成的星星、星云和星系,以及交互性的爆炸效果和连线效果,营造出一个动感十足的宇宙场景。用户可以通过点击鼠标触发星星的爆炸效果,产生出迷人的粒子效果。同时,星星之间的连线增加了整个星空的生动感。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-03-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 项目名称: 宇宙星空模拟器
    • 简介
      • 特色功能
        • 连线效果
          • 星云效果
            • 使用说明
              • 项目展示
                • 动态图展示
                • 图片展示
              • 项目源代码
                • 如何运行项目(超简单)
                  • 1.win+r打开命令行窗口
                  • 2.在窗口中复制粘贴下面内容
                  • 打开pycharm
                  • 复制粘贴源代码运行
                • 项目总结
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档