软件价值7-万花筒

模拟一个万花筒,鼠标点击可以变换图形

python 复制代码
import pygame
import sys
import math
import random

pygame.init()

# 设置窗口大小
WIDTH, HEIGHT = 800, 600
CENTER = (WIDTH // 2, HEIGHT // 2)

# 设置颜色
BLACK = (0, 0, 0)

# 初始化屏幕
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Kaleidoscope")

clock = pygame.time.Clock()


def generate_random_color():
    return random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)


def draw_kaleidoscope():
    num_slices = 12
    angle_increment = 360 / num_slices

    for i in range(num_slices):
        draw_pattern(i * angle_increment)


def draw_pattern(rotation_angle):
    num_lines = 100
    line_length = 200
    angle_increment = 360 / num_lines

    for i in range(num_lines):
        angle = math.radians(i * angle_increment)
        x1 = int(CENTER[0] + line_length * math.cos(angle))
        y1 = int(CENTER[1] + line_length * math.sin(angle))

        rotated_x1 = int(CENTER[0] + (x1 - CENTER[0]) * math.cos(math.radians(rotation_angle)) -
                         (y1 - CENTER[1]) * math.sin(math.radians(rotation_angle)))
        rotated_y1 = int(CENTER[1] + (x1 - CENTER[0]) * math.sin(math.radians(rotation_angle)) +
                         (y1 - CENTER[1]) * math.cos(math.radians(rotation_angle)))

        pygame.draw.line(screen, generate_random_color(), (rotated_x1, rotated_y1), (x1, y1), 2)


running = True
draw_kaleidoscope()
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            screen.fill(BLACK)
            draw_kaleidoscope()
            pygame.display.flip()

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

pygame.quit()
sys.exit()

结果:

改进:

改变算法 ,让万花筒的表现更绚丽多姿

相关推荐
hhzz16 分钟前
Barbershop:基于GAN和分割Mask的图像合成技术——从理论到实战全解析
图像处理·人工智能·python·深度学习·计算机视觉
Ulyanov2 小时前
雷达导引头Python仿真框架:GPU加速、6-DOF模型与半实物仿真接口
开发语言·python·雷达信号处理·雷达导引头
列逍2 小时前
博客系统测试
自动化测试·python·性能测试
星云开发3 小时前
拒绝无效加班!用Python打造自动化办公流,附Word/PDF互转硬核代码
python
dream_home84073 小时前
图像算法模型NPU适配与算法服务实战指南
人工智能·python·算法·npu 图像服务
AIGS0013 小时前
跨越语义鸿沟:企业本体语义平台的构建与落地
java·人工智能·python·机器学习·人工智能ai大模型应用
李可以量化3 小时前
PTrade 量化策略实战:二八轮动策略深度解析(下)
python
Mx_coder3 小时前
8年Java开发者AI转型第二周:RAG检索优化-从60%到90%准确率的3个关键技巧 (Day 13-14)
人工智能·python
自动化和Linux4 小时前
【pytest在pycharm中运行与CMD命令运行区别一】
python·pycharm·pytest
大鱼>4 小时前
多宠物家庭智能管理平台:云端架构与多设备协同实战
python·算法·iot·宠物