正文

打砖块2005-02-09 20:29:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/book/198.html

分享到:

/* 打砖块,TC2.0  by kingwei */

/*
    以下是数据信息,保存成文件brick0.txt
36
40 40 1
60 40 1
80 40 1
100 40 1
120 40 1
140 40 1
20 50  2
40 50  2
60 50  2
80 50  2
100 50 2
120 50 2
140 50 2
160 50 2
20 60  1
40 60  1
60 60  1
80 60  1
100 60 1
120 60 1
140 60 1
160 60 1
20 70  3
40 70  3
60 70  3
80 70  3
100 70 3
120 70 3
140 70 3
160 70 3
40 80  1
60 80  1
80 80  1
100 80 1
120 80 1
140 80 1
*/

#include <graphics.h>
#include <bios.h>
#include <stdio.h>

#define MAX 100
#define BLOCKL 10
#define BOARDL 100
#define DELAY 600
#define BOARDSPEED 10
#define MAXSPEED 3
#define R 1
#define TOP 0
#define BOTTOM 200
#define LEFT 0
#define RIGHT 200

int block[MAX][3]={0},bnum,countnum;
int x0=LEFT+1,y0=BOTTOM-1,vx=1,vy=-1;
int boardx=LEFT,boardv=0;

void LoadData()
{
   int i;
   FILE *fp;
   if((fp=fopen("brick0.txt","r"))==0)
   {
      printf("File error!");getch();
      closegraph();exit(1);
   }
   fscanf(fp,"%d",&bnum);
   countnum=bnum;
   for(i=0;i<bnum;i++)
   {
      fscanf(fp,"%d %d %d",&block[i][0],&block[i][1],&block[i][2]);
   }
   fclose(fp);
}
void DrawMap()
{
   bar(LEFT,TOP,LEFT,BOTTOM);
   bar(RIGHT,TOP,RIGHT,BOTTOM);
   bar(LEFT,TOP,RIGHT,TOP);
}
void DrawOneBlock(int i)
{
   setfillstyle(1,block[i][2]);
   bar(block[i][0],block[i][1],block[i][0]+BLOCKL*2,block[i][1]+BLOCKL);
   setfillstyle(0,0);
}
void DrawBlock()
{
   int i;
   for(i=0;i<bnum;i++)
      DrawOneBlock(i);
}
void ClearBlock(int i)
{
   setfillstyle(0,0);
   bar(block[i][0],block[i][1],block[i][0]+BLOCKL*2,block[i][1]+BLOCKL);
   setfillstyle(1,15);
}
void TouchWall(int x,int y)
{
   if(x<=LEFT||x>=RIGHT)
      vx=-vx;
   else if(y<=TOP)
      vy=-vy;
}
void TouchBoard(int x,int y)
{
   if(x>=boardx&&x<=boardx+BOARDL&&y>=BOTTOM)
   {
      vy=-vy;
      vx+=boardv/2;
   }
   else if(y>=BOTTOM)
   {
      printf("Fail!");
      getch();closegraph();exit(0);
   }
   if(vx>MAXSPEED)  vx=MAXSPEED;
   else if(vx<-MAXSPEED)  vx=-MAXSPEED;
}
void TouchBlock(int x,int y)
{
   int i;
   for(i=0;i<bnum;i++)
      if((block[i][0]!=-200&&block[i][1]!=-200)
     &&x>=block[i][0]&&x<=block[i][0]+BLOCKL*2
     &&y>=block[i][1]&&y<=block[i][1]+BLOCKL)
      {
     if(y0<=block[i][1]||y0>=block[i][1]+BLOCKL)
        vy=-vy;
     else
        vx=-vx;
     block[i][2]--;
     DrawOneBlock(i);
     if(!block[i][2])
     {
        ClearBlock(i);
        block[i][0]=block[i][1]=-200;
        countnum--;
     }
     return;
      }
}
void MoveBoard()
{
   if(bioskey(1))
   {
      switch(bioskey(0))
      {
      case 19200:boardv=-BOARDSPEED;break;
      case 19712:boardv=BOARDSPEED;break;
      case 283:closegraph();exit(0);
      }
   }
   else if(boardv>0) boardv--;
   else if(boardv<0) boardv++;
   boardx+=boardv;
   setfillstyle(0,0);
   bar(boardx-boardv,BOTTOM,boardx-boardv+BOARDL,BOTTOM+3);
   setfillstyle(1,15);
   bar(boardx,BOTTOM,boardx+BOARDL,BOTTOM+1);
}
void MoveBall()
{
   for(;countnum;)
   {
      setfillstyle(0,0);
      bar(x0-R,y0-R,x0+R,y0+R);
      MoveBoard();
      TouchBoard(x0+vx,y0+vy);
      TouchWall(x0+vx,y0+vy);
      TouchBlock(x0+vx,y0+vy);
      x0+=vx;y0+=vy;
      setfillstyle(1,15);
      DrawMap();
      circle(x0,y0,R);
      delay(DELAY);
   }
   printf("Congratulations!");
   getch();
}
void main()
{
   int gdriver, gmode;
   gdriver=VGA;
   gmode=VGAHI;
   initgraph(&gdriver, &gmode, "E:\\TURBOC2");
   LoadData();
   DrawBlock();
   DrawMap();
   MoveBall();
   closegraph();
}

阅读(3167) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册