⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tank.txt

📁 C语言写的坦克小游戏
💻 TXT
📖 第 1 页 / 共 2 页
字号:
#include <graphics.h> 
#include <stdlib.h> 
#include <dos.h> 
#include <conio.h> 
#include <bios.h> 
#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 map[20][20]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 
                  1,9,0,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,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,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 fire[5];/*5颗子弹*/ 
int score;/*分数*/ 
}Playone; 
struct a 
{ 
int x; 
int y; 
int color; 
int direction; 
int directiontwo;/*用来判断走的路是否往返*/ 
int fireplay;/*是否攻击的变量,随机生成*/ 
struct f fire; 
}amy[5];/*敌人的结构体,其实可以添加不同的颜色来表示不同种类的坦克*/ 
char key_state[128],key_pressed[128]; 
void Init();/*图象驱动开始*/ 
void End();/*图象驱动结束*/ 
void DrawMap();/*画地图*/ 
void DrawWater(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 GameOver();/*游戏失败*/ 
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(unsigned long microsec) /*延时函数 传入微秒数*/ 
{ 
union REGS r; 
r.h.ah=0x86; 
r.x.cx=microsec>>16; 
r.x.dx=microsec; 
int86(0x15,&r,&r); 
} 
void Init()/*图象驱动开始*/ 
{int gd=DETECT,gm; 
initgraph(&gd,&gm,"d:\\tc\\tc"); 
cleardevice(); 
InstallKeyboard(); 
} 
void End()/*图象驱动结束*/ 
{ 
ShutDownKeyboard(); 
closegraph(); 
} 
void DrawTone(int x,int y)/*画石头*/ 
{ 
setfillstyle(SOLID_FILL,7); 
bar(100+x*20-9,50+y*20-9,100+x*20+9,50+y*20+9); 
} 
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)/*画砖*/ 
{ 
setfillstyle(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,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); 
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 x,int y,int i)/*画敌人*/ 
{ 
if(amy[i].color==12) 
setcolor(12); 
else if(amy[i].color==13) 
setcolor(13); 
else/*这里是判断三种颜色的坦克*/ 
setcolor(14); 
circle(100+x*20,50+y*20,7); 
switch(amy[i].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 Score()/*输出分数*/ 
{ 
char s[10]; 
Playone.score+=10; 
sprintf(s,"%d",Playone.score); 
setfillstyle(SOLID_FILL,0); 
bar(550,100,640,130); 
settextstyle(0,0,2); 
setcolor(YELLOW); 
outtextxy(550,115,s); 
} 
void DrawMap()/*画地图*/ 
{int i,j,k; 
for(i=0;i<20;i++) 
   { 
   for(j=0;j<20;j++) 
    if(map[i][j]==1) 
     DrawTone(j,i); 
    else if(map[i][j]==2) 
     DrawBrick(j,i); 
    else if(map[i][j]==3) 
     DrawWater(j,i); 
    else if(map[i][j]==5) 
     DrawHome(j,i); 
    else if(map[i][j]==8) 
     { 
     Playone.x=i; 
     Playone.y=j; 
     Playone.direction=1; 
     DrawPlay(j,i); 
     for(k=0;k<5;k++) 
     Playone.fire[k].direction=-1;/*5颗子弹的方向都为-1,表示不存在*/ 
     } 
    else if(map[i][j]==9) 
    { 
    amy[0].x=1;amy[0].y=1;amy[0].direction=amy[0].directiontwo=3;/*第一个敌人*/ 
    amy[0].color=12; 
    DrawAmy(j,i,0); 
    } 
   } 
for(i=1;i<5;i++)/*敌人都没出现*/ 
amy[i].direction=amy[i].fire.direction=-1; 
outtextxy(210,450,"2003.10.1 milo_zy"); 
settextstyle(0,0,2);/*首次输出得分*/ 
setcolor(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_state[ScanCode]=0; 
    } 
else 
    { 
     key_state[ScanCode]=1; 
 
     key_pressed[ScanCode]=1; 
    } 
outportb(0x20,0x20); 
} 

void InstallKeyboard(void) 
{ 
int i; 
for(i=0;i<128;i++) 
key_state[i]=key_pressed[i]=0; 
OldInt9Handler=getvect(9);          /*中断向量值*/ 
setvect(9,NewInt9);                 /*中断程序NewInt9地址存入指定的中断向量表中INT 09H*/ 
} 

void ShutDownKeyboard(void) 
{ 
setvect(9,OldInt9Handler); 
} 

int GetKey(int ScanCode) 
{ 
int res; 
res=key_state[ScanCode]|key_pressed[ScanCode]; 
key_pressed[ScanCode]=0; 
return res; 
} 
void GameOver()/*游戏失败*/ 
{ 
setcolor(0); 
setfillstyle(SOLID_FILL,0);/*把老家给去除*/ 
fillellipse(100+9*20,50+18*20,9,9); 
nosound(); 
setcolor(RED); 
   settextstyle(0,0,4); 
outtextxy(150,5,"GAME OVER"); 
while(1) 
{ 
   if(GetKey(KEY_ESC)) 
   break; 
} 
} 
void GamePlay()/*玩游戏的过程*/ 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -