简易的c++躲避激光小游戏(稍微有点卡)

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

using namespace std;

const int width = 20; // 游戏界面宽度
const int height = 10; // 游戏界面高度
int playerX, playerY; // 玩家的坐标
int laserX, laserY; // 激光的坐标
bool gameOver; // 游戏是否结束

void Setup()
{
    gameOver = false;
    playerX = width / 2; // 初始化玩家坐标为界面中心
    playerY = height - 1;
    laserX = rand() % width; // 随机生成激光的初始位置
    laserY = 0;
}

void Draw()
{
    system("cls"); // 清屏
    for (int i = 0; i < width + 2; i++)
        cout << "#"; // 顶部边界
    cout << endl;

    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            if (x == 0)
                cout << "#"; // 左边界

            if (x == playerX && y == playerY)
                cout << "P"; // 玩家
            else if (x == laserX && y == laserY)
                cout << "*"; // 激光
            else
                cout << " ";

            if (x == width - 1)
                cout << "#"; // 右边界
        }
        cout << endl;
    }

    for (int i = 0; i < width + 2; i++)
        cout << "#"; // 底部边界
    cout << endl;
}

void Input()
{
    if (_kbhit()) // 检查是否有按键输入
    {
        switch (_getch())
        {
            case 'a':
                playerX--; // 左移
                break;
            case 'd':
                playerX++; // 右移
                break;
            case 'q':
                gameOver = true; // 游戏结束
                break;
        }
    }
}

void Logic()
{
    laserY++; // 激光下降

    if (laserY == playerY && laserX == playerX)
        gameOver = true; // 玩家与激光相撞

    if (laserY >= height - 1)
    {
        laserX = rand() % width; // 重置激光位置
        laserY = 0;
    }
}

int main()
{
    Setup();
    while (!gameOver)
    {
        Draw();
        Input();
        Logic();
        Sleep(10); // 控制游戏速度
    }
    cout << "Game Over!" << endl;

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值