C语言实战:超级玛丽游戏

发布于:2025-07-19 ⋅ 阅读:(12) ⋅ 点赞:(0)

以下是一个简化版的超级玛丽游戏实现,使用C语言和SDL库。这个版本包含了基本的游戏元素:角色移动、跳跃、平台和简单的碰撞检测。

#include <stdio.h>
#include <stdbool.h>
#include <SDL2/SDL.h>

#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define GRAVITY 0.5
#define JUMP_FORCE -10
#define PLAYER_SPEED 5

typedef struct {
    float x, y;
    float velX, velY;
    int width, height;
    bool isJumping;
} Player;

typedef struct {
    float x, y;
    int width, height;
} Platform;

SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
Player player;
Platform platforms[5];
bool gameRunning = true;

void initialize() {
    SDL_Init(SDL_INIT_VIDEO);
    window = SDL_CreateWindow("Super Mario Clone", SDL_WINDOWPOS_UNDEFINED, 
                             SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    
    // 初始化玩家
    player.x = 50;
    player.y = SCREEN_HEIGHT - 100;
    player.width = 40;
    player.height = 60;
    player.velX = 0;
    player.velY = 0;
    player.isJumping = false;
    
    // 初始化平台
    platforms[0] = (Platform){0, SCREEN_HEIGHT - 40, SCREEN_WIDTH, 40}; // 地面
    platforms[1] = (Platform){100, 450, 200, 20};
    platforms[2] = (Platform){400, 350, 200, 20};
    platforms[3] = (Platform){200, 250, 200, 20};
    platforms[4] = (Platform){500, 150, 200, 20};
}

void handleInput() {
    SDL_Event event;
    while (SDL_PollEvent(&event)) {
        if (event.type == SDL_QUIT) {
            gameRunning = false;
        }
    }
    
    const Uint8* keystates = SDL_GetKeyboardState(NULL);
    
    if (keystates[SDL_SCANCODE_LEFT]) {
        player.velX = -PLAYER_SPEED;
    } else if (keystates[SDL_SCANCODE_RIGHT]) {
        player.velX = PLAYER_SPEED;
    } else {
        player.velX = 0;
    }
    
    if (keystates[SDL_SCANCODE_SPACE] && !player.isJumping) {
        player.velY = JUMP_FORCE;
        player.isJumping = true;
    }
}

bool checkCollision(float x1, float y1, int w1, int h1, float x2, float y2, int w2, int h2) {
    return (x1 < x2 + w2 && x1 + w1 > x2 && y1 < y2 + h2 && y1 + h1 > y2);
}

void update() {
    // 应用重力
    player.velY += GRAVITY;
    
    // 更新位置
    player.x += player.velX;
    player.y += player.velY;
    
    // 边界检查
    if (player.x < 0) player.x = 0;
    if (player.x > SCREEN_WIDTH - player.width) player.x = SCREEN_WIDTH - player.width;
    
    // 平台碰撞检测
    player.isJumping = true;
    for (int i = 0; i < 5; i++) {
        Platform p = platforms[i];
        if (checkCollision(player.x, player.y, player.width, player.height, p.x, p.y, p.width, p.height)) {
            // 从上方碰撞
            if (player.velY > 0 && player.y + player.height < p.y + p.height) {
                player.y = p.y - player.height;
                player.velY = 0;
                player.isJumping = false;
            }
            // 从下方碰撞
            else if (player.velY < 0) {
                player.y = p.y + p.height;
                player.velY = 0;
            }
            // 从左侧碰撞
            else if (player.velX > 0) {
                player.x = p.x - player.width;
            }
            // 从右侧碰撞
            else if (player.velX < 0) {
                player.x = p.x + p.width;
            }
        }
    }
    
    // 防止掉出屏幕底部
    if (player.y > SCREEN_HEIGHT) {
        player.y = SCREEN_HEIGHT - player.height;
        player.velY = 0;
        player.isJumping = false;
    }
}

void render() {
    SDL_SetRenderDrawColor(renderer, 135, 206, 235, 255); // 天空蓝背景
    SDL_RenderClear(renderer);
    
    // 绘制平台
    SDL_SetRenderDrawColor(renderer, 139, 69, 19, 255); // 棕色平台
    for (int i = 0; i < 5; i++) {
        SDL_Rect platformRect = {(int)platforms[i].x, (int)platforms[i].y, 
                                platforms[i].width, platforms[i].height};
        SDL_RenderFillRect(renderer, &platformRect);
    }
    
    // 绘制玩家
    SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); // 红色马里奥
    SDL_Rect playerRect = {(int)player.x, (int)player.y, player.width, player.height};
    SDL_RenderFillRect(renderer, &playerRect);
    
    SDL_RenderPresent(renderer);
}

void cleanup() {
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
}

int main(int argc, char* argv[]) {
    initialize();
    
    while (gameRunning) {
        handleInput();
        update();
        render();
        SDL_Delay(16); // 约60FPS
    }
    
    cleanup();
    return 0;
}

如何运行这个游戏

  1. 你需要安装SDL2库:

    • 在Linux上:sudo apt-get install libsdl2-dev

    • 在Mac上:brew install sdl2

    • 在Windows上:下载SDL2开发库并配置你的编译器

  2. 编译命令:

    gcc super_mario.c -o super_mario -lSDL2
  3. 运行游戏:

    ./super_mario

游戏控制

  • 左右方向键:移动马里奥

  • 空格键:跳跃

扩展建议

这是一个非常基础的版本,你可以进一步扩展:

  1. 添加图形资源(替换简单的矩形)

  2. 实现敌人和碰撞检测

  3. 添加音效和背景音乐

  4. 实现关卡系统

  5. 添加金币收集机制

  6. 实现更复杂的物理系统

希望这个基础版本能帮助你开始开发自己的超级玛丽游戏!


网站公告

今日签到

点亮在社区的每一天
去签到