用c做的简单的坦克大战小游戏.docx

上传人:b****2 文档编号:23150208 上传时间:2023-05-08 格式:DOCX 页数:23 大小:18.95KB
下载 相关 举报
用c做的简单的坦克大战小游戏.docx_第1页
第1页 / 共23页
用c做的简单的坦克大战小游戏.docx_第2页
第2页 / 共23页
用c做的简单的坦克大战小游戏.docx_第3页
第3页 / 共23页
用c做的简单的坦克大战小游戏.docx_第4页
第4页 / 共23页
用c做的简单的坦克大战小游戏.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

用c做的简单的坦克大战小游戏.docx

《用c做的简单的坦克大战小游戏.docx》由会员分享,可在线阅读,更多相关《用c做的简单的坦克大战小游戏.docx(23页珍藏版)》请在冰豆网上搜索。

用c做的简单的坦克大战小游戏.docx

用c做的简单的坦克大战小游戏

//用c做的简单的坦克大战小游戏(给初学者点动力)

#include

#include

#include

#include

#include

#defineKEY_ESC0x01

#defineKEY_SPACE0x39

#defineKEY_UP0x48

#defineKEY_LEFT0x4b

#defineKEY_RIGHT0x4d

#defineKEY_DOWN0x50

/*1石头,2砖块,3水,5老家,8玩家,9敌人*/

intmap[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};

structf

{

intx;

inty;

intdirection;

};

structplay

{

intx;/*行坐标*/

inty;/*列坐标*/

intdirection;/*方向*/

structffire[5];/*5颗子弹*/

intscore;/*分数*/

}Playone;

structa

{

intx;

inty;

intcolor;

intdirection;

intdirectiontwo;/*用来判断走的路是否往返*/

intfireplay;/*是否攻击的变量,随机生成*/

structffire;

}amy[5];/*敌人的结构体,其实可以添加不同的颜色来表示不同种类的坦克*/

charkey_state[128],key_pressed[128];

voidInit();/*图象驱动开始*/

voidEnd();/*图象驱动结束*/

voidDrawMap();/*画地图*/

voidDrawWater(intx,inty);/*画水*/

voidDrawBrick(intx,inty);/*画砖*/

voidDrawTone(intx,inty);/*画石头*/

voidDrawHome(intx,inty);/*画老家*/

voidDrawBlack(intx,inty);/*去除内容*/

voidDrawPlay(intx,inty);/*画玩家*/

voidDrawAmy(intx,inty,inti);/*画敌人*/

voidScore();/*输出分数*/

voidGamePlay();/*玩游戏过程*/

voidGameOver();/*游戏失败*/

voidTimeDelay(unsignedlongmicrosec);/*延时函数传入微秒数*/

intGetKey(intScanCode);/*这里开始都是按键函数*/

voidinterruptfar(*OldInt9Handler)();

voidfarinterruptNewInt9();

voidInstallKeyboard();

voidShutDownKeyboard();

voidmain(void)

{

Init();

DrawMap();

GamePlay();

End();

}

voidTimeDelay(unsignedlongmicrosec)/*延时函数传入微秒数*/

{

unionREGSr;

r.h.ah=0x86;

r.x.cx=microsec>>16;

r.x.dx=microsec;

int86(0x15,&r,&r);

}

voidInit()/*图象驱动开始*/

{intgd=DETECT,gm;

initgraph(&gd,&gm,"d:

\\tc\\tc");

cleardevice();

InstallKeyboard();

}

voidEnd()/*图象驱动结束*/

{

ShutDownKeyboard();

closegraph();

}

voidDrawTone(intx,inty)/*画石头*/

{

setfillstyle(SOLID_FILL,7);

bar(100+x*20-9,50+y*20-9,100+x*20+9,50+y*20+9);

//作者:

c闲人2004-8-2000:

42 回复此发言

//--------------------------------------------------------------------------------

//2用c做的简单的坦克大战小游戏(给初学者点动力)

}

voidDrawWater(intx,inty)/*画水*/

{

setfillstyle(SOLID_FILL,BLUE);

bar(100+x*20-9,50+y*20-9,100+x*20+9,50+y*20+9);

}

voidDrawBrick(intx,inty)/*画砖*/

{

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);

}

voidDrawHome(intx,inty)/*画老家*/

{

setcolor(0);

setfillstyle(SOLID_FILL,GREEN);

fillellipse(100+x*20,50+y*20,9,9);

}

voidDrawBlack(intx,inty)/*去除内容*/

{

setcolor(0);

setfillstyle(SOLID_FILL,0);

bar(100+x*20-9,50+y*20-9,100+x*20+9,50+y*20+9);

}

voidDrawPlay(intx,inty)/*画玩家*/

{

setcolor(4);/*玩家为红色*/

circle(100+x*20,50+y*20,7);

switch(Playone.direction)/*判断玩家方向*/

{

case1:

line(100+x*20,50+y*20,100+x*20,50+y*20-9);break;/*上*/

case2:

line(100+x*20,50+y*20,100+x*20+9,50+y*20);break;/*右*/

case3:

line(100+x*20,50+y*20,100+x*20,50+y*20+9);break;/*下*/

case4:

line(100+x*20,50+y*20,100+x*20-9,50+y*20);break;/*左*/

}

}

voidDrawAmy(intx,inty,inti)/*画敌人*/

{

if(amy[i].color==12)

setcolor(12);

elseif(amy[i].color==13)

setcolor(13);

else/*这里是判断三种颜色的坦克*/

setcolor(14);

circle(100+x*20,50+y*20,7);

switch(amy[i].direction)/*判断玩家方向*/

{

case1:

line(100+x*20,50+y*20,100+x*20,50+y*20-9);break;/*上*/

case2:

line(100+x*20,50+y*20,100+x*20+9,50+y*20);break;/*右*/

case3:

line(100+x*20,50+y*20,100+x*20,50+y*20+9);break;/*下*/

case4:

line(100+x*20,50+y*20,100+x*20-9,50+y*20);break;/*左*/

}

}

voidScore()/*输出分数*/

{

chars[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);

}

voidDrawMap()/*画地图*/

{inti,j,k;

for(i=0;i<20;i++)

{

for(j=0;j<20;j++)

if(map[i][j]==1)

DrawTone(j,i);

elseif(map[i][j]==2)

DrawBrick(j,i);

elseif(map[i][j]==3)

DrawWater(j,i);

elseif(map[i][j]==5)

DrawHome(j,i);

elseif(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,表示不存在*/

}

elseif(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.1milo_zy");

settextstyle(0,0,2);/*首次输出得分*/

setcolor(9);

outtextxy(525,80,"Score");

setcolor(YELLOW);

outtextxy(550,115,"0");

}

voidfarinterruptNewInt9(void)

{

unsignedcharScanCode,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;

//作者:

c闲人2004-8-2000:

42 回复此发言

//--------------------------------------------------------------------------------

//3用c做的简单的坦克大战小游戏(给初学者点动力)

key_pressed[ScanCode]=1;

}

outportb(0x20,0x20);

}

voidInstallKeyboard(void)

{

inti;

for(i=0;i<128;i++)

key_state[i]=key_pressed[i]=0;

OldInt9Handler=getvect(9);/*中断向量值*/

setvect(9,NewInt9);/*中断程序NewInt9地址存入指定的中断向量表中INT09H*/

}

voidShutDownKeyboard(void)

{

setvect(9,OldInt9Handler);

}

intGetKey(intScanCode)

{

intres;

res=key_state[ScanCode]|key_pressed[ScanCode];

key_pressed[ScanCode]=0;

returnres;

}

voidGameOver()/*游戏失败*/

{

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,"GAMEOVER");

while

(1)

{

if(GetKey(KEY_ESC))

break;

}

}

voidGamePlay()/*玩游戏的过程*/

{

inti,j,lose=0;/*lose是1的时候表示失败*/

intt=0;

randomize();

while

(1)

{

for(i=0;i<5;i++)/*画敌人的子弹*/

{

if(amy[i].fire.direction>0)

putpixel(100+amy[i].fire.y*20,50+amy[i].fire.x*20,11);

}

for(i=0;i<=4;i++)/*画玩家子弹*/

{

if(Playone.fire[i].direction>0)/*大于0表示玩家子弹存在*/

putpixel(100+Playone.fire[i].y*20,50+Playone.fire[i].x*20,11);

}

TimeDelay(500000);/*关键的时间改动*/

for(i=0;i<5;i++)/*去敌人的子弹*/

{

if(amy[i].fire.direction>0)

putpixel(100+amy[i].fire.y*20,50+amy[i].fire.x*20,0);

}

for(i=0;i<=4;i++)/*去玩家除子弹*/

{

if(Playone.fire[i].direction>0)

putpixel(100+Playone.fire[i].y*20,50+Playone.fire[i].x*20,0);

}

for(i=0;i<=4;i++)/*玩家子弹位置的变化*/

{

if(Playone.fire[i].direction<0)

continue;

if(Playone.fire[i].direction==1)

{Playone.fire[i].x--;Playone.fire[i].y=Playone.fire[i].y;}

elseif(Playone.fire[i].direction==2)

{Playone.fire[i].y++;Playone.fire[i].y=Playone.fire[i].y;}

elseif(Playone.fire[i].direction==3)

{Playone.fire[i].x++;Playone.fire[i].y=Playone.fire[i].y;}

elseif(Playone.fire[i].direction==4)

{Playone.fire[i].y--;Playone.fire[i].y=Playone.fire[i].y;}

/*打到石头或者砖头的情况下减少子弹*/

if(map[Playone.fire[i].x][Playone.fire[i].y]==1)/*打到石头*/

Playone.fire[i].direction=-1;/*子弹消失*/

if(map[Playone.fire[i].x][Playone.fire[i].y]==2)/*打到砖头*/

{

Playone.fire[i].direction=-1;

DrawBlack(Playone.fire[i].y,Playone.fire[i].x);/*砖头打掉*/

map[Playone.fire[i].x][Playone.fire[i].y]=0;/*被打掉的地方变成可走的地方*/

}

if(map[Playone.fire[i].x][Playone.fire[i].y]==5)/*自己打到老家*/

{lose=1;break;}

for(j=0;j<5;j++)

{

if(amy[j].direction<0)/*不存在的不判断*/

continue;

if(amy[j].x==Playone.fire[i].x&&amy[j].y==Playone.fire[i].y)/*打中敌人*/

{

Playone.fire[i].direction=-1;

DrawBlack(Playone.fire[i].y,Playone.fire[i].x);

map[Playone.fire[i].x][Playone.fire[i].y]=0;/*被打掉的地方变成可走的地方*/

amy[j].fire.direction=amy[j].direction=-1;/*这个敌人消失*/

Score();/*输出得分*/

}

}

}

for(i=0;i<5;i++)/*敌人子弹位置的变化*/

//作者:

c闲人2004-8-2000:

42 回复此发言

//--------------------------------------------------------------------------------

//4用c做的简单的坦克大战小游戏(给初学者点动力)

{

if(amy[i].direction<0||amy[i].fire.direction<0)

continue;

if(amy[i].fire.direction==1)

{amy[i].fire.x--;amy[i].fire.y=amy[i].fire.y;}

elseif(amy[i].fire.direction==2)

{amy[i].fire.y++;amy[i].fire.x=amy[i].fire.x;}

elseif(amy[i].fire.direction==3)

{amy[i].fire.x++;amy[i].fire.y=amy[i].fire.y;}

elseif(amy[i].fire.direction==4)

{amy[i].fire.y--;amy[i].fire.x=amy[i].fire.x;}

/*打到石头或者砖头的情况下减少子弹*/

if(map[amy[i].fire.x][amy[i].fire.y]==1)/*打到石头*/

amy[i].fire.direction=-1;/*子弹消失*/

if(map[amy[i].fire.x][amy[i].fire.y]==2)/*打到砖头*/

{

amy[i].fire.direction=-1;

DrawBlack(amy[i].fire.y,amy[i].fire.x);/*砖头打掉*/

map[amy[i].fire.x][amy[i].fire.y]=0;/*被打掉的地方变成可走的地方*/

}

if(map[amy[i].fire.x][amy[i].fire.y]==5)/*敌人打到老家*/

{lose=1;break;}

if(amy[i].fire.x==Play

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 幼儿教育 > 幼儿读物

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1