c++(俄罗斯方块)

切记要用Visual Studio c++,要下载easy-X图形库 

#include "stdafx.h"
#include "tetrisnew.h"
#include<graphics.h>
#include<conio.h>	
#include<stdio.h>
#include<time.h>
 
// 所有方块数组库
const int block[BLOCKTYPE][BLOCKSTYLE][BLOCKSPACE] = 
{
	// 总共有8种方块,每种方块有4种样式
 
	// 'I'形方块
	{
		{
			BLOCKNONE,	BLOCKBLUE,	BLOCKBLUE,	BLOCKBLUE,	BLOCKBLUE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		},
		{
			BLOCKNONE,	BLOCKNONE,	BLOCKBLUE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKBLUE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKBLUE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKBLUE,	BLOCKNONE,	BLOCKNONE
		},
		{
			BLOCKBLUE,	BLOCKBLUE,	BLOCKBLUE,	BLOCKBLUE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		},
		{
			BLOCKNONE,	BLOCKNONE,	BLOCKBLUE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKBLUE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKBLUE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKBLUE,	BLOCKNONE,	BLOCKNONE
		}
	},
	// 'Z'形方块
	{
		{
			BLOCKNONE,	BLOCKGREEN,	BLOCKGREEN,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKGREEN,	BLOCKGREEN,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		},
		{
			BLOCKNONE,	BLOCKNONE,	BLOCKGREEN,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKGREEN,	BLOCKGREEN,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKGREEN,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		},
		{
			BLOCKNONE,	BLOCKGREEN,	BLOCKGREEN,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKGREEN,	BLOCKGREEN,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		},
		{
			BLOCKNONE,	BLOCKNONE,	BLOCKGREEN,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKGREEN,	BLOCKGREEN,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKGREEN,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		}
	},
	// 反'Z'形方块
	{
		{
			BLOCKNONE,	BLOCKNONE,	BLOCKCYAN,	BLOCKCYAN,	BLOCKNONE,
			BLOCKNONE,	BLOCKCYAN,	BLOCKCYAN,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		},
		{
			BLOCKNONE,	BLOCKNONE,	BLOCKCYAN,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKCYAN,	BLOCKCYAN,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKCYAN,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		},
		{
			BLOCKNONE,	BLOCKNONE,	BLOCKCYAN,	BLOCKCYAN,	BLOCKNONE,
			BLOCKNONE,	BLOCKCYAN,	BLOCKCYAN,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		},
		{
			BLOCKNONE,	BLOCKNONE,	BLOCKCYAN,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKCYAN,	BLOCKCYAN,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKCYAN,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		}
	},
	// 'T'形方块
	{
		{
			BLOCKNONE,	BLOCKNONE,	BLOCKRED,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKRED,	BLOCKRED,	BLOCKRED,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		},
		{
			BLOCKNONE,	BLOCKNONE,	BLOCKRED,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKRED,	BLOCKRED,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKRED,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		},
		{
			BLOCKNONE,	BLOCKRED,	BLOCKRED,	BLOCKRED,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKRED,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		},
		{
			BLOCKNONE,	BLOCKNONE,	BLOCKRED,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKRED,	BLOCKRED,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKRED,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		}
	},
	// 'T'形方块 X 2 增加T形方块出现概率
	{
		{
			BLOCKNONE,	BLOCKNONE,	BLOCKRED,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKRED,	BLOCKRED,	BLOCKRED,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		},
		{
			BLOCKNONE,	BLOCKNONE,	BLOCKRED,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKRED,	BLOCKRED,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKRED,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		},
		{
			BLOCKNONE,	BLOCKRED,	BLOCKRED,	BLOCKRED,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKRED,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		},
		{
			BLOCKNONE,	BLOCKNONE,	BLOCKRED,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKRED,	BLOCKRED,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKRED,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		}
	},
	// '田'形方块
	{
		{
			BLOCKNONE,	BLOCKNONE,	BLOCKMAGENTA,BLOCKMAGENTA,BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKMAGENTA,BLOCKMAGENTA,BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		},
		{
			BLOCKNONE,	BLOCKNONE,	BLOCKMAGENTA,BLOCKMAGENTA,BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKMAGENTA,BLOCKMAGENTA,BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		},
		{
			BLOCKNONE,	BLOCKNONE,	BLOCKMAGENTA,BLOCKMAGENTA,BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKMAGENTA,BLOCKMAGENTA,BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		},
		{
			BLOCKNONE,	BLOCKNONE,	BLOCKMAGENTA,BLOCKMAGENTA,BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKMAGENTA,BLOCKMAGENTA,BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		}
	},
	// 'L'形方块
	{
		{
			BLOCKNONE,	BLOCKNONE,	BLOCKBROWN,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKBROWN,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKBROWN,	BLOCKBROWN,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		},
		{
			BLOCKNONE,	BLOCKNONE,	BLOCKBROWN,	BLOCKBROWN,	BLOCKBROWN,
			BLOCKNONE,	BLOCKNONE,	BLOCKBROWN,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		},
		{
			BLOCKNONE,	BLOCKBROWN,	BLOCKBROWN,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKBROWN,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKBROWN,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		},
		{
			BLOCKNONE,	BLOCKNONE,	BLOCKBROWN,	BLOCKNONE,	BLOCKNONE,
			BLOCKBROWN,	BLOCKBROWN,	BLOCKBROWN,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		}
	},
	// 反'L'形方块
	{
		{
			BLOCKNONE,	BLOCKNONE,	BLOCKYELLOW,BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKYELLOW,BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKYELLOW,BLOCKYELLOW,BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		},
		{
			BLOCKNONE,	BLOCKNONE,	BLOCKYELLOW,BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKYELLOW,BLOCKYELLOW,BLOCKYELLOW,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		},
		{
			BLOCKNONE,	BLOCKNONE,	BLOCKYELLOW,BLOCKYELLOW,BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKYELLOW,BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKYELLOW,BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		},
		{
			BLOCKNONE,	BLOCKYELLOW,BLOCKYELLOW,BLOCKYELLOW,BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKYELLOW,BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,
			BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE,	BLOCKNONE
		}
	}
 
};
 
// 游戏实时画面
int tetrisMap[TETRISHEIGHT][TETRISWIDTH] = 
{	
 
};
 
GAME_TETRIS		player;		// 玩家数据
GAME_PREBLOCK	preBlock;	// 预生成方块数据
GAME_MOVE_BLOCK moveBlock;	// 当前控制方块数据
 
int main(void)
{
	tetrisrun();
	return 0;
}
 
void tetrisrun(void)
{
	tetrisInit();
	while(1)
	{
		player.ctime = GetTickCount();
		if( (player.ctime - 500 + 30*player.level) > player.ttime )
		{	
			player.moveFlag = TRUE;
			player.ttime = GetTickCount();
		}
		if(player.moveFlag)
		{
			player.moveFlag = FALSE;
 
			tetrisMoveDown();
			tetrisDraw();
		}
		if(_kbhit())
		{	
			player.key = _getch();
			tetrisKeyHandle();
		}
		if(player.newBlockFlag)
		{
			player.newBlockFlag = FALSE;
			tetrisLoadBlock();
			tetrisRemove();
			tetrisNewBlock();
			tetrisIsOver();
		}
		if(player.gameOverFlag)
		{
			player.gameOverFlag = FALSE;
			break;
		}
 
	}
 
}
 
void tetrisInit(void)
{
	int i,j,k;
	HWND window = initgraph(510, 620);	
	SetWindowText(window, "俄罗斯方块 - by耒阳阿杰");	
	//SetWindowPos(window,HWND_TOPMOST,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);	
 
	rectangle(GAMEMAP1 - 2,GAMEMAP2 - 2,GAMEMAP3 + 2,GAMEMAP4 + 2);		
	rectangle(GAMEMAP5 - 2,GAMEMAP6 - 2,GAMEMAP7 + 2,GAMEMAP8 + 2);		
	rectangle(GAMEGUIDE1 - 5,GAMEGUIDE2 - 5,GAMEGUIDE3 + 5,GAMEGUIDE4 + 5);	
 
	outtextxy(GAMEMAP5 + 10 ,GAMEMAP6 - 26,"下个方块");
	settextcolor(GREEN);
	outtextxy(GAMEGUIDE1 ,GAMEGUIDE2		,"W   :改变形状");
	outtextxy(GAMEGUIDE1 ,GAMEGUIDE2 + 25	,"A    :方块左移");
	outtextxy(GAMEGUIDE1 ,GAMEGUIDE2 + 50	,"S    :方块右移");
	outtextxy(GAMEGUIDE1 ,GAMEGUIDE2 + 75	,"D    :方块下移");
	outtextxy(GAMEGUIDE1 ,GAMEGUIDE2 + 100	,"R    :重新开始");
	outtextxy(GAMEGUIDE1 ,GAMEGUIDE2 + 125	,"ESC:退出游戏");
 
	memset ( &player, 0, sizeof ( GAME_TETRIS ) );
	memset ( &preBlock, 0, sizeof ( GAME_PREBLOCK ) );
	memset(tetrisMap,0,sizeof(tetrisMap));
	player.ttime = GetTickCount();
 
	srand((unsigned)time(NULL));		
	i = (rand() + 1) % BLOCKTYPE;
	j = rand() % BLOCKSTYLE;
	for(k = 0; k < BLOCKSPACE;k++)
	{
		preBlock.tetrisPreBlock[k] = block[i][j][k];
	}
	preBlock.blockType = i;
	preBlock.blockStyle = j;
	tetrisNewBlock();	
}
 
void tetrisDraw(void)
{
	int i,j;
	int x,y;
	char ch[20];
	
	settextstyle(20,12,"宋体");	
	settextcolor(CYAN);			
	if(player.level < 10)
	{
		sprintf_s(ch, "%s%d","LEVEL:", player.level);
	}
	else
	{	
		sprintf_s(ch, "%s","LEVEL:MAX");
	}
	outtextxy(GAMELEVEL1,GAMELEVEL2,ch);
	settextcolor(LIGHTBLUE);
	sprintf_s(ch, "%s%d","RANK:", player.rank);
	outtextxy(GAMERANK1,GAMERANK2,ch);
 
	setfillcolor(BLACK);
	solidrectangle(GAMEMAP5,GAMEMAP6,GAMEMAP7,GAMEMAP8);
 
	for(i = 0; i < BLOCKSPACE; i++)
	{
		switch(preBlock.tetrisPreBlock[i])
		{
			case BLOCKNONE:				
				continue;
				break;
			case BLOCKBLUE:				
				setfillcolor(BLUE);
				break;
			case BLOCKGREEN:			
				setfillcolor(GREEN);
				break;
			case BLOCKCYAN:				
				setfillcolor(CYAN);
				break;
			case BLOCKRED:				
				setfillcolor(RED);
				break;
			case BLOCKMAGENTA:			
				setfillcolor(MAGENTA);
				break;
			case BLOCKBROWN:			
				setfillcolor(BROWN);
				break;
			case BLOCKYELLOW:
				setfillcolor(YELLOW);
				break;
		}
		fillrectangle(GAMEMAP5 + (i%5) * SIDELENGTH ,GAMEMAP6 + (i/5) * SIDELENGTH ,GAMEMAP5 + ((i%5)+1)*SIDELENGTH ,GAMEMAP6 + ((i/5)+1) * SIDELENGTH );		
	}
 
	setfillcolor(BLACK);
	solidrectangle(GAMEMAP1,GAMEMAP2,GAMEMAP3,GAMEMAP4);
 
	for(i = 0; i < 4; i++)
	{
		x = moveBlock.blockSite[i][0];
		y = moveBlock.blockSite[i][1];
		switch(moveBlock.blockColor)
		{
			case BLOCKNONE:
				continue;
				break;
			case BLOCKBLUE:
				setfillcolor(BLUE);
				break;
			case BLOCKGREEN:
				setfillcolor(GREEN);
				break;
			case BLOCKCYAN:
				setfillcolor(CYAN);
				break;
			case BLOCKRED:
				setfillcolor(RED);
				break;
			case BLOCKMAGENTA:
				setfillcolor(MAGENTA);
				break;
			case BLOCKBROWN:
				setfillcolor(BROWN);
				break;
			case BLOCKYELLOW:
				setfillcolor(YELLOW);
				break;
		}
		fillrectangle(GAMEMAP1 + y * SIDELENGTH ,GAMEMAP2 + x * SIDELENGTH ,GAMEMAP1 + (y+1)*SIDELENGTH ,GAMEMAP2 + (x+1) * SIDELENGTH );		
	}
 
	for(i = 0; i < TETRISHEIGHT; i++)
	{
		for(j = 0; j < TETRISWIDTH; j++)
		{	
			switch(tetrisMap[i][j])
			{
				case BLOCKNONE:
					continue;
					break;
				case BLOCKBLUE:
					setfillcolor(BLUE);
					break;
				case BLOCKGREEN:
					setfillcolor(GREEN);
					break;
				case BLOCKCYAN:
					setfillcolor(CYAN);
					break;
				case BLOCKRED:
					setfillcolor(RED);
					break;
				case BLOCKMAGENTA:
					setfillcolor(MAGENTA);
					break;
				case BLOCKBROWN:
					setfillcolor(BROWN);
					break;
				case BLOCKYELLOW:
					setfillcolor(YELLOW);
					break;
			}
			fillrectangle(GAMEMAP1 + j * SIDELENGTH ,GAMEMAP2 + i * SIDELENGTH ,GAMEMAP1 + (j+1)*SIDELENGTH ,GAMEMAP2 + (i+1) * SIDELENGTH );		
		}
	}
}
 
void tetrisNewBlock(void)
{
	int i,j,k;
 
	k = 0;
	for(i = 0; i < BLOCKSPACE; i++)
	{
		if(preBlock.tetrisPreBlock[i])
		{
			moveBlock.blockColor = preBlock.tetrisPreBlock[i];	
			moveBlock.blockSite[k][0] = i/5;					
			moveBlock.blockSite[k][1] = 5 + i%5;				
			k++;
		}
	}
	player.blockType = preBlock.blockType;		
	player.blockStyle = preBlock.blockStyle;	
 
	srand((unsigned)time(NULL));		
	i = rand() % BLOCKTYPE;
	j = rand() % BLOCKSTYLE;
	for(k = 0; k < BLOCKSPACE;k++)
	{
		preBlock.tetrisPreBlock[k] = block[i][j][k];
	}
	preBlock.blockType = i;
	preBlock.blockStyle = j;
}
 
void tetrisMoveUp(void)
{
	int i,k;
	int bStyle;
	int ux,uy;					// 改变方块朝向前后,方块格的位移矢量
	int blocksite2[4][2];		// 记录改变朝向前,4个方块的方块库坐标
	int blocksite3[4][2];		// 记录改变朝向后,4个方块的方块库坐标
	int blocksite4[4][2];		// 记录改变朝向后,4个方块的游戏地图坐标
 
	k = 0;
	for(i = 0; i < BLOCKSPACE; i++)
	{
		if(k > 3)
		{	
			break;
		}
		if(block[player.blockType][player.blockStyle][i])
		{
			blocksite2[k][0] = i / 5;
			blocksite2[k][1] = i % 5;
			k++;
		}
	}
	k = 0;
	bStyle = (player.blockStyle + 1)%BLOCKSTYLE;	
	for(i = 0; i < BLOCKSPACE; i++)
	{
		if(k > 3)
		{	
			break;
		}
		if(block[player.blockType][bStyle][i])
		{
			blocksite3[k][0] = i / 5;
			blocksite3[k][1] = i % 5;
			k++;
		}
	}
	for(i = 0; i < 4; i++)
	{
		ux = blocksite3[i][0] - blocksite2[i][0];	
		uy = blocksite3[i][1] - blocksite2[i][1];	
 
		blocksite4[i][0] = moveBlock.blockSite[i][0] + ux;	
		blocksite4[i][1] = moveBlock.blockSite[i][1] + uy;	
		
		if( (blocksite4[i][0] < 0)					
			|| (blocksite4[i][1] < 0)
			|| (blocksite4[i][0] > TETRISHEIGHT - 1)
			|| (blocksite4[i][1] > TETRISWIDTH - 1)
			|| ( tetrisMap[blocksite4[i][0]][blocksite4[i][1]] ) )	
		{
			return;
		}
	}
	for(i = 0;i < 4; i++)
	{
		moveBlock.blockSite[i][0] = blocksite4[i][0];
		moveBlock.blockSite[i][1] = blocksite4[i][1];
	}
	player.blockStyle = bStyle;	
 
}
 
void tetrisMoveDown(void)
{
	int i;
	int x,y;
 
	for(i = 0; i < 4; i++)
	{
		if(moveBlock.blockSite[i][0] > TETRISHEIGHT - 2)
		{
			player.newBlockFlag = TRUE;
			return;
		}
	}
	for(i = 0; i < 4; i++)
	{
		x = moveBlock.blockSite[i][0] + 1;	
		y = moveBlock.blockSite[i][1];		
		if(tetrisMap[x][y])
		{
			player.newBlockFlag = TRUE;
			return;
		}
	}
	for(i = 0; i < 4; i++)
	{
		moveBlock.blockSite[i][0]++;
	}
 
}
 
void tetrisMoveLeft(void)
{
	int i;
	int x,y;
 
	for(i = 0; i < 4; i++)
	{
		x = moveBlock.blockSite[i][0];
		y = moveBlock.blockSite[i][1];
		if( (y - 1) < 0				
			|| tetrisMap[x][y - 1])	
		{
			return;	
		}
	}
	for(i = 0; i < 4; i++)
	{
		moveBlock.blockSite[i][1]--;
	}
 
}
 
void tetrisMoveRight(void)
{
	int i;
	int x,y;
	//k = 0;
	for(i = 0; i < 4; i++)
	{
		x = moveBlock.blockSite[i][0];
		y = moveBlock.blockSite[i][1];
		if( ((y + 1) >= TETRISWIDTH)	
			|| (tetrisMap[x][y + 1]) )	
		{
			return;	
		}
	}
	for(i = 0; i < 4; i++)
	{
		moveBlock.blockSite[i][1]++;
	}
 
}
 
void tetrisLoadBlock(void)
{
	int i;
	int x,y;
 
	for(i = 0; i < 4; i++)
	{
		x = moveBlock.blockSite[i][0];
		y = moveBlock.blockSite[i][1];
		if(!tetrisMap[x][y])
		{
			tetrisMap[x][y] = moveBlock.blockColor;
		}
	}
}
 
void tetrisIsOver(void)
{
	int i;
	int x,y;
 
	for(i = 0; i < 4; i++)
	{
		x = moveBlock.blockSite[i][0];
		y = moveBlock.blockSite[i][1];
		if(tetrisMap[x][y])
		{
			player.gameOverFlag = TRUE;
			tetrisDraw();
			tetrisQuit();
			return;
		}
	}
}
 
void tetrisQuit(void)
{
	int key,flag;
	flag = player.gameOverFlag;
	key = MessageBox(NULL,"是否退出游戏?","提示",MB_YESNO| MB_SYSTEMMODAL);
	switch(key)
	{
		case IDYES:
			player.gameOverFlag = TRUE;
			break;
		case IDNO:
			player.gameOverFlag = FALSE;
			break;
		default:
			break;
	}
	while(flag
		&& !player.gameOverFlag)
	{
		key = MessageBox(NULL,"检测到游戏已无法继续,\n请选择\"是\": 退出游戏;\n或者选择\"否\": 重启游戏;","提示",MB_YESNO| MB_SYSTEMMODAL);
		switch(key)
		{
			case IDYES:
				player.gameOverFlag = TRUE;
				break;
			case IDNO:
				tetrisInit();
				player.gameOverFlag = FALSE;
				return;
			default:
				break;
		}
	}
	CLEARKEY();	
}
 
void tetrisKeyHandle(void)
{
	switch(player.key)
	{
		case 'w':
		case 'W':
			tetrisMoveUp();	
			break;
		case 'a':
		case 'A':
			tetrisMoveLeft();
			break;
		case 's':
		case 'S':
			player.moveFlag = TRUE;
			player.ttime = GetTickCount();
			return;
		case 'd':
		case 'D':
			tetrisMoveRight();
			break;
		case ESC:
			tetrisQuit();
			break;
		case 'r':
		case 'R':
			tetrisReset();
			break;
		default:
			break;
	}
	tetrisDraw();
}
 
void tetrisReset(void)
{
	int key;
	key = MessageBox(NULL,"是否重新开始游戏?","提示",MB_YESNO| MB_SYSTEMMODAL);
	CLEARKEY();	
	switch(key)
	{
		case IDYES:
			tetrisInit();
			break;
		case IDNO:
			break;
		default:
			break;
	}
}
 
void tetrisRemove(void)
{
	int i,j,m,n;
	int flag,bnum;
	bnum = 0;
 
	for(i = TETRISHEIGHT - 1; i >= 0; i-- )
	{
		flag = 0;
		for(j = 0; j < TETRISWIDTH; j++)
		{
			if(!tetrisMap[i][j])
			{
				flag = 1;
				break;
			}
		}
		if(flag)
		{
			continue;
		}
		bnum++;		
		for(j = 0; j < TETRISWIDTH; j++)
		{
			tetrisMap[i][j] = BLOCKNONE;
		}
		for(m = i; m > 0; m--)
		{
			for(n = 0; n < TETRISWIDTH; n++)
			{
				tetrisMap[m][n] = tetrisMap[m - 1][n];
			}
		}
		i++;		
	}
 
	if(!bnum)
	{
		return;
	}
	switch(bnum)
	{
		case 0:
			break;
		case 1:
			player.rank += 10;
			break;
		case 2:
			player.rank += 20;
			break;
		case 3:
			player.rank += 40;
			break;
		case 4:
			player.rank += 80;
			break;
		default:
			break;
	}
	player.level = player.rank/100;
	if(player.level > 10)
	{
		player.level = 10;
	}
 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值