1、用c做的简单的坦克大战小游戏/用c做的简单的坦克大战小游戏(给初学者点动力) #include #include #include #include #include #define KEY_ESC 0x01 #define KEY_SPACE 0x39 #define KEY_UP 0x48 #define KEY_LEFT 0x4b #define KEY_RIGHT 0x4d #define KEY_DOWN 0x50 /*1石头,2砖块,3水,5老家,8玩家,9敌人*/ int map2020=1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,9,0
2、,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,2,2,2,2,0,0,2,2,2,2,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,2,0,0,2,0,1,1,1,1,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,2,2,2,2,2,2,2,0,0,0,0,0,0,0,2,2,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,0,1, 1,0,1,1,1,1,3,3,3,3,0,0,0,0,0,0,0,2,0,1, 1,0,0,0,0,0,0,0,0,
3、0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,3,3,3,0,1, 1,0,0,0,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,3,3,3,1,1,1,1,1,1,1,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
4、,0,0,0,1, 1,0,2,2,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,1, 1,0,2,2,0,0,0,0,2,2,2,0,0,0,2,2,0,0,0,1, 1,0,0,0,0,0,0,8,2,5,2,0,0,0,0,0,0,0,0,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1; struct f int x; int y; int direction; ; struct play int x;/*行坐标*/ int y;/*列坐标*/ int direction;/*方向*/ struct f fire5;/*5颗子弹*/ i
5、nt score;/*分数*/ Playone; struct a int x; int y; int color; int direction; int directiontwo;/*用来判断走的路是否往返*/ int fireplay;/*是否攻击的变量,随机生成*/ struct f fire; amy5;/*敌人的结构体,其实可以添加不同的颜色来表示不同种类的坦克*/ char key_state128,key_pressed128; void Init();/*图象驱动开始*/ void End();/*图象驱动结束*/ void DrawMap();/*画地图*/ void Dra
6、wWater(int x,int y);/*画水*/ void DrawBrick(int x,int y);/*画砖*/ void DrawTone(int x,int y);/*画石头*/ void DrawHome(int x,int y);/*画老家*/ void DrawBlack(int x,int y);/*去除内容*/ void DrawPlay(int x,int y);/*画玩家*/ void DrawAmy(int x,int y,int i);/*画敌人*/ void Score();/*输出分数*/ void GamePlay();/*玩游戏过程*/ void Gam
7、eOver();/*游戏失败*/ void TimeDelay(unsigned long microsec); /*延时函数 传入微秒数*/ int GetKey(int ScanCode);/*这里开始都是按键函数*/ void interrupt far (*OldInt9Handler)(); void far interrupt NewInt9(); void InstallKeyboard(); void ShutDownKeyboard(); void main(void) Init(); DrawMap(); GamePlay(); End(); void TimeDelay(
8、unsigned long microsec) /*延时函数 传入微秒数*/ union REGS r; r.h.ah=0x86; r.x.cx=microsec16; r.x.dx=microsec; int86(0x15,&r,&r); void Init()/*图象驱动开始*/ int gd=DETECT,gm; initgraph(&gd,&gm,d:tctc); cleardevice(); InstallKeyboard(); void End()/*图象驱动结束*/ ShutDownKeyboard(); closegraph(); void DrawTone(int x,int
9、 y)/*画石头*/ setfillstyle(SOLID_FILL,7); bar(100+x*20-9,50+y*20-9,100+x*20+9,50+y*20+9); /作者:c闲人 2004-8-20 00:42 回复此发言 /- /2 用c做的简单的坦克大战小游戏(给初学者点动力) void DrawWater(int x,int y)/*画水*/ setfillstyle(SOLID_FILL,BLUE); bar(100+x*20-9,50+y*20-9,100+x*20+9,50+y*20+9); void DrawBrick(int x,int y)/*画砖*/ setfil
10、lstyle(SOLID_FILL,6); bar(100+x*20-9,50+y*20-9,100+x*20+9,50+y*20+9); setcolor(15); line(100+x*20-9,50+y*20-4,100+x*20+9,50+y*20-4); line(100+x*20-9,50+y*20+4,100+x*20+9,50+y*20+4); line(100+x*20-4,50+y*20-9,100+x*20-4,50+y*20+9); line(100+x*20+4,50+y*20-9,100+x*20+4,50+y*20+9); void DrawHome(int x,
11、int y)/*画老家*/ setcolor(0); setfillstyle(SOLID_FILL,GREEN); fillellipse(100+x*20,50+y*20,9,9); void DrawBlack(int x,int y)/*去除内容*/ setcolor(0); setfillstyle(SOLID_FILL,0); bar(100+x*20-9,50+y*20-9,100+x*20+9,50+y*20+9); void DrawPlay(int x,int y)/*画玩家*/ setcolor(4);/*玩家为红色*/ circle(100+x*20,50+y*20,7
12、); switch(Playone.direction)/*判断玩家方向*/ case 1:line(100+x*20,50+y*20,100+x*20,50+y*20-9);break;/*上*/ case 2:line(100+x*20,50+y*20,100+x*20+9,50+y*20);break;/*右*/ case 3:line(100+x*20,50+y*20,100+x*20,50+y*20+9);break;/*下*/ case 4:line(100+x*20,50+y*20,100+x*20-9,50+y*20);break;/*左*/ void DrawAmy(int
13、x,int y,int i)/*画敌人*/ if(amyi.color=12) setcolor(12); else if(amyi.color=13) setcolor(13); else/*这里是判断三种颜色的坦克*/ setcolor(14); circle(100+x*20,50+y*20,7); switch(amyi.direction)/*判断玩家方向*/ case 1:line(100+x*20,50+y*20,100+x*20,50+y*20-9);break;/*上*/ case 2:line(100+x*20,50+y*20,100+x*20+9,50+y*20);bre
14、ak;/*右*/ case 3:line(100+x*20,50+y*20,100+x*20,50+y*20+9);break;/*下*/ case 4:line(100+x*20,50+y*20,100+x*20-9,50+y*20);break;/*左*/ void Score()/*输出分数*/ char s10; Playone.score+=10; sprintf(s,%d,Playone.score); setfillstyle(SOLID_FILL,0); bar(550,100,640,130); settextstyle(0,0,2); setcolor(YELLOW); o
15、uttextxy(550,115,s); void DrawMap()/*画地图*/ int i,j,k; for(i=0;i20;i+) for(j=0;j20;j+) if(mapij=1) DrawTone(j,i); else if(mapij=2) DrawBrick(j,i); else if(mapij=3) DrawWater(j,i); else if(mapij=5) DrawHome(j,i); else if(mapij=8) Playone.x=i; Playone.y=j; Playone.direction=1; DrawPlay(j,i); for(k=0;k5
16、;k+) Playone.firek.direction=-1;/*5颗子弹的方向都为-1,表示不存在*/ else if(mapij=9) amy0.x=1;amy0.y=1;amy0.direction=amy0.directiontwo=3;/*第一个敌人*/ amy0.color=12; DrawAmy(j,i,0); for(i=1;i5;i+)/*敌人都没出现*/ amyi.direction=amyi.fire.direction=-1; outtextxy(210,450,2003.10.1 milo_zy); settextstyle(0,0,2);/*首次输出得分*/ se
17、tcolor(9); outtextxy(525,80,Score); setcolor(YELLOW); outtextxy(550,115,0); void far interrupt NewInt9(void) unsigned char ScanCode,temp; ScanCode=inportb(0x60); temp=inportb(0x61); outportb(0x61,temp | 0x80); outportb(0x61,temp & 0x7f); if(ScanCode&0x80) ScanCode&=0x7f; key_stateScanCode=0; else ke
18、y_stateScanCode=1; /作者:c闲人 2004-8-20 00:42 回复此发言 /- /3 用c做的简单的坦克大战小游戏(给初学者点动力) key_pressedScanCode=1; outportb(0x20,0x20); void InstallKeyboard(void) int i; for(i=0;i128;i+) key_statei=key_pressedi=0; OldInt9Handler=getvect(9); /*中断向量值*/ setvect(9,NewInt9); /*中断程序NewInt9地址存入指定的中断向量表中INT 09H*/ void S
19、hutDownKeyboard(void) setvect(9,OldInt9Handler); int GetKey(int ScanCode) int res; res=key_stateScanCode|key_pressedScanCode; key_pressedScanCode=0; return res; void GameOver()/*游戏失败*/ setcolor(0); setfillstyle(SOLID_FILL,0);/*把老家给去除*/ fillellipse(100+9*20,50+18*20,9,9); nosound(); setcolor(RED); se
20、ttextstyle(0,0,4); outtextxy(150,5,GAME OVER); while(1) if(GetKey(KEY_ESC) break; void GamePlay()/*玩游戏的过程*/ int i,j,lose=0;/*lose是1的时候表示失败*/ int t=0; randomize(); while(1) for(i=0;i0) putpixel(100+amyi.fire.y*20,50+amyi.fire.x*20,11); for(i=0;i0)/*大于0表示玩家子弹存在*/ putpixel(100+Playone.firei.y*20,50+Pla
21、yone.firei.x*20,11); TimeDelay(500000);/*关键的时间改动*/ for(i=0;i0) putpixel(100+amyi.fire.y*20,50+amyi.fire.x*20,0); for(i=0;i0) putpixel(100+Playone.firei.y*20,50+Playone.firei.x*20,0); for(i=0;i=4;i+)/*玩家子弹位置的变化*/ if(Playone.firei.direction0) continue; if(Playone.firei.direction=1) Playone.firei.x-;Pl
22、ayone.firei.y=Playone.firei.y; else if(Playone.firei.direction=2) Playone.firei.y+;Playone.firei.y=Playone.firei.y; else if(Playone.firei.direction=3) Playone.firei.x+;Playone.firei.y=Playone.firei.y; else if(Playone.firei.direction=4) Playone.firei.y-;Playone.firei.y=Playone.firei.y; /*打到石头或者砖头的情况下
23、减少子弹*/ if(mapPlayone.firei.xPlayone.firei.y=1)/*打到石头*/ Playone.firei.direction=-1;/*子弹消失*/ if(mapPlayone.firei.xPlayone.firei.y=2)/*打到砖头*/ Playone.firei.direction=-1; DrawBlack(Playone.firei.y,Playone.firei.x);/*砖头打掉*/ mapPlayone.firei.xPlayone.firei.y=0;/*被打掉的地方变成可走的地方*/ if(mapPlayone.firei.xPlayon
24、e.firei.y=5)/*自己打到老家*/ lose=1;break; for(j=0;j5;j+) if(amyj.direction0)/*不存在的不判断*/ continue; if(amyj.x=Playone.firei.x&amyj.y=Playone.firei.y)/*打中敌人*/ Playone.firei.direction=-1; DrawBlack(Playone.firei.y,Playone.firei.x); mapPlayone.firei.xPlayone.firei.y=0;/*被打掉的地方变成可走的地方*/ amyj.fire.direction=amy
25、j.direction=-1;/*这个敌人消失*/ Score();/*输出得分*/ for(i=0;i5;i+)/*敌人子弹位置的变化*/ /作者:c闲人 2004-8-20 00:42 回复此发言 /- /4 用c做的简单的坦克大战小游戏(给初学者点动力) if(amyi.direction0|amyi.fire.direction0) continue; if(amyi.fire.direction=1) amyi.fire.x-;amyi.fire.y=amyi.fire.y; else if(amyi.fire.direction=2) amyi.fire.y+;amyi.fire.
26、x=amyi.fire.x; else if(amyi.fire.direction=3) amyi.fire.x+;amyi.fire.y=amyi.fire.y; else if(amyi.fire.direction=4) amyi.fire.y-;amyi.fire.x=amyi.fire.x; /*打到石头或者砖头的情况下减少子弹*/ if(mapamyi.fire.xamyi.fire.y=1)/*打到石头*/ amyi.fire.direction=-1;/*子弹消失*/ if(mapamyi.fire.xamyi.fire.y=2)/*打到砖头*/ amyi.fire.direction=-1; DrawBlack(amyi.fire.y,amyi.fire.x);/*砖头打掉*/ mapamyi.fire.xamyi.fire.y=0;/*被打掉的地方变成可走的地方*/ if(mapamyi.fire.xamyi.fire.y=5)/*敌人打到老家*/ lose=1;break; if(amyi.fire.x=Play
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1