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

📄 object.c

📁 TETRIS GAME, AUTO-PLAYER AND ARTIFICIAL INTELLIGENCY By Bluteau Thomas
💻 C
字号:
/************************/
/* Game Project for ACP */
/************************/

/***********************/
/*       TETRIS        */
/*    Thomas Bluteau   */
/***********************/


/***************************************************
  OBJECT.C is the library of all function that permit
  to create, move, change the object pieces 
****************************************************/
#include "function.h"
#include "CONST_DEFINE.h"




/*******
Put the object on the ground
	A double loop goes trough the object array and put the  
	significant square in the ground. 
	The position_X and position_Y gives indices where is the object
*******/
void placeObject(t_infos *infos,object_t current)
{
	int i,j;
	
	for(i=0;i<current.x;i++)				
		for(j=0;j<current.y;j++)
			if(current.form[i*current.y+j])
				infos->ground[(i+current.position_X)*GRID_HEIGHT+(j+current.position_Y)] = current.color;
}


/*******
Remove the object of the ground, the significant boxes will colored in black
	A double loop goes trough the object array and full of black  
	significant square in the ground.  
	The position_X and position_Y gives indices where is the object
*******/
void removeObject(t_infos *infos, object_t current)
{
	int i,j;
	for(i=0;i<current.x;i++)
		for(j=0;j<current.y;j++)
			if(current.form[i*current.y+j])
				infos->ground[(i+current.position_X)*GRID_HEIGHT+(j+current.position_Y)] = infos->black;
}


void swapObject(object_t * prev_current, object_t * current)
{
	*current = *prev_current;
	current->position_X = 5;
	current->position_Y = 0;
}
/***********
Move an object from his original place with a {vX,vY,R} deplacement vector
	This function is divided into 3 parts 
	For horizontal and vertical method are the same:
			-The object is remove of the ground
			-A deplacement is applied to it
			-The validity of the new place is checked
			-The object is put back on the ground at the new or old place, depending
			on the prvious check
	For rotation movement, and temporary array containing the form rotated is created,
	checked and applied if necessary.
	All the variable are put to 0 at the end of function.
		This funcion could be improve with a common function like "moveAllowed()"
***********/
void moveCurrent(t_infos *infos,object_t * current,mov_t *mov)
{
	int i,j,swap,rotAllow=1;
	int *tmpArr;
	removeObject(infos,*current);				// Hide the object for the test

	
	/***********X-axis*************/	
	if(mov->vX)									//Test if a X axis movement is required
		for(i=0;i<current->x;i++)				// Go trough the object array
			for(j=0;j<current->y;j++)			//    and test the validity of movement
				if(current->form[i*current->y + j])
					if(infos->ground[ (i+ current->position_X + mov->vX)*GRID_HEIGHT + (j+current->position_Y)])
					{
						mov->vX = 0;			// Inhibit the X axis movement 
					}			
	current->position_X += mov->vX;				// Apply the delpacement
	
	/***********Rotation*************/
	if(mov->R){
		tmpArr = malloc(current->x * current->y * sizeof(int)); //Temporary form to check validity
		
		for(j=0;j<current->y;j++)
			for(i=0;i<current->x;i++)
				tmpArr[i+j*current->x] = current->form[(current->y -j - 1) + i * current->y];
		
		
		
		for(i=0;i<current->y;i++)
				for(j=0;j<current->x;j++)
					if(current->form[i*current->y + j])
						if(infos->ground[ (i+ current->position_X + mov->vX)*GRID_HEIGHT + (j+current->position_Y)])
							rotAllow = 0;
		if(rotAllow)
		{		
			swap = current->x;
			current->x = current->y;
			current->y = swap;
			current->form = tmpArr;
		}
	}
	/***********Y-axis*************/
	if(infos->fallCompleted)
		mov->vY = 1;
	infos->fallCompleted = 0;
	for(i=0;i<current->x;i++)
		for(j=0;j<current->y;j++)
			if(current->form[i*current->y + j])
				if(infos->ground[ (i+current->position_X)*GRID_HEIGHT + (j+current->position_Y) + mov->vY])
				{
					mov->vY = 0;				// Cancel movement 
					infos->fallCompleted = 1;	// and close the fall of the piece
					//fallagain = 1;
				}
	
	current->position_Y += mov->vY;			// Apply Y axis movement

	/***********Finalize*************/
	placeObject(infos,*current);			// Finalise the deplacement putting back the
											// object on the ground

	mov->vY = 0;							// Reset the movement vector
	mov->vX = 0;							// (but let vY cause "gravity")
	mov->R = 0;	
	
										
}






/*******
Create a form define in the array of object
	Use the malloc function on object->form
*******/
void createObjet(t_infos *infos,object_t * object, int type)
{
	int i=0,j=0;
	switch(type)
	{
		/********SQUARE*********/
	case 0:			
		{
			object->color = infos->red;
			object->x=2;object->y=2;
			object->form = malloc(object->x * object->y * sizeof(int));
			for(i=0;i<4;i++)
				object->form[i]=1 ;
			break;
		}
		/********Z-FORM*********/
	case 1:		
		{
			object->color = infos->white;
			object->x=2;object->y=3;
			object->form = malloc((object->x * object->y) * sizeof(int));
			for(i=1;i<5;i++)
				object->form[i]=1;	
			object->form[0] = 0;
			object->form[5] = 0;
			break;
		}
			/********Z-FORM inverse*********/
	case 2:		
		{
			object->color = infos->orange;
			object->x=2;object->y=3;
			object->form = malloc((object->x * object->y) * sizeof(int));
			for(i=0;i<6;i++)
				object->form[i]=1;	
			object->form[2] = 0;
			object->form[3] = 0;
			break;
		}
		/********LINE*********/
	case 3:	
		{
			object->color = infos->green;
			object->x=1;object->y=4;
			object->form = malloc((object->x * object->y) * sizeof(int));
			for(i=0;i<4;i++)
				object->form[i]=1 ;
			break;
		}
		/********L-FORM*********/
	case 4:	
		{
			object->color = infos->yellow;
			object->x=2;object->y=3;
			object->form = malloc((object->x * object->y) * sizeof(int));
			for(i=0;i<3;i++)
				object->form[i]=1 ;	
			object->form[3] = 0;
			object->form[4] = 0;
			object->form[5] = 1;
			break;
		}
		/********L-FORM inverse*********/
	case 5:	
		{
			object->color = infos->marron;
			object->x=2;object->y=3;
			object->form = malloc((object->x * object->y) * sizeof(int));
			for(i=5;i>2;i--)
				object->form[i]=1 ;	
			object->form[0] = 0;
			object->form[1] = 0;
			object->form[2] = 1;
			break;
		}
		/********T-FORM*********/
	case 6:	
		{
			object->color = infos->blue;
			object->x=3;object->y=2;
			object->form = malloc((object->x * object->y) * sizeof(int));
			for(i=0;i<3;i++)
			{
				object->form[i*object->y]=1 ;	
				object->form[i*object->y + 1] =0;
			}
			object->form[3]=1;
			break;
		}
	}

	object->position_X = 17;
	object->position_Y = 9;
}


/*******
Detect and remove the full line, update the score and the total number of line.
*******/
void detect_line(t_infos *infos)
{
int i,j,nb_Lin,modulo_tmp;
	
	nb_Lin = 0;
	j =	GRID_HEIGHT-2;
	
	do
	{
		i=1;				
		while(infos->ground[j + i*GRID_HEIGHT] && i<GRID_WIDTH-1)
			i++;
		if(i==GRID_WIDTH-1)
		{
			nb_Lin++;
		}

		j--;
		
		for(i=1;i<GRID_WIDTH-1;i++)			// This loop shift the current line nblin under 
			infos->ground[j + nb_Lin + i*GRID_HEIGHT] = infos->ground[j + i*GRID_HEIGHT];
	
	

	}
	while(j>0);			//Main loop, trough the height
	

	modulo_tmp = infos->line%10;					// safe previous modulo

	infos->line += nb_Lin;			
	
	if(modulo_tmp > infos->line%10)					// Compare if a tenth line is completed
		infos->speed_level -= 50;			
	
	switch(nb_Lin)
		{
			case 1 :
				infos->score += 1;
				break;
			case 2:
				infos->score += 4;
				break;
			case 3:
				infos->score += 8;
				break;
			case 4:
				infos->score += 12;
				break;
		}

	if(nb_Lin)									//Display on tyhe console the score
		printf("\b\b\b%3d",infos->score);
}




⌨️ 快捷键说明

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