You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在SFML版Doodle Jump游戏中添加游戏结束功能?

给Doodle Jump添加游戏结束功能的实现方案

核心问题分析

你当前代码里的if (y > 500) dy = -10;是强制让角色反弹,所以无论怎么掉落都会被拉回,永远不会触发游戏结束。需要替换这个逻辑,改成检测角色是否真正掉出屏幕底部,再触发游戏结束状态。

具体修改步骤

  • 添加游戏状态变量标记游戏是否结束
  • 替换强制反弹逻辑,改为检测角色掉落出屏幕的条件
  • 游戏结束时暂停核心玩法逻辑,显示结束提示
  • 可选添加按回车重启游戏的功能

修改后的完整代码

#include <SFML/Graphics.hpp>
#include <time.h>
using namespace sf;

struct point
{
    int x, y;
};

// 重置游戏状态,方便重启
void resetGame(int& x, int& y, int& h, float& dx, float& dy, point plat[]) {
    x = 100;
    y = 100;
    h = 200;
    dx = 0;
    dy = 0;
    for (int i = 0; i < 10; i++) {
        plat[i].x = rand() % 400;
        plat[i].y = rand() % 533;
    }
}

int main()
{
    srand(time(0));

    RenderWindow app(VideoMode(400, 533), "Doodle Game!");
    app.setFramerateLimit(60);

    Texture t1, t2, t3;
    t1.loadFromFile("C:/Users/Dani/Documents/GAMES SFML/16_Games/02 Doodle Jump/images/background.png");
    t2.loadFromFile("C:/Users/Dani/Documents/GAMES SFML/16_Games/02 Doodle Jump/images/platform.png");
    t3.loadFromFile("C:/Users/Dani/Documents/GAMES SFML/16_Games/02 Doodle Jump/images/mark.png");

    Sprite sBackground(t1), sPlat(t2), sPers(t3);

    // 游戏结束状态标记
    bool gameOver = false;
    // 准备游戏结束文本(替换字体路径为你本地存在的字体)
    Font font;
    font.loadFromFile("arial.ttf");
    Text gameOverText("GAME OVER!", font, 40);
    gameOverText.setFillColor(Color::Red);
    gameOverText.setPosition(40, 200);
    Text restartText("Press ENTER to restart", font, 20);
    restartText.setFillColor(Color::White);
    restartText.setPosition(70, 260);

    point plat[20];
    int x = 100, y = 100, h = 200;
    float dx = 0, dy = 0;
    resetGame(x, y, h, dx, dy, plat);

    while (app.isOpen())
    {
        Event e;
        while (app.pollEvent(e))
        {
            if (e.type == Event::Closed)
                app.close();
            // 游戏结束时按回车重启
            if (e.type == Event::KeyPressed && e.key.code == Keyboard::Enter && gameOver) {
                gameOver = false;
                resetGame(x, y, h, dx, dy, plat);
            }
        }

        if (!gameOver) {
            // 玩家移动逻辑
            if (Keyboard::isKeyPressed(Keyboard::Right)) x += 3;        
            if (Keyboard::isKeyPressed(Keyboard::Left)) x -= 3;

            // 重力与位置更新
            dy += 0.2;
            y += dy;

            // 检测角色是否掉出屏幕底部(70为角色精灵高度,可根据实际调整)
            if (y > 533 + 70) {
                gameOver = true;
            }

            // 屏幕滚动逻辑
            if (y < h)
                for (int i = 0; i < 10; i++)    
                {
                    y = h;
                    plat[i].y = plat[i].y - dy;
                    if (plat[i].y > 533) { plat[i].y = 0; plat[i].x = rand() % 400; }
                }

            // 平台碰撞检测
            for (int i = 0; i < 10; i++)
                if ((x + 50 > plat[i].x) && (x + 20 < plat[i].x + 68)
                    && (y + 70 > plat[i].y) && (y + 70 < plat[i].y + 14) && (dy > 0))  dy = -10;

            // 限制角色左右边界,防止移出屏幕
            if (x < 0) x = 0;
            if (x > 400 - 70) x = 400 - 70;

            sPers.setPosition(x, y);
        }

        // 绘制内容
        app.draw(sBackground);
        if (!gameOver) {
            app.draw(sPers);
            for (int i = 0; i < 10; i++)    
            {
                sPlat.setPosition(plat[i].x, plat[i].y);
                app.draw(sPlat);
            }
        } else {
            // 绘制游戏结束提示
            app.draw(gameOverText);
            app.draw(restartText);
        }

        app.display();
    }

    return 0;
}

修改点说明

  1. 游戏状态控制:新增gameOver变量,游戏结束后暂停玩家移动、重力等核心逻辑
  2. 掉落检测逻辑:移除强制反弹代码,改为检测角色是否完全掉出屏幕底部,触发游戏结束
  3. 结束交互:添加游戏结束文本提示,支持按回车键重置游戏状态
  4. 边界优化:新增角色左右移动边界限制,避免角色移出屏幕

内容的提问来源于stack exchange,提问作者DANNY FRIALDE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 13:05:19