好文档就是一把金锄头!
欢迎来到金锄头文库![会员中心]
电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

C大作业坦克大战(精编版).docx

30页
  • 卖家[上传人]:说****
  • 文档编号:221544441
  • 上传时间:2021-12-11
  • 文档格式:DOCX
  • 文档大小:17.20KB
  • / 30 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • i n c l u d e < i o s t r e a m >#include #include #include #include usingnamespacestd;HANDLEMutex=CreateMutex(NULL,FALSE,NULL);/互/ 斥对象intGameOver=0; intlevel=0; intmap[23][23];// 坦克种类, Normal 为玩家坦克#defineNormal0 #defineRed1 #defineBlue2 #defineGreen3// 方向的宏定义#defineUp0 #defineDown1 #defineLeft2 #defineRight3// 地图标记的宏定义#defineEmpty0 #definePlayer1#definePlayerBullet2 #defineEnemyBullet3 #defineEnemy4 intKill;intKillRed; intKillGreen; intEnemyExist;voidSetPos(inti,intj)// 设定光标位置{COORDpos={i,j}; HANDLEOut=GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(Out,pos);}voidHideCurSor(void)// 隐藏光标{CONSOLE_CURSOR_INFOinfo={1,0};HANDLEOut=GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorInfo(Out,&info);}intsharp[4][12]={{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},};// 此数组用来保存坦克各个方向的形状信息DWORDWINAPIBulletfly(LPVOIDlpParameter);// 子弹函数申明voidUpdata();// 更新界面信息函数申明classTank// 坦克类{private:intDirection;// 方向inthotpoint[2];// 活动点intSpeed;// 速度intFirePower;// 火力public:Tank(intdir,inthot1,inthot2,inttyp,intspe,intfirepow)// 构造函数{Direction=dir; hotpoint[0]=hot1; hotpoint[1]=hot2; Type=typ; Speed=spe; FirePower=firepow;}intType;// 坦克的种类(详见宏定义) intID;// 坦克在 MAP中的标记(详见宏定义) intFireEnable;// 是否可以开火intLife;// 生命值voidRunning();// 运行函数intJudge(intx,inty,intID);// 判断是否可以绘制坦克voidDrawTank();// 重绘坦克voidRedraw();// 擦除坦克intGetSpeed()// 获取速度{returnSpeed;}intGetFire()// 获取火力{returnFirePower;}intGetDirection()// 获取方向{returnDirection;}intGetHotX()// 获取活动点坐标{returnhotpoint[0];}intGetHotY(){returnhotpoint[1];}voidIncreaseFire()// 火力+{FirePower++;}voidIncreaseSpeed()// 速度+{Speed++;}voidChangeDirection(intnewD)// 改变方向{Direction=newD;}voidChangePos(intx,inty)// 改变活动点{hotpoint[0]=x; hotpoint[1]=y;}};Tankplayer(Right,0,0,Normal,1,1);// 玩家Tankenemy(Left,20,0,Red,1,1);// 敌人voidTank::DrawTank()// 绘制坦克{inti;intnx,ny; if(Type==Red)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_IN TENSITY|FOREGROUND_RED);elseif(Type==Blue)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_IN TENSITY|FOREGROUND_BLUE);elseif(Type==Green)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_IN TENSITY|FOREGROUND_GREEN);elseif(Type==Normal)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_IN TENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);for(i=0;i<6;i++){nx=hotpoint[0]+sharp[Direction][i*2]; ny=hotpoint[1]+sharp[Direction][i*2+1];SetPos((ny+1)*2,nx+1);// 利用 sharp 数组相对于点 x,y 绘制形状map[nx][ny]=ID; cout<<" ■";}}voidTank::Redraw()// 擦除坦克,原理同上{inti; intnx,ny;for(i=0;i<6;i++){nx=hotpoint[0]+sharp[Direction][i*2]; ny=hotpoint[1]+sharp[Direction][i*2+1]; map[nx][ny]=Empty; SetPos((ny+1)*2,nx+1);cout<<"";}}intTank::Judge(intx,inty,intdir)// 判断当前是否可以绘制坦克{inti; intnx,ny;for(i=0;i<6;i++){nx=x+sharp[dir][i*2]; ny=y+sharp[dir][i*2+1];if(nx<0||nx>=23||ny<0||ny>=23||map[nx][ny]!=Empty)// 不能绘制,返回 1return1;}return0;}voidTank::Running()// 坦克运行函数{intnewD;// 坦克的运行while(1){if(Life==0){EnemyExist=0;// 敌人不存在return;}if(GameOver==1) return;if(FireEnable==1&&GameOver==0)// 如果可以开火{WaitForSingleObject(Mutex,INFINITE);// 线程拥有互斥对象FireEnable=0;// 设为不可开火HANDLEbullet=CreateThread(NULL,0,Bulletfly,&ID,0,NULL);// 创建子弹线程CloseHandle(bullet);ReleaseMutex(Mutex);// 释放互斥对象Sleep(100);}WaitForSingleObject(Mutex,INFINITE);// 线程拥有互斥对象srand((int)time(0)); newD=rand()%4;if(newD==Up)// 随机出新的方向并重新绘制坦克{Redraw();if(Judge(hotpoint[0]-1,hotpoint[1],newD)==0){hotpoint[0]--;Direction=newD;}else{if(Judge(hotpoint[0],hotpoint[1],newD)==0) Direction=newD;}}elseif(newD==Down){Redraw(); if(Judge(hotpoint[0]+1,hotpoint[1],newD)==0){hotpoint[0]++;Direction=newD;}else{if(Judge(hotpoint[0],hotpoint[1],newD)==0) Direction=newD;}}elseif(newD==Left){Redraw();if(Judge(hotpoint[0],hotpoint[1]-1,newD)==0){hotpoint[1]--;Direction=newD;}else{if(Judge(hotpoint[0],hotpoint[1],newD)==0) Direction=newD;}}elseif(newD==Right){Redraw(); if(Judge(hotpoint[0],hotpoint[1]+1,newD)==0){hotpoint[1]++;Direction=newD;}else{if(Judge(hotpoint[0],hotpoint[1],newD)==0) Direction=newD;}}if(GameOver==0&&Life!=0) DrawTank();ReleaseMutex(Mutex);// 释放互斥对象Sleep(500-80*Speed);}}/********************* 子弹线程函数 *******************/ DWORDWINAPIBulletfly(LPVOID。

      点击阅读更多内容
      关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
      手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
      ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.