【C语言】RussianBlock俄罗斯方块项目实现

这段代码实现了一个类似2048的游戏功能,包括方块的左移、右移、下移,检查是否出界、阻挡,合并方块,消除行并更新得分,以及判断游戏结束和创建新方块的逻辑。
摘要由CSDN通过智能技术生成

总览:

 实现内部功能:

 data_type.h:

#pragma once
#define TRUE 1
#define FALSE 0
typedef int _BOOL_;
typedef 
enum block_type{L,Z,TIAN,YI,T}BLOCK_TYPE;

 global_var.h:

#pragma once
extern int luoKuai[20][10];
extern int shiKuai[20][10];
extern int color;
extern int stop;
extern int score;

elsfk.h:

#pragma once
#include "data_type.h"
#include "global_var.h"
#include<stdlib.h>
/*左移动*/
void leftMove();
/*左不出界*/
_BOOL_ leftNotOut();
/*左不阻挡*/
_BOOL_ leftNotStop();

/*右移动*/
void rightMove();
/*右不出界*/
_BOOL_ rightNotOut();
/*右不阻挡*/
_BOOL_ rightNotStop();

/*下移动*/
void downMove();
/*下不出界*/
_BOOL_ downNotOut();
/*下不阻挡*/
_BOOL_ downNotStop();

/*合并*/
void merge();
/*消除行
 返回消除的行数
*/
int clearLine();
/*更新得分
lines:消除的行数*/
void updateScore(int lines);

_BOOL_ isOver();
/*创建新方块*/
void createNewLuoKuai();

void gameover();

 

 1.左移动

void leftMove()
{
	for (int k=1;k<10;k++)
	{
		for (int h=0;h<20 ;h++)
		{
			luoKuai[h][k-1] = luoKuai[h][k];
			luoKuai[h][k] = 0;
		}
	}
}

 2.左不出界

_BOOL_ leftNotOut()
{
	return TRUE;
}

3.左不阻挡

_BOOL_ leftNotStop()
{
	for (int k=1;k<10 ;k++)
	{
		for (int h=0;h<20 ;h++)
		{
			if (luoKuai[h][k] && shiKuai[h][k - 1])
			{
				return FALSE;
			}
		}
	}
	return TRUE;
}

4.右移动(可参考左移动

void rightMove()
{
	for (int k = 8; k>=0; k--)
	{
		for (int h = 0; h < 20; h++)
		{
			luoKuai[h][k + 1] = luoKuai[h][k];
			luoKuai[h][k] = 0;
		}
	}
}

5.右不出界

_BOOL_ rightNotOut()
{
	return TRUE;
}

6.右不阻挡

_BOOL_ rightNotStop()
{
	for (int k = 8; k >=0; k--)
	{
		for (int h = 0; h < 20; h++)
		{
			if (luoKuai[h][k] && shiKuai[h][k + 1])
			{
				return FALSE;
			}
		}
	}
	return TRUE;
}

7.下移动

void downMove()
{
	for (int h=18;h>=0 ;h--)
	{
		for (int k=0;k<10 ;k++)
		{
			luoKuai[h + 1][k] = luoKuai[h][k];
			luoKuai[h][k] = 0;

		}
	}
}

8.下不出界

 

_BOOL_ downNotOut()
{
	for (int k = 0; k < 10; k++)
	{
		if (luoKuai[19][k] != 0)
		{
			return FALSE;
		}
	}
	return TRUE;
}

9.下不阻挡

_BOOL_ downNotStop()
{
	for (int h=18;h>=0 ;h--)
	{
		for (int k = 0; k < 10; k++)
		{
			if (luoKuai[h][k] && shiKuai[h+1][k])
			{
				return FALSE;
			}
		}
	}
	return TRUE;
}

以上就是左移右移上移,左不出界,右不出界,上不出界,左不阻挡,右不阻挡,上不阻挡的代码实现。下面合并,消行,更新,创建新方块的处理。

合并:

void merge()
{
	for (int h = 0; h < 20; h++)
	{
		for (int k = 0; k < 10; k++)
		{
			if (luoKuai[h][k])
			{
				shiKuai[h][k] = luoKuai[h][k];
				luoKuai[h][k] = 0;
			}
		}
	}
}

消除行:

int clearLine()
{
	int count = 0;//记录清除行数
		for (int xh = 0; xh < 20; xh++)
		{
			//检验 本行能否消除
			_BOOL_ neng = TRUE;
			//验证
			for (int k = 0; k < 10; k++)
			{
				if (shiKuai[xh][k] == 0)
				{
					neng = FALSE;
					break;
				}
			}
		if (neng == TRUE)
		{
			count++;
			//消除行
			for (int h = xh - 1; h >= 0; h--)
			{
				for (int k = 0; k < 10; k++)
				{
					shiKuai[h + 1][k] = shiKuai[h][k];
					shiKuai[h][k]=0;
				}
			}

		}
	}
		return count;
}

更新得分:

void updateScore(int lines)
{
	switch (lines)
	{
	case 1:
		score += 20;
		break;
	case 2:
		score += 50;
		break;
	case 3:
		score += 100;
		break;
	case 4:
		score += 150;
		break;

	}
}

游戏自动结束

_BOOL_ isOver()
{
	for (int k = 3; k < 7; k++)
	{
		if (shiKuai[0][k])
		{
			return TRUE;
		}
	}
	return FALSE;
}

创建新方块:

void createNewLuoKuai()
{
	BLOCK_TYPE bt = (BLOCK_TYPE)(rand() % 5);
	color = rand() % 10 + 1;
	switch (bt)
	{
	case L:
		luoKuai[0][3] = color;
		luoKuai[1][3] = color;
		luoKuai[2][3] = luoKuai[2][4]=color;
		break;
	case Z:
		luoKuai[0][3] = luoKuai[0][4] = color;
		luoKuai[1][4] = luoKuai[1][5] = color;
		break;
	case TIAN:
		luoKuai[0][3] = luoKuai[0][4] = color;
		luoKuai[1][3] = luoKuai[1][4] = color;
	case YI:

			luoKuai[0][3] = color;
			luoKuai[1][3] = color;
			luoKuai[2][3] = color;
			luoKuai[3][3] = color;
			break;
	case T:
		luoKuai[0][3] = luoKuai[0][4] = luoKuai[0][5] = color;
		luoKuai[1][4] = color;
		break;

	}
}

 完整代码:

#include "elsfk.h"
void leftMove()
{
	for (int k=1;k<10;k++)
	{
		for (int h=0;h<20 ;h++)
		{
			luoKuai[h][k-1] = luoKuai[h][k];
			luoKuai[h][k] = 0;
		}
	}
}

_BOOL_ leftNotOut()
{
	return TRUE;
}

_BOOL_ leftNotStop()
{
	for (int k=1;k<10 ;k++)
	{
		for (int h=0;h<20 ;h++)
		{
			if (luoKuai[h][k] && shiKuai[h][k - 1])
			{
				return FALSE;
			}
		}
	}
	return TRUE;
}

void rightMove()
{
	for (int k = 8; k>=0; k--)
	{
		for (int h = 0; h < 20; h++)
		{
			luoKuai[h][k + 1] = luoKuai[h][k];
			luoKuai[h][k] = 0;
		}
	}
}

_BOOL_ rightNotOut()
{
	return TRUE;
}

_BOOL_ rightNotStop()
{
	for (int k = 8; k >=0; k--)
	{
		for (int h = 0; h < 20; h++)
		{
			if (luoKuai[h][k] && shiKuai[h][k + 1])
			{
				return FALSE;
			}
		}
	}
	return TRUE;
}

void downMove()
{
	for (int h=18;h>=0 ;h--)
	{
		for (int k=0;k<10 ;k++)
		{
			luoKuai[h + 1][k] = luoKuai[h][k];
			luoKuai[h][k] = 0;

		}
	}
}

_BOOL_ downNotOut()
{
	for (int k = 0; k < 10; k++)
	{
		if (luoKuai[19][k] != 0)
		{
			return FALSE;
		}
	}
	return TRUE;
}

_BOOL_ downNotStop()
{
	for (int h=18;h>=0 ;h--)
	{
		for (int k = 0; k < 10; k++)
		{
			if (luoKuai[h][k] && shiKuai[h+1][k])
			{
				return FALSE;
			}
		}
	}
	return TRUE;
}

void merge()
{
	for (int h = 0; h < 20; h++)
	{
		for (int k = 0; k < 10; k++)
		{
			if (luoKuai[h][k])
			{
				shiKuai[h][k] = luoKuai[h][k];
				luoKuai[h][k] = 0;
			}
		}
	}
}

int clearLine()
{
	int count = 0;//记录清除行数
		for (int xh = 0; xh < 20; xh++)
		{
			//检验 本行能否消除
			_BOOL_ neng = TRUE;
			//验证
			for (int k = 0; k < 10; k++)
			{
				if (shiKuai[xh][k] == 0)
				{
					neng = FALSE;
					break;
				}
			}
		if (neng == TRUE)
		{
			count++;
			//消除行
			for (int h = xh - 1; h >= 0; h--)
			{
				for (int k = 0; k < 10; k++)
				{
					shiKuai[h + 1][k] = shiKuai[h][k];
					shiKuai[h][k]=0;
				}
			}

		}
	}
		return count;
}

void updateScore(int lines)
{
	switch (lines)
	{
	case 1:
		score += 20;
		break;
	case 2:
		score += 50;
		break;
	case 3:
		score += 100;
		break;
	case 4:
		score += 150;
		break;

	}
}

_BOOL_ isOver()
{
	for (int k = 3; k < 7; k++)
	{
		if (shiKuai[0][k])
		{
			return TRUE;
		}
	}
	return FALSE;
}
void createNewLuoKuai()
{
	BLOCK_TYPE bt = (BLOCK_TYPE)(rand() % 5);
	color = rand() % 10 + 1;
	switch (bt)
	{
	case L:
		luoKuai[0][3] = color;
		luoKuai[1][3] = color;
		luoKuai[2][3] = luoKuai[2][4]=color;
		break;
	case Z:
		luoKuai[0][3] = luoKuai[0][4] = color;
		luoKuai[1][4] = luoKuai[1][5] = color;
		break;
	case TIAN:
		luoKuai[0][3] = luoKuai[0][4] = color;
		luoKuai[1][3] = luoKuai[1][4] = color;
	case YI:

			luoKuai[0][3] = color;
			luoKuai[1][3] = color;
			luoKuai[2][3] = color;
			luoKuai[3][3] = color;
			break;
	case T:
		luoKuai[0][3] = luoKuai[0][4] = luoKuai[0][5] = color;
		luoKuai[1][4] = color;
		break;

	}
}

void gameover()
{

}

 

主要思想:二维数组

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值