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

📄 tetris2.txt

📁 数据结构的源代码和配套讲义
💻 TXT
📖 第 1 页 / 共 2 页
字号:
   case 36: block[1][0] = block[1][1] = block[1][2] = block[0][2] = 1; break;

   case  7: block[0][0] = block[1][0] = block[2][0] = block[3][0] = 1; break;
	case 17: block[0][0] = block[0][1] = block[0][2] = block[0][3] = 1; break;
   case 27: block[0][0] = block[1][0] = block[2][0] = block[3][0] = 1; break;
	case 37: block[0][0] = block[0][1] = block[0][2] = block[0][3] = 1; break;
	default: Error("Unknown element to be generated."); break;
	}

   if ((kind == 7) || (kind == 27)) {x_size = 4; y_size = 1; }
	else if ((kind == 17) || (kind == 37)) {x_size = 1; y_size = 4;}
	else if (kind % 10 == 3) {x_size = 2; y_size = 2; }
	else if ((kind / 10 == 0) || (kind / 10 == 2)) {x_size = 3; y_size = 2;}
	else if ((kind / 10 == 1) || (kind / 10 == 3)) {x_size = 2; y_size = 3;}
   current_block = kind;
}

void GenerateNew(void)
{
	int temp = next_block;
   next_block = Random(7)+1;
   GenerateBlock(next_block);
	for (int i=0; i<4; i++) {
      for (int j=0; j<2; j++) {
         if (block[i][j] == 1)
           	draw_sprite(screen, (BITMAP*)graphics[BLOCK].dat, 360+i*20, 160+j*20);
			else draw_sprite(screen, (BITMAP*)graphics[EMPTY].dat, 360+i*20, 160+j*20);
		}
  	}
	GenerateBlock(temp);
   block_x = 3;
   block_y = 0;
   if (!CanPut()) {
      text_mode(-1);
      textout_centre(screen, font, "G A M E", 202, 152, 42);
		textout_centre(screen, font, "G A M E", 200, 150, 15);
      textout_centre(screen, font, "O V E R", 202, 202, 42);
      textout_centre(screen, font, "O V E R", 200, 200, 15);
		textout_centre(screen, (FONT*)graphics[GAME_FONT].dat, "Press Enter to", 202, 252, 42);
		textout_centre(screen, (FONT*)graphics[GAME_FONT].dat, "to start a new game", 202, 277, 42);
      textout_centre(screen, (FONT*)graphics[GAME_FONT].dat, "Press Enter to", 200, 250, 15);
      textout_centre(screen, (FONT*)graphics[GAME_FONT].dat, "to start a new game", 200, 275, 15);
      sprintf(text, "%3d", score);
      textout_centre(screen, (FONT*)graphics[GAME_FONT].dat, text, 202, 302, 42);
      textout_centre(screen, (FONT*)graphics[GAME_FONT].dat, text, 200, 300, 15);
		clear_keybuf();
		do {
        	if (key[KEY_ENTER]) break;
      } while (1==1);
   	clear_keybuf();
      NewGame();
   }
}

void PutBlock(void)
{
   for (int i=0; i<4; i++) {
      for (int j=0; j<4; j++) {
         if (block[i][j] == 1)
	         map[block_x+i][block_y+j] = block[i][j];
      }
   }
	if (soundOn) play_sample((SAMPLE*)graphics[MOVE_SOUND].dat, SoundVolume, 125, 1000, FALSE);
	DeleteRow();
}

void RotateBlock(void)
{
 	int old_block = current_block;
 	current_block += 10;
   if (current_block > 40) current_block -= 40;
   GenerateBlock(current_block);
   if (!CanPut())  {
     	current_block = old_block;
      GenerateBlock(current_block);
   }
}


int CanPut(void)
{
	for (int i=0; i<4; i++) {
		for (int j=0; j<4; j++) {
			if (block[i][j] == 1) {
           if (block_x + i > 9) return FALSE;
           if (block_x + i < 0) return FALSE;
           if (block_y + j > 19) return FALSE;
           if (map[block_x+i][block_y+j] == 1) return FALSE;
         }
		}
   }
	return TRUE;
}


void DrawMap(void)
{
 	clear(map_buffer);
	for (int i=0; i<10; i++) {
 		for (int j=0; j<20; j++) {
         if (map[i][j] == 1)
           	draw_sprite(map_buffer, (BITMAP*)graphics[BLOCK].dat, i*20, j*20);
      }
   }
	for (int i=0; i<4; i++) {
      for (int j=0; j<4; j++) {
         if (block[i][j] == 1)
           	draw_sprite(map_buffer, (BITMAP*)graphics[BLOCK].dat, block_x*20+i*20, block_y*20+j*20);

  	   }
   }
	blit(map_buffer, screen, 0, 0, 100, 40, 200, 400);
   text_mode(0);
   sprintf(text, "%7d", score);
   textout(screen, font, text, 570-text_length(font, text), 50, 15);
   sprintf(text, "%7d", dels / 10);
   textout(screen, font, text, 570-text_length(font, text), 65, 15);
}

void DeleteRow(void)
{
 	int sum, i, j, del = 0;
	for (i=19; i>=0; i--) {
      sum = 0;
      for (j=0; j<10; j++) sum += map[j][i];
		if (sum == 10) {
        	for (j=i; j>=1; j--) for (int k=0; k<10; k++) map[k][j] = map[k][j-1];
         i += 2;
         del++;
		}
   }
	if (del > 0)
     	if (soundOn)
        	play_sample((SAMPLE*)graphics[DELETE_SOUND].dat, SoundVolume, 125, 1000, FALSE);
   dels += del;
   if (del > 0) score += (100*del)+((del-1)*25)+(dels/10)*25;
   speed = 25-(dels/10)*2;
   if (speed < 2) speed = 2;
}

void NewGame(void)
{
 	if (score > HiScore.score) HiScore.score = score;
 	for (int i=0; i<20; i++)
      for (int j=0; j<10; j++) map[j][i] = 0;
   score = 0;
   next_block = Random(7)+1;
   GenerateNew();
   DrawMap();
   dels = start_level*10;
   speed = 25-(dels/10)*2;
   text_mode(0);
   sprintf(text, "Score:");
   textout(screen, font, text, 365, 50, 15);
   sprintf(text, "Level:");
   textout(screen, font, text, 365, 65, 15);
   sprintf(text, "High Score: ");
   textout(screen, font, text, 365, 80, 15);
   sprintf(text, "%7d", HiScore.score);
   textout(screen, font, text, 570-text_length(font, text), 80, 15);
   for (int i=0; i<start_blocks; i++) {
      for (int j=0; j<10; j++) {
         if (Random(2)) map[j][19-i] = 1;
      }
   }
	timer = 0;
}

void DrawMenu(int pos)
{
 	text_mode(-1);
	clear(temp_bitmap);
   drawing_mode(DRAW_MODE_COPY_PATTERN, (BITMAP*)graphics[PATTERN].dat, 0, 0);
   rectfill(temp_bitmap, 0, 0, 640, 480, 0);
   draw_sprite(temp_bitmap, (BITMAP*)graphics[TITLE].dat, 170, 10);
   textout(temp_bitmap, font, "N E W  G A M E", 100, 200, 36);
   textout(temp_bitmap, font, "N E W  G A M E", 101, 201, 1);
   textout(temp_bitmap, font, "O P T I O N S", 100, 250, 36);
   textout(temp_bitmap, font, "O P T I O N S", 101, 251, 1);
   textout(temp_bitmap, font, "E X I T", 100, 300, 36);
   textout(temp_bitmap, font, "E X I T", 101, 301, 1);
   draw_sprite(temp_bitmap, (BITMAP*)graphics[CURSOR].dat, 80, (pos-1)*50+200);
	blit(temp_bitmap, screen, 0, 0, 0, 0, 640, 480);
}

void MainMenu()
{
 	int ch;
   int pos = 1;
 	DrawMenu(1);
   fade_in((RGB*)graphics[GAME_PALLETE].dat, 2);             //fade in slowly
   do {
     	if (keypressed()) {
        	ch = readkey();
         if ((ch >> 8) == KEY_UP) {pos--; if (pos == 0) pos = 3;}
         else if ((ch >>8) == KEY_DOWN) {pos++; if (pos==4) pos=1;}
         else if ((ch >> 8) == KEY_ENTER) {
           	if (pos == 1) {
              	PlayGame();
            }
            else if (pos == 2)
              	SetVolume();
            else if (pos == 3)
              	break;
         }
      	else if ((ch >> 8) == KEY_ESC) break;
      	DrawMenu(pos);
      } // end if keypressed
   } while (1==1);
	fade_out(1);
   if (musicOn) stop_midi();
}

void PlayGame(void)
{
 	int ch;
   rectfill(screen, 0, 0, 640, 480, 0);
   DrawFrame();
   drawing_mode(DRAW_MODE_SOLID, NULL, 0, 0);
   rectfill(screen, 360, 40, 580, 100, 0);
   current_music = Random(3);
   if (musicOn) play_midi((MIDI*)graphics[Music[current_music]].dat, TRUE);
   draw_sprite(screen, (BITMAP*)graphics[TETRIS].dat, 340, 300);
   NewGame();
   while (1==1) {
     	if (keypressed()) {
         ch = readkey();
	     	if ((ch >> 8) == KEY_LEFT) {block_x--; if (!CanPut()) block_x++;
         }
			if ((ch >> 8) == KEY_RIGHT) {block_x++; if (!CanPut()) block_x--;
			}
	      if ((ch >> 8) == KEY_UP)   RotateBlock();
	      if ((ch >> 8) == KEY_DOWN) {
           	while (CanPut()) {DrawMap();	block_y++;
            };
         	block_y--;
         }
      	if ((ch >> 8) == KEY_F2) NewGame();
         else if ((ch >> 8) == KEY_F5) {
            if (musicOn) {
              	stop_midi();
               current_music++;
               if (current_music > 2) current_music = 0;
               play_midi((MIDI*)graphics[Music[current_music]].dat, TRUE);
            }
         }
	   	else if ((ch >> 8) == KEY_ESC) {
           text_mode(-1);
           textout(screen, font, "Do you really want", 125, 200, 15);
           textout(screen, font, "to quit? (Y/N)", 125, 225, 15);
           if ((ch = readkey()) >> 8 == KEY_Y)  break;
         }
      	else if ((ch >> 8) == KEY_F12){
           	BITMAP* bmp;
            bmp = create_sub_bitmap(screen, 0, 0, SCREEN_W, SCREEN_H);
            save_pcx("tetdat.pcx", bmp, (RGB*)graphics[GAME_PALLETE].dat);
            destroy_bitmap(bmp);
         }
			clear_keybuf();
      }
		if (timer > speed) {
	   	block_y++;
         if (!CanPut()) {block_y--;  PutBlock();  GenerateNew();
         }
      	timer = 0;
      }
      if (!CanPut()) {PutBlock(); GenerateNew();
      }
      DrawMap();
   }
	stop_midi();
}


main()
{
 	InitGame();
   MainMenu();
	allegro_exit();
   FILE *file;
   file = fopen("tetris.hsc", "wt");
   if (file) {
     	fwrite(&HiScore, sizeof(struct HiScoreStruct), 1, file);
      fclose(file);
   }
	printf("                             Ader Software 1997                        \n");
   printf("                             T E T R I S   v1.0                        \n");
   printf(" --------------------------------------------------------------------- \n");
   printf("                         Programing and Graphics by                    \n");
   printf("                              Andrew     Deren                         \n");
   printf("                             aderen@eecs.uic.edu                       \n");
   printf("           http://www.eecs.uic.edu/~aderen/ader/main.html         \n");
   printf(" --------------------------------------------------------------------- \n");
   printf("                            Special Thanks To:                         \n");
   printf("                      DJ Delorie - for making DJGPP                    \n");
   printf("           Shawn Hargreaves - for making allegro game library          \n");
   printf("              and all the other people who contributed to              \n");
   printf("                 DJGPP development tools and utilities.                \n");
   printf(" --------------------------------------------------------------------- \n");
   printf("\n\n\n");
}


⌨️ 快捷键说明

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