c语言 是男人就下100层

99ANYc3cd6
预计阅读时长 38 分钟
位置: 首页 C语言 正文

由于在纯文本环境中无法直接运行图形程序,我将为你提供一个完整的、基于Windows控制台的版本,这个版本使用conio.h库来实现简单的控制台图形和键盘输入,非常适合学习和快速上手。

c语言 是男人就下100层
(图片来源网络,侵删)

之后,我会再为你提供一个进阶版的思路,使用更现代的图形库(如 EasyXSDL)来实现真正的图形界面版本。


第一部分:基于Windows控制台的C语言版本

这个版本将使用 conio.h 库,它提供了在Windows控制台中进行键盘输入和屏幕操作的功能。

准备工作

你需要一个支持 conio.h 的C语言编译环境,Visual Studio 或 MinGW。

完整代码

将以下代码保存为 climb_down.c 文件。

c语言 是男人就下100层
(图片来源网络,侵删)
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>   // 用于控制台输入输出 (getch(), gotoxy())
#include <windows.h> // 用于 Sleep() 函数
#include <time.h>    // 用于 srand(), rand()
// --- 游戏常量定义 ---
#define SCREEN_WIDTH  40
#define SCREEN_HEIGHT 20
#define PLAYER_CHAR   '@'
#define PLATFORM_CHAR '='
#define PLATFORM_GAP  4 // 平台之间的最小垂直间隔
// --- 游戏状态结构体 ---
typedef struct {
    int x;
    int y;
    int velocity_y;
} Player;
typedef struct {
    int x;
    int y;
    int width;
} Platform;
// --- 全局变量 ---
Player player;
Platform platforms[10]; // 最多同时存在10个平台
int score = 0;
int game_over = 0;
// --- 函数声明 ---
void initGame();
void drawScreen();
void updatePlayer();
void updatePlatforms();
void handleInput();
void gotoxy(int x, int y);
// --- 主函数 ---
int main() {
    // 设置控制台窗口大小
    system("mode con: cols=41 lines=21");
    system("title 是男人就下100层 - C语言版");
    initGame();
    srand((unsigned int)time(NULL)); // 初始化随机数种子
    while (!game_over) {
        drawScreen();
        handleInput();
        updatePlayer();
        updatePlatforms();
        Sleep(50); // 控制游戏速度
    }
    // 游戏结束画面
    system("cls");
    gotoxy(SCREEN_WIDTH / 2 - 8, SCREEN_HEIGHT / 2);
    printf("游戏结束! 最终得分: %d", score);
    gotoxy(SCREEN_WIDTH / 2 - 8, SCREEN_HEIGHT / 2 + 1);
    printf("按任意键退出...");
    _getch();
    return 0;
}
// --- 游戏初始化 ---
void initGame() {
    // 初始化玩家
    player.x = SCREEN_WIDTH / 2;
    player.y = 2;
    player.velocity_y = 0;
    // 初始化平台
    for (int i = 0; i < 10; i++) {
        platforms[i].x = rand() % (SCREEN_WIDTH - 10); // 随机x位置
        platforms[i].y = i * 3 + 5; // 垂直分布
        platforms[i].width = 5 + rand() % 6; // 随机宽度
    }
}
// --- 绘制屏幕 ---
void drawScreen() {
    gotoxy(0, 0);
    // 绘制顶部边界
    for (int i = 0; i < SCREEN_WIDTH; i++) printf("#");
    printf("\n");
    // 绘制游戏区域
    for (int y = 1; y < SCREEN_HEIGHT; y++) {
        for (int x = 0; x < SCREEN_WIDTH; x++) {
            int is_player = (x == player.x && y == player.y);
            int is_platform = 0;
            for (int i = 0; i < 10; i++) {
                if (y == platforms[i].y && x >= platforms[i].x && x < platforms[i].x + platforms[i].width) {
                    is_platform = 1;
                    break;
                }
            }
            if (is_player) {
                printf("%c", PLAYER_CHAR);
            } else if (is_platform) {
                printf("%c", PLATFORM_CHAR);
            } else {
                printf(" "); // 空格
            }
        }
        printf("\n");
    }
    // 绘制底部边界
    for (int i = 0; i < SCREEN_WIDTH; i++) printf("#");
    printf("\n");
    // 显示分数
    gotoxy(0, SCREEN_HEIGHT + 1);
    printf("得分: %d", score);
}
// --- 更新玩家状态 ---
void updatePlayer() {
    // 应用重力
    player.velocity_y++;
    player.y += player.velocity_y;
    // 检查是否掉出屏幕底部
    if (player.y >= SCREEN_HEIGHT) {
        game_over = 1;
        return;
    }
    // 检查是否踩到平台
    int on_platform = 0;
    for (int i = 0; i < 10; i++) {
        if (player.y + 1 == platforms[i].y &&
            player.x >= platforms[i].x &&
            player.x < platforms[i].x + platforms[i].width) {
            on_platform = 1;
            player.y = platforms[i].y - 1; // 站在平台上
            player.velocity_y = 0; // 重置垂直速度
            break;
        }
    }
}
// --- 更新平台位置 ---
void updatePlatforms() {
    for (int i = 0; i < 10; i++) {
        // 平台向下移动,模拟玩家下落
        platforms[i].y++;
        // 如果平台移出屏幕底部,就重置到顶部
        if (platforms[i].y > SCREEN_HEIGHT) {
            platforms[i].y = 0;
            platforms[i].x = rand() % (SCREEN_WIDTH - 10);
            platforms[i].width = 5 + rand() % 6;
            score++; // 成功下落一层,加分
        }
    }
}
// --- 处理用户输入 ---
void handleInput() {
    if (_kbhit()) { // 检查是否有键盘输入
        char key = _getch();
        if (key == 'a' || key == 'A' || key == 75) { // 左箭头或A
            if (player.x > 0) player.x--;
        }
        if (key == 'd' || key == 'D' || key == 77) { // 右箭头或D
            if (player.x < SCREEN_WIDTH - 1) player.x++;
        }
        if (key == ' ' || key == 87) { // 空格或上箭头,用于跳跃
            // 只有在平台上时才能跳
            int on_platform = 0;
            for (int i = 0; i < 10; i++) {
                if (player.y + 1 == platforms[i].y &&
                    player.x >= platforms[i].x &&
                    player.x < platforms[i].x + platforms[i].width) {
                    on_platform = 1;
                    break;
                }
            }
            if (on_platform) {
                player.velocity_y = -1; // 给一个向上的速度
            }
        }
    }
}
// --- 控制台光标定位 ---
void gotoxy(int x, int y) {
    COORD coord;
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

如何编译和运行

  1. 保存代码:将上面的代码保存为 climb_down.c
  2. 打开命令提示符:按下 Win + R,输入 cmd 并回车。
  3. 编译代码
    • 如果你安装了 MinGW (GCC for Windows),在 climb_down.c 所在目录下运行:
      gcc climb_down.c -o climb_down.exe
    • 如果你使用的是 Visual Studio,创建一个“控制台应用”项目,并将代码粘贴进去,然后生成运行。
  4. 运行游戏:在命令提示符中输入:
    climb_down.exe

游戏玩法说明

  • 目标:控制 符号不断下落,不要掉出屏幕底部。
  • 控制
    • A / ← : 向左移动
    • D / → : 向右移动
    • W / ↑ / 空格 : 跳跃(只有站在平台上才能跳)
  • 得分:每成功下落一层(即一个平台移出屏幕),得分加1。

第二部分:进阶版 - 使用图形库(EasyX)

如果你想要一个拥有真正图形界面的版本,可以使用 EasyX 图形库,它非常适合C/C++初学者,可以让你轻松地绘制图片、处理鼠标键盘事件。

准备工作

  1. 安装 EasyX:访问 EasyX 官网 下载并安装。
  2. 修改代码:将上面的控制台版代码改造一下。

EasyX 版代码示例

这个版本将不再使用 gotoxyconio.h,而是使用 EasyX 的绘图函数。

#include <graphics.h> // EasyX 图形库头文件
#include <conio.h>    // 用于 _kbhit() 和 _getch()
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h> // 用于 Sleep()
// --- 游戏常量定义 ---
#define SCREEN_WIDTH  600
#define SCREEN_HEIGHT 800
#define PLAYER_SIZE   20
#define PLATFORM_HEIGHT 15
#define PLATFORM_GAP  100
// --- 游戏状态结构体 ---
typedef struct {
    int x;
    int y;
    int velocity_y;
} Player;
typedef struct {
    int x;
    int y;
    int width;
} Platform;
// --- 全局变量 ---
Player player;
Platform platforms[10];
int score = 0;
int game_over = 0;
// --- 函数声明 ---
void initGame();
void updateGame();
void drawGame();
void handleInput();
// --- 主函数 ---
int main() {
    // 初始化图形窗口
    initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);
    srand((unsigned int)time(NULL));
    initGame();
    // 游戏主循环
    while (!game_over) {
        beginBatch(); // 开始批量绘制,提高性能
        drawGame();
        updateGame();
        handleInput();
        endBatch();   // 结束批量绘制
        Sleep(20);    // 控制游戏帧率
    }
    // 游戏结束
    TCHAR s[100];
    _stprintf(s, _T("游戏结束! 最终得分: %d"), score);
    outtextxy(SCREEN_WIDTH / 2 - 100, SCREEN_HEIGHT / 2, s);
    _getch(); // 等待按键
    closegraph(); // 关闭图形窗口
    return 0;
}
void initGame() {
    player.x = SCREEN_WIDTH / 2;
    player.y = 100;
    player.velocity_y = 0;
    for (int i = 0; i < 10; i++) {
        platforms[i].x = rand() % (SCREEN_WIDTH - 100);
        platforms[i].y = i * PLATFORM_GAP + 200;
        platforms[i].width = 80 + rand() % 70;
    }
}
void drawGame() {
    // 清屏
    cleardevice();
    // 绘制玩家
    solidcircle(player.x, player.y, PLAYER_SIZE / 2);
    // 绘制平台
    for (int i = 0; i < 10; i++) {
        solidrectangle(platforms[i].x, platforms[i].y, 
                       platforms[i].x + platforms[i].width, 
                       platforms[i].y + PLATFORM_HEIGHT);
    }
    // 绘制分数
    TCHAR score_str[20];
    _stprintf(score_str, _T("Score: %d"), score);
    outtextxy(10, 10, score_str);
}
void updateGame() {
    // 应用重力
    player.velocity_y += 1;
    player.y += player.velocity_y;
    // 检查是否掉出屏幕
    if (player.y > SCREEN_HEIGHT + PLAYER_SIZE) {
        game_over = 1;
    }
    // 检查是否踩到平台
    for (int i = 0; i < 10; i++) {
        if (player.velocity_y > 0 && // 玩家正在下落
            player.y + PLAYER_SIZE / 2 >= platforms[i].y &&
            player.y - PLAYER_SIZE / 2 <= platforms[i].y + PLATFORM_HEIGHT &&
            player.x + PLAYER_SIZE / 2 >= platforms[i].x &&
            player.x - PLAYER_SIZE / 2 <= platforms[i].x + platforms[i].width) {
            player.y = platforms[i].y - PLAYER_SIZE / 2; // 站在平台上
            player.velocity_y = 0; // 重置速度
        }
    }
    // 移动平台
    for (int i = 0; i < 10; i++) {
        platforms[i].y += 3; // 平台下落速度
        if (platforms[i].y > SCREEN_HEIGHT) {
            platforms[i].y = -PLATFORM_HEIGHT;
            platforms[i].x = rand() % (SCREEN_WIDTH - platforms[i].width);
            score++;
        }
    }
}
void handleInput() {
    if (_kbhit()) {
        char key = _getch();
        if (key == 'a' || key == 'A') {
            player.x -= 10;
        }
        if (key == 'd' || key == 'D') {
            player.x += 10;
        }
        if (key == ' ' || key == 'w' || key == 'W') {
            // 简单的跳跃逻辑,可以优化为必须站在平台上才能跳
            if (player.velocity_y == 0 || player.velocity_y > 0) {
                 player.velocity_y = -15;
            }
        }
    }
    // 限制玩家不能移出屏幕
    if (player.x < PLAYER_SIZE / 2) player.x = PLAYER_SIZE / 2;
    if (player.x > SCREEN_WIDTH - PLAYER_SIZE / 2) player.x = SCREEN_WIDTH - PLAYER_SIZE / 2;
}

EasyX 版如何编译和运行

  1. 安装 EasyX:确保你已经按照官网教程安装好。
  2. 保存代码:将代码保存为 climb_down_easyx.c
  3. 编译:在支持 EasyX 的IDE(如 Visual Studio)中编译,或者使用 MinGW 的命令行:
    g++ climb_down_easyx.c -o climb_down_easyx.exe -mwindows
  4. 运行:双击运行生成的 .exe 文件。

这个图形版本会弹出一个窗口,视觉效果和控制体验会比控制台版好很多。

希望这个详细的教程能帮助你成功用C语言制作出“是男人就下100层”!从控制台版开始,理解了核心逻辑后,再尝试图形版,会是一个非常好的学习过程,祝你编码愉快!

c语言 是男人就下100层
(图片来源网络,侵删)
-- 展开阅读全文 --
头像
织梦友情链接底部版权如何正确设置?
« 上一篇 03-08
const在C语言中究竟是什么意思?
下一篇 » 前天

相关文章

取消
微信二维码
支付宝二维码

目录[+]