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

结果:

改进:

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

相关推荐
神仙别闹6 分钟前
基于 C++和 Python 实现计算机视觉
c++·python·计算机视觉
hongjianMa41 分钟前
【论文阅读】Hypercomplex Prompt-aware Multimodal Recommendation
论文阅读·python·深度学习·机器学习·prompt·推荐系统
饼干,1 小时前
第23天python内容
开发语言·python
酷柚易汛智推官2 小时前
基于librespot的定制化Spotify客户端开发:开源替代方案的技术实践与优化
python·开源·酷柚易汛
雪碧聊技术2 小时前
requests入门
python·requests·请求头的user-agent
面向星辰3 小时前
机器学习过拟合和正则化
python
浔川python社3 小时前
《Python 小程序编写系列》(第三部):简易文件批量重命名工具
python·小程序·apache
QD.Joker3 小时前
高德MCP服务接入
python
孤客网络科技工作室3 小时前
孤客截图工具 Pro - 从开发到打包的完整指南
python
Q_Q19632884753 小时前
python+django/flask的医院财务管理系统
spring boot·python·django·flask·node.js