📜  简单的 C++ 游戏代码 - C++ (1)

📅  最后修改于: 2023-12-03 15:41:08.921000             🧑  作者: Mango

简单的 C++ 游戏代码

介绍

这里提供一些简单的C++游戏代码。这些代码演示了一些基本的游戏编程概念,如游戏循环,输入处理和游戏物理。这些代码只是用作示例,您可以通过修改它们来创建您自己的游戏。

代码
Pong

下面是一个简单的Pong游戏示例,使用Windows API来绘制游戏场景和处理输入。游戏目标是击败对手,以获得更多的分数。您可以通过移动板子来控制球的弹跳方向。

#include <iostream>
#include <Windows.h>
using namespace std;

const int WIDTH = 60;  // 游戏场景宽
const int HEIGHT = 20; // 游戏场景高
const int PADDLE_SIZE = 6; // 球拍大小
const int BALL_SPEED = 1; // 球速

struct Point {
    int x, y;
};

// 绘制游戏场景
void drawScene(HANDLE hConsole, Point& ball, Point& paddle) {
    COORD pos = { 0, 0 };
    DWORD written = 0;
    FillConsoleOutputCharacter(hConsole, ' ', WIDTH * HEIGHT, pos, &written);

    // 绘制球
    pos.X = ball.x;
    pos.Y = ball.y;
    FillConsoleOutputCharacter(hConsole, 'O', 1, pos, &written);

    // 绘制球拍
    for (int i = 0; i < PADDLE_SIZE; i++) {
        pos.X = paddle.x;
        pos.Y = paddle.y + i;
        FillConsoleOutputCharacter(hConsole, '|', 1, pos, &written);
    }
}

int main() {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    DWORD dwMode = 0;
    GetConsoleMode(hConsole, &dwMode);
    dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; //启用支持VT100的终端状态
    SetConsoleMode(hConsole, dwMode);

    // 初始化游戏场景
    Point ball = { WIDTH / 2, HEIGHT / 2 };
    Point paddle = { WIDTH - 1, HEIGHT / 2 - PADDLE_SIZE / 2 };
    bool bUp = false, bDown = false;
    int score = 0;

    // 游戏循环
    while (1) {
        // 处理输入
        INPUT_RECORD inputRecord[1];
        DWORD count = 0;
        if (PeekConsoleInput(hConsole, inputRecord, 1, &count)) {
            if (inputRecord[0].EventType == KEY_EVENT &&
                inputRecord[0].Event.KeyEvent.bKeyDown) {
                switch (inputRecord[0].Event.KeyEvent.wVirtualKeyCode) {
                case VK_UP:
                    bUp = true; break;
                case VK_DOWN:
                    bDown = true; break;
                }
            }
        }
        // 移动球拍
        if (bUp) paddle.y--;
        if (bDown) paddle.y++;

        // 处理碰撞
        if (ball.y <= 1 || ball.y >= HEIGHT - 2) ball.y = ball.y < HEIGHT / 2 ? 1 : HEIGHT - 2;
        if (ball.x == paddle.x && ball.y > paddle.y && ball.y < paddle.y + PADDLE_SIZE) {
            ball.x -= BALL_SPEED;
            score++;
        }
        else {
            ball.x += BALL_SPEED;
        }

        // 绘制游戏场景
        drawScene(hConsole, ball, paddle);
        cout << "Score:" << score << endl;
        Sleep(30);
    }
    return 0;
}
Snake

下面是一个简单的Snake游戏示例,使用Windows API来绘制游戏场景和处理输入。游戏目标是控制蛇吃食物,以获得更高的分数。当蛇撞到墙壁或自己时,游戏将结束。

#include <iostream>
#include <Windows.h>
using namespace std;

const int WIDTH = 50;  // 游戏场景宽
const int HEIGHT = 20; // 游戏场景高
const int FOOD_SIZE = 2; // 食物大小

struct Point {
    int x, y;
};

// 绘制游戏场景
void drawScene(HANDLE hConsole, Point& snake, Point& food, bool bEat) {
    COORD pos = { 0, 0 };
    DWORD written = 0;
    FillConsoleOutputCharacter(hConsole, ' ', WIDTH * HEIGHT, pos, &written);

    // 绘制蛇头
    pos.X = snake.x;
    pos.Y = snake.y;
    FillConsoleOutputCharacter(hConsole, '@', 1, pos, &written);

    // 绘制食物
    pos.X = food.x;
    pos.Y = food.y;
    FillConsoleOutputCharacter(hConsole, (bEat ? ' ' : '*'), FOOD_SIZE, pos, &written);
}

int main() {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    // 初始化游戏场景
    Point snake = { WIDTH / 2, HEIGHT / 2 };
    Point food = { rand() % (WIDTH - FOOD_SIZE), rand() % (HEIGHT - FOOD_SIZE) };
    bool bLeft = false, bRight = false, bUp = false, bDown = false;
    bool bEat = false;
    int score = 0;

    // 游戏循环
    while (1) {
        // 处理输入
        INPUT_RECORD inputRecord[1];
        DWORD count = 0;
        if (PeekConsoleInput(hConsole, inputRecord, 1, &count)) {
            if (inputRecord[0].EventType == KEY_EVENT &&
                inputRecord[0].Event.KeyEvent.bKeyDown) {
                switch (inputRecord[0].Event.KeyEvent.wVirtualKeyCode) {
                case VK_LEFT:
                    bLeft = true;
                    bRight = false;
                    bUp = false;
                    bDown = false;
                    break;
                case VK_RIGHT:
                    bLeft = false;
                    bRight = true;
                    bUp = false;
                    bDown = false;
                    break;
                case VK_UP:
                    bLeft = false;
                    bRight = false;
                    bUp = true;
                    bDown = false;
                    break;
                case VK_DOWN:
                    bLeft = false;
                    bRight = false;
                    bUp = false;
                    bDown = true;
                    break;
                }
            }
        }
        // 移动蛇
        if (bLeft) snake.x--;
        if (bRight) snake.x++;
        if (bUp) snake.y--;
        if (bDown) snake.y++;

        // 处理边界
        if (snake.x < 0 || snake.x > WIDTH - 1 ||
            snake.y < 0 || snake.y > HEIGHT - 1) {
            cout << "Game Over! Score:" << score << endl;
            break;
        }

        // 处理食物
        if (snake.x == food.x && snake.y == food.y) {
            bEat = true;
            score++;
            food = { rand() % (WIDTH - FOOD_SIZE), rand() % (HEIGHT - FOOD_SIZE) };
        }
        else {
            bEat = false;
        }

        // 绘制游戏场景
        drawScene(hConsole, snake, food, bEat);
        cout << "Score:" << score << endl;
        Sleep(100);
    }
    return 0;
}

##总结

这里提供了两个简单的C++游戏示例,用于演示一些基本的游戏编程概念。您可以使用这些示例代码作为起点,创建您自己的游戏。