在快节奏的现代生活中,泡澡是一种极佳的放松方式。本文将带您开发一个简单的泡澡模拟软件,通过代码创造一个沉浸式的虚拟泡澡体验。我们将使用Python和Pygame库来实现这个项目,涵盖水温控制、气泡效果、音效和用户交互等核心功能。
项目概述
我们的泡澡模拟器将包含以下功能:
可调节的水温系统
动态气泡效果
环境音效(水流声、放松音乐)
虚拟香薰系统
简单的用户界面
代码实现
1 初始化设置
python
import pygame
import random
import sys
import ti me
# 初始化pygame
pygame init()
# 屏幕设置
WIDTH, HEIGHT = 800, 600
screen = pygame display set_mode((WIDTH, HEIGHT))
pygame display set_caption("虚拟泡澡体验")
# 颜色定义
WHITE = (255, 255, 255)
BLUE = (135, 206, 235)
LIGHT_BLUE = (173, 216, 230)
DARK_BLUE = (0, 100, 150)
BROWN = (139, 69, 19)
PINK = (255, 192, 203)
# 字体
font = pygame font SysFont('simhei', 24)
2 水温控制系统
python
class WaterTemperature:
def __init__(self):
self temp = 38 0 # 默认水温38度
self heating = False
self cooling = False
def adjust_temperature(self, change):
self temp += change
self temp = max(30, min(42, self temp)) # 限制在30-42度之间
def get_temp_color(self):
if self temp < 35:
return BLUE
elif self temp < 38:
return LIGHT_BLUE
elif self temp < 40:
return (100, 180, 220)
else:
return DARK_BLUE
代码参考:https://github.com/ms9l/na
def get_temp_text(self):
return f"水温: {self temp: 1f}°C"
3 气泡效果实现
python
class Bubble:
def __init__(self):
self radius = random randint(2, 10)
self x = random randint(0, WIDTH)
self y = HEIGHT + self radius
self speed = random uniform(0 5, 2 0)
self alpha = random randint(50, 150) # 透明度
def update(self):
self y -= self speed
self x += random uniform(-0 5, 0 5) # 轻微左右摆动
def draw(self, surface):
color = (255, 255, 255, self alpha)
pygame draw circle(surface, color, (int(self x), int(self y)), self radius)
class BubbleSystem:
def __init__(self):
self bubbles = []
self bubble_rate = 10 # 每帧生成气泡的概率(1/x)
def update(self):
# 随机生成新气泡
if random randint(1, self bubble_rate) == 1:
self bubbles append(Bubble())
# 更新和移除气泡
for bubble in self bubbles[:]:
bubble update()
if bubble y < -bubble radius * 2:
self bubbles remove(bubble)
def draw(self, surface):
for bubble in self bubbles:
bubble draw(surface)
代码参考:https://github.com/ms9l/nb
4 香薰系统
python
class AromaSystem:
def __init__(self):
self scents = {
'lavender': {'color': (147, 112, 219), 'name': '薰衣草'},
'rose': {'color': (255, 105, 180), 'name': '玫瑰'},
'eucalyptus': {'color': (100, 149, 237), 'name': '桉树'},
'none': {'color': (255, 255, 255, 0), 'name': '无香'}
}
self current_scent = 'none'
def change_scent(self, scent_name):
if scent_name in self scents:
self current_scent = scent_name
def get_current_scent(self):
return self scents[self current_scent]['name']
def get_scent_color(self):
return self scents[self current_scent]['color']
代码参考:https://github.com/ms9l/nc
5 主程序
python
def main():
clock = pygame ti me Clock()
running = True
# 初始化系统
temp_system = WaterTemperature()
bubble_system = BubbleSystem()
aroma_system = AromaSystem()
# 音效设置 (实际使用时需要加载真实音频文件)
try:
water_sound = pygame mixer Sound("water_sound wav")
relax_music = pygame mixer Sound("relax_music wav")
water_sound set_volume(0 3)
relax_music set_volume(0 2)
water_sound play(-1) # 循环播放
relax_music play(-1)
except:
print("警告: 无法加载音频文件,将静音运行")
# 主循环
while running:
for event in pygame event get():
if event type == pygame QUIT:
running = False
代码参考:https://github.com/ms9l/nd
# 键盘控制
elif event type == pygame KEYDOWN:
if event key == pygame K_UP:
temp_system adjust_temperature(0 5)
elif event key == pygame K_DOWN:
temp_system adjust_temperature(-0 5)
elif event key == pygame K_1:
aroma_system change_scent('lavender')
elif event key == pygame K_2:
aroma_system change_scent('rose')
elif event key == pygame K_3:
aroma_system change_scent('eucalyptus')
elif event key == pygame K_0:
aroma_system change_scent('none')
elif event key == pygame K_ESCAPE:
running = False
# 更新游戏状态
bubble_system update()
# 绘制
screen fill((0, 0, 0)) # 黑色背景
# 绘制浴缸
pygame draw rect(screen, (220, 220, 220), (100, 200, 600, 300), border_radius=20)
# 绘制水 (根据温度变化颜色)
water_color = temp_system get_temp_color()
pygame draw rect(screen, water_color, (120, 220, 560, 260), border_radius=15)
代码参考:https://github.com/ms9l/ne
# 绘制香薰效果 (半透明覆盖)
scent_color = aroma_system get_scent_color()
if scent_color[3] > 0: # 如果有颜色(非完全透明)
s = pygame Surface((WIDTH, HEIGHT), pygame SRCALPHA)
s fill((scent_color[0], scent_color[1], scent_color[2], 30))
screen blit(s, (0, 0))
# 绘制气泡
bubble_system draw(screen)
# 绘制浴缸边缘
pygame draw rect(screen, BROWN, (90, 190, 620, 320), 5, border_radius=25)
# 绘制UI文本
temp_text = font render(temp_system get_temp_text(), True, (0, 0, 0))
scent_text = font render(f"香薰: {aroma_system get_current_scent()}", True, (0, 0, 0))
controls_text = font render("↑↓调节水温 | 1-3选择香薰 | 0无香", True, (255, 255, 255))
screen blit(temp_text, (50, 50))
screen blit(scent_text, (50, 90))
screen blit(controls_text, (50, 550))
pygame display flip()
clock tick(60)
pygame quit()
sys exit()
if __name__ == "__main__":
main()
代码参考:https://github.com/ms9l/nf
功能扩展建议
增强视觉效果:
添加蒸汽效果
实现更复杂的水面波纹
添加蜡烛或灯光效果
音效增强:
添加不同香薰对应的背景音乐
实现水温变化时的水流声变化
添加点击按钮的音效
交互功能:
添加虚拟沐浴露/洗发水
实现毛巾擦拭效果
添加计时器功能
健康监测集成:
模拟心率监测
添加放松程度指示器
实现呼吸引导功能
结论
这个简单的泡澡模拟器展示了如何使用Python和Pygame创建一个基本的沉浸式体验。虽然它无法完全替代真实的泡澡体验,但通过代码,我们可以探索如何模拟和增强这种放松活动。您可以根据需要扩展这个基础框架,添加更多功能和细节,创造一个更加逼真的虚拟泡澡环境。
要运行完整程序,您需要安装Pygame库:
pip install pygame
希望这个项目能激发您对创意编程的兴趣,并为您提供一个放松的编程体验!