现在的位置: 首页 > 综合 > 正文

C-俄罗斯方块 TURBOC 2.0

2013年10月11日 ⁄ 综合 ⁄ 共 5116字 ⁄ 字号 评论关闭

#include<stdio.h>
#include<bios.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define SPACE 0x3920
#define ESC 0x11b
int TIMELIMIT;
int movekey,INDEX,wherex,wherey,disy=0;
int BOX[25][12];
struct shape
{
 int xy[8],next;
}type[19]={{0,0,0,1,0,2,1,0,1},{0,0,0,1,1,1,2,1,2},
          {0,2,1,0,1,1,1,2,3},{0,0,1,0,2,0,2,1,0},
          {0,0,0,1,0,2,1,1,5},{0,1,1,0,1,1,2,1,6},
          {0,1,1,0,1,1,1,2,7},{0,0,1,0,1,1,2,0,4},
          {0,0,1,0,1,1,1,2,9},{0,0,0,1,1,0,2,0,10},
          {0,0,0,1,0,2,1,2,11},{0,1,1,1,2,0,2,1,8},
          {0,1,0,2,1,0,1,1,13},{0,0,1,0,1,1,2,1,12},
          {0,0,0,1,1,1,1,2,15},{0,1,1,0,1,1,2,0,14},
          {0,0,0,1,1,0,1,1,16},{0,0,0,1,0,2,0,3,18},
          {0,0,1,0,2,0,3,0,17}};
void draw1(int x,int y,int color)
 {

  setfillstyle(1,color);
  bar((y+15)*16+1,(x+3)*16+1,(y+16)*16-1,(x+4)*16-1);

 }

void initbox()
 {
   int i,j,k;
   for(i=0,j=0,k=24;j<11;j++){BOX[i][j]=1;draw1(i,j,YELLOW);
         BOX[k][j]=1;draw1(k,j,YELLOW);}
   for(j=0,i=0,k=11;i<=24;i++){BOX[i][j]=1;draw1(i,j,YELLOW);
          BOX[i][k]=1;draw1(i,k,YELLOW);}
   for(i=1;i<=23;i++)
     for(j=1;j<=10;j++)
      BOX[i][j]=0;
  draw1(4,0,RED);draw1(4,11,RED);
 }

void typedraw(int color)
 {
  int x,y,i;
  for(i=0;i<=6;i+=2)
    {
     x=wherex-type[INDEX].xy[i+1];
     y=wherey+type[INDEX].xy[i];
     draw1(x,y,color);
    }
 }

int creattype(void)
 {
  randomize();
  return rand()%19;
 }

int judgechange(void)
{
 int result=1,i,j;
 switch(movekey)
  {
   case LEFT:for(i=0;i<=6;i+=2)
   if(BOX[wherex-type[INDEX].xy[i+1]][wherey+type[INDEX].xy[i]-1]==1)
              {result=0;break;}break;
   case RIGHT: for(i=0;i<=6;i+=2)
   if(BOX[wherex-type[INDEX].xy[i+1]][wherey+type[INDEX].xy[i]+1]==1)
              {result=0;break;}break;
   case DOWN: result=!judgestop();break;
   case UP: result=0;
     j=type[INDEX].next;
     for(disy=0;disy<=3;disy++)
     {  for(i=0;i<=6;i+=2)
       if(BOX[wherex-type[j].xy[i+1]][wherey-disy+type[j].xy[i]]==1)break;
       if(i==8)result=1;
     if(result==1)break;
     }
     break;

   case SPACE : break;
   case ESC : break;
   default: break;
  }
 return result;
}

void change(void)
 {

  switch(movekey)
  {
   case LEFT: typedraw(LIGHTBLUE);wherey--;typedraw(YELLOW);break;
   case RIGHT: typedraw(LIGHTBLUE);wherey++;typedraw(YELLOW);break;
   case UP:  typedraw(LIGHTBLUE);INDEX=type[INDEX].next;wherey-=disy;
        typedraw(YELLOW);break;
   case DOWN : down();break;
   case SPACE: getch();getch();break;
   case ESC: closegraph();exit(0);break;
   default: break;
  }
}

int judgestop(void)
{int i,x,y,result=0;
 for(i=0;i<=6;i+=2)
   {if(BOX[wherex-type[INDEX].xy[i+1]+1][wherey+type[INDEX].xy[i]]==1)
      { result=1;break;}
    }
  return result;
}

void stopfill(void)
 {
  int i,x,y;
  for(i=0;i<=6;i+=2)
   {BOX[x=wherex-type[INDEX].xy[i+1]][y=wherey+type[INDEX].xy[i]]=1;
      draw1(x,y,RED);
    }
 }

down(void)
 {
  typedraw(LIGHTBLUE);
  wherex++;
  typedraw(YELLOW);
 }

int setmovetime(void)
 {
   static long tm1,tm2;
   tm1=biostime(0,tm1) ;
   if(tm1-tm2>TIMELIMIT){tm2=tm1;return 1;}
   else return 0;
 }

void shownexttype(int x)
 {
  int i,j;
  for(i=0;i<=3;i++)
   for(j=12;j<=16;j++)
     draw1(i,j,LIGHTBLUE);
  for(i=0;i<=6;i+=2)
    draw1(3-type[x].xy[i+1],13+type[x].xy[i],YELLOW);
 }

int judgelive(void)
 {
  int i,result=1;
  for(i=1;i<=10;i++)
  if(BOX[3][i]==1){result=0;break;}
  return result;
 }

int linefull(int x)
 {
   int i,result=1;
   for(i=1;i<=10;i++)
     if(BOX[x][i]==0){result=0;break;}
   return result;
 }

void cutline(int x)
 {
   int i,j;
   for(i=x;i>=5;i--)
      for(j=1;j<=10;j++)
  if(BOX[i-1][j]==1){ draw1(i,j,RED); BOX[i][j]=1;}
    else {draw1(i,j,LIGHTBLUE); BOX[i][j]=0;}
    for(j=1;j<=10;j++)
 {draw1(4,j,LIGHTBLUE);BOX[4][j]=0;}
 }

main()
{
 int count,num1,num2,i,gd,gm,c;
 long score;
 detectgraph(&gd,&gm);
 initgraph(&gd,&gm,"c:/turboc2");
 do
  {
  do{printf("please choose the speed,1--10/n");
  scanf("%d",&TIMELIMIT);}while(TIMELIMIT<1||TIMELIMIT>10);
   TIMELIMIT=11-TIMELIMIT;
   score=0;
   setbkcolor(LIGHTBLUE);
   cleardevice();
   initbox();
   setcolor(RED);
   settextstyle(3,0,1);
   outtextxy(10,100,"the show of key");
   outtextxy(10,120,"direction::left+right+down");
   outtextxy(10,140,"change:: up");
   outtextxy(10,160,"pause:: space");
   outtextxy(50,208,"YOUR SPEED:");
   outtextxy(50,250,"YOUR SCORE:");
   gotoxy(25,14);printf("%d",11-TIMELIMIT);
   num1=creattype();
   do
    {
     count=0;
     num2=creattype();
     INDEX=num1;
     wherex=4;wherey=4;
     typedraw(BLACK);shownexttype(num2);
     while(1)
      {
       if(setmovetime())down();
  else {if(bioskey(1)!=0){movekey=bioskey(0);
        if(judgechange())change();
        }}
       if(judgestop()){ TIMELIMIT+=4;
    while(!setmovetime())
     { if(bioskey(1)!=0) {
        movekey=bioskey(0);
        if(judgechange()) change();
       }
     }
    TIMELIMIT-=4;
                         if(judgestop()){
       stopfill();
         for(i=0;i<=3;i++)
       if(linefull(wherex-i)){count++;cutline(wherex-i);wherex++;}
     else if(count!=0){ score+=(int)(pow(2,count-1)*100);count=0;}
     if(count==4){score+=(int)(pow(2,count-1)*100);count=0;}
     gotoxy(23,17);printf("%ld",score);
                          break;}

                      }

   }
    num1=num2;
  }while(judgelive());
 setcolor(RED);settextstyle(1,0,2);
 outtextxy(200,200,"if you want to try again: 'y'/n") ;
  do{c=bioskey(0);}while(c!=0x316e&&c!=0x1579);if(c==0x316e){closegraph();exit(0);}
 }while(1);
}

抱歉!评论已关闭.