Skip to content
TheChatArchiveThe Chat Archive
Geminigemini-2.5-flash· GoogleHugging Face LMArena Human Preference 140K

使用html搭建一个视觉小说游戏框架

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Googlegemma-3-27b-it’s answer.
  1. User

    使用html搭建一个视觉小说游戏框架

  2. Gemini Assistant

    好的,我们将使用HTML、CSS和JavaScript来搭建一个基本的视觉小说游戏框架。这个框架将包含:

    1. 背景:显示当前场景的图片。
    2. 角色立绘:显示当前说话的角色图片。
    3. 文本框:显示角色名字和对话/叙述文本。
    4. 选项:在故事关键点提供选择。
    5. 核心逻辑:通过JavaScript控制故事流程、场景切换、角色显示和文本更新。

    文件结构

    vn-game/
    ├── index.html
    ├── style.css
    ├── script.js
    └── assets/
        ├── backgrounds/
        │   ├── bg_park.jpg
        │   └── bg_classroom.jpg
        └── characters/
            ├── char_alice_normal.png
            ├── char_bob_happy.png
            └── char_bob_sad.png
    

    请准备好一些图片,放到 assets/backgrounds/ 和 assets/characters/ 文件夹中。


    1. index.html (HTML 结构)

    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>我的视觉小说</title>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <div id="game-container">
            <!-- 背景图片 -->
            <img id="background" src="" alt="场景背景">
    
            <!-- 角色立绘容器 -->
            <div id="character-container">
                <!-- 角色立绘将通过JS动态添加/修改 -->
                <img id="character-sprite" src="" alt="角色立绘" class="character-hidden">
            </div>
    
            <!-- 对话框 -->
            <div id="text-box">
                <div id="name-display"></div>
                <div id="dialogue-text">点击或按空格键继续...</div>
            </div>
    
            <!-- 选项容器 -->
            <div id="choices-container">
                <!-- 选项按钮将通过JS动态添加 -->
            </div>
        </div>
    
        <script src="script.js"></script>
    </body>
    </html>
    

    2. style.css (CSS 样式)

    body {
        margin: 0;
        overflow: hidden; /* 防止滚动条 */
        display: flex;
        justify-content: center;
        align-items: center;
        min-height: 100vh;
        background-color: #333;
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        color: #fff;
    }
    
    #game-container {
        width: 1280px; /* 游戏分辨率宽度 */
        height: 720px; /* 游戏分辨率高度 */
        position: relative;
        border: 2px solid #555;
        box-shadow: 0 0 20px rgba(0, 0, 0, 0.7);
        background-color: #000; /* 默认黑色背景 */
        overflow: hidden; /* 确保内容不溢出 */
    }
    
    /* 背景图片 */
    #background {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        object-fit: cover; /* 覆盖整个容器 */
        transition: opacity 1s ease-in-out; /* 背景切换动画 */
    }
    
    /* 角色立绘容器 */
    #character-container {
        position: absolute;
        bottom: 0;
        width: 100%;
        height: 80%; /* 角色立绘占据大部分高度 */
        display: flex;
        justify-content: center; /* 角色居中显示 */
        align-items: flex-end; /* 角色底部对齐 */
        pointer-events: none; /* 允许点击穿透到背景 */
    }
    
    /* 角色立绘 */
    #character-sprite {
        max-height: 100%; /* 确保角色立绘不超过容器 */
        width: auto;
        object-fit: contain;
        position: absolute;
        bottom: 0;
        /* 初始隐藏 */
        opacity: 0;
        transform: translateY(20px);
        transition: opacity 0.5s ease-out, transform 0.5s ease-out; /* 角色出现动画 */
    }
    
    .character-visible {
        opacity: 1 !important;
        transform: translateY(0) !important;
    }
    .character-hidden {
        opacity: 0 !important;
        transform: translateY(20px) !important;
    }
    
    
    /* 对话框 */
    #text-box {
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        min-height: 150px; /* 对话框高度 */
        background: rgba(0, 0, 0, 0.7); /* 半透明黑色背景 */
        padding: 20px;
        box-sizing: border-box;
        border-top: 2px solid rgba(255, 255, 255, 0.2);
        cursor: pointer; /* 表示可以点击 */
        user-select: none; /* 防止文本被选中 */
        z-index: 10; /* 确保在最上层 */
    }
    
    #name-display {
        font-size: 1.5em;
        font-weight: bold;
        color: #ffd700; /* 名字颜色 */
        margin-bottom: 10px;
        background: rgba(50, 50, 50, 0.8);
        padding: 5px 15px;
        display: inline-block;
        border-radius: 5px;
    }
    
    #dialogue-text {
        font-size: 1.2em;
        line-height: 1.6;
        color: #eee;
        white-space: pre-wrap; /* 保留空白和换行 */
    }
    
    /* 选项容器 */
    #choices-container {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%); /* 居中 */
        display: flex;
        flex-direction: column; /* 垂直排列 */
        gap: 15px; /* 选项间距 */
        width: 80%;
        max-width: 600px;
        z-index: 20; /* 在对话框之上 */
    }
    
    .choice-button {
        background-color: rgba(0, 0, 0, 0.8);
        color: #fff;
        border: 2px solid #555;
        padding: 15px 25px;
        font-size: 1.3em;
        cursor: pointer;
        border-radius: 8px;
        transition: background-color 0.3s ease, border-color 0.3s ease;
    }
    
    .choice-button:hover {
        background-color: rgba(70, 70, 70, 0.9);
        border-color: #ffd700;
    }
    
    /* 隐藏元素 */
    .hidden {
        display: none !important;
    }
    
    /* 文本打字效果辅助类 */
    .typing-finished #dialogue-text {
        white-space: pre-wrap; /* 确保显示所有文本 */
    }
    

    3. script.js (JavaScript 逻辑)

    // --- DOM 元素引用 ---
    const gameContainer = document.getElementById('game-container');
    const backgroundElement = document.getElementById('background');
    const characterSprite = document.getElementById('character-sprite');
    const nameDisplay = document.getElementById('name-display');
    const dialogueText = document.getElementById('dialogue-text');
    const choicesContainer = document.getElementById('choices-container');
    const textBox = document.getElementById('text-box');
    
    // --- 游戏状态变量 ---
    let currentSceneIndex = 0;
    let currentDialogueIndex = 0;
    let isTyping = false; // 是否正在打字
    let canAdvance = true; // 是否可以推进对话(用于控制点击)
    let typeInterval = 30; // 打字速度,毫秒
    
    // --- 故事数据 (核心) ---
    // 这是一个数组,每个元素代表一个场景
    const story = [
        // 场景 0: 公园
        {
            background: 'assets/backgrounds/bg_park.jpg',
            dialogues: [
                { name: '', text: '这是一个阳光明媚的下午,你在公园里散步。', character: { hide: true } },
                { name: '爱丽丝', text: '(突然,一个女孩出现在你面前)', character: { name: '爱丽丝', sprite: 'assets/characters/char_alice_normal.png' } },
                { name: '爱丽丝', text: '你好!我叫爱丽丝,很高兴见到你!', character: { name: '爱丽丝', sprite: 'assets/characters/char_alice_normal.png' } },
                { name: '', text: '你该如何回应呢?', character: { hide: true } },
                {
                    name: '',
                    text: '',
                    character: { hide: true },
                    choices: [
                        { text: '友善地打招呼', nextScene: 1, nextDialogue: 0 }, // 跳转到场景1,对话0
                        { text: '害羞地躲开', nextScene: 2, nextDialogue: 0 }  // 跳转到场景2,对话0
                    ]
                }
            ]
        },
        // 场景 1: 友善回应后的公园
        {
            background: 'assets/backgrounds/bg_park.jpg',
            dialogues: [
                { name: '你', text: '(你微笑着回应)你好,爱丽丝!', character: { hide: true } },
                { name: '爱丽丝', text: '太好了!你看起来很友好!', character: { name: '爱丽丝', sprite: 'assets/characters/char_alice_normal.png' } },
                { name: '', text: '你们聊了一会儿,感觉很愉快。', character: { hide: true } },
                { name: '爱丽丝', text: '时间不早了,我该走了。希望下次再见!', character: { name: '爱丽丝', sprite: 'assets/characters/char_alice_normal.png' } },
                { name: '', text: '爱丽丝离开了。你继续你的散步。', character: { hide: true } },
                { name: '', text: '(故事到此结束,你可以继续添加更多内容)', character: { hide: true } }
            ]
        },
        // 场景 2: 害羞躲开后的教室 (这里背景故意换一个,展示切换效果)
        {
            background: 'assets/backgrounds/bg_classroom.jpg', // 注意背景已切换
            dialogues: [
                { name: '你', text: '(你害羞地躲开了,爱丽丝看起来有些困惑。)', character: { hide: true } },
                { name: '爱丽丝', text: '呃...好吧,看来你比较内向呢。', character: { name: '爱丽丝', sprite: 'assets/characters/char_alice_normal.png' } },
                { name: '', text: '爱丽丝离开了,你感觉有些后悔。', character: { hide: true } },
                { name: '', text: '(你回到了教室,想着刚才的相遇...)', character: { hide: true } },
                { name: '', text: '(故事到此结束,但你可以想象之后发生的事情)', character: { hide: true } }
            ]
        }
        // 可以继续添加更多场景...
    ];
    
    // --- 游戏启动函数 ---
    function startGame() {
        loadScene(currentSceneIndex);
        // 添加点击事件监听器
        gameContainer.addEventListener('click', handleAdvance);
        // 添加键盘事件监听器 (空格键)
        document.addEventListener('keydown', handleKeyPress);
    }
    
    // --- 加载场景 ---
    function loadScene(sceneIndex) {
        if (sceneIndex < 0 || sceneIndex >= story.length) {
            console.error('无效的场景索引:', sceneIndex);
            return;
        }
        currentSceneIndex = sceneIndex;
        currentDialogueIndex = 0; // 重置对话索引
    
        const scene = story[currentSceneIndex];
        
        // 淡出当前背景
        backgroundElement.style.opacity = 0;
        setTimeout(() => {
            backgroundElement.src = scene.background;
            backgroundElement.style.opacity = 1; // 淡入新背景
            displayNextDialogue(); // 显示场景的第一个对话
        }, 1000); // 等待1秒让旧背景淡出
    }
    
    // --- 显示下一行对话 ---
    function displayNextDialogue() {
        hideChoices(); // 确保选项被隐藏
    
        const scene = story[currentSceneIndex];
        if (currentDialogueIndex >= scene.dialogues.length) {
            // 如果没有更多对话,可能是场景结束或需要跳转
            console.log('当前场景对话已结束。');
            // 可以在这里添加游戏结束逻辑,或者跳转到下一个默认场景
            return;
        }
    
        const dialogue = scene.dialogues[currentDialogueIndex];
    
        // 处理角色立绘显示/隐藏
        updateCharacterSprite(dialogue.character);
    
        // 处理对话文本和名字
        if (dialogue.name) {
            nameDisplay.textContent = dialogue.name;
            nameDisplay.classList.remove('hidden');
        } else {
            nameDisplay.textContent = '';
            nameDisplay.classList.add('hidden'); // 隐藏名字显示框
        }
    
        // 打字效果
        if (dialogue.text) {
            typeText(dialogue.text, () => {
                // 打字效果完成后,检查是否有选项
                if (dialogue.choices && dialogue.choices.length > 0) {
                    showChoices(dialogue.choices);
                } else {
                    canAdvance = true; // 可以点击或按键推进
                }
            });
        } else {
            // 如果没有文本,可能是纯选项或纯角色动作
            canAdvance = true;
            if (dialogue.choices && dialogue.choices.length > 0) {
                showChoices(dialogue.choices);
            }
        }
    }
    
    // --- 更新角色立绘 ---
    function updateCharacterSprite(characterData) {
        if (characterData && characterData.sprite) {
            characterSprite.src = characterData.sprite;
            characterSprite.alt = characterData.name || '角色立绘';
            characterSprite.classList.remove('character-hidden');
            characterSprite.classList.add('character-visible');
        } else {
            // 隐藏角色
            characterSprite.classList.remove('character-visible');
            characterSprite.classList.add('character-hidden');
        }
    }
    
    // --- 显示选项 ---
    function showChoices(choices) {
        canAdvance = false; // 有选项时不能点击推进对话
        choicesContainer.classList.remove('hidden');
        choicesContainer.innerHTML = ''; // 清空旧选项
    
        choices.forEach((choice, index) => {
            const button = document.createElement('button');
            button.classList.add('choice-button');
            button.textContent = choice.text;
            button.addEventListener('click', () => handleChoice(choice));
            choicesContainer.appendChild(button);
        });
    }
    
    // --- 隐藏选项 ---
    function hideChoices() {
        choicesContainer.classList.add('hidden');
        choicesContainer.innerHTML = '';
    }
    
    // --- 处理选项点击 ---
    function handleChoice(choice) {
        hideChoices();
        // 根据选项跳转到新的场景或新的对话
        if (choice.nextScene !== undefined) {
            loadScene(choice.nextScene);
            if (choice.nextDialogue !== undefined) {
                currentDialogueIndex = choice.nextDialogue; // 设置跳转后的起始对话
            }
        } else if (choice.nextDialogue !== undefined) {
            currentDialogueIndex = choice.nextDialogue;
            displayNextDialogue();
        } else {
            // 如果没有指定跳转,则继续当前场景的下一句
            currentDialogueIndex++;
            displayNextDialogue();
        }
        canAdvance = true; // 选项选择后可以继续推进
    }
    
    // --- 打字效果函数 ---
    function typeText(text, callback) {
        isTyping = true;
        dialogueText.textContent = ''; // 清空当前文本
        let i = 0;
        const typeWriter = () => {
            if (i < text.length) {
                dialogueText.textContent += text.charAt(i);
                i++;
                setTimeout(typeWriter, typeInterval);
            } else {
                isTyping = false;
                if (callback) callback();
            }
        };
        typeWriter();
    }
    
    // --- 推进对话 (点击或按键) ---
    function handleAdvance() {
        if (isTyping) {
            // 如果正在打字,点击则直接显示完整文本
            dialogueText.textContent = story[currentSceneIndex].dialogues[currentDialogueIndex].text;
            isTyping = false;
            // 如果打字完成的对话有选项,则显示选项
            if (story[currentSceneIndex].dialogues[currentDialogueIndex].choices) {
                showChoices(story[currentSceneIndex].dialogues[currentDialogueIndex].choices);
            } else {
                 canAdvance = true; // 确保打字快进后能继续推进
            }
        } else if (canAdvance) {
            // 如果不在打字且可以推进
            currentDialogueIndex++;
            displayNextDialogue();
        }
    }
    
    // --- 键盘事件处理 ---
    function handleKeyPress(event) {
        if (event.code === 'Space' || event.key === 'Enter') {
            event.preventDefault(); // 阻止默认行为(如滚动页面)
            handleAdvance();
        }
    }
    
    // --- 游戏初始化 ---
    document.addEventListener('DOMContentLoaded', startGame);
    

    如何运行:

    1. 创建一个名为 vn-game 的文件夹。
    2. 在 vn-game 文件夹中创建 index.html、style.css 和 script.js 文件,并将上述代码分别复制粘贴进去。
    3. 创建 assets 文件夹,并在其中创建 backgrounds 和 characters 子文件夹。
    4. 下载或创建一些图片:
      • bg_park.jpg 和 bg_classroom.jpg 放到 assets/backgrounds/
      • char_alice_normal.png、char_bob_happy.png 和 char_bob_sad.png 放到 assets/characters/ (确保图片路径与 script.js 中的一致)
      • 重要提示: 角色立绘最好是PNG格式,带有透明背景,这样它们可以叠加在背景上。
    5. 用浏览器打开 index.html 文件。

    现在你应该能看到一个带有文本框和背景的简单视觉小说游戏。点击文本框或按下空格键,故事就会推进。当出现选择时,点击对应的按钮即可。

    框架特点与可扩展性:

    • 分离结构、样式和逻辑:清晰的HTML、CSS、JavaScript分离,易于维护。
    • 响应式基础:game-container 的固定尺寸,但可以通过CSS的vw/vh或max-width/height进行调整以适应不同屏幕。
    • 故事数据驱动:所有的故事内容都存储在 story 数组中,易于编辑和扩展。
    • 打字效果:提供了一个简单的文本打字效果。
    • 场景和对话切换:通过 currentSceneIndex 和 currentDialogueIndex 控制流程。
    • 角色立绘管理:可以根据对话内容显示/隐藏或切换角色立绘。
    • 选项功能:支持多选项并根据选择跳转到不同的故事分支。
    • 基础动画:背景的淡入淡出和角色立绘的出现动画。

    进一步改进和扩展:

    1. 更复杂的角色管理:
      • 允许多个角色同时出现在屏幕上,并可以控制他们的位置(左、中、右)。
      • 角色表情变化。
      • 角色入场/退场动画。
    2. 音效/音乐:
      • 根据场景或对话播放背景音乐。
      • 播放角色的语音或特殊音效。
    3. 更丰富的文本效果:
      • 文本颜色、字体变化。
      • 文本特效(抖动、闪烁)。
    4. 保存/加载功能:保存玩家的游戏进度。
    5. 菜单系统:开始游戏、选项、退出等。
    6. 更精细的动画控制:使用CSS类或JS库(如GSAP)进行更复杂的过渡效果。
    7. 变量和条件判断:在JS中引入变量,根据玩家的选择或达成某些条件来改变故事走向。
    8. 旁白/内心独白模式:文本框样式变化。
    9. 全屏模式:提供切换全屏的按钮。
    10. 预加载资源:在游戏开始前加载所有图片和音频,避免加载延迟。
    11. 更强大的故事解析器:可以从JSON文件加载故事数据,而不是直接硬编码在JS中。

    这个框架为你的视觉小说游戏提供了一个坚实的基础,你可以根据自己的需求在此之上进行深度开发。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.