简单的射击游戏

Kahaer
2天前发布
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>

// 游戏区域尺寸
#define WIDTH 50
#define HEIGHT 20
#define ENEMY_COUNT 5
#define MAX_BULLETS 10

// 颜色定义
#define BLACK 0
#define BLUE 1
#define GREEN 2
#define CYAN 3
#define RED 4
#define MAGENTA 5
#define BROWN 6
#define LIGHTGRAY 7
#define DARKGRAY 8
#define LIGHTBLUE 9
#define LIGHTGREEN 10
#define LIGHTCYAN 11
#define LIGHTRED 12
#define LIGHTMAGENTA 13
#define YELLOW 14
#define WHITE 15

// 游戏元素结构定义
typedef struct {
    int x, y;          // 位置
    char shape;        // 显示字符
    int color;         // 颜色
    int active;        // 是否活跃
} Player;

typedef struct {
    int x, y;          // 位置
    int dx, dy;        // 移动方向
    char shape;        // 显示字符
    int color;         // 颜色
    int active;        // 是否活跃
} Enemy;

typedef struct {
    int x, y;          // 位置
    int dy;            // 移动方向
    char shape;        // 显示字符
    int color;         // 颜色
    int active;        // 是否活跃
} Bullet;

// 全局变量
Player player;
Enemy enemies[ENEMY_COUNT];
Bullet bullets[MAX_BULLETS];
int score = 0;
int lives = 3;
int gameOver = 0;

// 设置控制台文本颜色
void setColor(int color) {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, color);
}

// 设置光标位置
void setCursorPosition(int x, int y) {
    COORD coord;
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

// 隐藏光标
void hideCursor() {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO cursorInfo = { 1, 0 };
    SetConsoleCursorInfo(hConsole, &cursorInfo);
}

// 初始化游戏
void initGame() {
    // 初始化玩家
    player.x = WIDTH / 2;
    player.y = HEIGHT - 2;
    player.shape = '^';
    player.color = GREEN;
    player.active = 1;

    // 初始化敌人
    srand(time(NULL));
    for (int i = 0; i < ENEMY_COUNT; i++) {
        enemies[i].x = rand() % (WIDTH - 2) + 1;
        enemies[i].y = rand() % 5 + 1;
        enemies[i].dx = (rand() % 2 == 0) ? 1 : -1;
        enemies[i].dy = 0;
        enemies[i].shape = 'V';
        enemies[i].color = RED;
        enemies[i].active = 1;
    }

    // 初始化子弹
    for (int i = 0; i < MAX_BULLETS; i++) {
        bullets[i].x = 0;
        bullets[i].y = 0;
        bullets[i].dy = -1;  // 向上移动
        bullets[i].shape = '|';
        bullets[i].color = YELLOW;
        bullets[i].active = 0;
    }

    score = 0;
    lives = 3;
    gameOver = 0;
    hideCursor();
}

// 发射子弹
void shoot() {
    // 找到第一个不活跃的子弹
    for (int i = 0; i < MAX_BULLETS; i++) {
        if (!bullets[i].active) {
            bullets[i].x = player.x;
            bullets[i].y = player.y - 1;
            bullets[i].active = 1;
            break;
        }
    }
}

// 绘制游戏元素
void drawGame() {
    // 清屏
    system("cls");

    // 绘制边界
    setColor(WHITE);
    for (int i = 0; i <= WIDTH; i++) {
        setCursorPosition(i, 0);
        printf("-");
        setCursorPosition(i, HEIGHT);
        printf("-");
    }
    for (int i = 0; i <= HEIGHT; i++) {
        setCursorPosition(0, i);
        printf("|");
        setCursorPosition(WIDTH, i);
        printf("|");
    }

    // 绘制玩家
    if (player.active) {
        setColor(player.color);
        setCursorPosition(player.x, player.y);
        printf("%c", player.shape);
    }

    // 绘制敌人
    for (int i = 0; i < ENEMY_COUNT; i++) {
        if (enemies[i].active) {
            setColor(enemies[i].color);
            setCursorPosition(enemies[i].x, enemies[i].y);
            printf("%c", enemies[i].shape);
        }
    }

    // 绘制子弹
    for (int i = 0; i < MAX_BULLETS; i++) {
        if (bullets[i].active) {
            setColor(bullets[i].color);
            setCursorPosition(bullets[i].x, bullets[i].y);
            printf("%c", bullets[i].shape);
        }
    }

    // 显示分数和生命
    setColor(YELLOW);
    setCursorPosition(2, HEIGHT + 1);
    printf("分数: %d  |  生命: %d", score, lives);

    // 重置颜色
    setColor(WHITE);
}

// 更新游戏状态
void updateGame() {
    // 移动子弹
    for (int i = 0; i < MAX_BULLETS; i++) {
        if (bullets[i].active) {
            bullets[i].y += bullets[i].dy;

            // 子弹超出屏幕范围
            if (bullets[i].y <= 1) {
                bullets[i].active = 0;
            }
        }
    }

    // 移动敌人
    for (int i = 0; i < ENEMY_COUNT; i++) {
        if (enemies[i].active) {
            enemies[i].x += enemies[i].dx;

            // 敌人碰到边界改变方向
            if (enemies[i].x <= 1 || enemies[i].x >= WIDTH - 1) {
                enemies[i].dx = -enemies[i].dx;
                enemies[i].y++;  // 向下移动一步
            }

            // 敌人到达底部
            if (enemies[i].y >= HEIGHT - 1) {
                enemies[i].active = 0;
                lives--;

                // 检查游戏是否结束
                if (lives <= 0) {
                    gameOver = 1;
                }
            }
        }
    }

    // 检测子弹击中敌人
    for (int i = 0; i < MAX_BULLETS; i++) {
        if (bullets[i].active) {
            for (int j = 0; j < ENEMY_COUNT; j++) {
                if (enemies[j].active &&
                    bullets[i].x == enemies[j].x &&
                    bullets[i].y == enemies[j].y) {

                    // 击中敌人
                    bullets[i].active = 0;
                    enemies[j].active = 0;
                    score += 10;

                    // 生成新敌人
                    enemies[j].x = rand() % (WIDTH - 2) + 1;
                    enemies[j].y = 1;
                    enemies[j].dx = (rand() % 2 == 0) ? 1 : -1;
                    enemies[j].active = 1;
                }
            }
        }
    }

    // 检测敌人是否与玩家碰撞
    for (int i = 0; i < ENEMY_COUNT; i++) {
        if (enemies[i].active &&
            enemies[i].x == player.x &&
            enemies[i].y == player.y) {

            enemies[i].active = 0;
            lives--;

            // 生成新敌人
            enemies[i].x = rand() % (WIDTH - 2) + 1;
            enemies[i].y = 1;
            enemies[i].dx = (rand() % 2 == 0) ? 1 : -1;
            enemies[i].active = 1;

            // 检查游戏是否结束
            if (lives <= 0) {
                gameOver = 1;
            }
        }
    }
}

// 处理用户输入
void handleInput() {
    if (_kbhit()) {
        char key = _getch();
        switch (key) {
        case 'a':
        case 'A':
            if (player.x > 1) {
                player.x--;
            }
            break;
        case 'd':
        case 'D':
            if (player.x < WIDTH - 1) {
                player.x++;
            }
            break;
        case ' ':  // 空格键射击
            shoot();
            break;
        case 'q':
        case 'Q':
            gameOver = 1;
            break;
        }
    }
}

// 显示游戏结束画面
void showGameOver() {
    setCursorPosition(WIDTH / 2 - 6, HEIGHT / 2);
    setColor(RED);
    printf("游戏结束!");

    setCursorPosition(WIDTH / 2 - 10, HEIGHT / 2 + 2);
    printf("最终分数: %d", score);

    setCursorPosition(WIDTH / 2 - 15, HEIGHT / 2 + 4);
    setColor(YELLOW);
    printf("按任意键退出...");

    setColor(WHITE);
    _getch();
}

// 显示游戏说明
void showInstructions() {
    system("cls");
    setColor(CYAN);
    printf("\n\n");
    printf("        射击游戏说明        \n");
    printf("---------------------------\n");
    printf("使用 A 和 D 键左右移动\n");
    printf("按空格键发射子弹\n");
    printf("击中敌人获得分数\n");
    printf("敌人到达底部或碰到你会减少生命\n");
    printf("生命耗尽游戏结束\n");
    printf("按 Q 键退出游戏\n");
    printf("\n");
    printf("        按任意键开始        \n");
    setColor(WHITE);
    _getch();
}

// 主函数
int main() {
    showInstructions();
    initGame();

    while (!gameOver) {
        drawGame();
        handleInput();
        updateGame();
        Sleep(80);  // 控制游戏速度
    }

    showGameOver();
    return 0;
}
© 版权声明
THE END
喜欢就支持一下吧
点赞 0 分享 收藏
评论 抢沙发
OωO
取消