坦克大战写完了一半请高手帮忙指点下
hanzi.h/*头文件*/#ifndef _HANZI_H
#define _HANZI_H
/* 点阵汉字 */
void drawmat(char *mat,int matsize,int x,int y,int color)
/*依次:字模指针、点阵大小、起始坐标(x,y)、颜色*/
{
int i, j, k, n;
n = (matsize - 1) / 8 + 1;
for(j = 0; j < matsize; j++)
for(i = 0; i < n; i++)
for(k = 0;k < 8; k++)
if(mat[j * n + i] & (0x80 >> k)) /*测试为1的位则显示*/
putpixel(x + i * 8 + k, y + j, color);
}
char S1[]={
/* 以下是 '↑' 的 12点阵宋体 字模,24 byte */
0x04,0x00,0x04,0x00,0x0E,0x00,0x0E,0x00,
0x15,0x00,0x04,0x00,0x04,0x00,0x04,0x00,
0x04,0x00,0x04,0x00,0x04,0x00,0x00,0x00,
/* 以下是 '前' 的 12点阵宋体 字模,24 byte */
0x11,0x00,0x0A,0x40,0xFF,0xE0,0x00,0x00,
0x79,0x40,0x49,0x40,0x79,0x40,0x49,0x40,
0x79,0x40,0x48,0x40,0x59,0xC0,0x00,0x00,
/* 以下是 '进' 的 12点阵宋体 字模,24 byte */
0x44,0x80,0x24,0x80,0x1F,0xE0,0x04,0x80,
0xC4,0x80,0x5F,0xE0,0x44,0x80,0x44,0x80,
0x48,0x80,0xB0,0x80,0x9F,0xE0,0x00,0x00,
};
#endif
jiegou.h/*头文件*/
#ifndef _JIEGOU_H
#define _JIEGOU_H
/* 坦克 */
struct tank
{
int num;/* 坦克编号1,2 */
int x,y; /* 坦克的坐标 */
int fang;/* 坦克的方向0上,2下,4左,6右 */
int dw;/* 敌我 0为敌,1为我; */
};
/* 砖块 */
struct zhuan
{
int x;
int y;
};
/* 炮弹 */
struct pao
{
int fang;/* 炮弹的方向 */
int loo;/* 炮弹飞过的距离 */
int x;
int y;
};
/* 石头 */
struct st
{
int num;
int x;
int y;
};
#endif
/*主体函数*/
#include<graphics.h>
#include <dos.h>
#include <time.h>
#include "stdlib.h"
#include "Conio.h"
#include"hanzi.h"
#include"jiegou.h"
/* INTR为定时器中断号 */
#define INTR 0x1c
#define HQPH
/* oldhandler为存储原中断向量地址的变量 */
void interrupt (far*oldhandler)();
/* 图形系统初始化 */
void GraphicsAdapter();
/* 画地图 x在150-400,y在50-450*/
void map();
/* 画坦克长30宽20 */
void huatank(struct tank tanks);
/* 坦克移动 */
void interrupt far tankmove();
/* 画砖块 长宽为10像素*/
void zhuan(struct zhuan zhuans);
/* 画老家x在270-287,y在430-450 */
void HG();
/* 炮弹 */
void paopao(struct pao paod);
/* 石头 长宽为10像素*/
void stone(struct st sto);
/* 打炮弹 */
void dapao();
/* 总部血量 */
void HQhp();
struct tank tanks[20];/* tanks[0]为主坦克 */
struct zhuan zhuans; /* */
struct pao paod[20]; /* paod[0]为主坦克的炮弹 */
int numtank,numpao,key=0;
int wtanknum;
int hghp=0;/* 老家的血 */
void main()
{
struct st sto;
int x,y,i=1,j=1;
char *tanknum,*point,*grade; /* 坦克数,分数,难度 */
tanknum="5",point="100",grade="1";
zhuans.x=270;
zhuans.y=270;
sto.x=195;
sto.y=95;
GraphicsAdapter();
/* 初始化地图 */
map();
HG();
/* 初始化石头(x190-260,y90-100).(x300-360,y90-100).(x=220-340,y=250-260) */
for(i;i<=16;i++)
{
if(i<=7||i>=12)
{
stone(sto);
}
if(i<=15&&i>=4)
{
sto.y=255;
stone(sto);
sto.y=95;
}
sto.x+=10;
}
for(i=1;i<=5;i++)
{
numtank=0;
tanks[numtank].y=410;
tanks[numtank].x=280;
tanks[numtank].dw=1;
tanks[numtank].fang=0;
huatank(tanks[numtank]);
for(;;)
{
key=bioskey(0);
tankmove();
/* oldhandler=getvect(INTR);/* 保存原中断服务程序地址 */ */
/* setvect(INTR,tankmove); */
/* 发射炮弹 */
dapao();
/* setvect(INTR,oldhandler); */
}
}
getch();
closegraph();
}
/* 图形系统初始化 */
void GraphicsAdapter()
{
int gdr;
int gde;
detectgraph(&gdr,&gde);
switch(gdr)
{
case CGA:gde=1;
break;
case VGA:gde=2;
break;
case EGA:gde=1;
break;
case -2:
printf("\nGraphics adapter not installed");
exit(1);
default:
printf("\nGraphics adapter is not CGA,EGA,or VGA");
}
initgraph(&gdr,&gde,"c:\\Win-TC\\projects");
cleardevice();
}
/* 画地图 */
void map()
{
setbkcolor(0);
setcolor(15);
settextstyle(2,0,2);
outtextxy(149,20,"1:Esc key cloce!");
outtextxy(300,20,"2:+,-choice Grade.");
outtextxy(149,30,"3:up key up.");
outtextxy(300,30,"4:dow key dow.");
outtextxy(149,40,"5:left key left.");
outtextxy(300,40,"6:right key right.");
/* 地图 */
setcolor(3);
rectangle(149,49,501,451);
line(401,49,401,451);
setfillstyle(SOLID_FILL,7);
bar(150,50,400,450);
setfillstyle(SOLID_FILL,8);
bar(402,50,500,450);
/* 分数,难度 */
setcolor(5);
settextstyle(0,0,1);
outtextxy(420,60,"TankWar ");
setcolor(0);
settextstyle(2,0,2);
outtextxy(404,100,"Point:");
outtextxy(452,100,"0");
outtextxy(404,120,"Grade:");
outtextxy(452,120,"1");
/* 老家的血 */
setcolor(14);
outtextxy(408,180,"HQHP:");
setcolor(4);
setfillstyle(SOLID_FILL,4);
line(417,190,412,195);
line(417,190,422,195);
line(412,195,422,195);
rectangle(414,196,420,201);
setlinestyle(0,0,3);
line(425,196,476,196);
/* 坦克数 */
outtextxy(408,240,"TankNum");
setfillstyle(SOLID_FILL,12);
bar(412,260,423,275);
setfillstyle(SOLID_FILL,4);
bar(415,265,420,270);
setfillstyle(SOLID_FILL,0);
bar(417,255,418,265);
setcolor(0);
outtextxy(435,265,"5");
}
/* 画坦克 */
void huatank(struct tank tanks)
{
int x=tanks.x;
int y=tanks.y;
int color;
if(1==(tanks.dw))
{
color=6;
}else
{
color=0;
}
setfillstyle(SOLID_FILL,color);
bar(x-10,y-10,x+10,y+10);
setfillstyle(SOLID_FILL,color+5);
bar(x-6,y-6,x+6,y+6);
setfillstyle(SOLID_FILL,color+9);
if(0==(tanks.fang))
{
/* 向上的坦克 */
setfillstyle(10,15);
bar(x-2,y-20,x+2,y+1);
setcolor(7);
setlinestyle(0,0,3);
line(x-10,y+12,x+10,y+12);
}
else if(2==tanks.fang)
{
/* 向下的坦克 */
setfillstyle(10,15);
bar(x-2,y-1,x+2,y+20);
setcolor(7);
setlinestyle(0,0,3);
line(x-10,y-12,x+10,y-12);
}
else if(4==tanks.fang)
{
/* 向左的坦克 */
setfillstyle(10,15);
bar(x-20,y-2,x+1,y+2);
setcolor(7);
setlinestyle(0,0,3);
line(x+12,y-10,x+12,y+10);
}
else if(6==tanks.fang)
{
/* 向右的坦克 */
setfillstyle(10,15);
bar(x-1,y-2,x+20,y+2);
setcolor(7);
setlinestyle(0,0,3);
line(x-12,y-10,x-12,y+10);
}
}
/* 坦克移动 */
void interrupt tankmove()
{
int fang=tanks[numtank].fang;
/* 判断是否按下Esc */
if(0x011b==key)
{
exit(1);
}
/* 控制主坦克的移动 */
/* 向上 */
if((0x4800==key)&&((tanks[numtank].fang)==0))
{
tanks[numtank].y-=3;
}
/* 向上 */
else if(0x4800==key&&(tanks[numtank].fang)!=0)
{
tanks[numtank].fang=0;
}
/* 向下?*/
else if(0x5000==key&&(tanks[numtank].fang)==2)
{
tanks[numtank].y+=3;
}
/* 向下 */
else if(0x5000==key&&(tanks[numtank].fang)!=2)
{
tanks[numtank].fang=2;
}
/* 向左 */
else if(0x4b00==key&&(tanks[numtank].fang)==4)
{
tanks[numtank].x-=3;
}
/* 向左 */
else if(0x4b00==key&&(tanks[numtank].fang)!=4)
{
tanks[numtank].fang=4;
}
/* 向右 */
else if(0x4d00==key&&(tanks[numtank].fang)==6)
{
tanks[numtank].x+=3;
}
/* 向右 */
else if(0x4d00==key&&(tanks[numtank].fang)!=6)
{
tanks[numtank].fang=6;
}
if(!((!(tanks[numtank].y>80&&tanks[numtank].y<=435&&tanks[numtank].x>=180&&tanks[numtank].x<=385))||
(tanks[numtank].x>=175&&tanks[numtank].x<=275&&tanks[numtank].y>=75&&tanks[numtank].y<=115)||
(tanks[numtank].x>=285&&tanks[numtank].x<=375&&tanks[numtank].y>=75&&tanks[numtank].y<=115)||
(tanks[numtank].x>=205&&tanks[numtank].x<=355&&tanks[numtank].y>=235&&tanks[numtank].y<=275)))
{
huatank(tanks[numtank]);
if(fang!=tanks[numtank].fang)
{
setfillstyle(SOLID_FILL,7);
if(fang==0||fang==2)
bar((tanks[numtank].x-2),(tanks[numtank].y+(fang-1)*20),(tanks[numtank].x+2),(tanks[numtank].y+(fang-1)*11));
if(fang==4||fang==6)
bar((tanks[numtank].x+(fang-5)*20),(tanks[numtank].y-2),(tanks[numtank].x+(fang-5)*11),(tanks[numtank].y+2));
}
}
}
/* 画老家 */
void HG()
{
int x=280;
int y=450;
setcolor(0);
setfillstyle(7,4);
bar(x-6,y-13,x+6,y);
line(x,y-20,x-10,y-14);
line(x,y-20,x+10,y-14);
}
/* 画砖块 */
void zhuan(struct zhuan zhuans)
{
int x=zhuans.x;
int y=zhuans.y;
setcolor(0);
setlinestyle(0,0,0);
setfillstyle(9,15);
bar(x-5,y-5,x+5,y+5);
rectangle(x-5,y-5,x+5,y+5);
}
/* 炮弹 半径为3像素*/
void paopao(struct pao paod)
{
int x=paod.x;
int y=paod.y;
setlinestyle(0,0,0);
setcolor(12);
circle(x,y,2);
setcolor(4);
circle(x,y,1);
setcolor(7);
circle(x,y,3);
}
/* 打炮弹 */
void dapao()
{
if(0x3920==key)
{
int n;
numpao=numtank;
paod[numpao].fang=tanks[numpao].fang;
paod[numpao].x=tanks[numpao].x;
paod[numpao].y=tanks[numpao].y;
paod[numpao].loo=400;
if(paod[numpao].fang==0)
{
paod[numpao].y-=23;
}
if(paod[numpao].fang==2)
{
paod[numpao].y+=23;
}
if(paod[numpao].fang==4)
{
paod[numpao].x-=23;
}
if(paod[numpao].fang==6)
{
paod[numpao].x+=23;
}
for(n=0;n<(paod[numpao].loo);n++,delay(500))
{
if((!(paod[numpao].y>53&&paod[numpao].y<=446&&paod[numpao].x>=154&&paod[numpao].x<=396))||
(paod[numpao].x>=186&&paod[numpao].x<=264&&paod[numpao].y>=86&&paod[numpao].y<=104)||
(paod[numpao].x>=296&&paod[numpao].x<=364&&paod[numpao].y>=86&&paod[numpao].y<=104)||
(paod[numpao].x>=216&&paod[numpao].x<=344&&paod[numpao].y>=246&&paod[numpao].y<=264))
{
setfillstyle(SOLID_FILL,RED);
bar(paod[numpao].x-3,paod[numpao].y-3,paod[numpao].x+3,paod[numpao].y+3);
delay(10000);
setfillstyle(SOLID_FILL,7);
bar(paod[numpao].x-3,paod[numpao].y-3,paod[numpao].x+3,paod[numpao].y+3);
break;
}
else if((paod[numpao].x>=266&&paod[numpao].x<=293&&paod[numpao].y>=425))
{
setfillstyle(SOLID_FILL,RED);
bar(paod[numpao].x-3,paod[numpao].y-3,paod[numpao].x+3,paod[numpao].y+3);
delay(10000);
setfillstyle(SOLID_FILL,7);
bar(paod[numpao].x-3,paod[numpao].y-3,paod[numpao].x+3,paod[numpao].y+3);
HQhp();
break;
}
/* 向上飞 */
if(paod[numpao].fang==0)
{
paod[numpao].y-=1;
}
/* 向下 */
if(paod[numpao].fang==2)
{
paod[numpao].y+=1;
}
/* 向左 */
if(paod[numpao].fang==4)
{
paod[numpao].x-=1;
}
/* 向右 */
if(paod[numpao].fang==6)
{
paod[numpao].x+=1;
}
paopao(paod[numpao]);
}
}
}
/* 石头 长宽为10像素*/
void stone(struct st sto)
{
int x=sto.x;
int y=sto.y;
setcolor(0);
setlinestyle(0,0,0);
setfillstyle(7,4);
bar(x-5,y-5,x+5,y+5);
rectangle(x-5,y-5,x+5,y+5);
}
/* 总部血量 */
void HQhp()
{
hghp+=10;
if(hghp>=60)
{
setcolor(5);
settextstyle(0,0,3);
outtextxy(200,200,"GAME OVER ");
getch();
exit(1);
}
setcolor(7);
setlinestyle(0,0,3);
line(476-hghp,196,476,196);
}