- gf25071 的博客
太空射击游戏1.0(ai)
- @ 2026-3-5 13:57:41
太空射击游戏
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include <cstdio>
using namespace std;
// 屏幕尺寸(控制台窗口大小)
#define WIDTH 80
#define HEIGHT 25
// 游戏状态枚举
enum GameState {
START_SCREEN, // 启动界面
GAME_PLAYING, // 游戏中
GAME_OVER // 游戏结束
};
GameState currentState = START_SCREEN; // 默认显示启动界面
// 玩家飞船结构体
struct Player {
int x, y; // 位置
int score; // 得分
bool isAlive; // 是否存活
} player;
// 子弹结构体
struct Bullet {
int x, y; // 位置
bool isActive; // 是否激活
} bullets[20]; // 最多20颗子弹
// 敌方飞船结构体
struct Enemy {
int x, y; // 位置
int speed; // 移动速度
bool isActive; // 是否激活
} enemies[10]; // 最多10个敌机
// 清屏函数(替代图形库的cleardevice)
void ClearScreen() {
system("cls"); // 清空控制台
}
// 设置控制台光标位置
void GotoXY(int x, int y) {
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
// 隐藏控制台光标
void HideCursor() {
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
// 绘制启动界面
void DrawStartScreen() {
ClearScreen();
HideCursor(); // 隐藏光标
// 绘制游戏标题
int titleX = WIDTH / 2 - 10;
int titleY = HEIGHT / 2 - 5;
GotoXY(titleX, titleY);
cout << "======= 太空射击游戏 =======";
// 绘制操作说明
GotoXY(titleX - 5, titleY + 2);
cout << "操作说明:";
GotoXY(titleX - 3, titleY + 3);
cout << "A / ← :向左移动";
GotoXY(titleX - 3, titleY + 4);
cout << "D / → :向右移动";
GotoXY(titleX - 3, titleY + 5);
cout << "空格/回车:发射子弹";
GotoXY(titleX - 3, titleY + 6);
cout << "ESC:退出游戏";
// 绘制开始提示
GotoXY(titleX - 2, titleY + 8);
cout << "按 任意键 开始游戏 >>>";
}
// 初始化游戏
void InitGame() {
// 初始化玩家
player.x = WIDTH / 2;
player.y = HEIGHT - 2;
player.score = 0;
player.isAlive = true;
// 初始化子弹
for (int i = 0; i < 20; i++) {
bullets[i].isActive = false;
}
// 初始化敌机
srand((unsigned)time(NULL));
for (int i = 0; i < 10; i++) {
enemies[i].x = rand() % (WIDTH - 2);
enemies[i].y = rand() % (HEIGHT / 2);
enemies[i].speed = rand() % 2 + 1;
enemies[i].isActive = true;
}
HideCursor(); // 隐藏光标,避免闪烁
}
// 绘制游戏元素
void DrawGame() {
ClearScreen();
// 绘制边界
GotoXY(0, 0);
for (int i = 0; i < WIDTH; i++) cout << "-";
for (int i = 1; i < HEIGHT; i++) {
GotoXY(0, i); cout << "|";
GotoXY(WIDTH - 1, i); cout << "|";
}
GotoXY(0, HEIGHT);
for (int i = 0; i < WIDTH; i++) cout << "-";
// 绘制玩家飞船(用 '^' 模拟)
if (player.isAlive) {
GotoXY(player.x, player.y);
cout << "^";
}
// 绘制子弹(用 '|' 模拟)
for (int i = 0; i < 20; i++) {
if (bullets[i].isActive) {
GotoXY(bullets[i].x, bullets[i].y);
cout << "|";
}
}
// 绘制敌机(用 '#' 模拟)
for (int i = 0; i < 10; i++) {
if (enemies[i].isActive) {
GotoXY(enemies[i].x, enemies[i].y);
cout << "#";
}
}
// 绘制得分
GotoXY(WIDTH / 2 - 5, HEIGHT + 1);
cout << "得分: " << player.score;
// 游戏结束提示
if (!player.isAlive) {
currentState = GAME_OVER;
GotoXY(WIDTH / 2 - 10, HEIGHT / 2);
cout << "=== 游戏结束 ===";
GotoXY(WIDTH / 2 - 15, HEIGHT / 2 + 1);
cout << "按 R 重新开始 | 按 ESC 退出";
}
}
// 处理键盘输入
void HandleInput() {
if (_kbhit()) {
char key = _getch();
// 启动界面输入
if (currentState == START_SCREEN) {
if (key != 27) { // 不是ESC键就开始游戏
currentState = GAME_PLAYING;
InitGame(); // 初始化游戏数据
} else {
exit(0); // ESC退出
}
}
// 游戏中输入
else if (currentState == GAME_PLAYING) {
// 左移
if (key == 'a' || key == 'A' || key == 75) { // A键 或 左方向键
if (player.x > 1) player.x--;
}
// 右移
else if (key == 'd' || key == 'D' || key == 77) { // D键 或 右方向键
if (player.x < WIDTH - 2) player.x++;
}
// 发射子弹
else if (key == ' ' || key == 13) { // 空格键 或 回车键
// 找到空闲子弹
for (int i = 0; i < 20; i++) {
if (!bullets[i].isActive) {
bullets[i].x = player.x;
bullets[i].y = player.y - 1;
bullets[i].isActive = true;
break;
}
}
}
// 直接退出
else if (key == 27) {
exit(0);
}
}
// 游戏结束输入
else if (currentState == GAME_OVER) {
// 重新开始
if (key == 'r' || key == 'R') {
currentState = GAME_PLAYING;
InitGame();
}
// 退出游戏
else if (key == 27) {
exit(0);
}
}
}
}
// 更新游戏逻辑
void UpdateGame() {
if (currentState != GAME_PLAYING || !player.isAlive) return;
// 更新子弹位置
for (int i = 0; i < 20; i++) {
if (bullets[i].isActive) {
bullets[i].y--;
// 子弹超出屏幕则失效
if (bullets[i].y < 1) bullets[i].isActive = false;
}
}
// 更新敌机位置
for (int i = 0; i < 10; i++) {
if (enemies[i].isActive) {
enemies[i].y += enemies[i].speed;
// 敌机超出屏幕则重置位置
if (enemies[i].y >= HEIGHT - 1) {
enemies[i].x = rand() % (WIDTH - 2);
enemies[i].y = 1;
enemies[i].speed = rand() % 2 + 1;
}
}
}
// 碰撞检测:子弹击中敌机
for (int i = 0; i < 20; i++) {
if (bullets[i].isActive) {
for (int j = 0; j < 10; j++) {
if (enemies[j].isActive) {
// 简单碰撞检测(位置重合)
if (bullets[i].x == enemies[j].x && bullets[i].y == enemies[j].y) {
bullets[i].isActive = false;
enemies[j].x = rand() % (WIDTH - 2);
enemies[j].y = 1;
player.score += 10;
break;
}
}
}
}
}
// 碰撞检测:敌机撞击玩家
for (int i = 0; i < 10; i++) {
if (enemies[i].isActive) {
if (enemies[i].x == player.x && enemies[i].y == player.y) {
player.isAlive = false; // 玩家死亡,游戏结束
break;
}
}
}
}
// 主函数
int main() {
// 设置控制台窗口大小
system("mode con cols=80 lines=30");
// 游戏主循环
while (true) {
HandleInput(); // 处理输入
// 根据不同状态执行不同逻辑
if (currentState == START_SCREEN) {
DrawStartScreen(); // 显示启动界面
} else if (currentState == GAME_PLAYING) {
UpdateGame(); // 更新游戏逻辑
DrawGame(); // 绘制游戏画面
} else if (currentState == GAME_OVER) {
DrawGame(); // 显示游戏结束界面
}
Sleep(100); // 控制帧率
}
return 0;
}