c语言坦克大战源代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
/*游戏的整体思路大概是这样的?
首先是欢迎界面,然后进入游戏界面,最后是gameover的界面。
本来打算做单人游戏,后来发现让敌人自主移动比较困难,所以改成了双人游戏?
layer1控制按键是up,down,left,right,enter,player2控制按键是a,s,d,w,space。
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<graphics.h>
/*定义鼠标键值常量*/
#define ESC 0x011b/*玩家1坦克按键*/
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define ENTER 0x1c0d
#define up 0x1177/*玩家2坦克按键*/
#define down 0x1f73
#define left 0x1e61
#define right 0x2064
#define fire 0x246a
/*定义游戏常量*/
/*双人游戏*/
#define NUM 2
/*坦克宽度*/
#define WIDTH 20
/*坦克的数量,宽度*/
/*定义global变量*******************************************/
/*子弹的属性*/
struct myboom{
/*如果子弹life为0则代表子弹没有发射*/
int life;
int x,y;
int direction;
};
/*子弹们的初始属性*/
struct myboom iboom[NUM]=
{{0},{0}};
/*坦克的属性*/
struct mytank{
int life;
int x,y;
int direction;
};
/*坦克们的初始属性*/
struct mytank itank[NUM]=
{{3,10*WIDTH,22*WIDTH},{1,440,40}};
pre[NUM][2]={
{10*WIDTH,22*WIDTH},{440,40}};
/*xy[0]代表自己的坦克; xy[1]及以后代表敌军;
坦克坐标*/
/*存被子弹覆盖的图像*/
void *boom_save[NUM];
/*malloc开辟图像空间的大小*/
int size;
/*动画显示*/
void *save[NUM];
/*后来加上的。。用来让坦克一点一点的动*/
void *isave[NUM+1];
/*储存敌人移动方向*/
int direction=RIGHT;
/*接受键盘输入*/
int input[NUM]={DOWN};
;/*存子弹的图像*/
void *boomimg;
void *iboomimg[NUM];/**/
void loading();
/*声明函数************************************/
void draw(int what);
void move(int direction);
int canmove(int direction,int which);
void byebye(int mode);
void init();
int welcome();
void write(char *mat,int matsize,int x,int y,int color);
void tank(int x,int y,int which,int direction);
void wall(int mode,int x,int y);
int choose();
void boom(int direction,int prex,int prey,int width);
int howmove();
void imove(int direction,int which,int can);
void iboommove(int which);
void shoot(int which,int direction);
void getInitImg();
void drawboom();
void snake();
void isGameOver();
/*一些图形,“杀”代表player2,“我是李勇”代表player1*/
char sha[]={
/* 以下是 '杀' 的 16点阵宋体 字模,32 byte */
0x00,0x20,0x18,0x20,0x06,0x40,0x01,0x80,
0x02,0x60,0x0C,0x30,0x31,0x10,0x01,0x00,
0x7F,0xFE,0x01,0x00,0x09,0x20,0x09,0x10,
0x11,0x08,0x21,0x08,0x05,0x00,0x02,0x00,
};
char wo[]={
/* 以下是 '我' 的 16点阵宋体 字模,32 byte */
0x06,0x40,0x38,0x50,0x08,0x4
8,0x08,0x48,
0x08,0x40,0xFF,0xFE,0x08,0x40,0x08,0x48,
0x0E,0x28,0x38,0x30,0
xC8,0x20,0x08,0x50,
0x09,0x92,0x08,0x0A,0x28,0x06,0x10,0x02,
};
char shi[]={
/* 以下是 '是' 的 16点阵宋体 字模,32 byte */
0x00,0x00,0x0F,0xF0,0x08,0x10,0x0F,0xF0,
0x08,0x10,0x0F,0xF0,0x00,0x00,0xFF,0xFE,
0x01,0x00,0x09,0x00,0x09,0xF8,0x09,0x00,
0x15,0x00,0x23,0x00,0x40,0xFE,0x00,0x00,
};
char li[]={
/* 以下是 '李' 的 16点阵宋体 字模,32 byte */
0x01,0x00,0x01,0x00,0x7F,0xFC,0x03,0x80,
0x05,0x40,0x09,0x30,0x31,0x0E,0xDF,0xE4,
0x00,0x80,0x01,0x00,0x7F,0xFE,0x01,0x00,
0x01,0x00,0x01,0x00,0x05,0x00,0x02,0x00,
};
char yong[]={
/* 以下是 '勇' 的 16点阵宋体 字模,32 byte */
0x3F,0xF0,0x02,0x60,0x01,0x80,0x3F,0xF8,
0x21,0x08,0x3F,0xF8,0x21,0x08,0x3F,0xF8,
0x21,0x08,0x02,0x00,0x7F,0xF8,0x02,0x08,
0x04,0x08,0x08,0x08,0x10,0x28,0x20,0x10,
};
/*****地**********图*********一************号***********/
/*坐标含义:地图块类型,横坐标,纵坐标
地图一 地图块类型:3海水,2草地,1栅栏
.该地图使用vb得到的*/
int map1[]={2,7,1,3,8,1,3,9,1,3,
19,1,3,20,1,2,7,2,3,8,2,3,9,2,
3,19,2,3,20,2,3,1,3,3,2,3,3,3,
3,3,4,3,2,7,3,3,8,3,
3,9,3,3,12,3,3,13,3,3,14,3,3,
15,3,3,16,3,3,17,3,3,18,3,
3,19,3,3,20,3,3,23,3,3,24,
3,3,25,3,3,26,3,3,1,4,
3,2,4,3,3,4,3,4,4,2,7,4,3,
8,4,3,9,4,3,12,4,3,13,4,
3,14,4,3,15,4,3,16,4,3,17
,4,3,18,4,3,19,4,3,20,4,3,23,4,
3,24,4,3,25,4,3,26,4,2,1,5
,2,2,5,2,3,5,2,4,5,3,19,5,
3,20,5,3,23,5,3,24,5,2,25,
5,2,26,5,2,1,6,2,2,6,2,3,6,
2,4,6,3,19,6,3,20,6,3,23,
6,3,24,6,2,25,6,2,26,6,2,1,7,
2,2,7,3,3,7,3,4,7,3,5,7,3,
6,7,3,7,7,3,8,7,3,9,7,
3,10,7,3,11,7,2,14,7,2,15,
7,2,23,7,2,24,7,2,25,7,2,26,7,
2,1,8,2,2,8,2,3,8,2,4,8,3,
8,8,3,9,8,1,14,8,1,15,8,
2,23,8,2,24,8,2,25,8,2,26,
8,2,1,9,2,2,9,2,3,9,2,4,9,
2,8,9,2,9,9,3,16,9,3,17,9,
3,20,9,3,21,9,3,22,9,3,23,9,
3,24,9,1,6,10,1,7,10,2,8,10,
2,9,10,1,10,10,1,11,10,3,16,10,
3,17,10,3,20,10,3,21,10,3,22,
10,3,23,10,3,24,10,3,3,11,3,4,11,
3,5,11,3,6,11,3,7,11,2,8,11,
2,9,11,3,10,11,3,11,11,3,12,11,
3,13,11,3,14,11,3,15,11,3,16,
11,3,17,11,3,3,12,3,4,12,3,5,12,
3,6,12,3,7,12,2,8,12,2,9,12,3,
10,12,3,11,12,3,12,12,3,13,12,
3,14,12,3,15,12,3,16,12,3,17,
12,3,3,13,1,8,13,1,9,13,1,14,13,
1,15,13,1,21,13,1,22,13,3,25,13,
3,26,13,1,1,14,1,2,14,1,8,14,
1,9,14,1,14,14,1,15,14,1,21,
14,1,22,14,3,25,14,3,26,14,3,1,15,
3,2,15,3,3,15,3,4,15,3,6,15,3,7,
15,3,8,15,3,9,15,3,10,15,
3,11,15,1,14,15,1,15,15,2,19,15,
2,20,15,1,21,15,1,22,15,3,25,15,
3,26,15,3,1,16,3,2,16,3,3,16,3,
4,16,3,6,16,3,7,16,3,8,16,
3,9,16,3,10,16,3,11,16,1,14,16,
1,15,16,2,19,16,2,20,16,3,21,16,
3,22,16,2,23,16,2,24,16,3,25,16,
3,26,16,1,4,17,1,5,17,1,6,17,
2,8,17,2,9,17,2,10,17,2,11,17,2,
12,17,2,13,17,1,14,17,1,15,17,
2,16,17,2,17,17,1,18,17,3,19,17,
3,20,17,3,21,17,3,22,17,2,23,17,
2,24,17,3,25,17,3,26,17,1,4,18,
1,5,18,1,6,18,2
,8,18,2,9,18,
2,10,18,2,11,18,2,12,18,2,13,18,
1,18,18,3,19,18,3,20,18,3,21,18,
3,22,18,3,3,19,3,4,19,3,5,19,3,6,
19,3,7,19
,2,8,19,2,9,19,
3,19,19,3,20,19,3,3,20,3,4,20,3,
5,20,3,6,20,3,7,20,2,8,20,
2,9,20,1,11,20,1,12,20,1,13,20,1,
14,20,3,19,20,3,20,20,3,25,20,
3,26,20,3,3,21,3,4,21,1,11,21,1,
14,21,1,16,21,3,23,21,3,24,21,
3,25,21,3,26,21,3,3,22,3,4,22,1,
11,22,1,14,22,1,16,22};/*287*/
int map1_len=287; /*表示共有多少个三个一组的坐标*/
/*主函数开始***************************************************************/
int main()
{
int i, /*计数*/
mychoose; /*游戏选单:单人双人还是建设*/
int can[NUM],ix[NUM],iy[NUM],step;
int iinput,esc; char os;
/*判断是否是win7的系统*/
printf("Is your computer's OS Windows7 ? Y/N \n");
scanf("%c",&os);
if(os=='y'){
printf("\nWindows7 OS cannot run the game,please run it in OS winXP.\n");
printf("press any key to continue...\n");
getch();
return 0;
}else if(os!='n'){
printf("\nInput Y or N please ! Run the game again...\n\n");
printf("\n\npress any key to continue..");
getch();
getch();
return 0;
}
printf("Loading...");
delay(10000);delay(10000);delay(10000);delay(10000);
delay(10000);delay(10000);delay(10000);delay(10000);
delay(10000);delay(10000);delay(10000);delay(10000);
delay(10000);delay(10000);delay(10000);delay(10000);
init(); /*开始图形界面*/
/*画出并存储坦克和炮弹的图像到变量中*/
getInitImg();
/*假装载入游戏*/
loading();
loading();
/*欢迎界面*/
/*选择界面*/
mychoose=welcome();
if(mychoose==1){
/*进入单人游戏的标记*/
}
loading();
/*另外一个欢迎界面 */
snake();
loading();
cleardevice();
/*画界面*/
draw(0);
while(1){
/***************************************************************************/
/*主要部分*/
/*when没有按键输入*/
while(!kbhit()){
if(input[0]==ESC){ /*ESC键退出*/
settextstyle(0,0,2); /*设置颜色和字体大小*/
setfillstyle(1,15);/*类型,颜色*/
bar(0,170,639,245); /*画出横条*/
setcolor(4);
outtextxy(100,200,"Are you sure to exit? Y/N"); /*写字*/
esc=getch();
/*确认退出*/
if(esc=='y'){
byebye(1);return 0;
}else if(esc==ESC){
settextstyle(0,0,2); /*设置颜色和字体大小*/
setfillstyle(1,15);/*类型,颜色*/
bar(0,170,639,245); /*画出横条*/
outtextxy(200,200,"All right... Bye bye.."); /*写字*/
getch();
byebye(1);return 0;
}else{
settextstyle(0,0,2); /*设置颜色和字体大小*/
setfillstyle(1,15);/*类型,颜色*/
bar(0,170,639,245); /*画出横条*/
outtextxy(100,200,"You have to exit !!!"); /*写字*/
getch();
byebye(1);return 0;
}
}
for(i
=0;i<NUM;i++){
can[i]=canmove(input[i],i);/*判断坦克能否移动*/
}
/*循环显示,画出坦克子弹,并暂停一会*/
for(step=0;step<10;step++){
for(i=0;i<NUM;i++){
/*坦克移动*/
imove(input[i],i,can[i]);
/*子弹移动*/
iboommove(i);
/*移动过程中判断是否满足游戏结束的条件*/
isGameOver();
}
/*暂停一会,让人感到动画*/
delay(10000);
}
}/*end of while(!kbhit()) */
iinput=bioskey(0);
switch(iinput){
/*判断是哪个玩家的输入,并存入不同的变量中,或者发子弹*/
/*第一个玩家输入方向控制*/
case LEFT :
case RIGHT:
case UP:
case DOWN:
case ESC: input[0]=iinput;break;
/*第一个玩家输入发射子弹*/
case ENTER: shoot(0,itank[0].direction);break;
/*第2个玩家输入发射子弹*/
case fire: shoot(1,itank[1].direction);break;
/*第2个玩家输入方向控制
这里要分别赋值*/
case up:input[1]=UP;break;
case down:input[1]=DOWN;break;
case left:input[1]=LEFT;break;
case right:input[1]=RIGHT;break;
default:break;
}
/*ok 现在按了某个按键,并接收。若为确定键,则不接受,发子弹而已*/
}/*end of while(1) */
}
/***********************************************************************************/
/*画出墙壁:墙壁种类,墙壁坐标*/
void wall(int mode,int x,int y)
{
/*3海水,2草地,1栅栏.*/
int i;
switch(mode){
case 0: /*画边框*/
setfillstyle(1,8);
bar(x,y,x+WIDTH-1,y+WIDTH-1);break;
case 1: /*画砖头墙壁*/
setfillstyle(1,4);
bar(x,y,x+WIDTH-1,y+WIDTH-1);
line(x,y,x+WIDTH-1,y);
line(x,y,x,y+WIDTH-1);
line(x,y+WIDTH-1,x+WIDTH-1,y+WIDTH-1);
line(x+WIDTH-1,y,x+WIDTH-1,y+WIDTH-1);
for(i=1;i<4;i++)
line(x,y+i*5,x+WIDTH-1,y+i*5);
line(x+WIDTH/2,y,WIDTH/2+x,y+WIDTH/4);
line(x+WIDTH/2,y+WIDTH/2,x+WIDTH/2,y+WIDTH/4*3);
line(x+6,y+5,x+6,y+10);
line(x+6,y+15,x+6,y+WIDTH-1);
line(x+14,y+5,x+14,y+10);
line(x+14,y+15,x+14,y+WIDTH-1);
break;
/*画草地*/
case 2:
setfillstyle(1,2);
bar(x,y,x+WIDTH-1,y+WIDTH-1);
x=x+3;
y=y+3;
line(x+2,y+4,x+5,y+8);
line(x+4,y+2,x+5,y+8);
line(x+8,y+4,x+5,y+8);
break;
/*画海水*/
case 3:
setfillstyle(6,1);
bar(x,y,x+WIDTH-1,y+WIDTH-1);
break;
default:break;
}
}
/*显示字模的函数*/
void write(char *mat,int matsize,int x,int y,int color)
/*依次:字模指针、点阵大小,如8、16、
起始坐标(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);
}
/*游戏开始啦,返回选择了第几项
有三个选项:player 1;player 2;construction;
不过很明显,这三个选项在这个游戏里是一样的。*/
int welcome()
{
settextstyle(0,0,2);
outtextxy
(50,50,"I- 00 HI- 20000");
setcolor (4);settextstyle(0,0,8);
outtextxy(150,90,"METALS");settextstyle(0,0,8);
outtextxy(220,170,"TANK");
setcolor(15);settextstyle(0,0,2);
outtextxy(200,245,"player 1");
outtextxy(200,270,"player 2");
outtextxy(200,295,"construction");setcolor (4); settextstyle(0,0,3);
outtextxy(250,340,"NAMCOT"); setcolor(15); settextstyle(0,0,2);
outtextxy(110,395,"@ 1980 1985 MANCO LTD.");
outtextxy(190,420,"HUCK BY MS GROUP");
return choose();
}
/*游戏选单,返回选择了第几项*/
int choose(){
void *save,*pre;
int da,in,which=1;
int x=160,y=245;
/*保存游戏选单前面的箭头*/
da=imagesize(x,y,x+30,y+15);
save=malloc(da);
pre=malloc(da);
getimage(x,y,x+30,y+15,pre);
outtextxy(x,y,">>");
getimage(x,y,x+30,y+15,save);
/*根据输入上下来移动箭头*/
in=bioskey(0);
while(in){
/*如果按了enter则结束选项*/
if(in==ENTER){break;}
/*如果按了down*/
if(in==DOWN){
which=(which==3)?1:which+1;
putimage(x,y,pre,0);
if(which==1)y=y-50;
else y=y+25;
/*如果按了up*/
}else if(in==UP){
which=(which==1)?3:which-1;
putimage(x,y,pre,0);
if(which==3)y=y+50;
else y=y-25;
}
/*0代表简单复制*/
putimage(x,y,save,0);
in=bioskey(0);
}
return which ;
}
/*进入图形模式*/
void init()
{
int gd=DETECT, gm = 0;
registerbgidriver(EGAVGA_driver);
initgraph(&gd, &gm, "");
}
/*游戏结束了,写上gameover*/
void byebye(int mode)/*mode为零代表gameover,1代表中途退出*/
{
int i;
/*清除地图,设置背景色为黑色*/
loading();
cleardevice();
setbkcolor(0);
/*输出gameover*/
setcolor(4);settextstyle(0,0,5);
outtextxy(150,200,"GAME OVER");
if(mode==0){
settextstyle(0,0,1);
outtextxy(150,250,"You are defeated...");
}else{
settextstyle(0,0,1);
outtextxy(150,250,"You will exit the game.Welcome back...");
}
setcolor(15);settextstyle(0,0,2);
outtextxy(200,300,"Thank you for playing !!!");
getch();
getch();
getch();
/*释放指针指向的内存*/
for(i=0;i<NUM+1;i++) {
free(isave[i]);
free(sa
ve[i]);
}
exit(0);
}
/*画坦克:
坦克左上角坐标信息,那个player的坦克,坦克方向*/
void tank(int x,int y,int which,int direction)
{
setfillstyle(1,15);
bar(x,y,x+WIDTH-1,y+WIDTH-1);
/*0代表是wanjia的坦克*/
if(which==0){
switch(direction){
case DOWN:write(wo,16,x+2,y+2,4);break;
case RIGHT:write(shi,16,x+2,y+2,4);break;
case UP:write(li,16,x+2,y+2,4);break;
case LEFT:write(yong,16,x+2,y+2,4);break;
default:break;
}
}
else{
/*敌人的坦克*/
write(sha,16,x+2,y+2,4);
}
}
/*画出一些东西*/
void draw(int what)
{
int i;
switch(what){
case 0: /*画map1地图*/
for(i=0;i<map1_len*3;i+=3){
wall(map1[i],map
1[i+1]*WIDTH,map1[i+2]*WIDTH);
}
for(i=0;i<24;i++){
wall(0,0,WIDTH*i) ;
wall(0,WIDTH*27,i*WIDTH);
}
for(i=0;i<32;i++){
wall(0,i*WIDTH,23*WIDTH);
wall(0,i*WIDTH,0);
}
for(i=WIDTH;i<23*WIDTH;i+=WIDTH){
wall(0,31*WIDTH,i);
}
/*屏幕右端的显示*/
wall(1,29*WIDTH,3*WIDTH);
wall(2,29*WIDTH,5*WIDTH);
wall(3,29*WIDTH,7*WIDTH);
/*边框线*/
setcolor(6);
line(WIDTH,WIDTH,WIDTH,(480/WIDTH-1)*WIDTH);
line(WIDTH,WIDTH,WIDTH*(640/WIDTH-5),WIDTH);
line(WIDTH,(480/WIDTH-1)*WIDTH,WIDTH*(640/WIDTH-5),(480/WIDTH
-1)*WIDTH );
line(WIDTH*(640/WIDTH-5),WIDTH,WIDTH*(640/WIDTH-5),(480/WIDTH
-1)*WIDTH );
line(WIDTH*28,WIDTH,WIDTH*31,WIDTH);
line(WIDTH*28,WIDTH,WIDTH*28,WIDTH*23);
line(WIDTH*28,WIDTH*23,WIDTH*31,WIDTH*23);
line(WIDTH*31,WIDTH*23,WIDTH*31,WIDTH);
/*文字显示*/
setcolor(15);
settextstyle(0,0,1);
outtextxy(28*WIDTH+5,4*WIDTH,"DEFENCE");
outtextxy(28*WIDTH+13,6*WIDTH,"GRASS");
outtextxy(28*WIDTH+15,8*WIDTH,"RIVER");
tank(WIDTH*29,WIDTH*12,1,UP);
outtextxy(WIDTH*28+8,WIDTH*13+10,"ENERMY");
outtextxy(WIDTH*29,WIDTH*14,"25");
tank(WIDTH*29,WIDTH*16,0,UP);
outtextxy(WIDTH*29,WIDTH*17+10,"YOU");
outtextxy(WIDTH*29+10,WIDTH*18,"3");
break;
case 1:
/*另外的一张地图,,,这张地图没做*/
break;
default:break;
}
/*先保存原来的图像*/
for(i=0;i<NUM;i++){
getimage(itank[i].x,itank[i].y,itank[i].x+WIDTH-1,itank[i].y+WIDTH-1,isave[i]);
}
/*再画出坦克们*/
for(i=0;i<NUM;i++){
putimage(itank[i].x,itank[i].y,save[i],0);
}
}
void getInitImg()
{
/*画图存储*/
int i;
cleardevice();
size=imagesize(itank[0].x,itank[0].y,itank[0].x+WIDTH,itank[0].y+WIDTH);
for(i=0;i<NUM;i++) {
save[i]=malloc(size);
isave[i]=malloc(size);
}
/*分配内存空间*/
boomim
g=malloc(size);
tank(0,0,0,UP);
getimage(0,0,WIDTH,WIDTH,save[0]);
/*获取自己的坦克图像*/
getimage(0,0,WIDTH,WIDTH,isave[0]);
tank(0,0,1,DOWN);
for(i=1;i<NUM;i++){
getimage(0,0,WIDTH,WIDTH,save[i]);
/*获取敌人坦克图像*/
getimage(0,0,WIDTH,WIDTH,isave[i]);
}
cleardevice();
drawboom(0,0);
getimage(0,0,WIDTH,WIDTH,boomimg);
cleardevice();
}
/*画出炮弹:左上角坐标*/
void drawboom(int x,int y)
{
circle(x+10,y+10,5);
}
/*判断坦克能否移动:将要移动的方向,哪一个坦克;
返回1代表可以移动,0不能*/
int canmove(int direction,int which)
{
int i,ix,iy;
/*这里是判断能否移动,所以
不能直接修改坐标值。。因为也许是不能移动
.so 赋值给其他变量来判断*/
ix=itank[which].x;
iy=itank[which].y;
/*计算出移动后的坐标值,判断是否合法,不是预定的方向值则退出*/
switch(direction){
case DOWN: iy=itank[which].y+WIDTH;break;
case UP: iy=itank[which].y-WIDTH;break;
case LEFT: ix=itank[which].x-WIDTH;break;
case
RIGHT:ix=itank[which].x+WIDTH;break;
default: return 0;
}
for(i=0;i<map1_len*3;i+=3){
if(ix==map1[i+1]*WIDTH && iy==map1[i+2]*WIDTH){
/*如果遇到墙壁*/
if(map1[i]!=2)
/*如果不是草地..map1[i]代表墙壁类型*/
return 0;
}
}
/*如果遇到边缘,自然也不能移动,返回0*/
if(ix==0 || ix==27*WIDTH || iy==0 || iy==23*WIDTH)return 0;
else
/*除了以上的所有情况才返回1*/
return 1;
/*表示能移动*/
}
/***************************************************************************/
/*坦克移动:将要移动的方向,哪个坦克,能否移动*/
void imove(int direction,int which,int can)
{
int ix=0,iy=0;
/*如果能移动*/
if(can){
/*覆盖原来的坦克图像*/
putimage(itank[which].x,itank[which].y,isave[which],0);
/*获取方向*/
switch(direction){
case DOWN:iy=+2;break ;
case UP:iy=-2;break ;
case LEFT:ix=-2;break;
case RIGHT:ix=2;
}
/*改变坦克坐标*/
itank[which].x=itank[which].x+ix;
itank[which].y=itank[which].y+iy;
/*得到将要画坦克的区域的图像,以便还原*/
getimage(itank[which].x,itank[which].y,itank[which].x+WIDTH,itank[which].y+WIDTH,isave[which]);
/*画出坦克,完成坦克移动*/
tank(itank[which].x,itank[which].y,which,direction);
}
/*坦克方向赋新值*/
itank[which].direction=direction;
}
/*发射子弹前的准备工作*/
void shoot(int which,int direction)
{
int ix=0,iy=0;
/*如果某个坦克没有发子弹的话*/
if(iboom[which].life==0){
/*让子弹获取方向和生命*/
iboom[which].life=1;
/*子弹的方向赋值*/
iboom[which].direction=direction;
switch(direction){
case UP: iy=-1;break;
case DOWN:iy=1;break;
case LEFT:ix=-1;break;
case RIGHT:ix=1;break;
default:break;
}
/*改变子弹坐标*/
iboom[which].x=itank[which].x+ix*WIDTH;
iboom[which].y=itank[which].y+iy*WIDTH;
/*获取将要 画出子弹的区域的图像*/
getimage(iboom[which].x,iboom[which].y,iboom[which].x+WIDTH-1,iboom[which].y+WIDTH-1,iboomimg[which]);
}
}
void iboommove(int which)
{
int ix=0,iy=0;int i;
/*如果子弹超出了屏幕*/
if(iboom[which].life==1 && ( iboom[which].x> 26*WIDTH || iboom[which].x<WIDTH || iboom[which].y>22*WIDTH || iboom[which].y<WIDTH )){
/*表示子弹没了,过界了。*/
iboom[which].life=0;
/*让最后的子弹图像消失*/
putimage(iboom[which].x,iboom[which].y,iboomimg[which],0);
}
/*如果子弹还可以运动*/
if(iboom[which].life==1){
/*画图覆盖子弹*/
putimage(iboom[which].x,iboom[which].y,iboomimg[which],0);
switch(iboom[which].direction){
case DOWN:iy=+4;break ;
case UP:iy=-4;break ;
case LEFT:ix=-4;break;
case RIGHT:ix=4;
}
/*得到了子弹的坐标,根据方向*/
iboom[which].x=iboom[which].x+ix;
iboom[which].y=iboom[which].y+iy;
/*保存将要画出子弹的区域的图像*/
getimage(iboom[which].x,iboom
[which].y,iboom[which].x+WIDTH-1,iboom[which].y+WIDTH-1,iboomimg[which]);
/*画出子弹*/
drawboom(iboom[which].x,iboom[which].y);
}
}
/*游戏简介界面*************************************************/
void snake()
{
/*界面上窗口的基座标,左上顶点的坐标*/
int basicx=0,basicy=80,winwidth=639,winheight=320,i,j;
char *word[11]={"I am Li Yong, A student form WHU , and major in computer science.","Welcome to play the game!! It's my first C game work.","","","Directions:","Player1 uses up,down,right,left,center to play the game.",
"Player2 uses w,s,d,a,j to play the game","","","Have Fun with the game -- my best wishes ...","press Enter to enter the game."};
cleardevice();
/*设置bar的属性*/
setfillstyle(1,7);/*类型,颜色*/
/*动画,矩形变大*/
for(i=0,j=0;i<(winwidth+1)/2 && j<winheight/2;i+=2,j+=1){
bar(320-i,240-j,320+i,240+j);
}
/*输出文字*/
settextstyle(0,0,1);
for(i=1;i<=10;i++){
for(j=15;j>=1;j--){
setcolor(j);
outtextxy(30,basicy+20*i,word[i-1]);
delay(10000);
}
delay(10000);delay(10000);delay(10000);delay(10000);
delay(10000);delay(10000);delay(10000);delay(10000);
delay(10000);delay(10000);
}
/*press enter to continer*/
settextstyle(0,0,1);
while(1){
while(!kbhit()){
for(i=0;i<16;i++){
setcolor(i);
outtextxy(250,350,word[10]);
delay(1000);
}
}
if(bioskey(0)==ENTER)break;
}
/*结束动画,擦除矩形*/
for(i=0;i<640;i++){
setfillstyle(1,0);
bar(basicx,basicy,basi
cx+i,basicy+winheight);
}
cleardevice();
}
/*判断是否游戏结束*/
void isGameOver()
{
int d;
/*子弹打中了PLAYER2*/
if(0/*(iboom[0].x-itank[1].x)>0 && (iboom[0].x-itank[1].x)<WIDTH && (iboom[0].y-itank[1].y)>0 && (iboom[0].y-itank[1].y)<WIDTH*/){
setcolor(15);
/*设置颜色和字体大小*/
settextstyle(0,0,2);
/*画出横条*/
bar(0,190,639,225);
setcolor(4);
outtextxy(200,200,"PLAYER 2 IS DEAD !"); /*写字*/
/*暂停一下,让人看到gameover*/
getch();
byebye(0);
}/*再见*/
/*子弹打中了PLAYER1*/
if(0/*(iboom[1].x-itank[0].x)>0 && (iboom[1].x-itank[0].x)<WIDTH && (iboom[1].y-itank[0].y)>0 && (iboom[1].y-itank[0].y)<WIDTH*/){
setcolor(15);
/*设置颜色和字体大小*/
settextstyle(0,0,2);
/*画出横条*/
bar(0,170,639,245);
setcolor(4);
outtextxy(200,200,"PLAYER 1 IS DEAD !"); /*写字*/
getch();/*暂停一下*/
byebye(0);
}/*再见*/
/*子弹打中了墙壁*/
if(1){
/*在这里写代码,把墙壁破坏掉*/
}
/*坦克相撞了*/
d= (itank[0].x-itank[1].x)*(itank[0].x-itank[1].x)+(itank[0].y-itank[1].y)*(itank[0].y-itank[1].y);
if(0/*d==WIDTH*WIDTH*/){/*如果两个坦克靠在一起*/
setcolor(15);
settextstyle(0,0,2
); /*设置颜色和字体大小*/
bar(0,170,639,245); /*画出横条*/
setcolor(4);
outtextxy(200,200,"crashed into pieces"); /*写字*/
getch();/*暂停一下*/
byebye(0);
}/*再见*/
}
void loading()
{
/*循环time次*/
int i,j;int time=3;
cleardevice();
setcolor(15);settextstyle(0,0,2);
/*循环*/
for(j=0;j<time;j++){
outtextxy(200,200,"LOADING");
/*每次循环输出点*/
for(i=1;i<5;i++){
outtextxy(320+i*8,200,".");
/*延时 */
delay(10000);delay(10000);delay(10000);delay(10000);
delay(10000);delay(10000);delay(10000);delay(10000);
}
cleardevice();
}
}