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

结果:

改进:

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

相关推荐
nuclear20112 小时前
Python 从PPT文档中提取图片和图片信息(坐标、宽度和高度等)
python·powerpoint·ppt图片提取·提取ppt背景图片·提取pp所有图片
樱花穿过千岛湖3 小时前
第六章:Multi-Backend Configuration
人工智能·python·gpt·学习·ai
跳跳糖炒酸奶4 小时前
第十五讲、Isaaclab中在机器人上添加传感器
人工智能·python·算法·ubuntu·机器人
FACELESS VOID4 小时前
llama-factory微调报错:
python
_一条咸鱼_5 小时前
Python 名称空间与作用域深度剖析(二十七)
人工智能·python·面试
_一条咸鱼_5 小时前
Python之函数对象+函数嵌套(二十六)
人工智能·python·面试
_一条咸鱼_5 小时前
Python 文件操作之修改(二十二)
人工智能·python·面试
_一条咸鱼_5 小时前
Python 闭包函数:原理、应用与深度解析(二十八)
人工智能·python·面试
_一条咸鱼_5 小时前
Python 之文件处理编码字符(二十)
人工智能·python·面试
_一条咸鱼_5 小时前
Python 装饰器:代码功能的优雅增强(二十九)
人工智能·python·面试