#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;
}