西北工业大学计算机课大作业(C++)

既然发了一篇了,就再发一篇吧!

这次为大家制作了一个坦克大战游戏的大纲,相信坦克大战大家都玩过吧。

本文只提供简易大纲!!!

#include <iostream>
#include <vector>
#include <conio.h>
#include <windows.h>

using namespace std;

// 定义常量
const int kMapWidth = 20;
const int kMapHeight = 20;

// 定义方向
enum Direction {
    UP,
    DOWN,
    LEFT,
    RIGHT
};

// 定义坦克结构体
struct Tank {
    int x, y;
    Direction direction;
};

// 定义子弹结构体
struct Bullet {
    int x, y;
    Direction direction;
};

// 定义墙体结构体
struct Wall {
    int x, y;
};

class TankGame {
private:
    Tank playerTank;
    vector<Tank> enemyTanks;
    vector<Bullet> bullets;
    vector<Wall> walls;
    bool gameOver;

public:
    TankGame() : gameOver(false) {
        // 初始化玩家坦克
        playerTank = {1, 1, UP};

        // 初始化敌方坦克
        enemyTanks.push_back({kMapWidth - 2, kMapHeight - 2, DOWN});
        enemyTanks.push_back({kMapWidth / 2, kMapHeight / 2, LEFT});

        // 初始化墙体
        walls.push_back({5, 5});
        walls.push_back({6, 5});
        walls.push_back({7, 5});
        walls.push_back({8, 5});
        walls.push_back({9, 5});
        walls.push_back({10, 5});

        walls.push_back({15, 15});
        walls.push_back({15, 16});
        walls.push_back({15, 17});
    }

    // 显示游戏画面
    void render() {
        system("cls");

        // 显示墙体
        for (const auto& wall : walls) {
            cout << "#";
        }
        cout << endl;

        // 显示玩家坦克
        cout << "P";
        for (int i = 0; i < kMapWidth - 2; ++i) {
            cout << " ";
        }
        cout << "E" << endl;

        // 显示敌方坦克
        for (const auto& enemyTank : enemyTanks) {
            for (int i = 0; i < enemyTank.x; ++i) {
                cout << " ";
            }
            cout << "E" << endl;
        }

        // 显示子弹
        for (const auto& bullet : bullets) {
            for (int i = 0; i < bullet.x; ++i) {
                cout << " ";
            }
            cout << "*" << endl;
        }
    }

    // 处理用户输入
    void handleInput() {
        if (_kbhit()) {
            char key = _getch();
            switch (key) {
                case 'w':
                    moveTank(playerTank, UP);
                    break;
                case 's':
                    moveTank(playerTank, DOWN);
                    break;
                case 'a':
                    moveTank(playerTank, LEFT);
                    break;
                case 'd':
                    moveTank(playerTank, RIGHT);
                    break;
                case ' ':
                    shootBullet(playerTank);
                    break;
                case 'x':
                    gameOver = true;
                    break;
                default:
                    break;
            }
        }
    }

    // 移动坦克
    void moveTank(Tank& tank, Direction direction) {
        tank.direction = direction;

        switch (direction) {
            case UP:
                if (tank.y > 0) {
                    tank.y--;
                }
                break;
            case DOWN:
                if (tank.y < kMapHeight - 1) {
                    tank.y++;
                }
                break;
            case LEFT:
                if (tank.x > 0) {
                    tank.x--;
                }
                break;
            case RIGHT:
                if (tank.x < kMapWidth - 1) {
                    tank.x++;
                }
                break;
            default:
                break;
        }
    }

    // 发射子弹
    void shootBullet(const Tank& tank) {
        Bullet bullet = {tank.x, tank.y, tank.direction};
        bullets.push_back(bullet);
    }

    // 移动子弹
    void moveBullets() {
        for (auto& bullet : bullets) {
            switch (bullet.direction) {
                case UP:
                    if (bullet.y > 0) {
                        bullet.y--;
                    } else {
                        bullet = bullets.back();
                        bullets.pop_back();
                    }
                    break;
                case DOWN:
                    if (bullet.y < kMapHeight - 1) {
                        bullet.y++;
                    } else {
                        bullet = bullets.back();
                        bullets.pop_back();
                    }
                    break;
                case LEFT:
                    if (bullet.x > 0) {
                        bullet.x--;
                    } else {
                        bullet = bullets.back();
                        bullets.pop_back();
                    }
                    break;
                case RIGHT:
                    if (bullet.x < kMapWidth - 1) {
                        bullet.x++;
                    } else {
                        bullet = bullets.back();
                        bullets.pop_back();
                    }
                    break;
                default:
                    break;
            }
        }
    }

    // 检查碰撞
    void checkCollisions() {
        // 检查子弹与敌方坦克碰撞
        for (auto& bullet : bullets) {
            for (auto& enemyTank : enemyTanks) {
                if (bullet.x == enemyTank.x && bullet.y == enemyTank.y) {
                    bullets.erase(remove(bullets.begin(), bullets.end(), bullet), bullets.end());
                    enemyTanks.erase(remove(enemyTanks.begin(), enemyTanks.end(), enemyTank), enemyTanks.end());
                    break;
                }
            }
        }

        // 检查子弹与墙体碰撞
        for (auto& bullet : bullets) {
            for (const auto& wall : walls) {
                if (bullet.x == wall.x && bullet.y == wall.y) {
                    bullets.erase(remove(bullets.begin(), bullets.end(), bullet), bullets.end());
                    break;
                }
            }
        }
    }

    // 更新游戏状态
    void updateGame() {
        handleInput();
        moveBullets();
        checkCollisions();
        render();
        Sleep(100); // 控制游戏速度
    }

    // 运行游戏
    void run() {
        while (!gameOver) {
            updateGame();
        }

        // 游戏结束
        cout << "Game Over!" << endl;
    }
};

int main() {
    TankGame game;
    game.run();

    return 0;
}

若大家有建议,尽情指正。

本人大一,请多担待

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值