本文主要是介绍C++坦克大战源代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
效果截图:
源码
#include <iostream>
#include <time.h>
#include <windows.h>#define W 1 //上
#define S 2 //下
#define A 3 //左
#define D 4 //右
#define L 5 // 坦克有4条命void HideCursor() { //隐藏光标 CONSOLE_CURSOR_INFO cursor_info = { 1,0 };SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void GoToxy(int x, int y) { //光标移动,X、Y表示横、纵坐标COORD coord = { x, y };SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}//全局变量
int map[50][40];//地图二维数组
int B_num; //子弹编号
int Pos; //敌方坦克生成位置,-1为左边,0为中间,1为右边,2为我的坦克位置
int Speed = 7; //游戏速度
int Enemy; //还未出现的敌人
const char* Tank_Model[3][4] ={{"◢┃ ◣", "◢╦ ◣", "◢╦◣", "◢╦◣"},{"╠ █╣", "╠ █╣", "━█╣", "╠█━"},{"◥╩ ◤", "◥┃ ◤", "◥╩◤", "◥╩◤"}}; //坦克
class Tank{
public:int x, y; //中心坐标int Direction; //方向int Model; //模型int Revival; //复活次数int Num; //敌方坦克编号 bool Type; //我方坦克此参数为1bool Exist; //存活为1,不存活为0
}AI_tank[6], my_tank;
//子弹
class Bullet{
public:int x, y; //坐标int Direction; //方向bool Exist; //1为存在,0不存在bool Type; //0为敌方子弹,1为我方子弹
}bullet[50] ;//基本函数
void GoToxy(int x, int y); //光标移动
void HideCursor(); //隐藏光标void Key(); //键盘输入
void Init(); //初始化
void Pause(); //暂停
void Show(); //打印框架
void Print_Map(); //打印地图
void Cheak_Game(); //检测游戏胜负
void GameOver(); //游戏结束//坦克
void Creat_AI_T(Tank* AI_tank); //建立坦克
void Creat_My_T(Tank* my_tank); void Move_AI_T(Tank* AI_tank);//坦克移动
void Move_My_T(int turn); void Clear_T(int x, int y); //清除坦克
void Print_T(Tank tank); //打印坦克
bool Cheak_T(Tank tank, int direction); //检测障碍,1阻碍//子弹
void Creat_AI_B(Tank* tank); //敌方坦克发射子弹
void Creat_My_B(Tank tank);//我方坦克发射子弹
void Move_B(Bullet bullet[50]); //子弹移动
void Break_B(Bullet* bullet); //子弹碰撞
void Print_B(int x, int y);//打印子弹
void Clear_B(int x, int y); //清除子弹
int Cheak_B(int x, int y); //子弹前方情况void Show() { //打印框架 std::cout << " ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁";std::cout << "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁\n";for (int i = 0; i < 48; i++) {std::cout << "▕ ▏\n";}std::cout << " ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔";std::cout << "▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔\n";
}
void Print_Map() { // 打印地图 int Map[50][40] = {
//map里的值: 0为可通过陆地,1为砖,6为墙,100~105为敌方坦克,200为我的坦克,{ 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4 },{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1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