麻将机灯光效果模拟程序
下面是一个模拟麻将机灯光效果的Python程序,使用Pygame库创建了一个带有多种灯光效果的麻将机界面。
import pygame
import sys
import math
import random
from pygame import gfxdraw
# 初始化pygame
pygame.init()
# 设置窗口尺寸
WIDTH, HEIGHT = 1000, 700
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("麻将机灯光效果模拟")
# 颜色定义
BACKGROUND = (20, 30, 50)
TABLE_COLOR = (60, 80, 100)
TABLE_BORDER = (40, 50, 70)
LED_COLORS = [
(255, 50, 50), # 红色
(50, 255, 50), # 绿色
(50, 50, 255), # 蓝色
(255, 255, 50), # 黄色
(255, 50, 255), # 紫色
(50, 255, 255), # 青色
(255, 150, 50), # 橙色
]
# 麻将桌尺寸
TABLE_X = 100
TABLE_Y = 100
TABLE_WIDTH = 800
TABLE_HEIGHT = 500
CORNER_RADIUS = 30
# 灯光效果参数
current_effect = 0
effects = ["静态呼吸灯", "流水灯效果", "彩虹波动", "心跳效果", "随机闪烁", "跑马灯效果"]
effect_timer = 0
led_states = [] # 存储LED状态
# 初始化LED位置和状态
def init_leds():
led_states.clear()
# 麻将桌边缘LED
num_leds = 80
for i in range(num_leds):
# 计算位置 (沿着麻将桌边缘)
angle = 2 * math.pi * i / num_leds
if i < num_leds/4: # 上边
x = TABLE_X + TABLE_WIDTH * i / (num_leds/4)
y = TABLE_Y
elif i < num_leds/2: # 右边
x = TABLE_X + TABLE_WIDTH
y = TABLE_Y + TABLE_HEIGHT * (i - num_leds/4) / (num_leds/4)
elif i < num_leds*3/4: # 下边
x = TABLE_X + TABLE_WIDTH - TABLE_WIDTH * (i - num_leds/2) / (num_leds/4)
y = TABLE_Y + TABLE_HEIGHT
else: # 左边
x = TABLE_X
y = TABLE_Y + TABLE_HEIGHT - TABLE_HEIGHT * (i - num_leds*3/4) / (num_leds/4)
# 随机颜色和初始状态
color_idx = random.randint(0, len(LED_COLORS)-1)
brightness = random.uniform(0.3, 1.0)
pulse_speed = random.uniform(0.02, 0.05)
led_states.append({
'pos': (x, y),
'color_idx': color_idx,
'brightness': brightness,
'pulse_speed': pulse_speed,
'pulse_dir': 1 if random.random() > 0.5 else -1
})
# 中心区域LED
for i in range(20):
x = TABLE_X + TABLE_WIDTH/4 + random.random() * TABLE_WIDTH/2
y = TABLE_Y + TABLE_HEIGHT/4 + random.random() * TABLE_HEIGHT/2
color_idx = random.randint(0, len(LED_COLORS)-1)
brightness = random.uniform(0.3, 1.0)
pulse_speed = random.uniform(0.02, 0.05)
led_states.append({
'pos': (x, y),
'color_idx': color_idx,
'brightness': brightness,
'pulse_speed': pulse_speed,
'pulse_dir': 1 if random.random() > 0.5 else -1
})
# 绘制圆角矩形
def draw_rounded_rect(surface, rect, color, corner_radius):
"""Draw a rectangle with rounded corners"""
if corner_radius < 0:
raise ValueError(f"Corner radius must be >= 0; received {corner_radius}")
# Fill the entire rectangle
pygame.draw.rect(surface, color, rect, border_radius=corner_radius)
# Draw the border
pygame.draw.rect(surface, TABLE_BORDER, rect, 3, corner_radius)
# 绘制LED灯
def draw_led(surface, pos, color, brightness, size=8):
r, g, b = color
# 应用亮度
r = int(r * brightness)
g = int(g * brightness)
b = int(b * brightness)
# 绘制发光效果
pygame.draw.circle(surface, (r, g, b), (int(pos[0]), int(pos[1])), size)
for i in range(3):
glow_size = size * (1 + i * 0.5)
alpha = 100 - i * 30
glow_surf = pygame.Surface((glow_size*2, glow_size*2), pygame.SRCALPHA)
pygame.draw.circle(glow_surf, (r, g, b, alpha),
(glow_size, glow_size), glow_size)
surface.blit(glow_surf, (int(pos[0] - glow_size), int(pos[1] - glow_size)))
# 更新LED状态(根据当前效果)
def update_leds():
global effect_timer
effect_timer += 1
if current_effect == 0: # 静态呼吸灯
for led in led_states:
led['brightness'] += led['pulse_speed'] * led['pulse_dir']
if led['brightness'] > 1.0:
led['brightness'] = 1.0
led['pulse_dir'] = -1
elif led['brightness'] < 0.3:
led['brightness'] = 0.3
led['pulse_dir'] = 1
elif current_effect == 1: # 流水灯效果
for i, led in enumerate(led_states):
# 根据位置和计时器创建流动效果
phase = (i + effect_timer) % 80
led['brightness'] = 0.3 + 0.7 * (1 + math.sin(phase * 0.2)) / 2
elif current_effect == 2: # 彩虹波动
for i, led in enumerate(led_states):
# 改变颜色索引创建彩虹效果
phase = (i + effect_timer // 5) % len(LED_COLORS)
led['color_idx'] = phase
# 波动亮度
led['brightness'] = 0.5 + 0.5 * math.sin(effect_timer * 0.05 + i * 0.1)
elif current_effect == 3: # 心跳效果
for led in led_states:
# 创建心跳效果
t = effect_timer * 0.1
beat = abs(math.sin(t * 3)) ** 10 # 创建脉冲
led['brightness'] = 0.3 + 0.7 * beat
elif current_effect == 4: # 随机闪烁
if effect_timer % 


