12 c++版本的坦克大战

2024-04-27 11:44
文章标签 c++ 版本 大战 坦克

本文主要是介绍12 c++版本的坦克大战,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

呵呵 这大概是 大学里面的 c++ 贪吃蛇了吧 

有一些 面向对象的理解, 但是不多 

这里 具体的实现 就不赘述, 仅仅是 发一下代码 以及 具体的使用 

 

 

坦克大战

#include<iostream>
#include<windows.h>
#include<conio.h>
#include<ctime>
#include<string>
#include<list>
using namespace std;
#define N 4
class Tank_War;
void set_pos(int x, int y);
class Object
{	
protected:int x, y;int dir;int life;public:Object() : x(0), y(0), dir(-1),life(1)	{}Object(int x, int y, int dir, int life) : x(x), y(y), dir(dir), life(life) {}	 void move();void paint()  {	}void reduce_blood()	{	life -= 1; 	}void increase_blood()	{	life += 1; 	}bool is_failure();virtual void print(){cout<<x<<"--"<<y<<"=="<<life<<"--"<<endl;}
};class Missile : public Object
{
public:Missile() : Object() {}Missile(int x, int y, int dir, int life) : Object(x, y, dir, life) {}bool move();void paint(string icon);int get_x()const	{	return x;	}int get_y()const	{	return y;	}void set_info(int x, int y, int dir) {	this -> x = x; this -> y = y; this -> dir = dir;	};bool operator==(const Missile&_x){return (x==_x.x && y==_x.y&&dir==_x.dir);		}		//list中remove是需要重载==
};class Tank : public Object
{
private:int pre_dir;list<Missile> missile_list;public:Tank() : Object(), pre_dir(-1) {}Tank(int x, int y, int dir, int life) : Object(x, y, dir,life) {pre_dir = dir;};void del_before_move();void move(Tank_War &game, string name);void paint(string icon);int get_x()	const	{	return x;	}int get_y()	const	{	return y;	}int get_dir()const	{	return dir;	}void set_dir(int dir){	pre_dir = this -> dir;	this -> dir = dir;	}void set_location(int x, int y){	this -> x = x;	this -> y = y;	}void emission_missile();void missile_move();void check_missile(Tank_War &game, string name);bool check_missile_strike(Missile& missile, Tank& tank);
//	bool check_is_right(int x, int y, int dir);int distance_squre(int x_1, int y_1, int x_2, int y_2);int distance_squre(Tank& tank_2);bool operator==(const Tank&_x){return (x==_x.x && y==_x.y&&dir==_x.dir);		}
};class Tank_War
{
private:Tank lead;list<Tank> enemy_list;int tot;int grade;bool over;public:Tank_War():lead(28, 21, -1, 4), grade(0), tot(0), over(false) {};int game();void game_over()	{	over = true;	}int draw_menu();void draw_game_interface();Tank* get_lead()	{	return &lead;	}list<Tank>::iterator get_enemy_list_begin()	{	return enemy_list.begin();	}list<Tank>::iterator get_enemy_list_end()	{	return enemy_list.end();	}Tank* create_enemy();void enemy_move();
//	bool check_is_right(int x, int y, int dir);bool can_move(Tank &tank, string name);bool can_move(Tank& x, Tank& y);void check_missile(string name);void supply_enemy();int get_random_direction();list<Tank>::iterator remove_from_enemy_list(list<Tank>::iterator it)	{	return enemy_list.erase(it);		}void reduce_tot()	{	tot--;	}void increase_grade()	{	grade+=5;set_pos(68, 3);cout<<grade;	}
};int main()
{Tank_War g;g.game();system("pause");return 0;
}int Tank_War::game()
{bool is_move = true;int speed = draw_menu();system("cls");draw_game_interface();supply_enemy();while(!over){if(!lead.is_failure()){if(tot<N) 	supply_enemy();if(GetAsyncKeyState(VK_UP))	lead.set_dir(-1);else if(GetAsyncKeyState(VK_DOWN))	lead.set_dir(1);else if(GetAsyncKeyState(VK_LEFT))	lead.set_dir(-2);else if(GetAsyncKeyState(VK_RIGHT))	lead.set_dir(2);else is_move = false;if(is_move) lead.move(*this, "lead");	enemy_move();if(GetAsyncKeyState(VK_RETURN))	lead.emission_missile();	if(enemy_list.size() != 0){check_missile("lead");check_missile("enemy");}		}is_move = true;lead.missile_move();Sleep(100);}return 0;
}void set_pos(int x, int y)
{HANDLE cursor = GetStdHandle(STD_OUTPUT_HANDLE);COORD position = {x, y};SetConsoleCursorPosition(cursor, position);
}void draw_rect(int x_1, int y_1, int x_2, int y_2, string icon)
{if(x_1 > x_2)	swap(x_1, x_2);if(y_1 > y_2)	swap(y_1, y_2);int n_1 = y_2 - y_1 + 1;int n_2 = (x_2 - x_1)/icon.size() + 1;for(int i=0; i<n_1; i++){set_pos(x_1, y_1+i);for(int j=0; j<n_2; j++)cout<<icon;}
} int Tank_War::draw_menu()
{int speed = 0;draw_rect(0, 3, 79, 3, "-");draw_rect(0, 5, 79, 5, "-");set_pos(25, 2);cout<<"welcome to airplane war games ";set_pos(25, 4);cout<<"↑ up   ↓ down   enter confirm";set_pos(20, 10);cout<<"1.easy		easy mode enemy move slow";set_pos(20, 14);cout<<"2.difficult		difficult mode enemy move fast";set_pos(20, 20);cout<<"made by He Xiong .   begin at 2014_01_08";draw_rect(0, 19, 79, 19, "-");draw_rect(0, 22, 79, 22, "-");set_pos(18, 10);cout<<"->";int y = 10;while(true){if(GetAsyncKeyState(VK_UP) || GetAsyncKeyState(VK_DOWN)){if(y == 10){set_pos(18, y);		cout<<"  ";y = 14;set_pos(18, y);		cout<<"->";speed = 1;}else if(y == 14){set_pos(18, y);		cout<<"  ";y = 10;set_pos(18, y);		cout<<"->";speed = 0;}}else if(GetAsyncKeyState(VK_RETURN))		break;Sleep(200);}return speed;
}void Tank_War::draw_game_interface()
{draw_rect(0, 0, 78, 0, "◆");draw_rect(0, 23, 78, 23, "◆");draw_rect(0, 1, 0, 22, "◆");draw_rect(58, 1, 58, 22, "◆");draw_rect(78, 1, 78, 22, "◆");int x = 62;set_pos(x, 3);		cout<<"GRADE:0";set_pos(x, 5);		cout<<"LEVEL:1";set_pos(62, 7);		cout<<"↑ UP";set_pos(x, 9);		cout<<"↓ down";set_pos(x, 11);		cout<<"← left";set_pos(x, 13);		cout<<"→ right";set_pos(x, 17);		cout<<"life:■■";set_pos(x+5, 18);	cout<<"■■";lead.set_location(28, 10);lead.paint("■");set_pos(0, 0);
}Tank* Tank_War::create_enemy()
{int n = 0, k = 0;int x = 0, y = 0, dir = 0;Tank *p = NULL;srand((unsigned)time(NULL));while(k != 4){n = rand()%2;if(n%2 == 0){x = 4;			n = rand()%2;if(n%2 == 0)	y = 2;else			y = 21;}else{x = 54;			n = rand()%2;if(n%2 == 0)	y = 2;else			y = 21;}dir = get_random_direction();p = new Tank(x, y, dir, 1);enemy_list.push_back(*p);if(can_move(*p, "enemy")){	break;	}else	{	enemy_list.pop_back();	delete p;	p = NULL;	}k++;}return p;
}
/*
bool Tank::check_is_right(int x, int y, int dir)
{if(distance_squre(x, y, this -> x, this -> y) > 9)return true;else return false;
}
*//*
bool Tank_War::check_is_right(int x, int y, int dir)
{int flag = 0;if(!lead.check_is_right(x, y, dir))	++flag;list<Tank>::iterator it = NULL;for(it=enemy_list.begin(); it!=enemy_list.end(); ++it)if(!it -> check_is_right(x, y, dir))	++flag;if(flag == 0)	return true;else			return false;
}
*/
void Tank_War::supply_enemy()
{int n = N - tot;Tank *p = NULL;for(int i=0; i<n; i++){if((p = create_enemy()) != NULL){tot++;			p -> paint("■");delete p;		p = NULL;}}
}void Missile::paint(string icon)
{set_pos(x, y);	cout<<icon;
}void Tank::paint(string icon)
{draw_rect(x-2, y, x+2, y, icon);draw_rect(x, y-1, x, y+1, icon);if((dir+2)%2 == 0){set_pos(x-dir, y-1);	cout<<icon;set_pos(x-dir, y+1);	cout<<icon;}else if((dir+2)%2 == 1){set_pos(x-2, y-dir);	cout<<icon;set_pos(x+2, y-dir);	cout<<icon;}/*					//不是很好,前面两点的会闪set_pos(x-2, y-1);cout<<"■■■";set_pos(x-2, y);cout<<"■■■";set_pos(x-2, y+1);cout<<"■■■";if((dir+2)%2 == 0){set_pos(x+dir, y-1);	cout<<"  ";set_pos(x+dir, y+1);	cout<<"  ";draw_rect(x-dir, y-1, x-dir, y+1);}else if((dir+2)%2 == 1){set_pos(x-1, y+dir);	cout<<"  ";set_pos(x+1, y+dir);	cout<<"  ";draw_rect(x-2, y-dir, x+2, y-dir);}
*/
}bool Missile::move()
{if(x>2 && x<56 && y>1 && y <22){if((dir+2)%2 == 0){set_pos(x, y);	cout<<"  ";x += dir;		paint("●");}else if((dir+2)%2 == 1){set_pos(x, y);	cout<<"  ";y += dir;		paint("●");}return true;}else{if(x==0 || x==58 || y==0 || y==23)	;else{	set_pos(x, y);	cout<<"  ";}return false;}
}void Tank::move(Tank_War& game, string name)
{if(x>2 && x<56 && y>1 && y <22)if((dir+2)%2 == 0){Tank p(x+dir, y, dir, 1);if(x+dir>2 && x+dir<56){if(game.can_move(p, name)){//draw_rect(x-dir, y-1, x-dir, y+1, "  ");//draw_rect(x+dir, y, x+dir, y, "  ");	//为了解决在角落是的BUGdel_before_move();x+=dir;paint("■");}}else {if(pre_dir > 0)	draw_rect(x+dir, y-1, x+dir, y-1, "  ");else	draw_rect(x+dir, y+1, x+dir, y+1, "  ");paint("■");}}else{Tank p(x, y+dir, dir, 1);if(y+dir>1 && y+dir<22){if(game.can_move(p, name)){del_before_move();y+=dir;paint("■");}}else {if(pre_dir > 0)	draw_rect(x-2, y+dir, x-2, y+dir, "  ");else	draw_rect(x+2, y+dir, x+2, y+dir, "  ");paint("■");}}}void Tank::del_before_move()
{if(pre_dir == dir)	if((dir + 2) % 2 == 0)	draw_rect(x-dir, y-1, x-dir, y+1, "  ");else	draw_rect(x-2 , y-dir, x+2, y-dir, "  ");else if(pre_dir == -dir)	if((dir + 2) % 2 == 0)	draw_rect(x+pre_dir, y, x+pre_dir, y, "  ");else	draw_rect(x , y+pre_dir, x, y+pre_dir, "  ");else if(pre_dir == 2/dir)	if((dir + 2) % 2 == 0)	draw_rect(x-dir, y, x-dir, y-pre_dir, "  ");else	draw_rect(x, y-dir, x-pre_dir, y-dir, "  ");elseif((dir + 2) % 2 == 0)	draw_rect(x-dir, y, x-dir, y-pre_dir, "  ");else	draw_rect(x, y-dir, x-pre_dir, y-dir, "  ");
}bool Tank_War::can_move(Tank& tank, string name)
{if(name == "lead"){list<Tank>::iterator it = NULL;if(enemy_list.size() != 0){for(it=enemy_list.begin(); it!=enemy_list.end(); it++){if(!can_move(*it, tank))	return false;}}}else{int flag = 0;if(!can_move(lead, tank))	return false;list<Tank>::iterator it = NULL;if(enemy_list.size() != 0){for(it=enemy_list.begin(); it!=enemy_list.end(); it++){	if(!can_move(*it, tank)) flag++;/*if(tank == *it)	{if(flag)	flag = false;else		return false;}else{if(!can_move(tank,tank.get_dir() , *it))	return false;}*/}if(flag!=1) return false;else return true;}}return true;
}bool Tank_War::can_move(Tank& tank_1, Tank& tank_2)
{int x_1 = 0, x_2 = 0, y_1 = 0, y_2 = 0;int dis = tank_1.distance_squre(tank_2);if(dis > 8)	return true;else if(dis == 8){if((tank_1.get_dir()+2)%2 == 0){if(tank_1.get_dir() > 0)	{x_1 = tank_1.get_x();	x_2 = tank_2.get_x();y_1 = tank_1.get_y();	y_2 = tank_2.get_y();}else{x_1 = tank_2.get_x();	x_2 = tank_1.get_x();y_1 = tank_2.get_y();	y_2 = tank_1.get_y();}if(x_1 > x_2){if(y_1 > y_2){if(tank_2.get_dir() == tank_1.get_dir() || tank_2.get_dir() == 2/tank_1.get_dir())	return true;else	return false;}else{if(tank_2.get_dir() == tank_1.get_dir() || tank_2.get_dir() == -2/tank_1.get_dir())	return true;else	return false;}}else		return true;}else{if(tank_1.get_dir() > 0)	{x_1 = tank_1.get_x();	x_2 = tank_2.get_x();y_1 = tank_1.get_y();	y_2 = tank_2.get_y();}else{x_1 = tank_2.get_x();	x_2 = tank_1.get_x();y_1 = tank_2.get_y();	y_2 = tank_1.get_y();}if(y_1 > y_2){if(x_1 > x_2){if(tank_2.get_dir() == tank_1.get_dir() || tank_2.get_dir() == 2/tank_1.get_dir())	return true;else	return false;}else{if(tank_2.get_dir() == tank_1.get_dir() || tank_2.get_dir() == -2/tank_1.get_dir())	return true;else	return false;}}else		return true;}}else if(dis == 5)	{	//static int d=0;d++;set_pos(20,20+d);cout<<dis<<"D"<<endl;if((tank_1.get_dir()+2)%2 == 0){if(tank_1.get_dir() > 0)	{x_1 = tank_2.get_x();	x_2 = tank_1.get_x();y_1 = tank_2.get_y();	y_2 = tank_1.get_y();}else{x_1 = tank_1.get_x();	x_2 = tank_2.get_x();y_1 = tank_1.get_y();	y_2 = tank_2.get_y();}if(x_1 > x_2){if(y_1 > y_2){if(tank_2.get_dir() == -tank_1.get_dir() || tank_2.get_dir() == -2/tank_1.get_dir())	return true;else	return false;}else{if(tank_2.get_dir() == -tank_1.get_dir() || tank_2.get_dir() == -2/tank_1.get_dir())	return true;else	return false;}}else		return false;}else{if(tank_1.get_dir() < 0)	{x_1 = tank_1.get_x();	x_2 = tank_2.get_x();y_1 = tank_1.get_y();	y_2 = tank_2.get_y();}else{x_1 = tank_2.get_x();	x_2 = tank_1.get_x();y_1 = tank_2.get_y();	y_2 = tank_1.get_y();}if(y_1 > y_2){if(x_1 > x_2){if(tank_2.get_dir() == -tank_1.get_dir() || tank_2.get_dir() == -2/tank_1.get_dir())	return true;else	return false;}else{if(tank_2.get_dir() == -tank_1.get_dir() || tank_2.get_dir() == 2/tank_1.get_dir())	return true;else	return false;}}else		return false;}}else		return false;
}bool Object::is_failure()
{if(life != 0)	return false;else			return true;
}void Tank::emission_missile()
{Missile p;if((dir+2)%2 == 0)p.set_info(x+2*dir, y, dir);else if((dir+2)%2 == 1)p.set_info(x, y+2*dir, dir);missile_list.push_back(p);if(p.get_x()==0 || p.get_x()==58 || p.get_y()==0 || p.get_y()==23)	;else			p.paint("■");Sleep(100);
}void Tank::missile_move()
{list<Missile>::iterator it = NULL;if(missile_list.size() != 0)			//这里一定要判空for(it=missile_list.begin(); it!=missile_list.end(); it++){if(!it -> move())it = missile_list.erase(it);		//删除此步后当前迭代器会“失效”}
}void Tank_War::check_missile(string name)
{if(name == "lead")lead.check_missile(*this, "lead");else{list<Tank>::iterator it = NULL;for(it = enemy_list.begin(); it != enemy_list.end(); it++)it -> check_missile(*this, "enemy");}
}void Tank::check_missile(Tank_War &game, string name)
{list<Missile>::iterator it_1 = NULL;list<Tank>::iterator it_2 = NULL;if(name == "lead"){if(missile_list.size() != 0)for(it_1=missile_list.begin(); it_1!= missile_list.end(); it_1++){for(it_2=game.get_enemy_list_begin(); it_2!=game.get_enemy_list_end(); it_2++)if(it_1 != missile_list.end())if(check_missile_strike(*it_1, *it_2))	{it_1 = missile_list.erase(it_1);if(it_2 -> is_failure()){game.increase_grade();it_2 -> paint("  ");it_2 = game.remove_from_enemy_list(it_2);game.reduce_tot();}}	}}else{if(missile_list.size() != 0){if(it_1 != missile_list.end())for(it_1=missile_list.begin(); it_1!= missile_list.end(); it_1++)if(check_missile_strike(*it_1, *(game.get_lead())))	{it_1 = missile_list.erase(it_1);if(game.get_lead() -> is_failure())		game.game_over();else {game.get_lead() -> paint("  ");game.get_lead() -> set_location(28, 10);game.get_lead() -> paint("■");}}if(it_1 != missile_list.end())for(it_1=missile_list.begin(); it_1!= missile_list.end(); it_1++){for(it_2=game.get_enemy_list_begin(); it_2!=game.get_enemy_list_end(); it_2++)if(check_missile_strike(*it_1, *it_2))	{it_1 = missile_list.erase(it_1);it_2 -> increase_blood();}}}}
}bool Tank::check_missile_strike(Missile& missile, Tank& tank)
{int dis = 0;if((dis = distance_squre(missile.get_x(), missile.get_y(), tank.get_x(), tank.get_y())) <= 2){if(dis == 2){}else{missile.reduce_blood();tank.reduce_blood();missile.paint("  ");return true;}}return false;
}void Tank_War::enemy_move()
{int n = 0;srand((unsigned)time(NULL));list<Tank>::iterator it = NULL;if(enemy_list.size() != 0){for(it=enemy_list.begin(); it != enemy_list.end(); it++){it -> missile_move();n = rand()%3;if(n == 0)if(can_move(*it, "enemy")){n = get_random_direction();it -> set_dir(n);it->move(*this, "enemy");}n = rand()%8;if(n == 0)	it -> emission_missile();}}
}int Tank::distance_squre(int x_1, int y_1, int x_2, int y_2)
{return (x_2 - x_1) * (x_2 - x_1) + (y_2 - y_1) * (y_2 - y_1);
}int Tank::distance_squre(Tank& tank_2)
{return distance_squre(x/2, y, tank_2.get_x()/2, tank_2.get_y());
}int Tank_War::get_random_direction()
{int n = 0;srand((unsigned)time(NULL));if((n = rand()%4) < 2)		n -= 2;else		n -= 1;return n;
}

 

 

程序截图

首页

6e5ec33a8cee48ec9a8b3815a83072c7.png

 

 

程序执行界面

e0bd744179ac47c89c6b96f0c347b0ba.png

 

 

 

 

 

这篇关于12 c++版本的坦克大战的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/940414

相关文章

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

c++中std::placeholders的使用方法

《c++中std::placeholders的使用方法》std::placeholders是C++标准库中的一个工具,用于在函数对象绑定时创建占位符,本文就来详细的介绍一下,具有一定的参考价值,感兴... 目录1. 基本概念2. 使用场景3. 示例示例 1:部分参数绑定示例 2:参数重排序4. 注意事项5.

使用C++将处理后的信号保存为PNG和TIFF格式

《使用C++将处理后的信号保存为PNG和TIFF格式》在信号处理领域,我们常常需要将处理结果以图像的形式保存下来,方便后续分析和展示,C++提供了多种库来处理图像数据,本文将介绍如何使用stb_ima... 目录1. PNG格式保存使用stb_imagephp_write库1.1 安装和包含库1.2 代码解

C++实现封装的顺序表的操作与实践

《C++实现封装的顺序表的操作与实践》在程序设计中,顺序表是一种常见的线性数据结构,通常用于存储具有固定顺序的元素,与链表不同,顺序表中的元素是连续存储的,因此访问速度较快,但插入和删除操作的效率可能... 目录一、顺序表的基本概念二、顺序表类的设计1. 顺序表类的成员变量2. 构造函数和析构函数三、顺序表

使用C++实现单链表的操作与实践

《使用C++实现单链表的操作与实践》在程序设计中,链表是一种常见的数据结构,特别是在动态数据管理、频繁插入和删除元素的场景中,链表相比于数组,具有更高的灵活性和高效性,尤其是在需要频繁修改数据结构的应... 目录一、单链表的基本概念二、单链表类的设计1. 节点的定义2. 链表的类定义三、单链表的操作实现四、

java中不同版本JSONObject区别小结

《java中不同版本JSONObject区别小结》本文主要介绍了java中不同版本JSONObject区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录1. FastjsON2. Jackson3. Gson4. org.json6. 总结在Jav

使用C/C++调用libcurl调试消息的方式

《使用C/C++调用libcurl调试消息的方式》在使用C/C++调用libcurl进行HTTP请求时,有时我们需要查看请求的/应答消息的内容(包括请求头和请求体)以方便调试,libcurl提供了多种... 目录1. libcurl 调试工具简介2. 输出请求消息使用 CURLOPT_VERBOSE使用 C

C++实现获取本机MAC地址与IP地址

《C++实现获取本机MAC地址与IP地址》这篇文章主要为大家详细介绍了C++实现获取本机MAC地址与IP地址的两种方式,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 实际工作中,项目上常常需要获取本机的IP地址和MAC地址,在此使用两种方案获取1.MFC中获取IP和MAC地址获取