论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > C语言程序设计教程
Tag:新手,函数,指针,数据类型,对象,Turbo,入门,运算符,数组,结构,二级,,tc,游戏,试题,问答,编译,视频教程

C语言教程:射气球

文章类别:C语言程序设计 | 发表日期:2010-11-1 9:26:00

射气球

# include "graphics.h"
# include "stdlib.h"

# define ESC 0x11b

struct ball
{
 int color;
 int xx;
 int yy;
}balls[5]; /*定义气球*/

struct gun
{int flag;
 int color;
 int xx;
 int yy;
 int ff;
}guns[5]; /*定义子弹*/

char des[10]={104,0,101,0,108,0,108,0,111,0}; /*初始气球上的字母*/
int flag[5]={1,1,1,1,1};
long gun_a=0,gun_b=0; /*统计子弹数目*/
int depth[5]={5,5,5,5,5}; /*标记*/
void *buffer_ball[5],*buffer_gun[5]; /*定义图形存储指针*/
unsigned size;
char c[32]={0x08,0x00,0x0C,0x00,
 0x1B,0xFE,0x10,0x08,
 0x30,0x08,0x27,0xC8,
 0x64,0x48,0xA4,0x48, /*何__字模*/ 
 0x24,0x48,0x27,0xC8,
 0x24,0x48,0x20,0x08,
 0x20,0x08,0x20,0x28,
 0x20,0x10,0x00,0x00
 };
char y[32]={0x00,0x00,0x7C,0x3C,
 0x45,0xE0,0x44,0x20,
 0x44,0x20,0x7C,0x20,
 0x13,0xFE,0x10,0x20, /*跃__字模*/
 0x5C,0x50,0x50,0x50,
 0x50,0x88,0x50,0x88,
 0x5D,0x04,0xE1,0x06,
 0x02,0x04,0x04,0x00 
 }; 

void Init() /*初始函数*/
{
int gdriver=DETECT,gmode,ii;
registerbgidriver(EGAVGA_driver);
initgraph(&gdriver,&gmode,"");
randomize();
for(ii=0;ii<5;ii++)
{
 balls[ii].color=random(6)+1;
 balls[ii].xx=84+90*ii;
 balls[ii].yy=48;
 guns[ii].ff=0;
}
}

void Draw_name(char *c,int x,int y) /*汉字输出*/
{
char mask[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
int i,j,k;
for(k=0;k<16;k++)
{
for(i=0;i<2;i++)
for(j=0;j<8;j++)
  {if((c[k*2+i]&mask[j])!=0) putpixel(x,y,YELLOW);
   x++;
  }
y++;
x=590;
}
}

void Draw_m() /*画界面*/
{
int ii,jj;
setfillstyle(SOLID_FILL,11);
for(ii=0;ii<32;ii++)
{bar(25+15*ii,396,38+15*ii,409);
 bar(25+15*ii,20,38+15*ii,33);
}
for(ii=0;ii<24;ii++)
{bar(25,35+15*ii,38,48+15*ii);
 bar(490,35+15*ii,503,48+15*ii);
}
for(jj=0;jj<5;jj++)
{setfillstyle(SOLID_FILL,jj+5);
 for(ii=0;ii<15;ii++)
 bar(40+30*ii,320+15*jj,68+30*ii,333+15*jj);
}
settextstyle(0,0,1);
Draw_name(c,590,300);
Draw_name(y,590,330);
setcolor(RED);
outtextxy(530,400,"2005_12_15");
setcolor(14);
outtextxy(20,440,"Press 'ENTER' to start the game,then you can press 'ESC' to quit it");
settextstyle(0,0,4);
setcolor(12);
outtextxy(530,50,"K");
outtextxy(530,90,"E");
outtextxy(530,130,"Y");
setcolor(GREEN);
outtextxy(540,170,"B");
outtextxy(540,210,"O");
outtextxy(540,250,"A");
outtextxy(540,290,"R");
outtextxy(540,330,"D");
}

Draw_ball() /*画气球*/
{int ii;
settextstyle(0,0,2);
for(ii=0;ii<5;ii++)
{setcolor(balls[ii].color);
 circle(balls[ii].xx,balls[ii].yy,12);
 setfillstyle(SOLID_FILL,balls[ii].color);
 floodfill(balls[ii].xx,balls[ii].yy,balls[ii].color);
 setcolor(YELLOW);
 outtextxy(balls[ii].xx-7,balls[ii].yy-7,&des[ii*2]);
}
size=imagesize(balls[0].xx-12,balls[0].yy-12,balls[0].xx+12,balls[0].yy+12);
for(ii=0;ii<5;ii++)
{buffer_ball[ii]=malloc(size);
 getimage(balls[ii].xx-12,balls[ii].yy-12,balls[ii].xx+12,balls[ii].yy+12,buffer_ball[ii]);
}
}

void Draw_gun(int ii) /*画子弹*/
{
setcolor(guns[ii].color);
setlinestyle(0,0,3);
line(guns[ii].xx,guns[ii].yy,guns[ii].xx,guns[ii].yy+20);
line(guns[ii].xx,guns[ii].yy,guns[ii].xx-10,guns[ii].yy+5);
line(guns[ii].xx,guns[ii].yy,guns[ii].xx+10,guns[ii].yy+5);
line(guns[ii].xx,guns[ii].yy+10,guns[ii].xx-7,guns[ii].yy+15);
line(guns[ii].xx,guns[ii].yy+15,guns[ii].xx-7,guns[ii].yy+19);
line(guns[ii].xx,guns[ii].yy+10,guns[ii].xx+7,guns[ii].yy+15);
line(guns[ii].xx,guns[ii].yy+15,guns[ii].xx+7,guns[ii].yy+19);
}

void ser(int ii) /*当按键有效时,发射子弹*/
{flag[ii]=0;
 guns[ii].flag=1;
 guns[ii].color=random(11)+2;
 guns[ii].xx=84+90*ii;
 guns[ii].yy=298+15*(5-depth[ii]);
 if(guns[ii].ff==0) {guns[ii].ff=1;
 Draw_gun(ii);
 size=imagesize(guns[ii].xx-10,guns[ii].yy,guns[ii].xx+10,guns[ii].yy+20);
 buffer_gun[ii]=malloc(size);
 getimage(guns[ii].xx-10,guns[ii].yy,guns[ii].xx+10,guns[ii].yy+20,buffer_gun[ii]);
 }
 else {Draw_gun(ii);
 getimage(guns[ii].xx-10,guns[ii].yy,guns[ii].xx+10,guns[ii].yy+20,buffer_gun[ii]);
 }
}

void Draw_bal(int ii) /*当一个气球被击中后,重画气球*/
{
setcolor(balls[ii].color);
setlinestyle(0,0,1);
circle(balls[ii].xx,balls[ii].yy,12);
setfillstyle(SOLID_FILL,balls[ii].color);
floodfill(balls[ii].xx,balls[ii].yy,balls[ii].color);
setcolor(YELLOW);
outtextxy(balls[ii].xx-7,balls[ii].yy-7,(char *)&des[ii*2]);
getimage(balls[ii].xx-12,balls[ii].yy-12,balls[ii].xx+12,balls[ii].yy+12,buffer_ball[ii]);
}

void Update(int ii) /*有气球被击中后,一些数据更新*/ 
{
sound(1500);
delay(1000);
nosound();
flag[ii]=1;
gun_b++;
guns[ii].flag=0;
putimage(guns[ii].xx-10,guns[ii].yy,buffer_gun[ii],XOR_PUT);
setfillstyle(SOLID_FILL,0);
bar(balls[ii].xx-12,balls[ii].yy-12,balls[ii].xx+12,balls[ii].yy+12);
des[ii*2]=random(26)+97;
balls[ii].yy=48;
balls[ii].color=random(5)+1;
Draw_bal(ii);
}

void Play() /*游戏控制函数*/
{
 int key,ii,jj=0,dis;
 for(;;)
 {jj++;
 if(jj==6000)
{jj=0; /*让气球动起来*/
 for(ii=0;ii<5;ii++)
{putimage(balls[ii].xx-12,balls[ii].yy-12,buffer_ball[ii],XOR_PUT);
 balls[ii].yy+=1;
 if(balls[ii].yy>308+15*(5-depth[ii])) {setfillstyle(SOLID_FILL,0);
 bar(balls[ii].xx-15,balls[ii].yy+11,balls[ii].xx+15,balls[ii].yy+25);
 bar(balls[ii].xx-12,balls[ii].yy-12,balls[ii].xx+12,balls[ii].yy+12);
 depth[ii]--;
 if(depth[ii]==-1) {settextstyle(DEFAULT_FONT,0,3);
 setcolor(13);
 outtextxy(100,110,"GAME OVER!");
 printf("\n\n\n\n\n\n\n\n\n\n ");
 printf("Ji jian shu :%ld ji zhong :%ld ",gun_a,gun_b);
 getch();
 closegraph();
 exit(0);}
 des[ii*2]=random(26)+97;
 balls[ii].yy=48;
 balls[ii].color=random(5)+1;
 Draw_bal(ii);
}
 else putimage(balls[ii].xx-12,balls[ii].yy-12,buffer_ball[ii],COPY_PUT);
 if(guns[ii].flag==1) {putimage(guns[ii].xx-10,guns[ii].yy,buffer_gun[ii],XOR_PUT);
 guns[ii].yy-=4;
 putimage(guns[ii].xx-10,guns[ii].yy,buffer_gun[ii],COPY_PUT);
 dis=guns[ii].yy-balls[ii].yy;
 if(dis<8) Update(ii); /*让子弹动起来*/ 
 }
}

}
 if(bioskey(1)) 
{key=bioskey(0); /*读键盘扫描码*/
 if(key==ESC) break; 
 gun_a++;
 for(ii=0;ii<5;ii++)
 if(flag[ii]!=0&&key<<8>>8==des[ii*2]&&balls[ii].yy<308+15*(5-depth[ii])) /*按键有效*/
 { ser(ii);
 break;
 }
}
 }
}

main()
{
Init();
Draw_m();
Draw_ball();
while(getch()!=13); /*按ENTER继续*/
Play();
settextstyle(DEFAULT_FONT,0,3); /*结束处理*/
setcolor(13);
outtextxy(100,110,"BYE!");
printf("\n\n\n\n\n\n\n\n\n\n ");
printf("Ji jian shu :%ld ji zhong :%ld ",gun_a,gun_b);
getch();
closegraph();
}

 

cool !

视频教程列表
文章教程搜索
 
C语言程序设计推荐教程
C语言程序设计热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058