众所周知,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上发文章,多多包容)