eluosi.c

来自「C语言编写的俄罗斯方块游戏.」· C语言 代码 · 共 763 行 · 第 1/2 页

C
763
字号
	Speed.y=80;
	Speed.w=56;
	Speed.h=8;
	GetImage(Speed.x,Speed.y,Speed.w,Speed.h,Speed.mask);
	Mode.n=0;
	Mode.x=8;
	Mode.y=100;
	Mode.w=56;
	Mode.h=8;
	GetImage(Mode.x,Mode.y,Mode.w,Mode.h,Mode.mask);
}
void DrawBK(void)	/* 画游戏区背景 */
{
	int i,j;
	DrawImage(START_X,START_Y,120,180,game_info.mask);
	for(i=1;i<BK_Y-2;i++)
		for(j=2;j<BK_X-3;j++)
			if(game_info.bk[i][j]>=0)
				DrawImage(START_X+(j-2)*10,START_Y+(i-1)*10,10,10,
					  squ_image[game_info.bk[i][j]]);
}
void DrawThisSquare(void)	/* 画主方块 */
{
	int i,j;
	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
			if(squ_temp[ThisSqu.squ_num][ThisSqu.shape][i][j]==1)
				DrawImage(START_X+(ThisSqu.x+j)*10,
				          START_Y+(ThisSqu.y+i)*10,
				          10,10,
					  squ_image[ThisSqu.squ_num]);
}
void DrawNextSquare(void)	/* 画次方块 */
{
	int i,j;
	DrawImage(NextSqu.x,NextSqu.y,NextSqu.w,NextSqu.h,NextSqu.mask);
	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
			if(squ_temp[NextSqu.squ_num][NextSqu.shape][i][j]==1)
				DrawImage(NextSqu.x+j*10,
					  NextSqu.y+i*10,
					  10,10,
					  squ_image[NextSqu.squ_num]);
}
int IsRoadblock(int x,int y)	/* 判断主方块是否遇到障碍 */
{
	int i,j;
	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
			if(squ_temp[ThisSqu.squ_num][ThisSqu.shape][i][j]==1&&game_info.bk[y+i+1][x+j+2]!=-1)
				return 1;
	return 0;
}
void SquareIntoBK(void)	/* 将主方块放入背景 */
{
	int i,j;
	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
			if(squ_temp[ThisSqu.squ_num][ThisSqu.shape][i][j]==1)
				game_info.bk[ThisSqu.y+i+1][ThisSqu.x+j+2]=ThisSqu.squ_num;
}
void SquDownSound(void)
{
	if(game_info.music)
	{
		sound(400);
		delay(100);
		nosound();
	}
}
void SquLineSound(void)
{
	if(game_info.music)
	{
		sound(800);
		delay(100);
		sound(900);
		delay(50);
		sound(1000);
		delay(100);
		nosound();
	}
}
void AddNumeric(int sco)	/* 游戏计分及速度升级 */
{
	switch(sco)
	{
		case 1:
			Score.n+=100;
			break;
		case 2:
			Score.n+=200;
			break;
		case 3:
			Score.n+=400;
			break;
		case 4:
			Score.n+=800;
			break;
	}
	Line.n+=sco;
	PrintNumber(&Score);
	PrintNumber(&Line);
	if(Line.n>=game_info.k*MAX_LINE)
	{
		if(++Speed.n==MAX_SPEED)
			Speed.n=MAX_SPEED;
		game_info.k++;
		PrintNumber(&Speed);
	}
	SquLineSound();
}
int IsLine(void)	/* 判断是否成行 */
{
	int y=BK_Y-3,x,y1,s;
	static int sco=0;
	while(y>0)
	{
		s=0;
		for(x=2;x<BK_X-3;x++)
			if(game_info.bk[y][x]>=0)
				s++;
		if(s==12)
		{
			for(y1=y;y1>0;y1--)
				for(x=2;x<BK_X-3;x++)
				{
					if(y1==1)
						game_info.bk[y1][x]=-1;
					else
						game_info.bk[y1][x]=game_info.bk[y1-1][x];
				}
			sco++;
			return 1;
		}
		y--;
	}
	if(sco!=0)
		AddNumeric(sco);
	sco=0;
	return 0;
}
void WaitForEnter(void)	/* 游戏开始等待游戏者输入选择 */
{
	while(1)
	{
		if(GetKey(KEY_ESC))
			break;
		if(GetKey(KEY_ENTER))
			return;
	}
	ShutDownKeyboard();
	DeleteImage(squ_image,NUM_SQU);
	DeletePCX(&pcx_pic1);
	DeletePCX(&pcx_pic2);
	SetVideoMode(0x03);
	exit(0);
}
void SaveGame(void)	/* 保存游戏数据 */
{
	FILE *fp;
	if((fp=fopen("tetris.dat","wb"))==NULL)
		return;
	fwrite(&game_info,sizeof(GameInfo),1,fp);
	fwrite(&ThisSqu,sizeof(SquInfo),1,fp);
	fwrite(&NextSqu,sizeof(SquInfo),1,fp);
	fwrite(&Score,sizeof(NumberInfo),1,fp);
	fwrite(&Level,sizeof(NumberInfo),1,fp);
	fwrite(&Speed,sizeof(NumberInfo),1,fp);
	fwrite(&Mode,sizeof(NumberInfo),1,fp);
	fclose(fp);
}
void LoadGame(void)	/* 读取游戏数据 */
{
	FILE *fp;
	if((fp=fopen("tetris.dat","rb"))==NULL)
		return;
	fread(&game_info,sizeof(GameInfo),1,fp);
	fread(&ThisSqu,sizeof(SquInfo),1,fp);
	fread(&NextSqu,sizeof(SquInfo),1,fp);
	fread(&Score,sizeof(NumberInfo),1,fp);
	fread(&Level,sizeof(NumberInfo),1,fp);
	fread(&Speed,sizeof(NumberInfo),1,fp);
	fread(&Mode,sizeof(NumberInfo),1,fp);
	fclose(fp);
	PrintNumber(&Score);
	PrintNumber(&Line);
	PrintNumber(&Level);
	PrintNumber(&Speed);
	PrintNumber(&Mode);
}
void RandLevel(void)	/* 随机初始化游戏水平 */
{
	int i,j,m,n;
	Speed.n=0;
	Score.n=0;
	Line.n=0;
	game_info.k=1;
	for(i=0;i<BK_Y;i++)
		for(j=0;j<BK_X;j++)
			game_info.bk[i][j]=bk_temp[i][j];
	for(i=BK_Y-3;i>BK_Y-3-Level.n;i--)
	{
		n=0;
		for(j=2;j<BK_X-3;j++)
		{
			m=random(game_info.cur_squ_num+1)-1;
			if(m!=-1) n++;
			if(n<BK_X-5)
				game_info.bk[i][j]=m;
		}
	}
}
void main(void)	/* 主控函数 */
{
	int isend;
	InstallKeyboard();
	randomize();
	SetVideoMode(0x13);
	CreateDoubleBuffer();
	SetActiveBuffer(DOUBLE_BUFFER);
	InitPCX(&pcx_pic1,320,200);
	InitPCX(&pcx_pic2,320,200);
	LoadPCX("squ.pcx",&pcx_pic1,0);
	InitImage(squ_image,10,10,NUM_SQU);
	LoadImage(&pcx_pic1,squ_image,10,10,NUM_SQU,0,0);
	while(1)
	{
		InitGameData();
		DrawImage(0,0,320,200,pcx_pic1.buffer);
		ShowDoubleBuffer();
		WaitForEnter();
		DrawImage(0,0,320,200,pcx_pic2.buffer);
		PrintString(8,10,"SCORE:",218,0,0);
		PrintNumber(&Score);
		PrintString(8,30,"LINE:",218,0,0);
		PrintNumber(&Line);
		PrintString(8,50,"LEVEL:",218,0,0);
		PrintNumber(&Level);
		PrintString(8,70,"SPEED:",218,0,0);
		PrintNumber(&Speed);
		PrintString(8,90,"MODE:",218,0,0);
		PrintNumber(&Mode);
		PrintString(245,10,"NEXT:",218,0,0);
		isend=0;
		do
		{
			ThisSqu.x=5;
			ThisSqu.y=0;
			game_info.disp_clock=MAX_SPEED-Speed.n-1;
			DrawBK();
			DrawNextSquare();
			while(1)
			{
				GetImage(START_X+ThisSqu.x*10,START_Y+ThisSqu.y*10,ThisSqu.w,ThisSqu.h,ThisSqu.mask);
				DrawThisSquare();
				ShowDoubleBuffer();
				Wait(2);
				if(IsRoadblock(ThisSqu.x,ThisSqu.y))
				{
					PrintString(124,76,"GAME OVER",118,0,1);
					ShowDoubleBuffer();
					while(!GetKey(KEY_ENTER));
					ClearKey(KEY_ENTER);
					ClearKey(KEY_ESC);
					isend=1;
					break;
				}
				DrawImage(START_X+ThisSqu.x*10,START_Y+ThisSqu.y*10,ThisSqu.w,ThisSqu.h,ThisSqu.mask);
				if(GetKey(KEY_ESC))
				{
					ClearKey(KEY_ESC);
					isend=1;
					break;
				}
				if(GetKey(KEY_RIGHT))
				{
					if(!IsRoadblock(ThisSqu.x+1,ThisSqu.y))
						ThisSqu.x++;
				}
				if(GetKey(KEY_LEFT))
				{
					if(!IsRoadblock(ThisSqu.x-1,ThisSqu.y))
						ThisSqu.x--;
				}
				if(GetKey(KEY_UP))
				{
					if(++ThisSqu.shape>3)
						ThisSqu.shape=0;
					if(IsRoadblock(ThisSqu.x,ThisSqu.y))
					{
						if(--ThisSqu.shape<0)
							ThisSqu.shape=3;
					}
				}
				if(GetKey(KEY_SPACE))
				{
					if(--ThisSqu.shape<0)
						ThisSqu.shape=3;
					if(IsRoadblock(ThisSqu.x,ThisSqu.y))
					{
						if(++ThisSqu.shape>3)
							ThisSqu.shape=0;
					}
				}
				if(GetKey(KEY_S))
				{
					game_info.music=1-game_info.music;
				}
				if(GetKey(KEY_F1))
				{
					SaveGame();
					break;
				}
				if(GetKey(KEY_F2))
				{
					LoadGame();
					break;
				}
				if(GetKey(KEY_F3))
				{
					if(++Level.n==MAX_LEVEL)
						Level.n=0;
					RandLevel();
					PrintNumber(&Level);
					break;
				}
				if(GetKey(KEY_F4))
				{
					if(++Speed.n==MAX_SPEED)
						Speed.n=0;
					PrintNumber(&Speed);
					break;
				}
				if(GetKey(KEY_F5))
				{
					if(++Mode.n==MAX_MODE)
						Mode.n=0;
					PrintNumber(&Mode);
					switch(Mode.n)
					{
						case 0:
							game_info.cur_squ_num=7;
							break;
						case 1:
							game_info.cur_squ_num=11;
							break;
						case 2:
							game_info.cur_squ_num=15;
							break;
					}
					break;
				}
				if(GetKey(KEY_P))
				{
					while(!GetKey(KEY_ENTER));
					ClearKey(KEY_ENTER);
				}
				if(GetKey(KEY_DOWN)||--game_info.disp_clock<0)
				{
					ThisSqu.y++;
					game_info.disp_clock=MAX_SPEED-Speed.n-1;
					if(IsRoadblock(ThisSqu.x,ThisSqu.y))
					{
						SquDownSound();
						ThisSqu.y--;
						SquareIntoBK();
						while(IsLine());
						ThisSqu.squ_num=NextSqu.squ_num;
						ThisSqu.shape=NextSqu.shape;
						NextSqu.squ_num=random(game_info.cur_squ_num);
						NextSqu.shape=random(4);
						break;
					}
				}
			}
		}while(!isend);
	}
}

⌨️ 快捷键说明

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