论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: 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语言程序设计 | 发表日期:2008-9-24 14:43:50

这个方块写得很简单,我只保留了一部分功能,但能运行,

初学编程,不足之处请原谅,

不明白之处可以发邮件给我。

#include<graphics.h>
#include<stdio.h>
#include<dos.h>
#include<bios.h>
#define TIMEINT 5
#define X x-10
#define d 20
#define up 72
#define left 75
#define right 77
#define enter 2
#define esc 1
int x,y,boxshape,boxdir,start=1;//方块横纵坐标,方块外形,方块方向
int board[22][12];//桌面
int fq[7][4][9]={
          {{-2,0,-1,0,0,0,0,1,1},
           {0,-1,0,0,-1,1,0,1,2},
           {-2,0,-2,1,-1,1,0,1,3},
           {-1,-1,-1,0,-1,1,0,-1,0}},
          {{-1,-1,-1,0,0,0,0,1,1},
           {-1,0,-2,1,-1,1,0,0,2},
           {-1,-1,-1,0,0,0,0,1,3},
           {-1,0,-2,1,-1,1,0,0,0}},
   {{-2,0,-2,1,-1,0,0,0,1},
           {-1,-1,0,-1,0,0,0,1,2},
           {0,0,-2,1,-1,1,0,1,3},
           {-1,-1,-1,0,-1,1,0,1,0}},
   {{0,-1,-1,0,0,0,0,1,1},
           {-1,0,-2,1,-1,1,0,1,2},
           {-1,-1,-1,0,-1,1,0,0,3},
           {-2,0,-1,0,-1,1,0,0,0}},
          {{-1,0,-1,1,0,0,0,1,1},
           {-1,0,-1,1,0,0,0,1,1},
           {-1,0,-1,1,0,0,0,1,1},
           {-1,0,-1,1,0,0,0,1,1}},
          {{-3,0,-2,0,-1,0,0,0,1},
           {-1,-1,-1,0,-1,1,-1,2,2},
           {-3,0,-2,0,-1,0,0,0,1},
           {-1,-1,-1,0,-1,1,-1,2,2}},
          {{0,-1,-1,0,-1,1,0,0},
           {-2,0,-1,0,-1,1,0,1},
           {0,-1,-1,0,-1,1,0,0},
    {-2,0,-1,0,-1,1,0,1}},};

int collison();
void init();
void drawbox();
void clearoldbox();
int timeCome();
int get_key();
main()
{
int gdrive=VGA,gmode=VGAHI,errorcode;
int i,j,flag,k,m,key;
initgraph(&gdrive,&gmode,"d:\\tc");
errorcode=graphresult();
if(errorcode!=0)
{printf("the graph error:%s",grapherrormsg(errorcode));
printf("press any key to halt");
getch();
exit(1);
}
for(i=0;i<22;i++)
board[i][0]=board[i][11]=1;
for(j=0;j<12;j++)
board[21][j]=1;
for(i=1;i<=20;i++)
for(j=1;j<=10;j++)
board[i][j]=0;
init();
while(1)
{
  if(start==1){x=15;y=4;boxshape=rand()%7;boxdir=0;drawbox();start=0;}
    if(bioskey(1)!=0)
       {key=get_key();
        switch(key)
        {case left: clearoldbox();
                   x=x-1;
     if(collison())x=x+1;
                   else x=x;
                   drawbox();
                   break;
         case right: clearoldbox();
                     x=x+1;
                    if(collison())x=x-1;
                    else x=x;
                    drawbox();
                    break;
         case up:  clearoldbox();
                   boxdir=(boxdir+1)%4;
                   if(collison())boxdir=(boxdir+3)%4;
                   else boxdir=boxdir;
                   drawbox();
                   break;
          case esc:exit(0);
          default :break;
 }
 }
 if(timeCome())
           {clearoldbox();
            y=y+1;
            if(collison())
     {if(y>4)y=y-1;
      else {getch();exit(0);}
      board[y+fq[boxshape][boxdir][0]-1][x+fq[boxshape][boxdir][1]-9]=1;
      board[y+fq[boxshape][boxdir][2]-1][x+fq[boxshape][boxdir][3]-9]=1;
      board[y+fq[boxshape][boxdir][4]-1][x+fq[boxshape][boxdir][5]-9]=1;
      board[y+fq[boxshape][boxdir][6]-1][x+fq[boxshape][boxdir][7]-9]=1;
      drawbox();
             for(i=20;i>4;i--)
       { flag=1;
               for(j=1;j<11;j++)
                 {flag=flag&&board[i][j];
                  /*if(board[i][j]==0)flag=1;else flag=flag;*/}
    if(flag)
                   {
       for(k=i;k>4;k--)
       for(m=1;m<=10;m++)
                     board[k][m]=board[k-1][m];
                     init();
                    }
                }
                 start=1;
                }
                else
                drawbox();
        }
}
}
int collison()
{
 /*if(board[y+fq[boxshape][boxdir][0]-1][x+fq[boxshape][boxdir][1]-9]==1)return 1;
 if(board[y+fq[boxshape][boxdir][2]-1][x+fq[boxshape][boxdir][3]-9]==1)return 1;
 if(board[y+fq[boxshape][boxdir][4]-1][x+fq[boxshape][boxdir][5]-9]==1)return 1;
 if(board[y+fq[boxshape][boxdir][6]-1][x+fq[boxshape][boxdir][7]-9]==1)return 1;
 return 0;  */
 return(board[y+fq[boxshape][boxdir][2]-1][x+fq[boxshape][boxdir][3]-9]||
 board[y+fq[boxshape][boxdir][4]-1][x+fq[boxshape][boxdir][5]-9]||board[y+fq[boxshape][boxdir][6]-1][x+fq[boxshape][boxdir][7]-9]||
 board[y+fq[boxshape][boxdir][0]-1][x+fq[boxshape][boxdir][1]-9]);

}
void init()
{
 int i,j;
 cleardevice();
 setbkcolor(GREEN);
 setcolor(RED);
 rectangle(199,39,401,441);
 setfillstyle(1,15);
 for(i=20;i>4;i--)
  for(j=1;j<11;j++)
   if(board[i][j])bar((10+j-1)*d+1,(i+1)*d+1,(10+j)*d-1,(i+2)*d-1);
}
void drawbox()
{setfillstyle(1,15);
  bar(
      (x+fq[boxshape][boxdir][1])*d+1,
      (y+fq[boxshape][boxdir][0])*d+1,
      (x+fq[boxshape][boxdir][1]+1)*d-1,
      (y+fq[boxshape][boxdir][0]+1)*d-1);
      bar(
      (x+fq[boxshape][boxdir][3])*d+1,
      (y+fq[boxshape][boxdir][2])*d+1,
      (x+fq[boxshape][boxdir][3]+1)*d-1,
      (y+fq[boxshape][boxdir][2]+1)*d-1);
      bar(
      (x+fq[boxshape][boxdir][5])*d+1,
      (y+fq[boxshape][boxdir][4])*d+1,
      (x+fq[boxshape][boxdir][5]+1)*d-1,
      (y+fq[boxshape][boxdir][4]+1)*d-1);
      bar(
      (x+fq[boxshape][boxdir][7])*d+1,
      (y+fq[boxshape][boxdir][6])*d+1,
      (x+fq[boxshape][boxdir][7]+1)*d-1,
      (y+fq[boxshape][boxdir][6]+1)*d-1);
}
void clearoldbox()
{setfillstyle(1,GREEN);
  bar(
      (x+fq[boxshape][boxdir][1])*d,
      (y+fq[boxshape][boxdir][0])*d,
      (x+fq[boxshape][boxdir][1]+1)*d,
      (y+fq[boxshape][boxdir][0]+1)*d);
      bar(
      (x+fq[boxshape][boxdir][3])*d,
      (y+fq[boxshape][boxdir][2])*d,
      (x+fq[boxshape][boxdir][3]+1)*d,
      (y+fq[boxshape][boxdir][2]+1)*d);
      bar(
      (x+fq[boxshape][boxdir][5])*d,
      (y+fq[boxshape][boxdir][4])*d,
      (x+fq[boxshape][boxdir][5]+1)*d,
      (y+fq[boxshape][boxdir][4]+1)*d);
      bar(
      (x+fq[boxshape][boxdir][7])*d,
      (y+fq[boxshape][boxdir][6])*d,
      (x+fq[boxshape][boxdir][7]+1)*d,
      (y+fq[boxshape][boxdir][6]+1)*d);
}

int timeCome()
{
static long tm,old;
 tm=biostime(0,tm);
 if(tm-old<TIMEINT) return 0;
 else
 {
  old=tm; return 1;
 }
}
int get_key()
{union REGS rg;
 rg.h.ah=0;
 int86(0x16,&rg,&rg);
 return rg.h.ah;
}

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