简单短节的小游戏编程
# include & ltstdlib.h & gt
# include & ltdos.h & gt
# include & ltconio.h & gt
# include & ltbios.h & gt
#define KEY_ESC 0x01
#定义键空间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,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};
结构f
{
int x;
int y;
int方向;
};
结构播放
{
int x;/*线坐标*/
int y;/*列坐标*/
int方向;/*方向*/
建筑防火[5];/*五个项目符号*/
int分数;/*分数*/
} Playone
结构a
{
int x;
int y;
int颜色;
int方向;
int directiontwo/*用于判断道路是否往返*/
int fireplay/*是否攻击的变量,随机生成*/
建筑防火;
}艾米[5];/*敌人的结构,其实可以加上不同的颜色来代表不同种类的坦克*/
char key_state[128],key _ pressed[128];
void Init();/*图像驱动开始*/
void End();/*图像驱动结束*/
void draw map();/*画张地图*/
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玩法();/*游戏过程*/
void game over();/*游戏失败*/
void TimeDelay(无符号长微秒);/*延时功能传输到毫秒*/
int GetKey(int scan code);/*它以按键功能开始*/
void中断far(* old int 9 handler)();
void远中断new int 9();
void install keyboard();
void shut down keyboard();
无效总管(无效)
{
init();
draw map();
游戏性();
end();
}
Voidtime delay(无符号长微秒)/* Delay函数传送到毫秒*/
{
联盟规则r;
r . h . ah = 0x 86;
r . x . CX = microsec & gt;& gt16;
r . x . dx =微秒;
int 86(0x 15;r & amp;r);
}
Void Init()/*映像驱动器启动*/
{int gd=DETECT,GM;
init graph(& amp;gd,& ampgm," d:\ \ TC \ \ TC ");
clear device();
install keyboard();
}
Void End()/*图像驱动结束*/
{
shut down keyboard();
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,蓝色);
bar(100+x*20-9,50+y*20-9,100+x*20+9,50+y * 20+9);
}
Void DrawBrick(int x,int y)/* Draw brick */
{
setfillstyle(SOLID_FILL,6);
bar(100+x*20-9,50+y*20-9,100+x*20+9,50+y * 20+9);
set color(15);
线(100+x*20-9,50+y*20-4,100+x*20+9,50+y * 20-4);
线(100+x*20-9,50+y*20+4,100+x*20+9,50+y * 20+4);
线(100+x*20-4,50+y*20-9,65438+x * 20-4,50+y * 20+9);
线(100+x*20+4,50+y*20-9,100+x*20+4,50+y * 20+9);
}
Void DrawHome(int x,int y)/*绘制我的家乡*/
{
set color(0);
setfillstyle(SOLID_FILL,绿色);
filllellipse(100+x * 20,50+y*20,9,9);
}
Void DrawBlack(int x,int y)/*删除内容*/
{
set color(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)/* draw players */
{
set color(4);/*玩家是红色的*/
圆(100+x*20,50+y*20,7);
Switch(Playone.direction)/*判断玩家的方向*/
{
案例1:行(100+x*20,50+y*20,100+x*20,50+y * 20-9);打破;/*向上*/
情况二:线(100+x*20,50+y*20,100+x*20+9,50+y * 20);打破;/*右*/
案例三:线(100+x*20,50+y*20,100+x*20,50+y * 20+9);打破;/*向下*/
情况四:线(100+x*20,50+y*20,100+x*20-9,50+y * 20);打破;/*左*/
}
}
Void DrawAmy(int x,int y,int i)/*画出敌人*/
{
if(amy.color==12)
set color(12);
else if(amy.color==13)
set color(13);
Else/*这里是判断三种颜色的坦克*/
set color(14);
圆(100+x*20,50+y*20,7);
Switch(amy.direction)/*判断玩家的方向*/
{
案例1:行(100+x*20,50+y*20,100+x*20,50+y * 20-9);打破;/*向上*/
情况二:线(100+x*20,50+y*20,100+x*20+9,50+y * 20);打破;/*右*/
案例三:线(100+x*20,50+y*20,100+x*20,50+y * 20+9);打破;/*向下*/
情况四:线(100+x*20,50+y*20,100+x*20-9,50+y * 20);打破;/*左*/
}
}
Void Score()/*输出分数*/
{
char s[10];
playone . score+= 10;
sprintf(s," %d ",playone . score);
setfillstyle(SOLID_FILL,0);
吧(550,100,640,130);
settextstyle(0,0,2);
setcolor(黄色);
outtextxy(550,115,s);
}
Void DrawMap()/*绘制地图*/
{int i,j,k;
for(I = 0;我& lt20;i++)
{
for(j = 0;j & lt20;j++)
if(map[j]==1)
DrawTone(j,I);
else if(map[j]==2)
DrawBrick(j,I);
else if(map[j]==3)
DrawWater(j,I);
else if(map[j]==5)
DrawHome(j,I);
else if(map[j]==8)
{
playone . x = I;
playone . y = j;
playone . direction = 1;
DrawPlay(j,I);
for(k = 0;k & lt5;k++)
Playone.fire[k]。方向=-1;/*五颗子弹的方向都是-1,也就是说没有*/
}
else if(map[j]==9)
{
艾米[0]。x = 1;艾米[0]。y = 1;艾米[0]。方向=amy[0]。direction two = 3;/*第一个敌人*/
艾米[0]。color = 12;
DrawAmy(j,I,0);
}
}
for(I = 1;我& lt5;I++)/*敌人没有出现*/
Amy . direction = Amy . fire . direction =-1;
outtextxy(210,450," 2003.10.1 Milo _ zy ");
settextstyle(0,0,2);/*第一个输出分数*/
set color(9);
outtextxy(525,80,“分数”);
setcolor(黄色);
outtextxy(550,115," 0 ");
}
void远中断NewInt9(void)
{
无符号字符扫描码,temp
scan code = in portb(0x 60);
temp = inport b(0x 61);
outportb(0x61,temp | 0x 80);
outportb(0x61,temp & amp0x7f);
if(扫描码& amp0x80)
{
扫描代码和。= 0x7f
key _ state[scan code]= 0;
}
其他
{
key _ state[scan code]= 1;
key _ pressed[scan code]= 1;
}
outportb(0x20,0x 20);
}
void安装键盘(void)
{
int I;
for(I = 0;我& lt128;i++)
key _ state = key _ pressed = 0;
old int 9 handler = get vect(9);/*中断向量值*/
setvect(9,new int 9);/*中断程序NewInt9的地址存储在指定的中断向量表INT 09H*/
}
键盘(void)
{
setvect(9,old int 9 handler);
}
int GetKey(int ScanCode)
{
int res
RES = key _ state[ScanCode]| key _ pressed[ScanCode];
key _ pressed[ScanCode]= 0;
返回res
}
Void GameOver()/*游戏失败*/
{
set color(0);
setfillstyle(SOLID_FILL,0);/*摆脱你的家乡*/
filllellipse(100+9 * 20,50+18*20,9,9);
nosound();
setcolor(红色);
settextstyle(0,0,4);
outtextxy(150,5,“游戏结束”);
while(1)
{
if(GetKey(KEY_ESC))
打破;
}
}
Void GamePlay()/*玩游戏的过程*/
{
int i,j,lose = 0;/* Loss为1时表示失败*/
int t = 0;
randomize();
while(1)
{
for(I = 0;我& lt5;I++)/*画敌人的子弹*/
{
if(Amy . fire . direction & gt;0)
put pixel(100+Amy . fire . y * 20,50+amy.fire.x*20,11);
}
for(I = 0;我& lt=4;I++)/*绘制玩家项目符号*/
{
if(playone . fire . direction & gt;0)/*大于0表示玩家的子弹存在*/
put pixel(100+playone . fire . y * 20,50+Playone.fire.x*20,11);
}
时间延迟(500000);/*关键时间变化*/
for(I = 0;我& lt5;I++)/*给敌人的子弹*/
{
if(Amy . fire . direction & gt;0)
put pixel(100+Amy . fire . y * 20,50+amy.fire.x*20,0);
}
for(I = 0;我& lt=4;I++)/*删除玩家的项目符号*/
{
if(playone . fire . direction & gt;0)
put pixel(100+playone . fire . y * 20,50+Playone.fire.x*20,0);
}
for(I = 0;我& lt=4;I++)/*玩家子弹位置的变化*/
{
if(playone . fire . direction & lt;0)
继续;
if(playone . fire . direction = = 1)
{ playone . fire . x-;playone . fire . y = playone . fire . y;}
else if(playone . fire . direction = = 2)
{ playone . fire . y++;playone . fire . y = playone . fire . y;}
else if(playone . fire . direction = = 3)
{ playone . fire . x++;playone . fire . y = playone . fire . y;}
else if(playone . fire . direction = = 4)
{ playone . fire . y-;playone . fire . y = playone . fire . y;}
/*击中石头或砖块时减少子弹*/
if(map[playone . fire . x][playone . fire . y]= = 1)/*打石头*/
playone . fire . direction =-1;/*子弹消失*/
if(map[playone . fire . x][playone . fire . y]= = 2)/*撞砖*/
{
playone . fire . direction =-1;
DrawBlack(Playone.fire.y,playone . fire . x);/*砖块被敲掉*/
map[playone . fire . x][playone . fire . y]= 0;/*被击倒的地方变成了可行走的地方*/
}
if(map[playone . fire . x][playone . fire . y]= = 5)/*自己给家乡打电话*/
{ lose = 1;打破;}
for(j = 0;j & lt5;j++)
{
如果(艾米[j]。方向& lt0)/*不存在不判断*/
继续;
如果(艾米[j]。x = = playone . fire . x & amp;& amp艾米[j]。y==Playone.fire.y)/*打击敌人*/
{
playone . fire . direction =-1;
DrawBlack(Playone.fire.y,playone . fire . x);
map[playone . fire . x][playone . fire . y]= 0;/*被击倒的地方变成了可行走的地方*/
艾米[j].火.方向=艾米[j]。方向=-1;/*敌人消失了*/
score();/*输出分数*/
}
}
}
for(I = 0;我& lt5;I++)/*敌人子弹位置的变化*/
{
if(Amy . direction & lt;0 | | Amy . fire . direction & lt;0)
继续;
if(amy.fire.direction==1)
{ Amy . fire . x-;Amy . fire . y =艾米. fire . y;}
else if(amy.fire.direction==2)
{ Amy . fire . y++;Amy . fire . x =艾米. fire . x;}
else if(amy.fire.direction==3)
{ Amy . fire . x++;Amy . fire . y =艾米. fire . y;}
else if(amy.fire.direction==4)
{ Amy . fire . y-;Amy . fire . x =艾米. fire . x;}
/*击中石头或砖块时减少子弹*/
if(map[Amy . fire . x][Amy . fire . y]= = 1)/*撞石*/
Amy . fire . direction =-1;/*子弹消失*/
if(map[Amy . fire . x][Amy . fire . y]= = 2)/*撞砖*/
{
Amy . fire . direction =-1;
DrawBlack(amy.fire.y,Amy . fire . x);/*砖块被敲掉*/
map[Amy . fire . x][Amy . fire . y]= 0;/*被击倒的地方变成了可行走的地方*/
}
如果(图【艾米。fire . x][艾米。fire.y] = = 5)/*敌人击中要害*/
{ lose = 1;打破;}
if(Amy . fire . x = = playone . x & amp;& ampAmy.fire.y==Playone.y)/*打球员*/
{
for(j = 0;j & lt5;j++)
Playone.fire[j]。方向=-1;/*玩家的子弹全部消失*/
Amy . fire . direction =-1;
DrawBlack(amy.fire.y,Amy . fire . x);
map[Amy . fire . x][Amy . fire . y]= 0;/*被击倒的地方变成了可行走的地方*/
lose = 1;打破;/*好人被击倒后失败*/
}
}
nosound();
for(I = 0;我& lt5;I++)/*随机改变敌方方向*/
{
if(Amy . direction & lt;0)
继续;
while(1)
{
Amy . direction two = random(4)+1;
if(Amy . direction = = 1 & amp;& amp艾米.方向二==3)
继续;
if(Amy . direction = = 3 & amp;& ampamy.directiontwo==1)
继续;
if(Amy . direction = = 2 & amp;& amp艾米.方向二==4)
继续;
if(Amy . direction = = 4 & amp;& ampAmy.directiontwo==2)/*这里的四个if是判断是否有往返*/
继续;
if(Amy . direction two = = 3 & amp;& amp(map[Amy . x+1][Amy . y]= = 3 | | map[Amy . x+1][Amy . y]= = 1 | | map[Amy . x+1][Amy . y]= = 2))
继续;
if(Amy . direction two = = 1 & amp;& amp(map[Amy . x-1][Amy . y]= = 3 | | map[Amy . x-1][Amy . y]= = 1 | | map[Amy . x-1][Amy . y]= = 2))
继续;
if(Amy . direction two = = 2 & amp;& amp(map[Amy . x][Amy . y+1]= = 3 | | map[Amy . x][Amy . y+1]= = 1 | | map[Amy . x][Amy . y+1]= = 2))
继续;
if(Amy . direction two = = 4 & amp;& amp(map[Amy . x][Amy . y-1]= = 3 | | map[Amy . x][Amy . y-1]= = 1 | | map[Amy . x][Amy . y-1]= = 2))
继续;/*以上四个是撞墙还是水什么的*/
DrawBlack(amy.y,Amy . x);/*抹掉原来的地方*/
amy.direction =艾米. amy.directiontwo
if(amy.direction==1)
{ Amy . x-;Amy . y = Amy . y;}
if(amy.direction==3)
{ Amy . x++;Amy . y = Amy . y;}
if(amy.direction==2)
{ Amy . y++;艾米. x =艾米. x;}
if(amy.direction==4)
{ Amy . y-;艾米. x =艾米. x;}
if(Amy . x = = playone . x & amp;& ampAmy.y==Playone.y)/*碰撞*/
lose = 1;
If(map[amy.x][amy.y]==5)/*敌人打到自己的家乡*/
lose = 1;
DrawAmy(amy.y,amy.x,I);/*吸引敌人*/
if(Amy . fire . direction & lt;0)
Amy . fireplay = random(4);
if(Amy . fireplay = = 1 & amp;& amp艾米.火.方向& lt0)/*启动*/
{
Amy . fire . direction =艾米. amy.direction/*子弹与敌人同向*/
Amy . fire . x = Amy . x;
Amy . fire . y = Amy . y;
}
打破;
}
}
If(lose)/*因失败跳出循环,显示失败*/
{ game over();打破;}
if(GetKey(KEY_ESC))
打破;
If(GetKey(KEY_UP))/* Up */
{
if(playone . direction = = 1 & amp;& ampmap[playone . x-1][playone . y]!=1。& ampmap[playone . x-1][playone . y]!=2)
{
if(map[playone . x-1][playone . y]= = 3)
继续;
DrawBlack(Playone.y,playone . x);/*如果正在移动,只要方向与按下的方向一致*/
playone . x-;
playone . direction = 1;
DrawPlay(Playone.y,playone . x);
}
否则/*只调整枪头的方向*/
{
DrawBlack(Playone.y,playone . x);
playone . direction = 1;
DrawPlay(Playone.y,playone . x);
}
}
else if(GetKey(KEY _ DOWN))/* DOWN */
{
if(playone . direction = = 3 & amp;& ampmap[playone . x+1][playone . y]!=1。& ampmap[playone . x+1][playone . y]!=2)
{
if(map[playone . x+1][playone . y]= = 3)
继续;
DrawBlack(Playone.y,playone . x);
playone . x++;
playone . direction = 3;
DrawPlay(Playone.y,playone . x);
}
其他
{
DrawBlack(Playone.y,playone . x);
playone . direction = 3;
DrawPlay(Playone.y,playone . x);
}
}
If(GetKey(KEY_RIGHT))/*向右*/
{
if(playone . direction = = 2 & amp;& ampmap[playone . x][playone . y+1]!=1。& ampmap[playone . x][playone . y+1]!=2)
{
if(map[playone . x][playone . y+1]= = 3)
继续;
DrawBlack(Playone.y,playone . x);
playone . y++;
playone . direction = 2;
DrawPlay(Playone.y,playone . x);
}
其他
{
DrawBlack(Playone.y,playone . x);
playone . direction = 2;
DrawPlay(Playone.y,playone . x);
}
}
If(GetKey(KEY_LEFT))/*向左*/
{
if(playone . direction = = 4 & amp;& ampmap[playone . x][playone . y-1]!=1。& ampmap[playone . x][playone . y-1]!=2)
{
if(map[playone . x][playone . y-1]= = 3)
继续;
DrawBlack(Playone.y,playone . x);
playone . y-;
playone . direction = 4;
DrawPlay(Playone.y,playone . x);
}
其他
{
DrawBlack(Playone.y,playone . x);
playone . direction = 4;
DrawPlay(Playone.y,playone . x);
}
}
If(GetKey(KEY_SPACE))/*发射子弹*/
{
for(I = 0;我& lt5;I++)/*使用循环找出是否有项目符号可以使用*/
if(playone . fire . direction & lt;0)
{
声音(300);
playone . fire . direction = playone . direction;/*子弹和坦克方向一致*/
playone . fire . x = playone . x;
playon e . fire . y = playon e . y;
打破;/*找到后跳出循环*/
}
}
if(map[playone . x][playone . y]= = 5)/*玩家偶遇家乡*/
lose = 1;
for(I = 0;我& lt5;I++)/*判断是否因为自己的控制打中了敌人*/
{
if(Amy . direction & lt;0)
继续;
if(Amy . x = = playone . x & amp;& ampAmy.y==Playone.y)/*碰撞*/
lose = 1;
}
If(lose)/*因失败跳出循环,显示失败*/
{ game over();打破;}
t++;/*加入某个程序,新的敌人就会出现*/
If(t==30) /*是时候增加敌人了*/
{ t = 0;
for(I = 0;我& lt5;i++)
if(Amy . direction & lt;0)
{
Amy . direction = Amy . direction two = 3;
Amy . x = 1;
Amy . y = random(3);/*随机方向*/
if(amy.y==0)
Amy . y = 1;
else if(amy.y==1)
Amy . y = 9;
Else/*这里和上面的两个判断是判断敌人的初始位置*/
Amy . y = 18;
Amy . color = random(3)+12;/*颜色随机*/
DrawAmy(amy.y,amy.x,I);
打破;/*找到一个后,循环出去*/
}
}
}
}