软件价值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()

结果:

改进:

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

相关推荐
北冥有鱼被烹4 分钟前
Linux timeout 命令完全指南:指定固定时间并发送固定信号
python
SunnyDays101112 分钟前
如何使用 Python 将 PowerPoint(PPT 或 PPTX)转换为 Markdown
python·ppt转markdown·pptx转markdown·powerpoint转md
PiaoKe___13 分钟前
云手机原理与 Python 自动化实战:ADB、REST API、WebSocket 群控入门
python·智能手机·自动化
co_wait15 分钟前
Python 迭代器(一)
python
Yolanda_20222 小时前
15.dataloader的使用
python
杨杨杨大侠9 小时前
Jev、Kev、Laya:决策模型怎么选,什么时候需要微调?
人工智能·python·agent
LOVE️YOU9 小时前
Python 在定义函数时,究竟定义的是什么?
python
段一凡-华北理工大学10 小时前
大模型应用开发 100 天:Python + LLM 从入门到精通 day26~Prompt 调试与优化——A/B 测试与效果评估
windows·python·大模型·prompt·智能体·提示词工程·高炉智能化
Minecraft红客10 小时前
以撒的结合
python·游戏·电脑·娱乐
Circ.10 小时前
Python 调试 Litellm 本地大模型接口:模型列表与对话接口实操踩坑记录
开发语言·python