C++大作业坦克大战.docx

上传人:b****3 文档编号:1519059 上传时间:2022-10-22 格式:DOCX 页数:24 大小:62.83KB
下载 相关 举报
C++大作业坦克大战.docx_第1页
第1页 / 共24页
C++大作业坦克大战.docx_第2页
第2页 / 共24页
C++大作业坦克大战.docx_第3页
第3页 / 共24页
C++大作业坦克大战.docx_第4页
第4页 / 共24页
C++大作业坦克大战.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

C++大作业坦克大战.docx

《C++大作业坦克大战.docx》由会员分享,可在线阅读,更多相关《C++大作业坦克大战.docx(24页珍藏版)》请在冰豆网上搜索。

C++大作业坦克大战.docx

C++大作业坦克大战

C++大作业坦克大战

#include

#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);

}

可以绘制坦克

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_INTENSITY|FOREGROUND_RED);

elseif(Type==Blue)

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_BLUE);

elseif(Type==Green)

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN);

elseif(Type==Normal)

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|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)//不能绘制,返回1

return1;

}

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(LPVOIDlpParameter)

{

int*ID=(int*)lpParameter;//ID用来获取发射子弹坦克的ID

intPos[2];//子弹活动点

intdirection;

intSpeed;

inttype;

inthit=0;//击中标记

intoldx,oldy;//旧活动点

intflag=0;//子弹是否有移动的标记

if(*ID==Player)//如果是玩家坦克

{

type=PlayerBullet;

direction=player.GetDirection();

Speed=player.GetFire();

Pos[0]=player.GetHotX();

Pos[1]=player.GetHotY();

}

elseif(*ID==Enemy)//如果是敌人坦克

{

type=EnemyBullet;

direction=enemy.GetDirection();

Speed=enemy.GetFire();

Pos[0]=enemy.GetHotX();

Pos[1]=enemy.GetHotY();

}

if(direction==Up)//根据坦克的位置和方向确定子弹的初始坐标

{

Pos[0]--;

Pos[1]++;

}

elseif(direction==Down)

{

Pos[0]+=3;

Pos[1]++;

}

elseif(direction==

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 院校资料

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1