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

📄 tetris2.txt

📁 数据结构的源代码和配套讲义
💻 TXT
📖 第 1 页 / 共 2 页
字号:
/**********************************************************
 *  T E T R I S v1.0
 *  programming and graphics Andrew Deren
 *
 *  1 / 30 / 97
 **********************************************************/

//inlucde files
#include <stdio.h>
#include <allegro.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <dir.h>
#include "tdat.h"	//header file for data file created by grabber

//high score stucture name not implemented yet
struct HiScoreStruct {
	int score;
   char name[12];
};

//musics used from tdat.dat
int Music[3] = {MUSIC01, MUSIC02, MUSIC03};
int start_level;			//start from level selected under options
int start_blocks;			//number of initial rows on the map
int block[4][4];			//current block
int next_block;			//next block to be put
int map[10][20];			//map
int block_x, block_y;	//location of the block
int x_size, y_size;		//size of the block
volatile int timer;		//timer of the game
int current_block;		//current block type
int speed;					//speed of the game
int score, dels;			//game score and number of rows deleted
char text[256];			//some text used for using textout
struct HiScoreStruct HiScore;		//high score
int current_music;		//music currently played
BITMAP *map_buffer;		//double buffer for map displaying
DATAFILE *graphics;		//data file with graphics and sounds
BITMAP *temp_bitmap;		//temp 640x480 bitmap used by options and initial screens
int MusicVolume;			//current volume of the music
int SoundVolume;			//current volume of the sounds
bool soundOn;				//sounds on or not
bool musicOn;				//music on or not

//function prototypes
int Random(int x);
void IncrementTimer(...);
void DeleteRow(void);
int CanPut(void);
void NewGame(void);
void SetVolume(void);
void PlayGame(void);
void DrawVolume(int pos);
void SetVolume(void);
void Error(char *);
void InitGame(void);
void ClearBlock(void);
void GenerateBlock(int);
void DrawFrame(void);
void GenerateNew(void);
void PutBlock(void);
void RotateBlock(void);
void DrawMap(void);
void NewGame(void);
void DrawMenu(void);
void MainMenu(void);
void PlayGame(void);

//returns random number up to x-1
int Random(int x) {return random() % x;}

//increment game timer
void IncrementTimer(...)
{
 	timer++;
}
END_OF_FUNCTION(IncrementTimer);

//draw the options screen with cursor at pos
void DrawVolume(int pos)
{
 	clear(temp_bitmap);	//clear temp bitmap
   text_mode(-1);			//set transparent text mode
   //set drawing mode to pattern from datafile
   drawing_mode(DRAW_MODE_COPY_PATTERN, (BITMAP*)graphics[PATTERN].dat, 0, 0);
   //fill the temp buffer
   rectfill(temp_bitmap, 0, 0, 640, 480, 0);
   //put some text
   textout(temp_bitmap, font, "O P T I O N S", 140, 10, 36);
   textout(temp_bitmap, font, "Sound: ", 100, 50, 36);
   textout(temp_bitmap, font, "Music: ", 100, 100, 36);

   if (soundOn) textout(temp_bitmap, font, " On", 250, 50, 36);
   else textout(temp_bitmap, font, "Off", 250, 50, 36);
   if (musicOn) textout(temp_bitmap, font, " On", 250, 100, 36);
   else textout(temp_bitmap, font, "Off", 250, 100, 36);

	sprintf(text, "Sound Volume: %4d", SoundVolume);
   textout(temp_bitmap, font, text, 150, 130, 36);
   sprintf(text, "Music Volume: %4d", MusicVolume);
   textout(temp_bitmap, font, text, 150, 180, 36);
   sprintf(text, "Start Level: %4d", start_level);
   textout(temp_bitmap, font, text, 150, 230, 36);
   sprintf(text, "Start Blocks: %4d", start_blocks);
   textout(temp_bitmap, font, text, 150, 280, 36);
   //draw all the scorlbars
   for (int i=0; i<10; i++) {
		draw_sprite(temp_bitmap, (BITMAP*)graphics[BAR].dat, 106+i*25, 150);
      draw_sprite(temp_bitmap, (BITMAP*)graphics[BAR].dat, 106+i*25, 200);
      draw_sprite(temp_bitmap, (BITMAP*)graphics[BAR].dat, 106+i*25, 250);
      draw_sprite(temp_bitmap, (BITMAP*)graphics[BAR].dat, 106+i*25, 300);
   }
   draw_sprite(temp_bitmap, (BITMAP*)graphics[BAR2].dat, 100+SoundVolume, 150);
   draw_sprite(temp_bitmap, (BITMAP*)graphics[BAR2].dat, 100+MusicVolume, 200);
   draw_sprite(temp_bitmap, (BITMAP*)graphics[BAR2].dat, 100+start_level*25, 250);
   draw_sprite(temp_bitmap, (BITMAP*)graphics[BAR2].dat, 100+start_blocks*25, 300);
   draw_sprite(temp_bitmap, (BITMAP*)graphics[CURSOR].dat, 80, pos*50);
   textout(temp_bitmap, font, "Press arrow keys to change, ESC to exit", 100, 400, 36);
	blit(temp_bitmap, screen, 0, 0, 0, 0, 640, 480);
}

//options screen
void SetVolume(void)
{
 	int ch;
   int position = 1;
   DrawVolume(1);
	do {
     	if (keypressed()) {
        	ch = readkey();
         if ((ch >> 8) == KEY_DOWN) {position++; if (position == 7) position = 1;}
      	else if ((ch >> 8) == KEY_UP) {position--; if (position == 0) position = 6;}
         else if ( ((ch >> 8) == KEY_RIGHT) || ((ch >>8) == KEY_LEFT)){
      		if (position == 1) {if (soundOn) soundOn = FALSE; else soundOn = TRUE;}
         	else if (position == 2) {if (musicOn) musicOn = FALSE; else musicOn = TRUE;}
            else if (position == 3) {
              	if ((ch >> 8) == KEY_RIGHT) {
                 	SoundVolume+= 25;
                  if (SoundVolume > 255) SoundVolume=255;
               }
            	else {SoundVolume-=25;if (SoundVolume < 0) SoundVolume=0;}
            }
            else if (position == 4) {
              	if ((ch >> 8) == KEY_RIGHT) {
                 	MusicVolume+= 25;
                  if (MusicVolume > 255) MusicVolume=255;
               }
            	else {MusicVolume-=25; if (MusicVolume < 0) MusicVolume = 0;}
            }
         	else if (position == 5) {
               if ((ch >> 8) == KEY_RIGHT) {
                 	start_level++;
                  if (start_level > 10) start_level = 10;
               }
            	else {start_level--; if (start_level < 0) start_level = 0;}
            }
         	else if ( position == 6) {
              	if ((ch >> 8) == KEY_RIGHT) {
                 	start_blocks++;
                  if (start_blocks > 10) start_blocks = 10;
                }
                else {start_blocks--; if (start_blocks < 0) start_blocks = 0;}
            }
         } //end key up or down
      	else if ((ch >> 8) == KEY_ESC) break;
         DrawVolume(position);
      }//end keypressed;
   }while (1==1);
	set_volume(SoundVolume, MusicVolume);
	clear(screen);
}

void Error(char *string)
{
 	allegro_exit();
 	printf("Error: %s\n", string);
   exit(1);
}

void InitGame(void)
{
  	allegro_init();  			//initialize allegro
   install_keyboard();  	//install keyboard handler
   install_timer();			//install timer
   set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);	//set graphics mode to 640x480x256
	srandom((int)time(NULL));
   if (install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL) != 0)
		Error("Error initializing sound.");
   if (!file_exists("tdat.dat", FA_RDONLY | FA_ARCH, NULL))
       Error("Cannot find tdat.dat file.");
	graphics = load_datafile("tdat.dat");	//load graphics
  	map_buffer = create_bitmap(200, 400);	//create double buffer
   temp_bitmap = create_bitmap(640, 480);
   timer = 0;
   LOCK_FUNCTION(IncrementTimer);
   LOCK_VARIABLE(timer);
   install_int(IncrementTimer, 20);
   speed = 15;
   set_clip(map_buffer, 0, 0, 200, 400);
   score = 0;
   dels = 1;
  	FILE *file = fopen("tetris.hsc", "rb");
	if (file) {
   	fread(&HiScore, sizeof(struct HiScoreStruct), 1, file);
      fclose(file);
   }
  	else {
     	HiScore.score = 0;
      strcpy(HiScore.name, "Unknown");
   }
	font = (FONT*)graphics[TECH_FONT].dat;
   soundOn = TRUE;
   musicOn = TRUE;
   SoundVolume = 180;
   MusicVolume = 180;
   set_volume(SoundVolume, MusicVolume);
   start_level = 0;
   start_blocks = 0;
   current_music = Random(3);
   set_pallete(black_pallete);
}

void DrawFrame(void)
{
 	draw_sprite(screen, (BITMAP*)graphics[WALL03].dat, 80, 20);
   draw_sprite(screen, (BITMAP*)graphics[WALL04].dat, 300, 20);
   draw_sprite(screen, (BITMAP*)graphics[WALL05].dat, 80, 440);
   draw_sprite(screen, (BITMAP*)graphics[WALL06].dat, 300, 440);

   draw_sprite(screen, (BITMAP*)graphics[WALL03].dat, 340, 20);
   draw_sprite(screen, (BITMAP*)graphics[WALL04].dat, 580, 20);
   draw_sprite(screen, (BITMAP*)graphics[WALL05].dat, 340, 100);
   draw_sprite(screen, (BITMAP*)graphics[WALL06].dat, 580, 100);

   draw_sprite(screen, (BITMAP*)graphics[WALL03].dat, 340, 140);
   draw_sprite(screen, (BITMAP*)graphics[WALL04].dat, 440, 140);
   draw_sprite(screen, (BITMAP*)graphics[WALL05].dat, 340, 200);
   draw_sprite(screen, (BITMAP*)graphics[WALL06].dat, 440, 200);

   for (int i=0; i<11; i++) {
      draw_sprite(screen, (BITMAP*)graphics[WALL01].dat, 360+i*20, 20);
      draw_sprite(screen, (BITMAP*)graphics[WALL01].dat, 360+i*20, 100);
   }
	for (int i=0; i<3; i++) {
      draw_sprite(screen, (BITMAP*)graphics[WALL02].dat, 340, 40+i*20);
      draw_sprite(screen, (BITMAP*)graphics[WALL02].dat, 580, 40+i*20);

	}
	for (int i=0; i<4; i++) {
      draw_sprite(screen, (BITMAP*)graphics[WALL01].dat, 360+i*20, 140);
      draw_sprite(screen, (BITMAP*)graphics[WALL01].dat, 360+i*20, 200);
   }
   for (int i=0; i<10; i++) {
		draw_sprite(screen, (BITMAP*)graphics[WALL01].dat, 100+i*20, 20);
      draw_sprite(screen, (BITMAP*)graphics[WALL01].dat, 100+i*20, 440);
   }
	for (int i=0; i<2; i++) {
      draw_sprite(screen, (BITMAP*)graphics[WALL02].dat, 340, 160+i*20);
      draw_sprite(screen, (BITMAP*)graphics[WALL02].dat, 440, 160+i*20);
   }
	for (int i=0; i<20; i++) {
 		draw_sprite(screen, (BITMAP*)graphics[WALL02].dat, 80, 40+i*20);
      draw_sprite(screen, (BITMAP*)graphics[WALL02].dat, 300, 40+i*20);
   }
}

void ClearBlock(void)
{
 	for (int i=0; i<4; i++)  for (int j=0; j<4; j++) block[i][j] = 0;
}

void GenerateBlock(int kind)
{
 	ClearBlock();
	switch (kind) {
	case  1: block[1][0] = block[2][0] = block[0][1] = block[1][1] = 1; break;
   case 11: block[0][0] = block[0][1] = block[1][1] = block[1][2] = 1; break;
   case 21: block[1][0] = block[2][0] = block[0][1] = block[1][1] = 1; break;
   case 31: block[0][0] = block[0][1] = block[1][1] = block[1][2] = 1; break;

   case  2:	block[0][0] = block[1][0] = block[1][1] = block[2][1] = 1; break;
	case 12:	block[1][0] = block[1][1] = block[0][1] = block[0][2] = 1; break;
	case 22:	block[0][0] = block[1][0] = block[1][1] = block[2][1] = 1; break;
	case 32:	block[1][0] = block[1][1] = block[0][1] = block[0][2] = 1; break;

	case  3: block[0][0] = block[0][1] = block[1][0] = block[1][1] = 1; break;
	case 13: block[0][0] = block[0][1] = block[1][0] = block[1][1] = 1; break;
	case 23: block[0][0] = block[0][1] = block[1][0] = block[1][1] = 1; break;
	case 33: block[0][0] = block[0][1] = block[1][0] = block[1][1] = 1; break;

	case  4: block[1][0] = block[0][1] = block[1][1] = block[2][1] = 1; break;
	case 14: block[0][0] = block[0][1] = block[0][2] = block[1][1] = 1; break;
   case 24: block[0][0] = block[1][0] = block[2][0] = block[1][1] = 1; break;
   case 34: block[0][1] = block[1][0] = block[1][1] = block[1][2] = 1; break;

   case  5: block[2][0] = block[0][1] = block[1][1] = block[2][1] = 1; break;
   case 15: block[0][0] = block[0][1] = block[0][2] = block[1][2] = 1; break;
   case 25: block[0][0] = block[1][0] = block[2][0] = block[0][1] = 1; break;
	case 35: block[0][0] = block[1][0] = block[1][1] = block[1][2] = 1; break;

	case  6: block[0][0] = block[0][1] = block[1][1] = block[2][1] = 1; break;
	case 16: block[0][0] = block[1][0] = block[0][1] = block[0][2] = 1; break;
   case 26: block[0][0] = block[1][0] = block[2][0] = block[2][1] = 1; break;

⌨️ 快捷键说明

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