c语言编写坦克大战源代码.docx
- 文档编号:9296021
- 上传时间:2023-02-04
- 格式:DOCX
- 页数:15
- 大小:17.32KB
c语言编写坦克大战源代码.docx
《c语言编写坦克大战源代码.docx》由会员分享,可在线阅读,更多相关《c语言编写坦克大战源代码.docx(15页珍藏版)》请在冰豆网上搜索。
c语言编写坦克大战源代码
#include"tank.h"
#include"ConOperator.h"
#include
#include
#include
#include
usingnamespacestd;
TankGame:
:
TankGame(intw,inth)
{
//设定当前关数
no=1;
//设定游戏整体高宽
wide=w;
high=h;
HANDLEhStdOut=GetStdHandle(STD_OUTPUT_HANDLE);
COORDsizePos={2*wide,high};
SetConsoleScreenBufferSize(hStdOut,sizePos);
//分配游戏空间状态表
gameSpace=newGameSpace*[high];
for(inti=0;i!
=high;i++)
gameSpace[i]=newGameSpace[wide];
//添加围墙
wall=newWall(gameSpace,wide,high);
wall->draw();
}
voidTankGame:
:
load_init()
{
//初始化游戏空间
for(inty=1;y!
=high-1;y++){
for(intx=1;x!
=wide-1;x++){
gameSpace[y][x].have=false;
gameSpace[y][x].kind=0;
gameSpace[y][x].no=0;
}
}
//添加地图
map.load(gameSpace,no);
//初始化自己坦克
me.init_xy();
me.trans_direction
(1);
me.set_no(0);
me.init_move(gameSpace,wide/2-2,high-5);
//按顺序诞生敌人坦克
creatPlace=0;
for(inti=0;i!
=3;i++){
freezeTime[i]=REBIRTHTIME;
enemy[i].init_xy();
enemy[i].set_kind
(2);
enemy[i].set_no(i+1);
reset(enemy[i]);
}
//数目初始化
leftCount=17;
existCount=3;
//显示图片
me.draw_tank();
for(inti=0;i!
=3;i++)
enemy[i].draw_tank();
map.draw();
}
TankGame:
:
~TankGame()
{
if(wall)
deletewall;
if(gameSpace){
for(inti=0;i!
=high;i++)
delete[]gameSpace[i];
delete[]gameSpace;
}
}
voidTankGame:
:
reset(Tank&tank)
{
boolsuccess=false;
intt=3;
while(t&&!
success){
creatPlace++;
if(creatPlace==4)
creatPlace=1;
//诞生地点选择
switch(creatPlace){
case1:
tank.trans_direction(3);//朝下
if(tank.reset(gameSpace,1,1))
success=true;
break;
case2:
tank.trans_direction(3);
if(tank.reset(gameSpace,wide/2-2,1))
success=true;
break;
case3:
tank.trans_direction(3);
if(tank.reset(gameSpace,wide-4,1))
success=true;
break;
}
if(!
success)
t--;
}
}
voidTankGame:
:
start()
{
charcommand;
enter_picture(command);
if(command!
=ENTER)
return;
while(me.blood>0){
//initdataforruning
load_init();
//runatask
runing_a_task();
//determinewhyreturn
if(me.blood>0){
no++;
if(no>map.total){
success_gameover();
break;
}
charcommand;
turn_to_next_task(command);
if(command!
=ENTER)
break;
}
elsegame_over();
}
}
//void
voidTankGame:
:
runing_a_task()
{
while((leftCount||existCount)&&me.blood){
clock_tnow=clock();
charcommand=-1;//主人命令
while(clock()-now update_keyBoard_char(command); /*更新敌人坦克*/ for(inti=0;i! =3;i++){ if(enemy[i].blood==0) continue; if(enemy[i].freezeTime==0){ enemy[i].rand_direction(gameSpace);//产生随机方向 } else{ if(enemy[i].speed==0){ enemy[i].move(gameSpace); if(bullet[i+1].disapper){ intshot=-1; bullet[i+1]=enemy[i].rand_shot(gameSpace,i+1,shot); if(shot==0) me.be_shot(gameSpace); bullet[i+1].registration(gameSpace); } enemy[i].freezeTime--; }else{ enemy[i].speed--; } } } /*更新自己坦克*/ if(command>=0&&command<4){ me.trans_to_direction(command); me.move(gameSpace); } /*更新子弹*/ for(inti=0;i! =4;i++){ if(! bullet[i].disapper){//存在的子弹 intshot; bullet[i].move(gameSpace,shot);//运行,是否射中 if(shot==0) me.be_shot(gameSpace); elseif(shot>=1&&shot<=3){ enemy[shot-1].be_shot(gameSpace); existCount--; }elseif(shot>=7&&shot<=10){ bullet[shot-7].unregistration(gameSpace); bullet[shot-7].clear(); bullet[shot-7].init(bullet[shot-7].kind); } } } /*自己产生子弹*/ if(bullet[0].disapper){ if(command==32){ intshot; bullet[0]=me.make_bullet(gameSpace,0,shot); if(shot>=1&&shot<=3){ enemy[shot-1].be_shot(gameSpace); existCount--; } bullet[0].registration(gameSpace); } } /*诞生缺损敌人*/ if(leftCount>0){ for(inti=0;i! =3;i++){ if(enemy[i].blood==0){ if(freezeTime[i]==0){ freezeTime[i]=REBIRTHTIME; reset(enemy[i]); existCount++; leftCount--; } elsefreezeTime[i]--; } } } } } voidTankGame: : update_keyBoard_char(char&c) { if(_kbhit()){ c=getch(); if(c==-32){ c=getch(); if(c! =UP&&c! =DOWN&&c! =LEFT&&c! =RIGHT) c=-1; elseif(c==RIGHT) c=0; elseif(c==UP) c=1; elseif(c==LEFT) c=2; else c=3; } else c=32; } } voidTankGame: : enter_picture(char&command) { stringcaptions[6]; captions[0]="◥◣◢◤■■■■■◥◣◢◤"; captions[1]="◥◣◢◤◢◤◥◣◢◤"; captions[2]="◥◣◢◤◢◤◥◣◢◤"; captions[3]="◢◤◢◤◢◤◥◣"; captions[4]="◢◤◢◤◢◤◥◣"; captions[5]="◢◤■■■■■◢◤◥◣"; strings=""; color(0x0e); for(inti=0;i! =6;i++) grid(7,6+i,captions[i]); Frameframe(30,15); frame.draw_frame(4,19,0x09); stringsentence[3]; sentence[0]="Thissmallgameismadebyyzxin2010."; sentence[1]="Wlecometoplayit."; sentence[2]="AllRightsReserved."; color(0x0e); for(inti=0;i! =3;i++){ for(intk=0;k! =sentence[i].size();k++){ grid(14+k,21+2*i,sentence[i][k]); sleep(50); } sleep(1000); } grid(7,28,"Enter: 开始ESC: 退出"); while(true){ if(_kbhit()){ command=getch(); if(command==ENTER||command==ESC) break; } } //clearscreenpictureinwall clear_picture(); } voidTankGame: : turn_to_next_task(char&command) { clear_picture(); color(0x0e); grid(18,20,"第"); grid(20,20,no); grid(22,20,"关"); while(true){ if(_kbhit()){ command=getch(); if(command==ENTER||command==ESC) break; } } grid(18,20,""); } voidTankGame: : success_gameover() { clear_picture(); Frameframe(30,15); frame.draw_frame(4,10,0x09); stringsentence[3]; sentence[0]="CongratulationsforSuccessfullyfinishingalltasks"; sentence[1]="Thankyouforusingitdeeply."; sentence[2]="Andwelcometouseitnexttime."; color(0x0e); for(inti=0;i! =3;i++){ for(intk=0;k! =sentence[i].size();k++){ grid(12+k,14+2*i,sentence[i][k]); sleep(50); } sleep(1000); } while(true){ if(_kbhit()){ charcommand=getch(); if(command==ENTER||command==ESC) break; } } } voidTankGame: : game_over() { clear_picture(); Frameframe(30,15); frame.draw_frame(4,15,0x09); stringsentence[4]; sentence[0]="Gameover,butyoucantryitagain."; sentence[1]="Thankyouforusingitdeeply."; sentence[2]="Andwelcometouseitnexttime."; sentence[3]="Allrightsreserved.(Violatorswillbeprosecuted.)."; color(0x0e); for(inti=0;i! =4;i++){ for(intk=0;k! =sentence[i].size();k++){ grid(13+k,18+2*i,sentence[i][k]); sleep(50); } sleep(1000); } while(true){ if(_kbhit()){ charcommand=getch(); if(command==ENTER||command==ESC) break; } } } voidTankGame: : clear_picture() { intcirX=wide/2; intcirY=high/2; intdepth=cirY>cirX? cirY+2: cirX+2; intlx,rx,uy,by; intl,r,u,b; for(inti=0;i! =depth;i++){ lx=cirX-i; rx=cirX+i; uy=cirY-i; by=cirY+i; uy<=0? u=1: u=uy; by>=high-1? b=high-2: b=by; lx<=0? l=1: l=lx; rx>=wide-1? r=wide-2: r=rx; if(rx for(intk=u;k<=b;k++) grid(rx,k,""); } if(lx>0){ for(intk=u;k<=b;k++) grid(lx,k,""); } if(by for(intk=l;k<=r;k++) grid(k,by,""); } if(uy>0){ for(intk=l;k<=r;k++) grid(k,uy,""); } } } voidTankGame: : test() { for(inty=0;y! =40;y++){ for(intx=0;x! =40;x++){ if(gameSpace[y][x].have)//&&gameSpace[y][x].kind==3) grid(x,42+y,(char)gameSpace[y][x].no); else grid(x,42+y,""); } } }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言 编写 坦克 大战 源代码
![提示](https://static.bdocx.com/images/bang_tan.gif)