目录

c小游戏7个代码

c++小游戏7个代码

以下是10个不同类型的C++小游戏代码示例,这些游戏可以帮助你更深入地理解C++编程和游戏开发的基本概念:

1. 猜数字游戏

#include <iostream>
#include <cstdlib>
#include <ctime>

int main() {
srand(time(0));
int secretNumber = rand() % 100 + 1;
int guess;
int attempts = 0;

    std::cout << "猜数字游戏!" << std::endl;
    do {
        std::cout << "请输入你的猜测: ";
        std::cin >> guess;
        attempts++;
        if (guess > secretNumber) {
            std::cout << "太大了!" << std::endl;
        } else if (guess < secretNumber) {
            std::cout << "太小了!" << std::endl;
        } else {
            std::cout << "恭喜你,猜对了!" << std::endl;
            std::cout << "你用了 " << attempts << " 次尝试。" << std::endl;
        }
    } while (guess != secretNumber);

    return 0;

}

2. 井字棋(Tic-Tac-Toe)

// 井字棋代码较为复杂,这里只提供一个简化的版本
#include <iostream>
#include <vector>

int main() {
std::vector<std::vector<char>> board(3, std::vector<char>(3, ' '));
char currentPlayer = 'X';
bool gameOver = false;

    while (!gameOver) {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                std::cout << board[i][j];
                if (board[i][j] == ' ') std::cout << "|";
            }
            std::cout << std::endl;
        }

        int row, col;
        std::cout << "玩家 " << currentPlayer << " 的回合. 输入行和列 (0-2): ";
        std::cin >> row >> col;

        if (board[row][col] == ' ') {
            board[row][col] = currentPlayer;
            currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
        } else {
            std::cout << "该位置已被占用,请重新输入。" << std::endl;
        }
    }

    return 0;

}

3. 贪吃蛇(Snake Game)

贪吃蛇游戏的代码较为复杂,通常需要使用图形库(如 SFML 或 SDL)来实现。这里不提供完整代码,但可以提供一个基本的框架:

// 贪吃蛇游戏代码较为复杂,这里只提供一个简化的框架
#include <iostream>
#include <conio.h>
#include <windows.h>

using namespace std;

const int width = 20;
const int height = 20;

struct Snake {
int x, y;
};

void Draw(Snake snake[], int length) {
system("cls");
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;

    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            if (j == 0)
                cout << "#";
            bool print = false;
            for (int k = 0; k < length; k++) {
                if (snake[k].x == j && snake[k].y == i) {
                    cout << "O";
                    print = true;
                }
            }
            if (!print) cout << " ";
            if (j == width - 1)
                cout << "#";
        }
        cout << endl;
    }

    for (int i = 0; i < width + 2; i++)
        cout << "#";
    cout << endl;

}

int main() {
Snake snake[100];
int length = 1;
snake[0].x = width / 2;
snake[0].y = height / 2;
char direction;

    while (true) {
        if (_kbhit()) {
            direction = _getch();
            switch (direction) {
                case 'a':
                    // 向左移动
                    break;
                case 'd':
                    // 向右移动
                    break;
                case 'w':
                    // 向上移动
                    break;
                case 's':
                    // 向下移动
                    break;
            }
        }
        Draw(snake, length);
        Sleep(100);
    }

    return 0;

}

4. 扫雷(Minesweeper)

扫雷游戏的代码也较为复杂,通常需要使用图形库来实现。这里不提供完整代码,但可以提供一个基本的框架:

// 扫雷游戏代码较为复杂,这里只提供一个简化的框架
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>

using namespace std;

const int rows = 10;
const int cols = 10;
const int mines = 10;

void PrintBoard(vector<vector<char>>& board) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << board[i][j] << " ";
}
cout << endl;
}
}

int main() {
vector<vector<char>> board(rows, vector<char>(cols, ' '));
vector<pair<int, int>> minePositions;

    srand(time(0));
    for (int i = 0; i < mines; i++) {
        int row = rand() % rows;
        int col = rand() % cols;
        minePositions.push_back(make_pair(row, col));
        board[row][col] = '*';
    }

    PrintBoard(board);

    return 0;

}

5. 俄罗斯方块(Tetris)

俄罗斯方块游戏的代码也较为复杂,通常需要使用图形库来实现。这里不提供完整代码,但可以提供一个基本的框架:

// 俄罗斯方块游戏代码较为复杂,这里只提供一个简化的框架
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>

using namespace std;

const int rows = 20;
const int cols = 10;

enum Tetromino { I, J, L, O, S, T, Z };

struct TetrominoShape {
vector<vector<int>> shape;
int x, y;
};

TetrominoShape GetTetromino(Tetromino type) {
TetrominoShape tetromino;
switch (type) {
case I:
tetromino.shape = {{1, 1, 1, 1}};
break;
case J:
tetromino.shape = {{1, 0, 0}, {1, 1, 1}};
break;
case L:
tetromino.shape = {{0, 0, 1}, {1, 1, 1}};
break;
case O:
tetromino.shape = {{1, 1}, {1, 1}};
break;
case S:
tetromino.shape = {{0, 1, 1}, {1, 1, 0}};
break;
case T:
tetromino.shape = {{0, 1, 0}, {1, 1, 1}};
break;
case Z:
tetromino.shape = {{1, 1, 0}, {0, 1, 1}};
break;
}
tetromino.x = cols / 2 - 2;
tetromino.y = 0;
return tetromino;
}

void PrintBoard(vector<vector<int>>& board) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << board[i][j] << " ";
}
cout << endl;
}
}

int main() {
vector<vector<int>> board(rows, vector<int>(cols, 0));
Tetromino currentTetromino = static_cast<Tetromino>(rand() % 7);
TetrominoShape tetromino = GetTetromino(currentTetromino);

    PrintBoard(board);

    return 0;

}

6. 打砖块游戏

打砖块游戏的代码也较为复杂,通常需要使用图形库来实现。这里不提供完整代码,但可以提供一个基本的框架:

// 打砖块游戏代码较为复杂,这里只提供一个简化的框架
#include <iostream>
#include <conio.h>
#include <windows.h>

using namespace std;

const int width = 40;
const int height = 20;
const int paddleWidth = 10;

struct Ball {
int x, y;
int dx, dy;
};

struct Paddle {
int x, y;
};

void Draw(Ball ball, Paddle paddle, vector<vector<bool>>& bricks) {
system("cls");
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;

    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            if (j == 0)
                cout << "#";
            bool drawBrick = false;
            for (int k = 0; k < bricks.size(); k++) {
                if (bricks[k][i][j]) {
                    cout << "[]";
                    drawBrick = true;
                }
            }
            if (!drawBrick) {
                if (ball.x == j && ball.y == i)
                    cout << "O";
                else if (paddle.x <= j && paddle.x + paddleWidth >= j && paddle.y == i)
                    cout << "=";
                else
                    cout << " ";
            }
            if (j == width - 1)
                cout << "#";
        }
        cout << endl;
    }

    for (int i = 0; i < width + 2; i++)
        cout << "#";
    cout << endl;

}

int main() {
Ball ball = {width / 2, height - 1, 1, -1};
Paddle paddle = {width / 2 - paddleWidth / 2, height - 2};
vector<vector<bool>> bricks(height, vector<bool>(width, true));

    while (true) {
        if (_kbhit()) {
            char key = _getch();
            if (key == 'a' && paddle.x > 0)
                paddle.x--;
            if (key == 'd' && paddle.x + paddleWidth < width)
                paddle.x++;
        }

        ball.x += ball.dx;
        ball.y += ball.dy;

        if (ball.x <= 0 || ball.x >= width - 1)
            ball.dx = -ball.dx;
        if (ball.y <= 0)
            ball.dy = -ball.dy;
        if (ball.y >= height - 1)
            break;

        if (ball.x >= paddle.x && ball.x <= paddle.x + paddleWidth && ball.y == paddle.y)
            ball.dy = -ball.dy;

        for (int i = 0; i < bricks.size(); i++) {
            for (int j = 0; j < bricks[i].size(); j++) {
                if (bricks[i][j] && ball.x == j && ball.y == i) {
                    bricks[i][j] = false;
                    ball.dy = -ball.dy;
                }
            }
        }

        Draw(ball, paddle, bricks);
        Sleep(100);
    }

    return 0;

}

7. 迷宫游戏

迷宫游戏的代码也较为复杂,通常需要使用图形库来实现。这里不提供完整代码,但可以提供一个基本的框架:

// 迷宫游戏代码较为复杂,这里只提供一个简化的框架
#include <iostream>
#include <vector>
#include <stack>

using namespace std;

const int rows = 10;
const int cols = 10;

struct Cell {
int x, y;
bool visited;
bool walls[4]; // 0: up, 1: right, 2: down, 3: left
};

vector<vector<Cell>> CreateMaze(int rows, int cols) {
vector<vector<Cell>> maze(rows, vector<Cell>(cols));
stack<Cell*> stack;

    Cell* start = &maze[0][0];
    start->visited = true;
    stack.push(start);

    while (!stack.empty()) {
        Cell* current = stack.top();
        stack.pop();

        vector<int> neighbors;

        if (current->y > 0 && !maze[current->y - 1][current->x].visited)
            neighbors.push_back(0);
        if (current->x < cols - 1 && !maze[current->y][current->x + 1].visited)
            neighbors.push_back(1);
        if (current->y < rows - 1 && !maze[current->y + 1][current->x].visited)
            neighbors.push_back(2);
        if (current->x > 0 && !maze[current->y][current->x - 1].visited)
            neighbors.push_back(3);

        if (!neighbors.empty()) {
            stack.push(current);
            int randomIndex = rand() % neighbors.size();
            int wall = neighbors[randomIndex];

            switch (wall) {
                case 0: // up
                    current->walls[0] = false;
                    maze[current->y - 1][current->x].walls[2] = false;
                    maze[current->y - 1][current->x].visited = true;
                    stack.push(&maze[current->y - 1][current->x]);
                    break;
                case 1: // right
                    current->walls[1] = false;
                    maze[current->y][current->x + 1].walls[3] = false;
                    maze[current->y][current->x + 1].visited = true;
                    stack.push(&maze[current->y][current->x + 1]);
                    break;
                case 2: // down
                    current->walls[2] = false;
                    maze[current->y + 1][current->x].walls[0] = false;
                    maze[current->y + 1][current->x].visited = true;
                    stack.push(&maze[current->y + 1][current->x]);
                    break;
                case 3: // left
                    current->walls[3] = false;
                    maze[current->y][current->x - 1].walls[1]