☰
梦幻衣橱-二次元换装小游戏开发学习笔记
2026/10/10 2:58:23 网站建设 项目流程

一、项目介绍
本次项目借助AI工具,使用HTML、CSS、JavaScript开发一款交互式二次元换装小游戏《梦幻衣橱Pro》。游戏界面分为三大板块:左侧衣橱面板、中间人物预览区、右侧角色信息与操作面板。玩家可以自由挑选连衣裙、上衣、下装、鞋子、发型、配饰、脸型、眼型、眉毛、妆容,还能修改发色,支持随机搭配、重置装扮、保存造型等功能。页面搭配粉色梦幻房间背景、漂浮星星动画,换装时有闪光特效,整体风格可爱,交互流畅。

二、实现思路

  1. 页面布局:采用三栏式布局,左侧放置各类装扮分类按钮与素材缩略图;中间区域展示人物底图,各类装扮素材分层叠加;右侧面板用于显示当前装扮、角色命名、发色选择以及操作按钮。
  2. 图层管理:发型、衣服、配饰等每一类素材独立作为一个图层。同一类型装扮同一时间只显示一个,避免图层重叠混乱。
  3. JS交互逻辑:点击衣橱里的装扮缩略图,触发JS函数,先隐藏该分类下所有素材图层,再展示选中的装扮,同时触发换装闪光动画;随机搭配功能自动随机选取各类装扮组合;重置装扮清空所有选择;保存造型可以记录当前搭配。
  4. CSS美化:设置粉色渐变背景,编写CSS动画实现星星漂浮效果,美化按钮样式,调整所有素材图层的定位,保证人物素材对齐。

三、完整源代码

✨ 梦幻衣橱 - 二次元换装游戏 Pro ✨
  • { margin: 0; padding: 0; box-sizing: border-box; }

body {
font-family: ‘ZCOOL KuaiLe’, cursive, sans-serif;
min-height: 100vh;
background: linear-gradient(135deg, #ffe4f0 0%, #ffd1dc 25%, #ffc0cb 50%, #ffb6c1 75%, #ffdab9 100%);
overflow-x: hidden;
position: relative;
}

.floating-star {
position: fixed;
font-size: 20px;
animation: floatStar 6s ease-in-out infinite;
pointer-events: none;
z-index: 1;
}
@keyframes floatStar {
0%, 100% { transform: translateY(0) rotate(0deg); opacity: 0.8; }
50% { transform: translateY(-20px) rotate(180deg); opacity: 1; }
}

.game-title {
text-align: center;
padding: 12px 0;
position: relative;
z-index: 10;
}
.game-title h1 {
font-size: 32px;
color: #ff6b9d;
text-shadow: 2px 2px 0 #fff, 4px 4px 0 #ffb6c1;
letter-spacing: 4px;
}
.game-title .bow {
display: inline-block;
font-size: 26px;
margin: 0 10px;
animation: bowBounce 2s ease-in-out infinite;
}
@keyframes bowBounce {
0%, 100% { transform: rotate(-10deg); }
50% { transform: rotate(10deg); }
}

.game-container {
display: flex;
max-width: 1450px;
margin: 0 auto;
padding: 5px 20px;
height: calc(100vh - 70px);
gap: 15px;
position: relative;
z-index: 10;
}

/* 左侧衣橱面板 */
.wardrobe-panel {
width: 300px;
background: linear-gradient(180deg, #fff0f5 0%, #ffe4ec 100%);
border: 3px solid #ffb6c1;
border-radius: 20px;
padding: 12px;
box-shadow: 0 8px 32px rgba(255, 107, 157, 0.2), inset 0 2px 10px rgba(255, 255, 255, 0.5);
display: flex;
flex-direction: column;
position: relative;
}
.wardrobe-panel::before {
content: ‘🎀’;
position: absolute;
top: -15px;
left: 50%;
transform: translateX(-50%);
font-size: 28px;
z-index: 5;
}
.panel-title {
text-align: center;
color: #ff6b9d;
font-size: 18px;
margin-bottom: 10px;
padding-bottom: 6px;
border-bottom: 2px dashed #ffb6c1;
}

/* 分类标签 */
.category-tabs {
display: flex;
flex-wrap: wrap;
gap: 5px;
margin-bottom: 10px;
justify-content: center;
}
.category-tab {
padding: 6px 12px;
background: linear-gradient(180deg, #fff 0%, #ffe4ec 100%);
border: 2px solid #ffb6c1;
border-radius: 18px;
cursor: pointer;
font-size: 13px;
color: #ff6b9d;
transition: all 0.3s ease;
position: relative;
user-select: none;
min-height: 32px;
display: flex;
align-items: center;
}
.category-tab:hover {
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(255, 107, 157, 0.3);
}
.category-tab.active {
background: linear-gradient(180deg, #ff6b9d 0%, #ff8fab 100%);
color: white;
border-color: #ff6b9d;
transform: scale(1.05);
}
.category-tab .emoji { margin-right: 3px; }
.category-tab .has-item-dot {
position: absolute;
top: -2px;
right: -2px;
width: 9px;
height: 9px;
background: #ff6b9d;
border: 2px solid white;
border-radius: 50%;
display: none;
}
.category-tab.has-item .has-item-dot { display: block; }

/* 服装网格 */
.clothing-grid {
flex: 1;
overflow-y: auto;
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
padding: 4px;
align-content: start;
}
.clothing-grid::-webkit-scrollbar { width: 7px; }
.clothing-grid::-webkit-scrollbar-track { background: #ffe4ec; border-radius: 3px; }
.clothing-grid::-webkit-scrollbar-thumb { background: #ffb6c1; border-radius: 3px; }

.clothing-item {
aspect-ratio: 1;
background: white;
border: 2px solid #ffd1dc;
border-radius: 10px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
transition: all 0.3s ease;
position: relative;
overflow: hidden;
animation: gridFadeIn 0.3s ease-out backwards;
}
.clothing-item:nth-child(1) { animation-delay: 0s; }
.clothing-item:nth-child(2) { animation-delay: 0.04s; }
.clothing-item:nth-child(3) { animation-delay: 0.08s; }
.clothing-item:nth-child(4) { animation-delay: 0.12s; }
.clothing-item:nth-child(5) { animation-delay: 0.16s; }
.clothing-item:nth-child(6) { animation-delay: 0.20s; }
.clothing-item:nth-child(7) { animation-delay: 0.24s; }
.clothing-item:nth-child(8) { animation-delay: 0.28s; }
.clothing-item:nth-child(9) { animation-delay: 0.32s; }

@keyframes gridFadeIn {
from { opacity: 0; transform: translateY(8px) scale(0.95); }
to { opacity: 1; transform: translateY(0) scale(1); }
}

.clothing-item:hover {
border-color: #ff6b9d;
transform: scale(1.05);
box-shadow: 0 4px 15px rgba(255, 107, 157, 0.3);
}
.clothing-item.selected {
border-color: #ff6b9d;
background: linear-gradient(135deg, #fff0f5 0%, #ffe4ec 100%);
box-shadow: 0 0 0 3px rgba(255, 107, 157, 0.3);
}
.clothing-item .item-preview {
width: 82%;
height: 82%;
display: flex;
align-items: center;
justify-content: center;
pointer-events: none;
}
.clothing-item .item-preview svg,
.clothing-item .item-preview svg * { pointer-events: none; }
.clothing-item .item-name {
position: absolute;
bottom: 1px;
left: 0;
right: 0;
text-align: center;
font-size: 9px;
color: #ff6b9d;
opacity: 0;
transition: opacity 0.3s;
pointer-events: none;
}
.clothing-item:hover .item-name { opacity: 1; }
.clothing-item .remove-btn {
position: absolute;
top: 2px;
right: 2px;
width: 16px;
height: 16px;
background: #ff6b9d;
color: white;
border-radius: 50%;
font-size: 11px;
display: none;
align-items: center;
justify-content: center;
cursor: pointer;
line-height: 1;
}
.clothing-item.selected .remove-btn { display: flex; }

/* 中间预览区 */
.preview-panel {
flex: 1;
display: flex;
align-items: center;
justify-content: center;
position: relative;
}
.room-background {
position: absolute;
width: 100%;
height: 100%;
border-radius: 30px;
overflow: hidden;
background: linear-gradient(180deg, #fff5f8 0%, #ffe4ec 40%, #ffd1dc 100%);
border: 4px solid #ffb6c1;
box-shadow: 0 10px 40px rgba(255, 107, 157, 0.2);
}
.room-decor {
position: absolute;
width: 100%;
height: 100%;
pointer-events: none;
}
.window {
position: absolute;
top: 40px;
right: 50px;
width: 100px;
height: 130px;
background: linear-gradient(180deg, #87ceeb 0%, #b0e0e6 100%);
border: 4px solid #fff;
border-radius: 50px 50px 10px 10px;
}
.window::before { content: ‘’; position: absolute; top: 50%; left: 0; right: 0; height: 4px; background: #fff; }
.window::after { content: ‘’; position: absolute; left: 50%; top: 0; bottom: 0; width: 4px; background: #fff; }
.curtain {
position: absolute;
top: 30px;
width: 35px;
height: 160px;
background: linear-gradient(180deg, #ff6b9d 0%, #ffb6c1 100%);
border-radius: 0 0 18px 18px;
}
.curtain.left { right: 135px; }
.curtain.right { right: 25px; }
.curtain::before { content: ‘🎀’; position: absolute; top: -8px; left: 50%; transform: translateX(-50%); font-size: 16px; }
.dresser {
position: absolute;
bottom: 50px;
left: 30px;
width: 90px;
height: 100px;
background: linear-gradient(180deg, #deb887 0%, #d2b48c 100%);
border-radius: 8px;
border: 3px solid #c4a777;
}
.dresser::before { content: ‘🪞’; position: absolute; top: -45px; left: 50%; transform: translateX(-50%); font-size: 45px; }
.dresser::after { content: ‘💄’; position: absolute; top: -18px; left: 50%; transform: translateX(-50%); font-size: 14px; }
.rug {
position: absolute;
bottom: 15px;
left: 50%;
transform: translateX(-50%);
width: 280px;
height: 35px;
background: radial-gradient(ellipse, #ff6b9d 0%, #ff8fab 50%, transparent 70%);
border-radius: 50%;
opacity: 0.3;
}
.stars-decor {
position: absolute;
top: 20px;
left: 30px;
font-size: 26px;
animation: twinkle 2s ease-in-out infinite;
}
.stars-decor.s2 { top: 70px; left: 120px; font-size: 18px; animation-delay: 0.5s; }
.stars-decor.s3 { top: 35px; right: 190px; font-size: 22px; animation-delay: 1s; }
@keyframes twinkle {
0%, 100% { opacity: 1; transform: scale(1); }
50% { opacity: 0.5; transform: scale(0.8); }
}
.heart-float {
position: absolute;
font-size: 14px;
animation: heartFloat 4s ease-in-out infinite;
}
@keyframes heartFloat {
0%, 100% { transform: translateY(0) scale(1); opacity: 0.7; }
50% { transform: translateY(-15px) scale(1.2); opacity: 1; }
}

.character-container {
position: relative;
z-index: 5;
width: 240px;
height: 520px;
display: flex;
align-items: center;
justify-content: center;
animation: breathe 3s ease-in-out infinite;
}
@keyframes breathe {
0%, 100% { transform: translateY(0); }
50% { transform: translateY(-4px); }
}
.character-svg {
width: 100%;
height: 100%;
filter: drop-shadow(0 10px 20px rgba(255, 107, 157, 0.3));
transition: transform 0.3s ease;
}
.character-svg.dress-up-flash {
animation: dressUpFlash 0.5s ease-out;
}
@keyframes dressUpFlash {
0% { transform: scale(1); filter: drop-shadow(0 10px 20px rgba(255, 107, 157, 0.3)); }
30% { transform: scale(1.06); filter: drop-shadow(0 10px 30px rgba(255, 107, 157, 0.6)) brightness(1.15); }
100% { transform: scale(1); filter: drop-shadow(0 10px 20px rgba(255, 107, 157, 0.3)); }
}

/* 右侧面板 */
.info-panel {
width: 260px;
display: flex;
flex-direction: column;
gap: 12px;
}
.info-card {
background: linear-gradient(180deg, #fff0f5 0%, #ffe4ec 100%);
border: 3px solid #ffb6c1;
border-radius: 18px;
padding: 12px;
box-shadow: 0 8px 32px rgba(255, 107, 157, 0.2);
position: relative;
}
.info-card::before {
content: ‘⭐’;
position: absolute;
top: -10px;
left: 18px;
font-size: 22px;
}
.info-card-title {
color: #ff6b9d;
font-size: 16px;
margin-bottom: 8px;
text-align: center;
}
.outfit-list {
display: flex;
flex-direction: column;
gap: 5px;
max-height: 160px;
overflow-y: auto;
}
.outfit-list::-webkit-scrollbar { width: 5px; }
.outfit-list::-webkit-scrollbar-track { background: #ffe4ec; border-radius: 2px; }
.outfit-list::-webkit-scrollbar-thumb { background: #ffb6c1; border-radius: 2px; }
.outfit-item {
display: flex;
align-items: center;
gap: 6px;
padding: 5px 8px;
background: white;
border-radius: 8px;
border: 1px solid #ffd1dc;
font-size: 12px;
color: #ff6b9d;
}
.outfit-item .item-icon { font-size: 16px; }
.outfit-item .remove-item {
margin-left: auto;
cursor: pointer;
color: #ff6b9d;
font-weight: bold;
font-size: 14px;
}
.outfit-item .remove-item:hover { color: #ff4757; }
.empty-outfit {
text-align: center;
color: #ffb6c1;
font-size: 12px;
padding: 15px 0;
}
.action-buttons {
display: flex;
flex-direction: column;
gap: 7px;
}
.action-btn {
padding: 8px 16px;
background: linear-gradient(180deg, #ff6b9d 0%, #ff8fab 100%);
color: white;
border: none;
border-radius: 22px;
cursor: pointer;
font-family: ‘ZCOOL KuaiLe’, cursive;
font-size: 14px;
transition: all 0.3s ease;
box-shadow: 0 4px 12px rgba(255, 107, 157, 0.4);
}
.action-btn:hover {
transform: translateY(-2px);
box-shadow: 0 6px 18px rgba(255, 107, 157, 0.5);
}
.action-btn.secondary {
background: linear-gradient(180deg, #fff 0%, #ffe4ec 100%);
color: #ff6b9d;
border: 2px solid #ffb6c1;
}
.character-name { text-align: center; margin-bottom: 6px; }
.character-name input {
width: 100%;
padding: 6px 10px;
border: 2px solid #ffb6c1;
border-radius: 12px;
background: white;
color: #ff6b9d;
font-family: ‘ZCOOL KuaiLe’, cursive;
font-size: 14px;
text-align: center;
outline: none;
transition: border-color 0.3s;
}
.character-name input:focus { border-color: #ff6b9d; }
.character-name input::placeholder { color: #ffd1dc; }

.color-picker {
display: flex;
flex-wrap: wrap;
gap: 5px;
justify-content: center;
margin-top: 6px;
}
.color-option {
width: 22px;
height: 22px;
border-radius: 50%;
cursor: pointer;
border: 2px solid white;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
transition: transform 0.2s;
}
.color-option:hover { transform: scale(1.2); }
.color-option.active {
border-color: #ff6b9d;
transform: scale(1.2);
}

✨
⭐
💫
🌟
🎀

梦幻衣橱 Pro

🎀
✨ 我的衣橱 ✨
👗连衣裙
👚上衣
👖下装
👠鞋子
💇发型
💎配饰
💍首饰
🧑脸型
👁️眼型
🎨瞳色
✏️眉毛
💄妆容
✨
⭐
💫
💕
💗
💖
🌸 角色信息 🌸
当前装扮:0件
<div class="info-card"> <div class="info-card-title">🎀 当前装扮 🎀</div> <div class="outfit-list" id="outfitList"> <div class="empty-outfit">还没有选择服装哦~<br>快去衣橱看看吧!</div> </div> </div> <div class="info-card"> <div class="info-card-title">⭐ 操作 ⭐</div> <div class="action-buttons"> <button class="action-btn" onclick="randomOutfit()">🎲 随机搭配</button> <button class="action-btn secondary" onclick="resetOutfit()">🔄 重置装扮</button> <button class="action-btn secondary" onclick="saveOutfit()">💾 保存造型</button> </div> </div> <div class="info-card"> <div class="info-card-title">🎨 发色选择 🎨</div> <div class="color-picker" id="hairColorPicker"></div> </div>

四、代码运行结果

五、踩坑记录
1.图层重叠问题:最开始切换发型、衣服时,新旧装扮图层会叠加在一起,画面杂乱。解决:写JS逻辑,每次选中新装扮,先隐藏同分类全部图层,再渲染选中素材。
生成换装种类少且对服装造型定位较为死板,前几次生成的内容较不合理。
2 .材错位:部分衣服、发型素材和人物底图对不齐,出现偏移。通过调整指令让AI改变绝对定位参数,统一素材坐标,解决错位问题。
3.生成换装种类少且对服装造型定位较为死板,前几次生成的内容较不合理。

六、学习心得
本次《梦幻衣橱Pro》小游戏是一次AI辅助前端开发实践。我负责构思游戏整体功能与画面风格,由AI协助生成HTML、CSS、JS基础代码,再由我阅读代码、调试bug、修改细节。在这个过程中我学会读懂前端代码,理解图层控制、JS点击事件和CSS动画原理。我也意识到AI生成的代码不能直接使用,需要人工排查错误、调整逻辑。后续可以继续增加服饰素材,丰富游戏交互效果。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询