#include <iostream>
#include <windows.h>
using namespace std;
// 坦克的结构体
struct Tank {
int x, y; // 坦克的位置
};
// 子弹的结构体
struct Bullet {
int x, y; // 子弹的位置
};
// 游戏的主函数
int main() {
Tank myTank = { 5, 5 }; // 创建一个玩家坦克
Tank enemyTank = { 10, 10 }; // 创建一个敌方坦克
Bullet bullet = { 7, 7 }; // 创建一个子弹
// 游戏循环
while (true) {
// 检查玩家输入
cout << "Press W to move up, S to move down, A to move left, D to move right, or spacebar to fire." << endl;
char input;
cin >> input;
switch (input) {
case 'W':
myTank.y -= 1;
break;
case 'S':
myTank.y += 1;
break;
case 'A':
myTank.x -= 1;
break;
case 'D':
myTank.x += 1;
break;
case ' ':
bullet.x = myTank.x;
bullet.y = myTank.y;
break;
}
// 移动子弹
if (bullet.x > 0 && bullet.x < 20 && bullet.y > 0 && bullet.y < 20) {
bullet.y -= 1;
} else {
bullet.x = 0;
bullet.y = 0;
}
// 检查碰撞
if (myTank.x == enemyTank.x && myTank.y == enemyTank.y) {
cout << "You hit the enemy tank!" << endl;
break;
}
// 输出游戏状态
cout << "Tank: (" << myTank.x << "," << myTank.y << ")" << endl;
cout << "Enemy Tank: (" << enemyTank.x << "," << enemyTank.y << ")" << endl;
cout << "Bullet: (" << bullet.x << "," << bullet.y << ")" << endl;
Sleep(1000); // 等待1秒
}
return 0;
}
C++做坦克大作战
最新推荐文章于 2024-06-12 22:16:03 发布