俄罗斯方块源代码
#include<stdio.h>
#include<stdlib.h>
#include<dos.h>
#include<graphics.h> /*系统提供的头文件*/
#define TIMER 0x1c /*定义时钟中断的中断号*/
#define VK_LEFT 0x4b00/*左移键*/
#define VK_RIGHT 0x4d00/*右移键*/
#define VK_DOWN 0x5000 /*加速键*/
#define VK_UP 0x4800 /*变形键*/
#define VK_SPACE 0x3920 /*变形键*/
#define VK_END 0x4f00 /*暂停键*/
#define VK_ESC 0x011b
#define VK_ENTER 0x1c0d
#define BSIZE 16 /*方块的边长是16个象素*/
#define MAX_SHAPE 19 /*总共有19种各形态的方块*/
#define BOARD_WIDTH 10 /*游戏面板的宽度,以方块的宽度为单位*/ #define
BOARD_HEIGHT 20/*游戏面板的高度,以方块的宽度为单位*/
#define BGCOLOR BLACK /*背景色*/
#define FORECOLOR WHITE /*前景色*/
#define FALSE 0
#define TRUE 1
#define EMPTY 0
#define FILLED 1
#define BOARD_LEFT_X 10 /*游戏面板左上角的横坐标*/
#define BOARD_LEFT_Y 5 /*游戏面板左上角的纵坐标*/
/*定义全局变量*/
extern int Gameboard[BOARD_WIDTH+2][BOARD_HEIGHT+2];
extern int nCurrent_block_index ; /*当前下落的方块的索引号*/ extern int
nNext_block_index ; /*下一个方块的索引号*/
extern int nSpeed, nScore; /*速度和得分*/
extern int nSpeedUpScore; /*第一次要加速需达到的分数*/ extern int bAccel, bOver;
extern int nOriginX, nOriginY;/*某一形状的原点的绝对坐标*/
extern unsigned int TimerCounter; /* 计时变量,每秒钟增加18 */
struct block{
int arrXY[8];
int nColor;
int nNext;
}; /*保存某一形状信息的结构体*/
typedef struct block BLOCK;
extern BLOCK arrayBlock[19];
void interrupt newtimer(void);
void SetTimer(void interrupt(*IntProc)(void));
void KillTimer();
void InitializeGraph();
void InitializeGameboard() ;
void DrawSquare(int x, int y);
void DrawBlock(int BlockIndex, int sx, int sy,int color);
int IsConflict(int BlockIndex, int x, int y);
void HandleLeft(int BlockIndex,int *x, int *y);
void HandleRight(int BlockIndex,int *x, int *y);
void HandleUp(int *BlockIndex,int *x, int *y);
int HandleDown(int BlockIndex,int *x, int *y);
int IsLineFull(int y);
void KillLine(int y);
int KillLines(int y);
int IsGameOver();
int GameOver();
void StartGame();
void ProcessInGame();
/**********************************************************
* 函数原型:void InitializeGraph() * *
传入参数:无 * * 返 回 值:无 * * 函数功能:初始化进入
*
**********************************************************/
void InitializeGraph()
{
int gdriver = VGA, gmode=VGAHI, errorcode;
/* 初始化图形模式*/
initgraph(&gdriver, &gmode, "c:\\turboc2");
形模式图
/* 读取初始化结果 */
errorcode = graphresult();
if (errorcode != grOk) /* 错误发生 */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* 返回错误码 */
}
}
/**********************************************************
* 函数原型:void InitializeGameboard() *
* 传入参数:无 *
* 返 回 值:无 *
* 函数功能:初始化游戏面板以及下一形状提示框、计分框和难度框 *
**********************************************************/
void InitializeGameboard()
{
/* 绘制游戏面板(即游戏区域)*/
setfillstyle(SOLID_FILL,BGCOLOR);
bar(BSIZE*BOARD_LEFT_X,BSIZE*BOARD_LEFT_Y,BSIZE*(BOARD_LEFT_X+BOARD_WIDTH),BSIZE
*(BOARD_LEFT_Y+BOARD_HEIGHT));
setcolor(WHITE);
rectangle(BSIZE*BOARD_LEFT_X,BSIZE*BOARD_LEFT_Y,BSIZE*(BOARD_LEFT_X+BOARD_WIDTH)
,BSIZE*(BOARD_LEFT_Y+BOARD_HEIGHT));
/*绘制下一形状提示框*/
setcolor(BLUE);
settextjustify(CENTER_TEXT, BOTTOM_TEXT);
outtextxy(BSIZE*(25+4), BSIZE*(5+1), "next");
setfillstyle(SOLID_FILL, BGCOLOR);
bar(BSIZE*(24.5+2), BSIZE*6, BSIZE*(24.5+2+5), BSIZE*(6+5));
setcolor(YELLOW);
rectangle(BSIZE*(24.5+2), BSIZE*6, BSIZE*(24.5+2+5), BSIZE*(6+5));
/*绘制速度框*/
setcolor(BLUE);
settextjustify(CENTER_TEXT, BOTTOM_TEXT);
outtextxy(BSIZE*(25+4), BSIZE*(12+1), "level"); setfillstyle(SOLID_FILL,
BGCOLOR);
bar(BSIZE*25,BSIZE*13, BSIZE*(25+8), BSIZE*(13+1)); setcolor(YELLOW);
rectangle(BSIZE*25,BSIZE*13, BSIZE*(25+8), BSIZE*(13+1)); setcolor(RED);
settextjustify(CENTER_TEXT, BOTTOM_TEXT);
outtextxy(BSIZE*(25+4), BSIZE*(13+1), "0");
/*绘制计分框*/
setcolor(BLUE);
settextjustify(CENTER_TEXT, BOTTOM_TEXT);
outtextxy(BSIZE*(25+4), BSIZE*(19+1), "score"); setfillstyle(SOLID_FILL, BGCOLOR);
bar(BSIZE*25,BSIZE*20, BSIZE*(25+8), BSIZE*(20+1)); setcolor(YELLOW);
rectangle(BSIZE*25,BSIZE*20, BSIZE*(25+8), BSIZE*(20+1)); setcolor(RED);
settextjustify(CENTER_TEXT, BOTTOM_TEXT);
outtextxy(BSIZE*(25+4), BSIZE*(20+1), "0");
}
int Gameboard[BOARD_WIDTH+2][BOARD_HEIGHT+2];
int nCurrent_block_index;/* 当前下落的方块的索引号*/ int nNext_block_index ; /*下一个方
块的索引号*/
int nSpeed, nScore; /*速度和得分*/
int nSpeedUpScore = 1000; /*第一次要加速需达到的分数*/ int bAccel, bOver;
int nOriginX=5, nOriginY=1;/*某一形状的原点的绝对坐标*/ BLOCK arrayBlock[19]={
/*x1,y1,x2,y2,x3,y3,x4,y4, color, next*/
{ 0,-2, 0,-1, 0, 0, 1, 0, CYAN, 1}, /* */ {-1, 0, 0, 0, 1,-1, 1, 0, CYAN,
2}, /* # */ { 0,-2, 1,-2, 1,-1, 1, 0, CYAN, 3}, /* # */ {-1,-1,-1, 0, 0,-1, 1,-1, CYAN, 0}, /* ## */
{ 0,-2, 0,-1, 0, 0, 1,-2,MAGENTA, 5}, /* */ {-1,-1,-1, 0, 0, 0, 1, 0,MAGENTA,
6}, /* ## */ { 0, 0, 1,-2, 1,-1, 1, 0,MAGENTA, 7}, /* # */ {-1,-1, 0,-1, 1,-1, 1, 0,MAGENTA, 4}, /* # */
{-1, 0, 0,-1, 0, 0, 1, 0,YELLOW, 9}, /* */ {-1,-1, 0,-2, 0,-1, 0, 0,YELLOW, 10}, /* */
{-1,-1, 0,-1, 0, 0, 1,-1,YELLOW, 11}, /* # */
{ 0,-2, 0,-1, 0, 0, 1,-1,YELLOW, 8}, /* ### */
{-1, 0, 0,-1, 0, 0, 1,-1, BROWN, 13}, /* ## */
{ 0,-2, 0,-1, 1,-1, 1, 0, BROWN, 12}, /* ## */
{-1,-1, 0,-1, 0, 0, 1, 0, WHITE, 15}, /* ## */
{ 0,-1, 0, 0, 1,-2, 1,-1, WHITE, 14}, /* ## */
{ 0,-3, 0,-2, 0,-1, 0, 0, RED, 17},/* # */
{-1, 0, 0, 0, 1, 0, 2, 0, RED, 16},/* # */
/* # */ /* # */
{ 0,-1, 0, 0, 1,-1, 1, 0, BLUE, 18},/* ## */
/* ## */ };
/**********************************************************
* 函数原型:void StartGame *
* 传入参数:
*
* 返 回 值:无
* * 函数功能:游戏开始时调用的函数,其中绘制界面需调用函数 *
* InitializeGameboard(), 接下来需初始化游戏面板的 *
* 各个方块和一些全局变量的初值 *
**********************************************************/
void StartGame()
{
int i,j;
/*设置游戏面板中每个方块的初始值*/
for(j=0;j<=BOARD_HEIGHT;j++)
for(i=0;i<BOARD_WIDTH+2;i++)
{
if(i==0 || i==BOARD_WIDTH+1)
Gameboard[i][j] = FILLED;
else
Gameboard[i][j] = EMPTY;
}
for(i=0;i<BOARD_WIDTH+2;i++)
Gameboard[i][BOARD_HEIGHT+1] = FILLED;
InitializeGameboard(); () 无
/*设置游戏变量的初值*/
nNext_block_index = -1; /*游戏初始,没有下一个形状的索引号*/
nSpeed = 0;
nScore = 0;
}
/**********************************************************
* 函数原型:void ProcessInGame() *
* 传入参数:无 *
* 返 回 值:无 *
* 函数功能:核心函数,主要用于处理在游戏中的各种事件(如按下各种按键) *
**********************************************************/
void ProcessInGame()
{
int key;
bioskey(0);
randomize();
while(1)
{
if(nNext_block_index==-1)
{
nCurrent_block_index = rand()%19;
nNext_block_index = rand()%19;
/*绘制下一个提示形状*/
DrawBlock(nNext_block_index,
19,6,arrayBlock[nNext_block_index].nColor );
}
else
{
nCurrent_block_index = nNext_block_index;
DrawBlock(nNext_block_index, 19,6,BGCOLOR ); /* 消除原来的提示形状 */
nNext_block_index = rand()%19;
DrawBlock(nNext_block_index,
19,6,arrayBlock[nNext_block_index].nColor ); /*绘制下一个提示形状 */
}
nOriginX=5, nOriginY=1;
TimerCounter = 0;
DrawBlock(nCurrent_block_index, nOriginX,nOriginY,
arrayBlock[nCurrent_block_index].nColor );/*在面板函数原型:void main()
*
* 传入参数:无 *
* 返 回 值:无 *
* 函数功能:入口函数,包含俄罗斯方块程序的主
*
**********************************************************/
void main()
{
InitializeGraph();
SetTimer(newtimer); /*设置新的时钟中断*/
while(1)
{
StartGame();
ProcessInGame();
if(GameOver())
break;
bOver = FALSE;
}
KillTimer();
closegraph();
}
unsigned int TimerCounter=0; /* 计时变量,每秒钟增加18 */
/**********************************************************
* 函数原型:void interrupt (*oldtimer)(void) *
* 传入参数:无 *
* 返 回 值:无 *
* 函数功能:指向原来时钟中断处理过程入口的中断处理函数指针(句柄) *
**********************************************************/
void interrupt (*oldtimer)(void);
/**********************************************************
* 函数原型:void interrupt newtimer(void) *
* 传入参数:无 *
* 返 回 值:无 *
* 函数功能:新的时钟中断处理函数
*
**********************************************************/
void interrupt newtimer(void)
{
(*oldtimer)();
TimerCounter++;
}
/**********************************************************
* 函数原型:void SetTimer(void interrupt(*)(void)) *
* 传入参数:无 *
* 返 回 值:无 *
* 函数功能:设置新的时钟中断处理函数 *
**********************************************************/
void SetTimer(void interrupt(*IntProc)(void))
{
oldtimer=getvect(TIMER);
disable();
setvect(TIMER,IntProc);
enable();
}
/**********************************************************
* 函数原型:void KillTimer() *
* 传入参数:无 *
* 返 回 值:无 *
* 函数功能:恢复原先的时钟中断处理函数 *
**********************************************************/
void KillTimer()
{
disable();
setvect(TIMER,oldtimer);
enable();
}
/**********************************************************
* 函数原型:void DrawSquare(int x, int y) *
* 传入参数:游戏面板中的横坐标x,纵坐标y *
* 返 回 值:无 *
* 函数功能:在坐标(x, y)处绘制方块 *
**********************************************************/
void DrawSquare(int x, int y)
{
if(y<1)
return;
bar(BSIZE*(x+9)+1,BSIZE*(y+4)+1,BSIZE*(x+10)-1,BSIZE*(y+5)-1);
}
/**********************************************************
* 函数原型:void DrawBlock(int BlockIndex, int sx, int sy,int color) *
* 传入参数:形状的索引BlockIndex,绝对横坐标x,绝对纵坐标y,颜色color *
* 返 回 值:无 *
* 函数功能:在坐标(sx, sy)处绘制颜色为color的形状 *
**********************************************************/
void DrawBlock(int BlockIndex, int sx, int sy,int color)
{
int i,c;
setfillstyle(SOLID_FILL, color);
for(i=0;i<7;i+=2)
DrawSquare(arrayBlock[BlockIndex].arrXY[i]+sx,
arrayBlock[BlockIndex].arrXY[i+1]+sy);
}
/**********************************************************
* 函数原型:int IsConflict(int BlockIndex, int x, int y) *
* 传入参数:形状的索引BlockIndex,绝对横坐标x,绝对纵坐标y *
* 返 回 值:无冲突返回0,有冲突返回1 *
* 函数功能:判断形状是否能存在于坐标(x, y)处 *
**********************************************************/
int IsConflict(int BlockIndex, int x, int y)
{
int i;
for (i=0;i<=7;i++,i++)
{
if (arrayBlock[BlockIndex].arrXY[i]+x<1 ||
arrayBlock[BlockIndex].arrXY[i]+x>10)
return TRUE;
if (arrayBlock[BlockIndex].arrXY[i+1]+y<1)
continue;
if
(Gameboard[arrayBlock[BlockIndex].arrXY[i]+x][arrayBlock[BlockIndex].arrXY[i+1]+
y])
return TRUE;
}
return FALSE;
}
/**********************************************************
* 函数原型:int HandleLeft(int BlockIndex,int *x, int *y) *
* 传入参数:形状的索引BlockIndex,绝对横坐标的指针*x,绝对纵坐标的 *
* 指针*y *
* 返 回 值:无 *
* 函数功能:按下左方向键时的处理函数 *
**********************************************************/
void HandleLeft(int BlockIndex,int *x, int *y) /*按下左方向键时的处理函数*/
{
if(!IsConflict(BlockIndex,*x-1,*y))
{
DrawBlock(BlockIndex,*x,*y,BGCOLOR); /*擦除原先的形状*/
(*x)--;
DrawBlock(BlockIndex, *x, *y, arrayBlock[BlockIndex].nColor); /*绘制当前
形状*/
}
}
/**********************************************************
* 函数原型:int HandleRight(int BlockIndex,int *x, int *y) *
* 传入参数:形状的索引BlockIndex,绝对横坐标的指针*x,绝对纵坐标的 *
* 指针*y *
* 返 回 值:无 *
* 函数功能:按下右方向键时的处理函数 *
**********************************************************/
void HandleRight(int BlockIndex,int *x, int *y)/*按下右方向键时的处理函数*/
{
if(!IsConflict(BlockIndex,*x+1,*y))
{
DrawBlock(BlockIndex,*x,*y,BGCOLOR); /*擦除原先的形状*/
(*x)++;
DrawBlock(BlockIndex, *x, *y, arrayBlock[BlockIndex].nColor); /*绘制当前
形状*/
}
}
/**********************************************************
* 函数原型:int HandleUp(int BlockIndex,int *x, int *y) *
* 传入参数:形状的索引BlockIndex,绝对横坐标的指针*x,绝对纵坐标的 *
* 指针*y *
* 返 回 值:无 *
* 函数功能:按下上方向键(旋转键)时的处理函数 *
**********************************************************/
void HandleUp(int *BlockIndex,int *x, int *y) /*按下旋转键时的处理函数*/
{
int NextBlockIndex, i;
static int arrayOffset[5]={0,-1,1,-2,2};
NextBlockIndex = arrayBlock[*BlockIndex].nNext;
for(i=0;i<5;i++)
if(!IsConflict(NextBlockIndex, *x+arrayOffset[i],*y))
{
DrawBlock(*BlockIndex, *x, *y, BGCOLOR); /*擦除原先的形状*/
*BlockIndex = arrayBlock[*BlockIndex].nNext;
(*x) += arrayOffset[i];
DrawBlock(*BlockIndex, *x, *y, arrayBlock[*BlockIndex].nColor); /*绘制当
前形状*/
}
}
/**********************************************************
* 函数原型:int HandleDown(int BlockIndex,int *x, int *y) *
* 传入参数:形状的索引BlockIndex,绝对横坐标的指针*x,绝对纵坐标的 *
* 指针*y *
* 返 回 值:仍在自由下落返回0,无法下落了返回1 *
* 函数功能:按下向下方向键或自由下落时的处理函数 *
**********************************************************/
int HandleDown(int BlockIndex,int *x, int *y)/*按下下方向键或自由下落时的处理函数
*/
{
char ScoreBuffer[10]={0},SpeedBuffer[10]={0};
int i;
int NumLinesKilled=0;
/*if(TimerCounter>(20-nSpeed*2))*/
{
TimerCounter = 0; /*重置时钟中断*/
if(!IsConflict(BlockIndex,*x,*y+1)) /*仍在下落*/
{
DrawBlock(BlockIndex,*x,*y,BGCOLOR); /*擦除原先的形状*/
(*y)++;
DrawBlock(BlockIndex, *x, *y, arrayBlock[BlockIndex].nColor); /*绘制
当前形状*/
return FALSE;/*仍在下落返回FALSE*/
}
else /*无法再下落了*/
{
DrawBlock(BlockIndex,*x,*y,FORECOLOR);
for (i=0;i<=7;i++,i++)
{
if ((*y)+arrayBlock[BlockIndex].arrXY[i+1]<1)
continue;
Gameboard[(*x)+arrayBlock[BlockIndex].arrXY[i]][(*y)+arrayBlock[BlockIndex].arrX
Y[i+1]]=1;
}
NumLinesKilled = KillLines(*y);
if(NumLinesKilled>0)
{
switch(NumLinesKilled)
{
case 1:
nScore+=100;
case 2:
nScore+=300;
case 3:
nScore+=500;
case 4:
nScore+=800;
}
/*重绘计分框*/
setfillstyle(SOLID_FILL,BLACK);
bar(BSIZE*25,BSIZE*20, BSIZE*(25+8), BSIZE*(20+1));
setcolor(YELLOW);
rectangle(BSIZE*25,BSIZE*20, BSIZE*(25+8), BSIZE*(20+1));
itoa(nScore,ScoreBuffer, 10);
setcolor(RED);
settextjustify(CENTER_TEXT, BOTTOM_TEXT);
outtextxy(BSIZE*(25+4), BSIZE*(20+1), ScoreBuffer);
if(nScore > nSpeedUpScore)
{
nSpeed++;
nSpeedUpScore+= nSpeed*1000;
/*重绘速度框*/
setfillstyle(SOLID_FILL,BLACK);
bar(BSIZE*25,BSIZE*13, BSIZE*(25+8), BSIZE*(13+1)); setcolor(YELLOW);
rectangle(BSIZE*25,BSIZE*13, BSIZE*(25+8), BSIZE*(13+1));
itoa(nSpeed,SpeedBuffer,10);
setcolor(YELLOW);
settextjustify(CENTER_TEXT, BOTTOM_TEXT);
outtextxy(BSIZE*(25+4), BSIZE*(13+1), SpeedBuffer); }
}
if(IsGameOver())
bOver = TRUE;
return TRUE; /*下落到底返回TRUE*/
}
}
}
/**********************************************************
* 函数原型:int IsLineFull(int y) *
* 传入参数:纵坐标y *
* 返 回 值:填满返回1,否则返回0 *
* 函数功能:判断第y行是否已被填满 *
**********************************************************/
int IsLineFull(int y)
{
int i;
for(i=1;i<=10;i++)
if(!Gameboard[i][y])
return FALSE;
return TRUE;
}
/**********************************************************
* void KillLine(int
*
* 传入参数:纵坐标y *
* 返 回 值:无 *
* 函数功能:消去第
*
**********************************************************/
void KillLine(int y)
{
int i,j;
for(j=y;j>=2;j--)
for(i=1;i<=10;i++)
{
if(Gameboard[i][j]==Gameboard[i][j-1])
continue;
if(Gameboard[i][j-1]==FILLED)
{
Gameboard[i][j]=FILLED;
setfillstyle(SOLID_FILL,FORECOLOR);
}
else /*Gameboard[i][j-1]==EMPTY*/
{
Gameboard[i][j] = EMPTY;
setfillstyle(SOLID_FILL,BGCOLOR);
} y) y行
DrawSquare(i,j);
}
}
/**********************************************************
* 函数原型:int KillLines(int y) * *
传入参数:纵坐标y * * 返 回 值:消去的行数 *
* 函数功能:消去第y行以及与第y行连续的上面被填满的行 *
**********************************************************/
int KillLines(int y)
{
int i, j, LinesKilled=0;
for(i=0;i<4;i++)
{
while(IsLineFull(y))
{
KillLine(y);
LinesKilled++;
i++;
}
y--;
if(y<1)
break;
}
return LinesKilled;
}
/**********************************************************
* 函数原型:int
*
* 传入参数:无 *
* 返 回 值:游戏结束返回1,否则返回0 *
* 函数功能:判断游戏是
*
**********************************************************/
int IsGameOver()
{
int i;
for(i=1;i<=10;i++)
if(Gameboard[i][1])
return TRUE;
return FALSE;
}
/**********************************************************
* 函数原型:int IsGameOver() 否结束 GameOver()
*
* 传入参数:无 *
* 返 回 值:退出游戏返回1,否则返回0 *
* 函数功能:在界面上输出游戏结束信息,并根据用户按键选择决定是否退出游戏*
**********************************************************/
int GameOver()
{
int key;
settextjustify(CENTER_TEXT,TOP_TEXT);
/* 输出游戏结束信息 */
setcolor(RED);
outtextxy(BSIZE*15,BSIZE*12,"Game Over");
setcolor(GREEN);
outtextxy(BSIZE*15,BSIZE*14,"Enter : New Game");
outtextxy(BSIZE*15,BSIZE*15,"Esc : Exit");
for(;;)
{
while(!bioskey(1))
;
key=bioskey(0);
if (key==VK_ENTER)
return FALSE; /* 按下回车键,重新开始游戏 */
if (key==VK_ESC)
return TRUE; /* 按下ESC键,退出游戏 */
}
}