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

结果:

改进:

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

相关推荐
阿华的代码王国1 分钟前
【JavaEE】——文件IO的应用
开发语言·python
电饭叔39 分钟前
《python语言程序设计》2018版第8章19题几何Rectangle2D类(下)-头疼的几何和数学
开发语言·python
程序猿小D1 小时前
第二百六十七节 JPA教程 - JPA查询AND条件示例
java·开发语言·前端·数据库·windows·python·jpa
杰哥在此3 小时前
Python知识点:如何使用Multiprocessing进行并行任务管理
linux·开发语言·python·面试·编程
zaim15 小时前
计算机的错误计算(一百一十四)
java·c++·python·rust·go·c·多项式
PythonFun9 小时前
Python批量下载PPT模块并实现自动解压
开发语言·python·powerpoint
炼丹师小米9 小时前
Ubuntu24.04.1系统下VideoMamba环境配置
python·环境配置·videomamba
GFCGUO9 小时前
ubuntu18.04运行OpenPCDet出现的问题
linux·python·学习·ubuntu·conda·pip
985小水博一枚呀11 小时前
【深度学习基础模型】神经图灵机(Neural Turing Machines, NTM)详细理解并附实现代码。
人工智能·python·rnn·深度学习·lstm·ntm
萧鼎12 小时前
Python调试技巧:高效定位与修复问题
服务器·开发语言·python