C++上课摸鱼小游戏

众所周知,C++课上久了会脑阔疼,所以我就做了一格“摸     鱼”小游戏,

操作大概说一下

ad键左右移动,q退出,[space]发射子弹

(敌人死亡的时候会有冷却属于正常现象)

#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>
#include <cmath>
#include <chrono>
#include <thread>
#include <windows.h>
const int width = 42;
const int height = 22;
const int enemyRespawnRange = 15; // 敌人刷新范围
const int enemyCooldownMin = 4;   // 敌人刷新冷却最小时间(秒)
const int enemyCooldownMax = 8;   // 敌人刷新冷却最大时间(秒)
const int enemySpeed = 1;         // 敌人速度
const int bossHealth = 5;         // BOSS生命值

int playerX, playerY;
std::vector<int> bulletX, bulletY;
int enemyX, enemyY;
bool isEnemyActive;
bool isPlayerAlive;
int score;
bool isBossActive;
int bossHealthRemaining;

void Setup()
{
    playerX = width / 2;
    playerY = height - 1;
    bulletX.clear();
    bulletY.clear();
    enemyX = -1;
    enemyY = -1;
    isEnemyActive = false;
    isPlayerAlive = true;
    score = 0;
    isBossActive = false;
    bossHealthRemaining = bossHealth;

    // 初始化随机数种子
    std::srand(std::time(nullptr));
}

void Draw()
{
    system("cls"); // 清屏

    // 绘制上边界
    for (int i = 0; i < width; i++)
        std::cout << "-";
    std::cout << std::endl;

    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            if (x == 0 || x == width - 1) // 绘制左右边界
                std::cout << "|";
            else if (y == playerY && x == playerX)
                std::cout << "P"; // 玩家
            else if (isEnemyActive && y == enemyY && x == enemyX)
                std::cout << "E"; // 敌人
            else if (std::find(bulletX.begin(), bulletX.end(), x) != bulletX.end() && std::find(bulletY.begin(), bulletY.end(), y) != bulletY.end())
                std::cout << "|"; // 子弹
            else if (isBossActive && y == height - 2 && x == width - 2)
                std::cout << "B"; // BOSS
            else
                std::cout << " "; // 空白
        }
        std::cout << std::endl;
    }

    // 绘制下边界
    for (int i = 0; i < width; i++)
        std::cout << "-";
    std::cout << std::endl;
}

void Input()
{
    if (_kbhit())
    {
        char key = _getch();
        switch (key)
        {
            case 'a':
                if (playerX > 1)
                    playerX--;
                break;
            case 'd':
                if (playerX < width - 2)
                    playerX++;
                break;
            case ' ':
                bulletX.push_back(playerX);
                bulletY.push_back(playerY - 1);
                break;
            case 'q':
                exit(0);
                break;
        }
    }
}

void Update()
{
    // 更新子弹位置
    for (int i = 0; i < bulletX.size(); i++)
    {
        bulletY[i]--;
        if (bulletY[i] < 0)
        {
            bulletX.erase(bulletX.begin() + i);
            bulletY.erase(bulletY.begin() + i);
            i--;
        }
    }

    // 更新敌人位置
    if (isEnemyActive)
    {
        enemyY += enemySpeed;
        if (enemyY >= playerY)
        {
            isPlayerAlive = false;
            return;
        }
        // 检查敌人是否被子弹击中
        for (int i = 0; i < bulletX.size(); i++)
        {
            if (enemyX == bulletX[i] && enemyY == bulletY[i])
            {
                isEnemyActive = false;
                bulletX.erase(bulletX.begin() + i);
                bulletY.erase(bulletY.begin() + i);
                score++; // 击败敌人,分数加一
                break;
            }
        }
    }
    else
    {
        std::this_thread::sleep_for(std::chrono::seconds(std::rand() % (enemyCooldownMax - enemyCooldownMin + 1) + enemyCooldownMin)); // 冷却时间
        int respawnX = std::rand() % (2 * enemyRespawnRange) - enemyRespawnRange + playerX; // 在玩家附近随机生成敌人的X坐标
        enemyX = std::max(1, std::min(width - 2, respawnX)); // 确保敌人的X坐标在边界内
        enemyY = 0;
        isEnemyActive = true;
    }
}

int main()
{
    Setup();

    while (isPlayerAlive)
    {
        Draw();
        Input();
        Update();
    }

    std::cout << "Game Over! Your Score: " << score << std::endl;
	system("pause");
    return 0;
}

(第一次在CSDN上发文章,多多包容)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值