版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、#include<iostream>#include<stdlib.h>#include<windows.h>#include<time.h>#include<conio.h>using namespace std;HANDLE Mutex=CreateMutex(NULL,FALSE,NULL);/互斥对象int GameOver=0;int level=0;int map2323;/坦克种类,Normal为玩家坦克#define Normal 0#define Red 1#define Blue 2#define Green 3/
2、方向的宏定义#define Up 0#define Down 1#define Left 2#define Right 3/地图标记的宏定义#define Empty 0#define Player 1#define PlayerBullet 2#define EnemyBullet 3#define Enemy 4int Kill;int KillRed;int KillGreen;int EnemyExist;void SetPos(int i,int j)/设定光标位置COORD pos=i,j;HANDLE Out=GetStdHandle(STD_OUTPUT_HANDLE); Se
3、tConsoleCursorPosition(Out, pos);void HideCurSor(void)/隐藏光标CONSOLE_CURSOR_INFO info=1,0;HANDLE Out=GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorInfo(Out,&info);int sharp412=0,1,1,0,1,1,1,2,2,0,2,2,0,0,0,2,1,0,1,1,1,2,2,1,0,1,0,2,1,0,1,1,2,1,2,2,0,0,0,1,1,1,1,2,2,0,2,1,;/此数组用来保存坦克各个方向的形状信息DWOR
4、D WINAPI Bulletfly(LPVOID lpParameter);/子弹函数申明void Updata();/更新界面信息函数申明class Tank/坦克类private:int Direction;/方向int hotpoint2;/活动点int Speed;/速度int FirePower;/火力public:Tank(int dir,int hot1,int hot2,int typ,int spe,int firepow)/构造函数Direction=dir;hotpoint0=hot1;hotpoint1=hot2;Type=typ;Speed=spe;FirePowe
5、r=firepow;int Type;/坦克的种类(详见宏定义)int ID;/坦克在MAP中的标记(详见宏定义)int FireEnable;/是否可以开火int Life;/生命值void Running();/运行函数int Judge(int x,int y,int ID);/判断是否可以绘制坦克void DrawTank();/重绘坦克void Redraw();/擦除坦克int GetSpeed()/获取速度return Speed;int GetFire()/获取火力return FirePower;int GetDirection()/获取方向return Direction;
6、int GetHotX()/获取活动点坐标return hotpoint0;int GetHotY()return hotpoint1;void IncreaseFire()/火力+FirePower+;void IncreaseSpeed()/速度+Speed+;void ChangeDirection(int newD)/改变方向Direction=newD;void ChangePos(int x,int y)/改变活动点hotpoint0=x;hotpoint1=y;Tank player(Right,0,0,Normal,1,1);/玩家Tank enemy(Left,20,0,Re
7、d,1,1);/敌人void Tank:DrawTank()/绘制坦克 int i; int nx,ny; if(Type=Red)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED);else if(Type=Blue)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_BLUE);else if(Type=Green)SetConso
8、leTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_GREEN);else if(Type=Normal)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); for(i=0;i<6;i+) nx=hotpoint0+sharpDirectioni*2;ny=hotpoin
9、t1+sharpDirectioni*2+1;SetPos(ny+1)*2,nx+1);/利用sharp数组相对于点x,y绘制形状mapnxny=ID;cout<<""void Tank:Redraw()/擦除坦克,原理同上 int i; int nx,ny; for(i=0;i<6;i+) nx=hotpoint0+sharpDirectioni*2;ny=hotpoint1+sharpDirectioni*2+1;mapnxny=Empty;SetPos(ny+1)*2,nx+1);cout<<" "int Tank:J
10、udge(int x,int y,int dir)/判断当前是否可以绘制坦克int i;int nx,ny;for(i=0;i<6;i+) nx=x+sharpdiri*2;ny=y+sharpdiri*2+1;if(nx<0|nx>=23|ny<0|ny>=23|mapnxny!=Empty)/不能绘制,返回1return 1;return 0;void Tank:Running()/坦克运行函数int newD;/坦克的运行while(1)if(Life=0)EnemyExist=0;/敌人不存在return;if(GameOver=1)return;if(F
11、ireEnable=1&&GameOver=0)/如果可以开火WaitForSingleObject(Mutex,INFINITE);/线程拥有互斥对象FireEnable=0;/设为不可开火HANDLE bullet=CreateThread(NULL,0,Bulletfly,&ID,0,NULL);/创建子弹线程CloseHandle(bullet);ReleaseMutex(Mutex);/释放互斥对象Sleep(100);WaitForSingleObject(Mutex,INFINITE);/线程拥有互斥对象srand(int)time(0); newD=ra
12、nd()%4;if(newD=Up)/随机出新的方向并重新绘制坦克Redraw();if(Judge(hotpoint0-1,hotpoint1,newD)=0)hotpoint0-;Direction=newD;elseif(Judge(hotpoint0,hotpoint1,newD)=0)Direction=newD; else if(newD=Down)Redraw();if(Judge(hotpoint0+1,hotpoint1,newD)=0)hotpoint0+;Direction=newD;elseif(Judge(hotpoint0,hotpoint1,newD)=0)Dir
13、ection=newD; else if(newD=Left)Redraw();if(Judge(hotpoint0,hotpoint1-1,newD)=0)hotpoint1-;Direction=newD;elseif(Judge(hotpoint0,hotpoint1,newD)=0)Direction=newD; else if(newD=Right)Redraw();if(Judge(hotpoint0,hotpoint1+1,newD)=0)hotpoint1+;Direction=newD;elseif(Judge(hotpoint0,hotpoint1,newD)=0)Dire
14、ction=newD; if(GameOver=0&&Life!=0) DrawTank();ReleaseMutex(Mutex);/释放互斥对象Sleep(500-80*Speed);/*子弹线程函数*/DWORD WINAPI Bulletfly(LPVOID lpParameter)int *ID=(int *)lpParameter;/ID用来获取发射子弹坦克的IDint Pos2;/子弹活动点int direction;int Speed;int type;int hit=0;/击中标记int oldx,oldy;/旧活动点int flag=0;/子弹是否有移动的标
15、记if(*ID=Player)/如果是玩家坦克type=PlayerBullet;direction=player.GetDirection();Speed=player.GetFire();Pos0=player.GetHotX();Pos1=player.GetHotY(); else if(*ID=Enemy)/如果是敌人坦克type=EnemyBullet;direction=enemy.GetDirection();Speed=enemy.GetFire();Pos0=enemy.GetHotX();Pos1=enemy.GetHotY();if(direction=Up)/根据坦克
16、的位置和方向确定子弹的初始坐标Pos0-;Pos1+;else if(direction=Down)Pos0+=3;Pos1+;else if(direction=Left)Pos0+;Pos1-;else if(direction=Right)Pos0+;Pos1+=3;/子弹的运行while(1)WaitForSingleObject(Mutex,INFINITE);/这个不再注释了。if(flag=1&&hit!=1)/擦除原位置 mapoldxoldy=Empty; SetPos(oldy+1)*2,oldx+1); cout<<" "i
17、f(GameOver=1)return 0;if(hit=1|Pos0<0|Pos0>22|Pos1<0|Pos1>22)/如果击中ReleaseMutex(Mutex);Sleep(500);if(type=PlayerBullet)player.FireEnable=1;else if(type=EnemyBullet)enemy.FireEnable=1;break;switch(mapPos0Pos1)/子弹经过的MAP的标记 case Empty:/如果是空位置就绘制子弹 mapPos0Pos1=type;SetPos(Pos1+1)*2,Pos0+1);Se
18、tConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);cout<<"" break; case Player:/如果是玩家位置 if(type!=PlayerBullet) player.Life-;/生命减少 if(player.Life<=0) GameOver=1; Updata(); hit=1; break; case Enemy:/如果是敌人位
19、置 if(type!=PlayerBullet)hit=1;elsehit=1;Kill+;if(Kill%20=0&&player.Life<5)/击杀数+player.Life+;if(enemy.Type=Red)/如果击杀红坦克KillRed+;if(KillRed%10=0&&player.GetFire()<5)player.IncreaseFire();if(enemy.Type=Green)/如果击杀绿坦克KillGreen+;if(KillGreen%10=0&&player.GetSpeed()<5)play
20、er.IncreaseSpeed();enemy.Redraw();/擦除敌人enemy.Life=0;/敌人死亡Updata(); break;oldx=Pos0;oldy=Pos1;if(direction=Up)/子弹移动 Pos0-; else if(direction=Down) Pos0+; else if(direction=Left) Pos1-; else if(direction=Right) Pos1+; ReleaseMutex(Mutex); flag=1; Sleep(60-10*Speed); return 0;/*敌人线程函数*/DWORD WINAPI Tan
21、kRuning(LPVOID lpParameter)Sleep(400);int Pos;int Start2;/敌人起始地址int typ;int fire;int spe;while(1)if(GameOver=1)return 0; srand(int)time(0);/随机出敌人起始地址 Pos=rand()%4; if(Pos=0) Start0=2; Start0=2; else if(Pos=1) Start0=2; Start1=18; else if(Pos=2) Start0=18; Start1=2; else if(Pos=3) Start0=18; Start1=1
22、8; if(player.Judge(Start0,Start1,Down)=0) break;WaitForSingleObject(Mutex,INFINITE);srand(int)time(0);typ=rand()%3+1;/随机出敌人的种类if(typ=Blue)spe=1+level;fire=1+level;else if(typ=Red)spe=1+level;fire=3+level;else if(typ=Green)spe=3+level;fire=1+level; enemy=Tank(Down,Start0,Start1,typ,spe,fire);/重新生成敌人坦
23、克enemy.ID=Enemy;enemy.Life=1;enemy.FireEnable=1;ReleaseMutex(Mutex);enemy.Running();return 0;void Init()/初始化函数Kill=0;KillRed=0;KillGreen=0;player=Tank(Left,0,0,Normal,1,1);enemy=Tank(Left,0,0,Red,1,1);player.Life=2;player.FireEnable=1;enemy.Life=0;enemy.FireEnable=1;player.ID=Player;enemy.ID=Enemy;E
24、nemyExist=0;void Updata()/更新界面信息SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);int i;SetPos(53,0);cout<<"生命值:"SetPos(53,1);for(i=0;i<5;i+)if(i<player.Life)cout<<""elsecout<<
25、;" "SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_GREEN);SetPos(53,3);cout<<"移动速度:"SetPos(53,4);for(i=0;i<5;i+)if(i<player.GetSpeed()cout<<""elsecout<<" "SetConsoleTextAttribute(GetStdHandle(ST
26、D_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED);SetPos(53,5);cout<<"火力:"SetPos(53,6);for(i=0;i<5;i+)if(i<player.GetFire()cout<<""elsecout<<" " SetPos(53,8);SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREG
27、ROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);cout<<"杀敌数:"<<Kill;SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED);SetPos(53,9);cout<<"杀死红坦克:"<<KillRed;SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FO
28、REGROUND_INTENSITY| FOREGROUND_GREEN);SetPos(53,10);cout<<"杀死绿坦克:"<<KillGreen;void DrawMap()/画界面SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);system("cls");int i;for(i=0;i<25;i+) S
29、etPos(i*2,0); cout<<""for(i=1;i<25;i+)SetPos(0,i);cout<<""SetPos(24*2,i);cout<<""for(i=0;i<25;i+) SetPos(i*2,24); cout<<""Updata();void Welcome()/欢迎界面int x;system("cls");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA
30、NDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);SetPos(10,5);cout<<""SetPos(10,6);cout<<" 坦克大战控制台版 "SetPos(10,7);cout<<""SetPos(10,8);cout<<" 方向键移动,空格键射击 "SetPos(10,9);cout<<" 敌人分为3种,蓝色为普通敌人 &
31、quot;SetPos(10,10);cout<<" 红色敌人高射速,绿色敌人高机动性 "SetPos(10,11);cout<<" 每杀死10个红坦克,玩家射速提高(最高五级) "SetPos(10,12);cout<<" 每杀死10个绿坦克,玩家移动性提高(最高五级)"SetPos(10,13);cout<<" 每杀死20个坦克,玩家生命+1(最高五格) "SetPos(10,14);cout<<""SetPos(10,15);co
32、ut<<" 何某作(百度ID:HapHapYear) "SetPos(10,16);cout<<" 按1-3选择难度 "SetPos(10,17);cout<<""while(1)x=getch();if(x<='3'&&x>='1')break;level=x-'0'-1;int main()Init();HideCurSor();Welcome();DrawMap();HANDLE temp;int newD;playe
33、r.DrawTank();while(GameOver=0)if(GetAsyncKeyState(VK_UP)/按键上WaitForSingleObject(Mutex,INFINITE);newD=Up;player.Redraw();if(player.Judge(player.GetHotX()-1,player.GetHotY(),newD)=0)/移动玩家坦克,原理和敌人函数一样player.ChangePos(player.GetHotX()-1,player.GetHotY();player.ChangeDirection(newD);elseif(player.Judge(p
34、layer.GetHotX(),player.GetHotY(),newD)=0)player.ChangeDirection(newD); if(GameOver=0) player.DrawTank();ReleaseMutex(Mutex);Sleep(200-player.GetSpeed()*20);/按键延迟,决定玩家坦克的速度else if(GetAsyncKeyState(VK_DOWN)/按键下,同上WaitForSingleObject(Mutex,INFINITE);newD=Down;player.Redraw();if(player.Judge(player.GetH
35、otX()+1,player.GetHotY(),newD)=0)player.ChangePos(player.GetHotX()+1,player.GetHotY();player.ChangeDirection(newD);elseif(player.Judge(player.GetHotX(),player.GetHotY(),newD)=0)player.ChangeDirection(newD); if(GameOver=0) player.DrawTank();ReleaseMutex(Mutex);Sleep(200-player.GetSpeed()*20);else if(
36、GetAsyncKeyState(VK_RIGHT)/按键右,同上WaitForSingleObject(Mutex,INFINITE);newD=Right;player.Redraw();if(player.Judge(player.GetHotX(),player.GetHotY()+1,newD)=0)player.ChangePos(player.GetHotX(),player.GetHotY()+1);player.ChangeDirection(newD);elseif(player.Judge(player.GetHotX(),player.GetHotY(),newD)=0)player.ChangeDirection(newD); if(GameOver=0) player.DrawTank();ReleaseMutex(Mutex);Sleep(200-player.GetSpeed()*20);else if(GetAsyncKeySt
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年度LED灯具安装与节能效果评估合同3篇
- 2024粮食购销代理合作合同版B版
- 2024薪资保密制度与员工隐私保护专项合同3篇
- 2025年度现代农业土地承包种植技术引进合同3篇
- 2025年度智能大门研发与购销合同4篇
- 2025年度数据中心能源优化承包经营协议4篇
- 2024水泵安装项目施工合作合同版B版
- 2025年度五星级酒店厨师服务合同细则4篇
- 2024跨越国界的艺术品买卖合同
- 2025年度水利工程承揽工程施工合同4篇
- 锐途管理人员测评试题目的
- 焊接材料-DIN-8555-标准
- 工程索赔真实案例范本
- 重症医学科运用PDCA循环降低ICU失禁性皮炎发生率品管圈QCC持续质量改进成果汇报
- 个人股权证明书
- 医院运送工作介绍
- 重症患者的容量管理
- 学习游戏对中小学生学业成绩的影响
- 小学四年级上册递等式计算100题及答案
- 新版个人简历Excel表格模板共2联
- (完整)中国象棋教案
评论
0/150
提交评论