⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 els6.cpp

📁 有排行榜
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		    y * BLOCKSIZE + 66-2);
		rectangle(    x * BLOCKSIZE + 360,
			y * BLOCKSIZE + 50,
			x * BLOCKSIZE + 376,
			y * BLOCKSIZE + 66);

	    }
	}
    }

/*这个画出下一个方块的预览*/
	/*确认在page页里画好了*/
setvisualpage(page);
setcolor(FORECOLOR);
rectangle(360,60,440,130);
    setcolor(YELLOW);
    settextstyle(0,0,1);

    outtextxy(362,219,"score:");
	score=killline*10;
	sprintf(sc,"%d",score);
	setcolor(RED);
	settextjustify(LEFT_TEXT,TOP_TEXT);
	outtextxy(362,233,sc);
    setcolor(YELLOW);
    outtextxy(362,171,"level:");
       level=(score/100)+1;
       sprintf(lv,"%d",level);
       setcolor(RED);
       settextjustify(LEFT_TEXT,TOP_TEXT);
       outtextxy(362,185,lv);

//showbmp();                             /*将它显示出来*/
}



void initplayboard(void) {            /*初始化地图*/
int x, y;
   for(y = 0; y < EDGEY; y++) {
	for(x = 0; x < EDGEX; x++) {
	    if(x < 2 || x > EDGEX - 3 || y > EDGEY - 3)
		playboard[y][x] = 1;
	    else playboard[y][x] = 0;
	}
    }                          }
void start() {                /*开始游戏*/
    int x, y;
    initplayboard();                  /*初始化游戏板*/
    srand(GetTickCount());      /*初始化随机发生器*/
rebuidblock();
    setTimer(&tDown, speed, 1); /*启动时钟(快慢两个)*/
    setTimer(&tFast, FAST_INTV, 1);
    next();      /*建立预览*/


}


void rotateBlock(int shape1[5][5], int shape2[5][5]) {
				/*旋转block1输出到block2*/
    int x, y;
for(x = 0; x < 5; x++)
	for(y = 4; y >= 0; y--)
	    shape2[y][x] = shape1[x][4 - y];
}

void rebuidblock() {    /*新建下一个形状并放到nextblock中*/
    int i, x, y;
i = random(7);
    for(y = 0; y < 5; y++)
	for(x = 0; x < 5; x++)
	    nextblock[y][x] = shapes[i].shape[y][x];
}

int drop() {
    if(checkblock(presentX, presentY+1, presentblock)) {
	presentY++;            /*测试下落盒在这个位置*/
	return 1;               /*上是否有冲突,没有的话*/
    }                           /*直接设置presentY*/
    return 0;
}

void fillblock() {                /*填充presentblock */
    int x, y;
for(y = 0; y < 5; y++)
for(x = 0; x < 5; x++)
	    if(presentblock[y][x])
		playboard[y + presentY][x + presentX] = presentblock[y][x];
}

void clear() {
    int x, y;
    int x1, y1;
    int flag;
    for(y = 0; y < EDGEY - 2; y++) {  /*最后两行保留行*/
	flag = 1;                 /*假设为满*/
	for(x = 2; x < EDGEX - 2; x++) {    /*保留列~*/
	    if(!playboard[y][x]) {
		flag = 0;
		break;
	    }
	}
	if(flag) {
		 killline++;              /*向下移动一行*/
	    for(y1 = y; y1 > 0; y1--)
		for(x1 = 2; x1 < EDGEX - 2; x1++)
		    playboard[y1][x1] = playboard[y1 - 1][x1];
	    for(x1 = 2; x1 < EDGEX - 2; x1++)
		playboard[0][x1] = 0;
			  /*并清除掉第一行*/


	}
    }
		  // changescore(killline);
		  // changelevel();
}

int move(int direction) {            /*返回成功与否*/
    int newx;
    if(direction) newx = presentX + 1;
		      /*与drop一样,准备移动后的坐标*/
    else newx = presentX - 1;
    if(checkblock(newx, presentY, presentblock)) { /*测试是否冲突*/
	presentX = newx;           /*可以的话切换presentX*/
	return 1;
    }
    return 0;
}

int checkblock(int x, int y, int shape[5][5]) {
		/*测试block在playboard里x,y位置上是否能着陆*/
/*这个是最关键的一个函数,它判断是否产生非空冲突*/
/*但算法还是很简单的*/
    int x1, y1;
    for(y1 = 0; y1 < 5; y1++)
	for(x1 = 0; x1< 5; x1++)
	    if(playboard[y1 + y][x1 + x] && shape[y1][x1])
		return 0;
    return 1;
}

int rotate() {
    int x, y;
    int newblock[5][5];    /*我们必须将当前方块转动到新的方块*/
			 /*再对这个新的方块的冲突作测试*/
    rotateBlock(presentblock, newblock);    /*转动到新的方块*/
    if(checkblock(presentX, presentY, newblock)) {
			 /*并且新的方块能放到地图上而不冲突*/
	for(y = 0; y < 5; y++)
	    for(x = 0; x < 5; x++)
		presentblock[y][x] = newblock[y][x];     /*复制进来*/
	return 1;
    }
    else return 0;
}

int next() {            /*创建下落元素失败返回0*/
    int x, y;
    presentX = EDGEX / 2 - 2;  /*重新指定小盒位置*/
    presentY = 0;
    for(y = 0; y < 5; y++)
	for(x = 0; x < 5; x++)
	    presentblock[y][x] = nextblock[y][x];/*将nextBlock复制过来*/
    rebuidblock();          /*重建nextBlock*/
    return checkblock(presentX, presentY, presentblock);
}

void changespeed()
{if(score>=(level*100)&&level<9)
speed=speed-level/3;
 setTimer(&tDown, speed, 1);
}

void gameover()
{

int i,j;
for(i= 2; i<12 ; i++) {
	for(j = 3; j < 19; j++) {
	    setfillstyle(SOLID_FILL,YELLOW);
	    setcolor(BGCOLOR);
		    bar(   i* BLOCKSIZE + ORGNX,
			    j* BLOCKSIZE + ORGNY,
			    i * BLOCKSIZE + ORGNX + BLOCKSIZE,
			    j * BLOCKSIZE + ORGNY + BLOCKSIZE);
		    rectangle(    i * BLOCKSIZE + ORGNX,
			j * BLOCKSIZE + ORGNY,
			i * BLOCKSIZE + ORGNX + BLOCKSIZE,
			j * BLOCKSIZE + ORGNY + BLOCKSIZE);
		delay(100);
				 }
			}
	     settextstyle(0,0,4);
	     setcolor(RED);
	     outtextxy(100,100,"Game Over");
	     delay(1500);

}



int main() {
    int  key;             /*记录当前按键*/
    int i;
    Timer *ptDown;        /*下落所指向的时钟(有快慢)*/
    Timer timer;
    setTimer(&timer, 1, 1);
    int gd = VGA, gm = VGAMED;
    initgraph(&gd, &gm, "D:\\TC\\BGI");

    renovate();

start();
		   /*新游戏...*/
     for(;;)
 {    if(bioskey(1))
       key=bioskey(0);
       else key=0;/*得到键盘扫描码*/

       switch(key)
	     {     /*对读到的key进行判断*/
	case UP: {
	    rotate();     /*上,旋转下落方块*/
	    break;
	 }
	case DOWN:{
	    ptDown = &tFast;  /*使用tFast时钟 */
	    break;
	 }
	case LEFT: {
	    move(0);      /*左移*/
	    break;
	 }
	case RIGHT:  {
	    move(1);      /*右移*/
	    break;
	 }
	case SPACE: {
	    for(int i=0;i<=20;i++)
	    drop();
	    break;
	}

	case ESC:   {

	  if((fp=fopen("hzk16","rb"))==NULL)
      {
	outtextxy(139,448,"can't find file hzk16");
	getch();
	exit(0);
      }
	 setcolor(RED);
	 WriteHzStr("退出游戏?",360,275,RED);
	 outtextxy(360,295,"(Y / N)");
	 fclose(fp);
	 getch();

	 key=bioskey(0);
	   if (key==YES)
	   {
	     closegraph();
	     exit(1);

		   }
	   if (key==NO)
	   {

	   start();
	   setfillstyle(SOLID_FILL,BGCOLOR);
	   bar(360,273,460,320);


	   }
 break; }

       default:  ptDown = &tDown;  /*使用原来速度 */
	 }
   if(checkTimer(ptDown)) {  /*在上面已设置了下落要使用的时钟在ptDown里*/
	    if(!drop()) {
		changespeed();       /*下落,失败返回0*/
		fillblock();        /*写到地图里*/
		clear();         /*清除满行*/
		if(!next())
	       { /*新建下落,失败则游戏结束*/
		  for(i=0;i<1;i++)
		  gameover();

		  if((fp=fopen("hzk16","rb"))==NULL)
      {
	outtextxy(139,448,"can't find file hzk16");
	getch();
	exit(0);
      }

	 WriteHzStr("请按回车继续!",40,300,RED);

	 fclose(fp);
	 getch();
		  setcolor(BLACK);
		  settextstyle(0,0,1);
		  order(score);
		  delay(100);
		 closegraph();
		exit(0);
	       }
	   }
       }

   if(checkTimer(&timer))
	   renovate();
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -