c++打飞机小游戏

这是我搜集到的小游戏

#include <iostream> 
using namespace std; 
double shengmingli=2000;//定义主角初始生命力 
int gongjili=150;//定义主角初始攻击力 
int fangyuli=200;//定义主角初始防御力 
int money=9999999;//定义主角初始金钱数量 
bool guoguan;//定义是否通关判定 
void wuqidian();//定义武器店函数 
void yaodian();//定义药店函数 
void guaiwu1();//定义小怪物函数 
void guaiwu2();//定义大怪物函数 
int main() 
{ 
	cout<<"欢迎你开始玩打怪物小游戏!\n"; 
	cout<<"小镇\n"; 
	cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl; 
	cout<<"和一家武器店。\n"; 
	int xiaozhen;//定义选择项目 
	cout<<"1.去武器店"<<endl; 
cout<<"2.去药品店"<<endl; 
cout<<"3.去打小怪物"<<endl; 
cout<<"4.去打大怪物"<<endl; 
cout<<"5.退出游戏"<<endl; 
cout<<"6.显示你的状态"<<endl; 
cin>>xiaozhen; 
while(xiaozhen!=5)//输入5时退出游戏 
{ 
if(shengmingli<=0)//主角生命力小于等于0时游戏结束 
{ 
cout<<"你死啦!"<<endl; 
break; 
} 
if(guoguan) 
{ 
cout<<"恭喜通关!"<<endl; 
break; 
} 
if(xiaozhen==6)//输入6可检测自己的状态 
{ 
cout<<"你的生命力:"<<shengmingli<<endl; 
cout<<"你的攻击力:"<<gongjili<<endl; 
cout<<"你的防御力:"<<fangyuli<<endl; 
cout<<"你拥有的钱:"<<money<<endl; 
} 
else 
switch(xiaozhen) 
{ 
case 1 : wuqidian();break; 
case 2 : yaodian();break; 
case 3 : guaiwu1();break; 
case 4 : guaiwu2();break; 
default : cout<<"请不要乱选!"<<endl;break; 
} 
cin>>xiaozhen; 
} 
if(xiaozhen==5) 
{ 
cout<<"正在退出游戏……"<<endl; 
} 
cin.get(); 
cin.get(); 
return 0; 
} 
void wuqidian() 
{ 
cout<<"欢迎来到武器店!"<<endl; 
cout<<"1、买小刀(1M加2攻击力)"<<endl; 
cout<<"2、买短剑(2M加20攻击力)"<<endl; 
cout<<"3、买大砍刀(5M加40攻击力)"<<endl; 
cout<<"4、买双节棍(7M加60攻击力)"<<endl; 
cout<<"5、买盾牌(2M加30防御力)"<<endl; 
cout<<"6、买铠甲(5M加60防御力)"<<endl; 
cout<<"7、离开武器店"<<endl; 
int wuqidian; 
cin>>wuqidian; 
while(wuqidian!=7)//输入7时结束函数 
{ 
switch(wuqidian) 
{ 
case 1 : if(money<10) 
cout<<"你的钱不够"<<endl;//钱不够时返回Flase 
else  
cout<<"购买成功!"<<endl;//钱足够时返回True 
gongjili+=2; 
money-=1; 
break; 
case 2 : if(money<80) 
cout<<"你的钱不够"<<endl; 
else  
cout<<"购买成功!"<<endl; 
gongjili+=20; 
money-=80; 
break; 
case 3 : if(money<140) 
cout<<"你的钱不够"<<endl; 
    else  
cout<<"购买成功!"<<endl; 
gongjili+=40; 
money-=140; 
break; 
case 4 : if(money<200) 
cout<<"你的钱不够"<<endl; 
else  
cout<<"购买成功!"<<endl; 
gongjili+=60; 
money-=200; 
break; 
case 5 : if(money<60) 
cout<<"你的钱不够"<<endl; 
else  
cout<<"购买成功!"<<endl; 
fangyuli+=30; 
money-=60; 
break; 
fangyuli+=60; 
money-=100; 
break; 
default : cout<<"无"<<endl; 
    break; 
}  
cin>>wuqidian; 
} 
if(wuqidian==7) 
{	  //返回main()主函数 
cout<<"欢迎下次再来!"<<endl; 
cout<<"欢迎你开始玩打怪物小游戏!\n"; 
cout<<"小镇\n"; 
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl; 
cout<<"和一家武器店。\n"; 
cout<<"1.去武器店"<<endl; 
cout<<"2.去药品店"<<endl; 
cout<<"3.去打小怪物"<<endl; 
cout<<"4.去打大怪物"<<endl; 
cout<<"5.退出游戏"<<endl; 
cout<<"6.显示你的状态"<<endl; 
} 
} 
/* 
yaodian()的设置与wuqidian()相同,可参照阅读. 
*/ 
void yaodian() 
{ 
cout<<"欢迎来到药品店!"<<endl; 
cout<<"1、买1号补血药(10M加200生命)"<<endl; 
cout<<"2、买2号补血药(50M加1000生命力)"<<endl; 
cout<<"3、买3号补血药(100M加2200生命力)"<<endl; 
cout<<"4、离开武器店"<<endl; 
int yaodian; 
cin>>yaodian; 
while(yaodian!=4) 
{ 
switch(yaodian) 
{ 
case 1 : if(money<10) 
cout<<"你的钱不够"<<endl; 
else  
cout<<"购买成功!"<<endl; 
shengmingli+=200; 
money-=10; 
break; 
case 2 : if(money<50) 
cout<<"你的钱不够"<<endl; 
else  
cout<<"购买成功!"<<endl; 
shengmingli+=1000; 
money-=50; 
break; 
case 3 : if(money<100) 
cout<<"你的钱不够"<<endl; 
else  
cout<<"购买成功!"<<endl; 
shengmingli+=2200; 
money-=100; 
break; 
default : cout<<"无"<<endl; 
break; 
} 
cin>>yaodian; 
} 
if(yaodian==4) 
{	   
cout<<"欢迎下次再来!"<<endl;	   
cout<<"欢迎你开始玩打怪物小游戏!\n"; 
cout<<"小镇\n"; 
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl; 
cout<<"和一家武器店。\n"; 
cout<<"1.去武器店"<<endl; 
cout<<"2.去药品店"<<endl; 
cout<<"3.去打小怪物"<<endl; 
cout<<"4.去打大怪物"<<endl; 
cout<<"5.退出游戏"<<endl; 
cout<<"6.显示你的状态"<<endl; 
} 
} 
/*这里是两个战斗函数,使用指针来处理.避免造成内存崩溃.*/ 
void guaiwu1() 
{ 
cout<<"开始与小怪物战斗!!!"<<endl; 
double* g_shengmingli=new double;//定义怪物生命 
int* g_gongjili=new int;//定义怪物攻击力 
int* g_fangyuli=new int;//定义怪物防御力 
    int* g_money=new int;//定义怪物金钱 
*g_shengmingli=100; 
*g_gongjili=5; 
*g_fangyuli=3; 
*g_money=5; 
double* tongji1=new double;//用来计算主角对怪物的杀伤 
double* tongji2=new double;//用来计算怪物对主角的杀伤 
*tongji1=0; 
*tongji2=0; 
int* huihe=new int;//定义回合数 
*huihe=1; 
cout<<"你开始对小怪物进行攻击!"<<endl; 
int* xuanze=new int; 
/* 
攻击计算公式 
杀伤=攻击力*2-防御力 
玩家每回合可以选择攻击与逃跑 
*/ 
while((*g_shengmingli)>0 && shengmingli>0 && (*xuanze)!=2) 
{ 
cout<<"现在是"<<"第"<<*huihe<<"回合!"<<endl; 
cout<<"请选择你的动作:\n"; 
cout<<"1、攻击\n2、逃跑\n"; 
cin>>*xuanze; 
switch((*xuanze)) 
{ 
case 1 : cout<<"你对小怪物发动了攻击!"<<endl; 
*g_shengmingli-=gongjili*2-(*g_fangyuli); 
*tongji1=gongjili*2-(*g_fangyuli); 
cout<<"你打掉了小怪物"<<*tongji1<<"的生命!"<<endl; 
cout<<"小怪物还剩"<<(*g_shengmingli)-(*tongji1)<<"点生命"<<endl; 
shengmingli-=(*g_gongjili)*2-fangyuli; 
*tongji2=(*g_gongjili)*2-fangyuli; 
cout<<"小怪物对你发动了攻击!"<<endl; 
cout<<"小怪物打掉了你"<<*tongji2<<"的生命!"<<endl; 
cout<<"你还剩"<<shengmingli-(*tongji2)<<"点生命"<<endl;break; 
case 2 : cout<<"你决定逃跑!"<<endl; 
cout<<"逃跑成功!"<<endl;continue; 
default : cout<<"请不要乱选!"<<endl; 
} 
(*huihe)++; 
} 
if((*g_shengmingli)<=0) 
{//杀死怪物后的返回 
cout<<"小怪物被你杀死了!你真厉害!!!"<<endl; 
money+=(*g_money); 
cout<<"欢迎你开始玩打怪物小游戏!\n"; 
cout<<"小镇\n"; 
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl; 
cout<<"和一家武器店。\n"; 
cout<<"1.去武器店"<<endl; 
cout<<"2.去药品店"<<endl; 
cout<<"3.去打小怪物"<<endl; 
cout<<"4.去打大怪物"<<endl; 
cout<<"5.退出游戏"<<endl; 
cout<<"6.显示你的状态"<<endl; 
} 
else 
if(shengmingli<=0) 
{//被怪物杀死后的返回 
cout<<"你被小怪物杀死了!游戏结束!!!"<<endl; 
} 
else 
if((*xuanze)==2) 
{//逃跑的返回 
cout<<"你逃回了小镇!"<<endl; 
cout<<"欢迎你开始玩打怪物小游戏!\n"; 
cout<<"小镇\n"; 
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl; 
cout<<"和一家武器店。\n"; 
cout<<"1.去武器店"<<endl; 
cout<<"2.去药品店"<<endl; 
cout<<"3.去打小怪物"<<endl; 
cout<<"4.去打大怪物"<<endl; 
cout<<"5.退出游戏"<<endl; 
cout<<"6.显示你的状态"<<endl; 
} 
delete g_shengmingli; 
delete g_gongjili; 
delete g_fangyuli; 
delete g_money; 
delete tongji1; 
delete tongji2; 
} 
/* 
设置均与void函数guaiwu1()相同,可参照上例阅读. 
*/ 
void guaiwu2() 
{ 
cout<<"开始与大怪物战斗!!!"<<endl; 
double* g_shengmingli=new double; 
int* g_gongjili=new int; 
int* g_fangyuli=new int; 
*g_shengmingli=3600; 
*g_gongjili=500; 
*g_fangyuli=500; 
double* tongji1=new double; 
double* tongji2=new double; 
*tongji1=0; 
*tongji2=0; 
int* huihe=new int; 
*huihe=1; 
cout<<"你开始对大怪物进行攻击!"<<endl; 
int* xuanze=new int; 
while((*g_shengmingli)>0 && shengmingli>0 && (*xuanze)!=2) 
{ 
cout<<"现在是"<<"第"<<*huihe<<"回合!"<<endl; 
cout<<"请选择你的动作:\n"; 
cout<<"1、攻击\n2、逃跑\n"; 
cin>>*xuanze; 
switch((*xuanze)) 
{ 
case 1 : cout<<"你对大怪物发动了攻击!"<<endl; 
*g_shengmingli-=gongjili*2-(*g_fangyuli); 
*tongji1=gongjili*2-(*g_fangyuli); 
cout<<"你打掉了大怪物"<<*tongji1<<"的生命!"<<endl; 
cout<<"大怪物还剩"<<(*g_shengmingli)-(*tongji1)<<"点生命"<<endl; 
shengmingli-=(*g_gongjili)*2-fangyuli; 
*tongji2=(*g_gongjili)*2-fangyuli; 
cout<<"大怪物对你发动了攻击!"<<endl; 
cout<<"大怪物打掉了你"<<*tongji2<<"的生命!"<<endl; 
cout<<"你还剩"<<shengmingli-(*tongji2)<<"点生命"<<endl;break; 
case 2 : cout<<"你决定逃跑!"<<endl; 
cout<<"逃跑成功!"<<endl;continue; 
default : cout<<"请不要乱选!"<<endl; 
} 
(*huihe)++; 
} 
if((*g_shengmingli)<=0) 
{ 
    cout<<"大怪物被你杀死了!你真厉害!!!"<<endl; 
guoguan=true; 
cout<<"欢迎你开始玩打怪物小游戏!\n"; 
cout<<"小镇\n"; 
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl; 
cout<<"和一家武器店。\n"; 
cout<<"1.去武器店"<<endl; 
cout<<"2.去药品店"<<endl; 
cout<<"3.去打小怪物"<<endl; 
cout<<"4.去打大怪物"<<endl; 
cout<<"5.退出游戏"<<endl; 
cout<<"6.显示你的状态"<<endl; 
} 
else 
if(shengmingli<=0) 
{ 
cout<<"你被大怪物杀死了!游戏结束!!!"<<endl; 
} 
else 
if((*xuanze)==2) 
{ 
cout<<"你逃回了小镇!"<<endl; 
cout<<"欢迎你开始玩打怪物小游戏!\n"; 
cout<<"小镇\n"; 
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl; 
cout<<"和一家武器店。\n"; 
cout<<"1.去武器店"<<endl; 
cout<<"2.去药品店"<<endl; 
cout<<"3.去打小怪物"<<endl; 
cout<<"4.去打大怪物"<<endl; 
cout<<"5.退出游戏"<<endl; 
cout<<"6.显示你的状态"<<endl; 
} 
delete g_shengmingli; 
delete g_gongjili; 
delete g_fangyuli; 
delete tongji1; 
delete tongji2; 
}

 

#include<iostream>
#include<windows.h>
#include<conio.h>
#include<time.h>
#include<string>
using namespace std;
 
/*=============== all the structures ===============*/
 
typedef struct Frame
{
	COORD position[2];
	int flag;
}Frame;
 
 
/*=============== all the functions ===============*/
 
void SetPos(COORD a)// set cursor 
{
	HANDLE out=GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(out, a);
}
 
void SetPos(int i, int j)// set cursor
{
	COORD pos={i, j};
	SetPos(pos);
}
 
void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = {1, 0}; 
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
 
//把第y行,[x1, x2) 之间的坐标填充为 ch
void drawRow(int y, int x1, int x2, char ch)
{
	SetPos(x1,y);
	for(int i = 0; i <= (x2-x1); i++)
		cout<<ch;
}
 
//在a, b 纵坐标相同的前提下,把坐标 [a, b] 之间填充为 ch
void drawRow(COORD a, COORD b, char ch)
{
	if(a.Y == b.Y)
		drawRow(a.Y, a.X, b.X, ch);
	else
	{
		SetPos(0, 25);
		cout<<"error code 01:无法填充行,因为两个坐标的纵坐标(x)不相等";
		system("pause");
	}
}
 
//把第x列,[y1, y2] 之间的坐标填充为 ch
void drawCol(int x, int y1, int y2, char ch)
{
	int y=y1;
	while(y!=y2+1)
	{
		SetPos(x, y);
		cout<<ch;
		y++;
	}
}
 
//在a, b 横坐标相同的前提下,把坐标 [a, b] 之间填充为 ch
void drawCol(COORD a, COORD b, char ch)
{
	if(a.X == b.X)
		drawCol(a.X, a.Y, b.Y, ch);
	else
	{
		SetPos(0, 25);
		cout<<"error code 02:无法填充列,因为两个坐标的横坐标(y)不相等";
		system("pause");
	}
}
 
//左上角坐标、右下角坐标、用row填充行、用col填充列
void drawFrame(COORD a, COORD  b, char row, char col)
{
	drawRow(a.Y, a.X+1, b.X-1, row);
	drawRow(b.Y, a.X+1, b.X-1, row);
	drawCol(a.X, a.Y+1, b.Y-1, col);
	drawCol(b.X, a.Y+1, b.Y-1, col);
}
 
void drawFrame(int x1, int y1, int x2, int y2, char row, char col)
{
	COORD a={x1, y1};
	COORD b={x2, y2};
	drawFrame(a, b, row, col);
}
 
void drawFrame(Frame frame, char row, char col)
{
	COORD a = frame.position[0];
	COORD b = frame.position[1];
	drawFrame(a, b, row, col);
}
 
void drawPlaying()
{
	drawFrame(0, 0, 48, 24, '=', '|');//	draw map frame;
	drawFrame(49, 0, 79, 4, '-', '|');//		draw output frame
	drawFrame(49, 4, 79, 9, '-', '|');//		draw score frame
	drawFrame(49, 9, 79, 20, '-', '|');//	draw operate frame
	drawFrame(49, 20, 79, 24, '-', '|');//	draw other message frame
	SetPos(52, 6);
	cout<<"得分:";
	SetPos(52, 7);
	cout<<"称号:";
	SetPos(52,10);
	cout<<"操作方式:";
	SetPos(52,12);
	cout<<"  a,s,d,w 控制战机移动。";
	SetPos(52,14);
	cout<<"  p 暂停游戏。";
	SetPos(52,16);
	cout<<"  e 退出游戏。";
}
 
//在[a, b)之间产生一个随机整数
int random(int a, int b)
{
	int c=(rand() % (a-b))+ a;
	return c;
}
 
//在两个坐标包括的矩形框内随机产生一个坐标
COORD random(COORD a, COORD b)
{
	int x=random(a.X, b.X);
	int y=random(a.Y, b.Y);
	COORD c={x, y};
	return c;
}
 
bool  judgeCoordInFrame(Frame frame, COORD spot)
{
	if(spot.X>=frame.position[0].X)
		if(spot.X<=frame.position[1].X)
			if(spot.Y>=frame.position[0].Y)
				if(spot.Y<=frame.position[0].Y)
					return true;
	return false;
}
 
void printCoord(COORD a)
{
	cout	<<"( "<<a.X<<" , "<<a.Y<<" )";
}
 
void printFrameCoord(Frame a)
{
	printCoord(a.position[0]);
	cout	<<" - ";
	printCoord(a.position[1]);
}
 
int drawMenu()
{
	SetPos(30, 1);
	cout<<"P l a n e  W a r";
	drawRow(3, 0, 79, '-');
	drawRow(5, 0, 79, '-');
	SetPos(28, 4);
	cout<<"w 和 s 选择, k 确定";
	SetPos(15, 11);
	cout<<"1. 简单的敌人";
	SetPos(15, 13);
	cout<<"2. 冷酷的敌人";
	drawRow(20, 0, 79, '-');
	drawRow(22, 0, 79, '-');
	SetPos(47, 11);
	cout<<"简单的敌人:";
	SetPos(51, 13);
	cout<<"简单敌人有着较慢的移动速度。";
	SetPos(24, 21);
	cout<<"制作:<bits/stdc++.h>";
	int j=11;
	SetPos(12, j);
	cout<<">>";
 
	//drawFrame(45, 9, 79, 17, '=', '|');
 
	while(1)
	{	if( _kbhit() )
		{	
			char x=_getch();
			switch (x)
			{
			case 'w' :
					{	
						if( j == 13)
						{
							SetPos(12, j);
							cout<<" ";
							j = 11;
							SetPos(12, j);
							cout<<">>";
							SetPos(51, 13);
							cout<<"            ";
							SetPos(47, 11);
							cout<<"简单的敌人:";
							SetPos(51, 13);
							cout<<"简单敌人有较慢的移动速度,比较容易对付";
						}
						break;
					}
			case 's' :
					{	
						if( j == 11 )
						{
							SetPos(12, j);
							cout<<" ";		
							j = 13;
							SetPos(12, j);
							cout<<">>";
							SetPos(51, 13);
							cout<<"              ";
							SetPos(47, 11);
							cout<<"冷酷的敌人:";
							SetPos(51, 13);
							cout<<"冷酷的敌人移动速度较快,不是很好对付哟。";
						}
						break;
					}
			case 'k' :
					{	
						if (j == 8)	return 1;
						else return 2;
					}
			}
		}
	}
}
 
/* 
DWORD WINAPI MusicFun(LPVOID lpParamte)
{
	//DWORD OBJ;
	sndPlaySound(TEXT("bgm.wav"), SND_FILENAME|SND_ASYNC);
	return 0;
}
*/
 
 
/*================== the Game Class ==================*/
 
class Game
{
public:
	COORD position[10];
	COORD bullet[10];
	Frame enemy[8];
	int score;
	int rank;
	int rankf;
	string title;
	int flag_rank;
 
	Game ();
	
	//初始化所有
	void initPlane();
	void initBullet();
	void initEnemy();
 
	//初始化其中一个
	//void initThisBullet( COORD );
	//void initThisEnemy( Frame );
 
	void planeMove(char);
	void bulletMove();
	void enemyMove();
	
	//填充所有
	void drawPlane();
	void drawPlaneToNull();
	void drawBullet();
	void drawBulletToNull();
	void drawEnemy();
	void drawEnemyToNull();
 
	//填充其中一个
	void drawThisBulletToNull( COORD );
	void drawThisEnemyToNull( Frame );
 
	void Pause();
	void Playing();
	void judgePlane();
	void judgeEnemy();
 
	void Shoot();
 
	void GameOver();
	void printScore();
};
 
Game::Game()
{
	initPlane();
	initBullet();
	initEnemy();
	score = 0;
	rank = 25;
	rankf = 0;
	flag_rank = 0;
}
 
void Game::initPlane()
{
	COORD centren={39, 22};
	position[0].X=position[5].X=position[7].X=position[9].X=centren.X;
	position[1].X=centren.X-2;	
	position[2].X=position[6].X=centren.X-1;
	position[3].X=position[8].X=centren.X+1;
	position[4].X=centren.X+2;
	for(int i=0; i<=4; i++)
		position[i].Y=centren.Y;
	for(int i=6; i<=8; i++)
		position[i].Y=centren.Y+1;
	position[5].Y=centren.Y-1;
	position[9].Y=centren.Y-2;
}
 
void Game::drawPlane()
{
	for(int i=0; i<9; i++)
	{
		SetPos(position[i]);
		if(i!=5)
			cout<<"O";
		else if(i==5)
			cout<<"|";		
	}
}
 
void Game::drawPlaneToNull()
{
	for(int i=0; i<9; i++)
	{
		SetPos(position[i]);
		cout<<" ";
	}	
}
 
void Game::initBullet()
{
	for(int i=0; i<10; i++)
		bullet[i].Y = 30;
}
 
void Game::drawBullet()
{
	for(int i=0; i<10; i++)
	{
		if( bullet[i].Y != 30)
		{
			SetPos(bullet[i]);
			cout<<"^";	
		}
	}
}
 
void Game::drawBulletToNull()
{
	for(int i=0; i<10; i++)
		if( bullet[i].Y != 30 )
			{
				COORD pos={bullet[i].X, bullet[i].Y+1};
				SetPos(pos);
				cout<<" ";
			}	
}
 
void Game::initEnemy()
{
	COORD a={1, 1};
	COORD b={45, 15};
	for(int i=0; i<8; i++)
	{
		enemy[i].position[0] = random(a, b);
		enemy[i].position[1].X = enemy[i].position[0].X + 3;
		enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
	}
}
 
void Game::drawEnemy()
{
	for(int i=0; i<8; i++)
		drawFrame(enemy[i].position[0], enemy[i].position[1], '-', '|');
}
 
void Game::drawEnemyToNull()
{
	for(int i=0; i<8; i++)
	{
		drawFrame(enemy[i].position[0], enemy[i].position[1], ' ', ' ');
	}		
}
 
void Game::Pause()
{
	SetPos(61,2);
	cout<<"               ";
	SetPos(61,2);
	cout<<"暂停中...";
	char c=_getch();
	while(c!='p')
		c=_getch();
	SetPos(61,2);
	cout<<"         ";
}
 
void Game::planeMove(char x)
{
	if(x == 'a')
		if(position[1].X != 1)
			for(int i=0; i<=9; i++)
				position[i].X -= 2;
				
	if(x == 's')
		if(position[7].Y != 23)
			for(int i=0; i<=9; i++)
				position[i].Y += 1;
 
	if(x == 'd')
		if(position[4].X != 47)
			for(int i=0; i<=9; i++)
				position[i].X += 2;
 
	if(x == 'w')
		if(position[5].Y != 3)
			for(int i=0; i<=9; i++)
				position[i].Y -= 1;
}
 
void Game::bulletMove()
{
	for(int i=0; i<10; i++)
	{
		if( bullet[i].Y != 30)
		{
			bullet[i].Y -= 1;
			if( bullet[i].Y == 1 )
			{
				COORD pos={bullet[i].X, bullet[i].Y+1};
				drawThisBulletToNull( pos );
				bullet[i].Y=30;
			}
				
		}
	}
}
 
void Game::enemyMove()
{
	for(int i=0; i<8; i++)
	{
		for(int j=0; j<2; j++)
			enemy[i].position[j].Y++;
 
		if(24 == enemy[i].position[1].Y)
		{
			COORD a={1, 1};
			COORD b={45, 3};
			enemy[i].position[0] = random(a, b);
			enemy[i].position[1].X = enemy[i].position[0].X + 3;
			enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
		}
	}
}
 
void Game::judgePlane()
{
	for(int i = 0; i < 8; i++)
		for(int j=0; j<9; j++)
			if(judgeCoordInFrame(enemy[i], position[j]))
			{
				SetPos(62, 1);
				cout<<"坠毁";
				drawFrame(enemy[i], '+', '+');
				Sleep(1000);
				GameOver();
				break;
			}
}
 
void Game::drawThisBulletToNull( COORD c)
{
	SetPos(c);
	cout<<" ";
}
 
void Game::drawThisEnemyToNull( Frame f )
{
	drawFrame(f, ' ', ' ');
}
 
void Game::judgeEnemy()
{
	for(int i = 0; i < 8; i++)
		for(int j = 0; j < 10; j++)
			if( judgeCoordInFrame(enemy[i], bullet[j]) )
			{
				score += 5;
				drawThisEnemyToNull( enemy[i] );
				COORD a={1, 1};
				COORD b={45, 3};
				enemy[i].position[0] = random(a, b);
				enemy[i].position[1].X = enemy[i].position[0].X + 3;
				enemy[i].position[1].Y = enemy[i].position[0].Y + 2;					
				drawThisBulletToNull( bullet[j] );
				bullet[j].Y = 30;
			}
}
 
void Game::Shoot()
{
	for(int i=0; i<10; i++)
		if(bullet[i].Y == 30)
		{
			bullet[i].X = position[5].X;
			bullet[i].Y = position[5].Y-1;
			break;
		}
}
 
void Game::printScore()
{
	if(score == 120 && flag_rank == 0)
	{
		rank -= 3;
		flag_rank = 1;
	}
 
	else if( score == 360 && flag_rank == 1)
	{
		rank -= 5;
		flag_rank = 2;
	}
	else if( score == 480 && flag_rank == 2)
	{
		rank -= 5;
		flag_rank = 3;
	}
	int x=rank/5;
	SetPos(60, 6);
	cout<<score;
 
	if( rank!=rankf )
	{
		SetPos(60, 7);
		if( x == 5)
			title="初级飞行员";
		else if( x == 4)
			title="中级飞行员";
		else if( x == 3)
			title="高级飞行员";
		else if( x == 2 )
			title="王牌飞行员";
		cout<<title;
	}
	rankf = rank;
}
 
void Game::Playing()
{
	//HANDLE MFUN;
	//MFUN= CreateThread(NULL, 0, MusicFun, NULL, 0, NULL); 
 
	drawEnemy();
	drawPlane();
 
	int flag_bullet = 0;
	int flag_enemy = 0;
 
	while(1)
	{
		Sleep(8);
		if(_kbhit())
		{
			char x = _getch();
			if ('a' == x || 's' == x || 'd' == x || 'w' == x)
			{
				drawPlaneToNull();
				planeMove(x);
				drawPlane();
				judgePlane();
			}			
			else if ('p' == x)
				Pause();
			else if( 'k' == x)
				Shoot();
			else if( 'e' == x)
			{
				//CloseHandle(MFUN);
				GameOver();
				break;
			}
				
		}
		/* 处理子弹 */
		if( 0 == flag_bullet )
		{
			bulletMove();
			drawBulletToNull();
			drawBullet();
			judgeEnemy();
		}			
		flag_bullet++;
		if( 5 == flag_bullet )
			flag_bullet = 0;
 
		/* 处理敌人 */
		if( 0 == flag_enemy )
		{
			drawEnemyToNull();
			enemyMove();			
			drawEnemy();
			judgePlane();
		}
		flag_enemy++;
		if( flag_enemy >= rank )
			flag_enemy = 0;
 
		/* 输出得分 */
		printScore();
	}
}
 
void Game::GameOver()
{
	system("cls");				
	COORD p1={28,9};
	COORD p2={53,15};
	drawFrame(p1, p2, '=', '|');
	SetPos(36,12);
	string str="Game Over!";
	for(int i=0; i<str.size(); i++)
	{
		Sleep(80);
		cout<<str[i];
	}
	Sleep(1000);
	system("cls");
	drawFrame(p1, p2, '=', '|');
	SetPos(31, 11);
	cout<<"击落敌机:"<<score/5<<" 架";
	SetPos(31, 12);
	cout<<"得  分:"<<score;
	SetPos(31, 13);
	cout<<"获得称号:"<<title;
	SetPos(30, 16);
	Sleep(1000);
	cout<<"继续? 是(y)| 否(n)制作:<bits/stdc++.h>";
as:
	char x=_getch();
	if (x == 'n')
		exit(0);
	else if (x == 'y')
	{
		system("cls");
		Game game;
		int a = drawMenu();
		if(a == 2)
			game.rank = 20;
		system("cls");
		drawPlaying();
		game.Playing();
	}
	else goto as;
}
 
/*================== the main function ==================*/
int main()
{
	//游戏准备
	srand((int)time(0));	//随机种子
	HideCursor();	//隐藏光标
	
	Game game;
	int a = drawMenu();
	if(a == 2)
		game.rank = 20;
	system("cls");
	drawPlaying();
	game.Playing();
}

 

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<conio.h>
#include<windows.h>
#include<cstdlib>
#include<ctime>
using namespace std;
 
#define MAXN 35
#define MIDX 10
#define MIDY 40
#define CG 25
#define CK 80
 
int G,K,Lnum,Wnum;//G为地图高,K为地图,Lnum为地图中的雷数,Wnum为剩余的小旗数
int nx,ny;//现在光标所在的位置
bool QR=0,Lose=0,is_flag_true[MAXN][MAXN];//QR为确认模式是否打开,Lose为是否输,第三个是这个位置上的旗是否放对
char map[MAXN][MAXN],tmap[MAXN][MAXN];//第一个是只有雷和空地的地图,第二个是玩家能看到的地图
int map1[MAXN][MAXN],mapc[MAXN][MAXN];//map1为数字的地图,其中0代表空地,-1为雷,1-8为周围雷的个数
//mapc为当前格子的颜色
int col[10]={240,249,242,252,241,244,243,240,248};//col[i]表示windows扫雷中i的颜色,col[0]为空格的颜色
int d[10][4]={{0},{0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};//8个方向
bool ZB;//作弊是否打开
 
/*各种函数*/
void color(int);//颜色
void gto(int,int);//光标位置
void make();//制作随机地图
void print();//打印地图等
bool check(int,int);//判断坐标是否合法
bool is_win();//判断是否赢
bool is_lose();//是否输
void dfs(int,int);//用深搜来打开方块
void st(int,int);//试探,即windows扫雷中的左右键同时按
void flag(int,int);//小旗
void bj(int,int);//标记
void swt();//确认模式
void again();//重新开始
void zb();//作弊模式
void mainmain();//主函数
void print_real_map();//打印最终的地图
void begin();//各种操作
 
int main()
{
    mainmain();
}
 
 
void color(int a){SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);}
void gto(int x,int y)
{
    COORD pos;pos.X=y;pos.Y=x;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
 
void make()
{
    for(int i=1;i<=G;i++)
        for(int j=1;j<=K;j++)
            map[i][j]='#';//初始化
    for(int i=1;i<=Lnum;i++)
    {
        int x=rand()%G+1,y=rand()%K+1;
        while(map[x][y]=='O')
            x=rand()%G+1,y=rand()%K+1;
        map[x][y]='O';
    }//随机放雷
    for(int i=1;i<=G;i++)
        for(int j=1;j<=K;j++)
        {
            if(map[i][j]=='O')map1[i][j]=-1,mapc[i][j]=240;//如果是雷
            else
            {
                for(int k=1;k<=8;k++)
                    if(map[i+d[k][0]][j+d[k][1]]=='O')
                        map1[i][j]++;//计算周围雷的个数
                mapc[i][j]=col[map1[i][j]];//根据格子上的数设置颜色
            }
        }
    for(int i=1;i<=G;i++)
        for(int j=1;j<=K;j++)
            if(mapc[i][j]==0)//空地
                mapc[i][j]=240;
}
void print()
{
    system("cls");
    gto(0,MIDY-4); color(233); printf("扫雷");
    color(240);
    gto(1,MIDY);
    for(int i=2;i<=G+1;i++)
    {
        gto(i,0);
        for(int j=1;j<=K;j++)
            printf("#"),tmap[i-1][j]='#';//初始化玩家所看到的地图
    }
 
    gto(2,0);
    nx=2,ny=0;
    color(15);
    printf("@");
 
    color(15);
    gto(2,2*K+5);printf("-----规则-----");
    gto(3,2*K+5);printf("wasd:选择位置");
    gto(4,2*K+5);printf("空格:打开");
    gto(5,2*K+5);printf("1键:试探周围8个方块,如果其中有雷则不会打开,无");
    gto(6,2*K+5);printf("     雷或旗帜标对了则会将周围无雷的位置打开,");
    gto(7,2*K+5);printf("     如果试探时周围有标错的旗帜,则会游戏失败");
    gto(8,2*K+5);printf("     必须额外确认一次,以便查看周围被试探的区域");
    gto(9,2*K+5);printf("2键:放置/取消小旗(F)");
    gto(10,2*K+5);printf("3键:放置/取消标记(?)");
    gto(11,2*K+5);printf("4键:打开/关闭确认模式,即每次操作需再按一次确认");
    gto(12,2*K+5);printf("5键:打开/关闭作弊模式,即显示原本地图");
    gto(13,2*K+5);printf("0键:重新开始");//打印规则
 
    gto(G+4,0);printf("-----操作提示-----\n");
    printf("请选择方块进行操作");
 
    gto(1,2*K+10);color(12);printf("剩余小旗数:%d",Wnum=Lnum);
}
 
bool check(int x,int y){return y>=0&&y<K&&x>=2&&x<G+2;}
//因为地图是从2行0列开始打的,而地图是从1行1列开始存的,所以gto(x,y)走到的是map[x-1][y+1]
bool is_win()
{
    int cnt=0;
    for(int i=1;i<=G;i++)
        for(int j=1;j<=K;j++)
            if(map[i][j]=='#'&&map1[i][j]==-1)
                cnt++;
    if(cnt==Lnum) return 1;
    //所有没被打开的方块都是雷=>胜利
    for(int i=1;i<=G;i++)
        for(int j=1;j<=K;j++)
            if((tmap[i][j]!='F'&&map1[i][j]==-1)||(tmap[i][j]=='F'&&map1[i][j]!=-1))
                return 0;
    return 1;
    //所有雷都标有旗
}
bool is_lose(){return Lose;}
 
void dfs(int x,int y)
{
    if(map1[x-1][y+1]>0)//只要边界全部是数字就return
    {
        gto(x,y),color(mapc[x-1][y+1]),printf("%d",map1[x-1][y+1]);
        tmap[x-1][y+1]=map1[x-1][y+1]+'0';
        return;
    }
    gto(x,y);color(255);
    tmap[x-1][y+1]=' ';
    printf(" ");//因为下面判断了雷,上面判断了数字,这里就一定是空地
    for(int i=1;i<=8;i++)
    {
        int xx=x+d[i][0]-1,yy=y+d[i][1]+1;//这里的xx和yy是在map中的,而不是gto中的
        if(check(xx+1,yy-1)&&tmap[xx][yy]=='#'&&map1[xx][yy]!=-1)//所以check和dfs的参数要变化
            dfs(xx+1,yy-1);
    }
}
void st(int x,int y)
{
    for(int i=1;i<=8;i++)
    {
        int xx=x+d[i][0],yy=y+d[i][1];
        if(check(xx,yy))
        {
            gto(xx,yy);
            if(tmap[xx-1][yy+1]!='#')
                color(mapc[xx-1][yy+1]-128);//减去128使周围的8个格子的背景颜色变为灰色
            else
                color(112);//这里特判一下'#',应该可以不用
            printf("%c",tmap[xx-1][yy+1]);
        }
    }
    gto(G+5,0),color(15),printf("请确认                                      ");
    //试探必须额外确认一次,规则上有说
    char c=getch();
    if(c=='1')
    {
        for(int i=1;i<=8;i++)
        {
            int xx=x+d[i][0],yy=y+d[i][1];
            if(check(xx,yy))
                if(tmap[xx-1][yy+1]=='F'&&map1[xx-1][yy+1]!=-1)//试探时有格子的小旗标错了=>失败
                {
                    Lose=1;
                    return;
                }
        }
        for(int i=1;i<=8;i++)
        {
            int xx=x+d[i][0],yy=y+d[i][1];
            if(check(xx,yy))
                if(map1[xx-1][yy+1]==-1&&tmap[xx-1][yy+1]!='F')//试探是有格子为雷=>取消打开
                    return;
        }
        for(int i=1;i<=8;i++)
        {
            int xx=x+d[i][0],yy=y+d[i][1];
            if(check(xx,yy)&&tmap[xx-1][yy+1]=='#')//打开周围8个格子
                dfs(xx,yy);
        }
    }
}
void flag(int x,int y)
{
    x-=1,y+=1;
    if(tmap[x][y]=='F')//原本为小旗=>取消小旗
    {
        tmap[x][y]='#';mapc[x][y]=240;
        gto(x+1,y-1),color(240),printf("#");
        Wnum++;//更新小旗数
    }
    else//否则就放置小旗
    {
        is_flag_true[x][y]=map1[x][y]==-1;//判断小旗是否放对
        tmap[x][y]='F';mapc[x][y]=253;
        gto(x+1,y-1),color(253),printf("F");
        Wnum--;//更新小旗数
    }
    gto(1,2*K+10);color(12);printf("剩余小旗数:       ");
    gto(1,2*K+22);printf("%d",Wnum);//更新小旗数
}
void bj(int x,int y)//和放小旗差不多,只是不用更新is_flag_true
{
    x-=1,y+=1;
    if(tmap[x][y]=='?')
    {
        gto(x+1,y-1),color(240),printf("#");
        tmap[x][y]='#';mapc[x][y]=240;
    }
    else
    {
        if(tmap[x][y]=='F')//如果原本这个位置上是小旗,而你把它变为了标记,就要更新小旗数
        {
            Wnum++;
            gto(1,2*K+10);color(12);printf("剩余小旗数:       ");
            gto(1,2*K+22);printf("%d",Wnum);
        }
        gto(x+1,y-1),color(240),printf("?");
        tmap[x][y]='?';mapc[x][y]=240;
    }
}
void swt(){QR=!QR;}
void zb()
{
    if(ZB)//如果本来作弊打开了就把作弊地图清除
    {
        for(int i=1;i<=G;i++)
        {
            gto(i+1,K+2);
            for(int j=1;j<=K;j++)
                color(15),printf(" ");
        }
        ZB=0;
    }
    else//否则打印作弊地图
    {
        for(int i=1;i<=G;i++)
        {
            gto(i+1,K+2);
            for(int j=1;j<=K;j++)
            {
                color(mapc[i][j]);
                if(map1[i][j]==-1) printf("O");
                else if(map1[i][j]>0) printf("%d",map1[i][j]);
                else printf(" ");
            }
        }
        ZB=1;
    }
}
void again()
{
    G=K=Lnum=nx=ny=Lose=ZB=0;
    QR=0;
    memset(is_flag_true,0,sizeof(is_flag_true));
    memset(map,0,sizeof(map));
    memset(tmap,0,sizeof(tmap));
    memset(map1,0,sizeof(map1));
    memset(mapc,0,sizeof(mapc));
    color(15);
    system("cls");//初始化
    mainmain();
}
 
void begin()//各种操作
{
    char c=getch(); 
    gto(G+5,0),color(15),printf("请选择方块进行操作");
    color(240);
    if(c=='w'&&check(nx-1,ny))
    {
        gto(nx,ny);
        if(tmap[nx-1][ny+1]!='#'||tmap[nx-1][ny+1]==' ')
            color(mapc[nx-1][ny+1]);
        printf("%c",tmap[nx-1][ny+1]);
        gto(nx-=1,ny);color(15);printf("@");
    }
    else if(c=='s'&&check(nx+1,ny))
    {
        gto(nx,ny);if(tmap[nx-1][ny+1]!='#'||tmap[nx-1][ny+1]==' ')color(mapc[nx-1][ny+1]);printf("%c",tmap[nx-1][ny+1]);
        gto(nx+=1,ny);color(15);printf("@");
    }
    else if(c=='a'&&check(nx,ny-1))
    {
        gto(nx,ny);if(tmap[nx-1][ny+1]!='#'||tmap[nx-1][ny+1]==' ')color(mapc[nx-1][ny+1]);printf("%c",tmap[nx-1][ny+1]);
        gto(nx,ny-=1);color(15);printf("@");
    }
    else if(c=='d'&&check(nx,ny+1))
    {
        gto(nx,ny);if(tmap[nx-1][ny+1]!='#'||tmap[nx-1][ny+1]==' ')color(mapc[nx-1][ny+1]);printf("%c",tmap[nx-1][ny+1]);
        gto(nx,ny+=1);color(15);printf("@");
    }
    //上下左右移动
    else
    {
        if(c==' '&&(!(tmap[nx-1][ny+1]<='9'&&tmap[nx-1][ny+1]>='0'))&&tmap[nx-1][ny+1]!='F')
        {
            mapc[nx-1][ny+1]=col[map1[nx-1][ny+1]];//如果本来放了标记,mapc[nx-1][ny+1]的颜色为黑色,在打开时里面的颜色却不一定是黑色
            if(QR)
            {
                gto(G+5,0),color(15),printf("请确认                                      ");
                if(getch()==' ')
                {
                    if(map1[nx-1][ny+1]==-1) {Lose=1;return;}
                    dfs(nx,ny);
                }
            }
            else
            {
                if(map1[nx-1][ny+1]==-1) {Lose=1;return;}
                dfs(nx,ny);
            }
        }
        else if(c=='1')
        {
            if(QR)
            {
                gto(G+5,0),color(15),printf("请确认                                      ");
                if(getch()=='1') st(nx,ny);
            }
            else st(nx,ny);
            for(int i=1;i<=8;i++)
            {
                int xx=nx+d[i][0],yy=ny+d[i][1];
                if(check(xx,yy))
                {
                    gto(xx,yy);
                    if(tmap[xx-1][yy+1]!='#') color(mapc[xx-1][yy+1]);
                    else color(240);
                    printf("%c",tmap[xx-1][yy+1]);
                }
            }
        }
        else if(c=='2'&&(tmap[nx-1][ny+1]>'9'||tmap[nx-1][ny+1]<'1'))
        {
            if(QR)
            {
                gto(G+5,0),color(15),printf("请确认                                      ");
                if(getch()=='2') flag(nx,ny);
            }
            else flag(nx,ny);
        }
        else if(c=='3'&&(tmap[nx-1][ny+1]>'9'||tmap[nx-1][ny+1]<'1'))
        {
            if(QR)
            {
                gto(G+5,0),color(15),printf("请确认                                      ");
                if(getch()=='3') bj(nx,ny);
            }
            else bj(nx,ny);
        }
        else if(c=='4')
        {
            if(QR)
            {
                gto(G+5,0),color(15),printf("请确认                                      ");
                if(getch()=='4') swt();
            }
            else swt();
        }
        else if(c=='5')
        {
            if(QR)
            {
                gto(G+5,0),color(15),printf("请确认                                      ");
                if(getch()=='5') zb();
            }
            else zb();
        }
        else if(c=='0')
        {
            if(QR)
            {
                gto(G+5,0),color(15),printf("请确认                                      ");
                if(getch()=='0') again();
            }
            else again();
        }
    }
}
 
void mainmain()
{
    system("mode con cols=120 lines=35");//设置窗口大小
    srand((unsigned)time(NULL));
    int mode;
    printf("1.初级\n2.中级\n3.高级\n4.自定义\n");
    scanf("%d",&mode);if(mode>4) mode=4;
    if(mode==1) G=9,K=9,Lnum=10;
    else if(mode==2) G=16,K=16,Lnum=40;
    else if(mode==3) G=16,K=30,Lnum=99;//三种等级的参数
    else
    {
        printf("请输入雷区高度:");scanf("%d",&G);
        printf("请输入雷区宽度:");scanf("%d",&K);
        printf("请输入雷个数(建议不超过总大小的三分之一):");scanf("%d",&Lnum);
        if(G>24) G=24;if(K>30) K=30;
        if(G<9) G=9;if(K<9) K=9;
        if(Lnum<10) Lnum=10;if(Lnum>G*K*9/10) Lnum=G*K*9/10;
        //控制参数的范围,最后一个if是雷的数量不超过地图大小的9/10
    }
    make();
    print();
    while(1)
    {
        begin();
        bool f1=is_win(),f2=is_lose();
        if(f1||f2)
        {
            gto(0,0);
            if(f1)
                color(202),gto(0,0),printf("你 赢 了!!是否重来?(y/n)");
            if(f2)
                color(137),gto(0,0),printf("你 输 了!!是否重来?(y/n)");//输赢
            print_real_map();
            char c=getch();
            if(c=='y'||c=='Y') again();
            else
            {
                color(15);
                system("cls");
                gto(MIDX,MIDY-5);
                printf("欢迎下次再来");
                return;
            }
        }
    }
}
void print_real_map()
{
    color(240);
    for(int i=1;i<=G;i++)
    {
        gto(i+1,0);
        for(int j=1;j<=K;j++)
        {
            if(tmap[i][j]=='F'&&is_flag_true[i][j]==0)//如果旗标错了显示红色的X
                color(252),printf("X");
            else if(map1[i][j]==-1)//雷为黑色O
                color(240),printf("O");
            else if(map1[i][j]==0)//空
                color(240),printf(" ");
            else//数字
                color(mapc[i][j]),printf("%d",map1[i][j]);
        }
    }
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值