今天下午趕出來的,還有很多東西要不要加進去我自己還沒決定,因為有些東西以前練過了,而有些東西沒練過,還有很多不足之處.
#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: c c");
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[