code个黑洞(Python版)

本文介绍了如何使用Python的pygame库创建一个简单的2D游戏,包含粒子生成、运动和黑洞吞噬功能。通过随机位置和速度的粒子,模拟出动态效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import pygame
import random

# 初始化Pygame
pygame.init()

# 设置窗口尺寸
width, height = 800, 600
screen = pygame.display.set_mode((width, height))

# 定义颜色
black = (0, 0, 0)
white = (255, 255, 255)
blue = (0, 0, 255)

# 定义黑洞的参数
blackhole_radius = 20
blackhole_x = width // 2
blackhole_y = height // 2

# 创建粒子类
class Particle:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.radius = random.randint(1, 5)
        self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        self.speed = random.randint(1, 3)

    def update(self):
        angle = random.uniform(0, 2 * 3.1415)
        self.x += self.speed * 0.1 * pygame.math.cos(angle)
        self.y += self.speed * 0.1 * pygame.math.sin(angle)
        self.speed += 0.1

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

# 创建粒子列表
particles = []

running = True
clock = pygame.time.Clock()

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill(black)

    # 绘制黑洞
    pygame.draw.circle(screen, black, (blackhole_x, blackhole_y), blackhole_radius)

    # 生成新的粒子
    if random.random() < 0.1:
        particle = Particle(random.randint(0, width), random.randint(0, height))
        particles.append(particle)

    # 更新和绘制粒子
    for particle in particles:
        particle.update()
        particle.draw()

        # 粒子被黑洞吞噬
        if (blackhole_x - particle.x) ** 2 + (blackhole_y - particle.y) ** 2 <= blackhole_radius ** 2:
            particles.remove(particle)

    pygame.display.flip()
    clock.tick(60)

pygame.quit()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值