/***************************************************************************/
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <graphics.h>
#include "fairy.h"
#include "graph.h"
static shi gLayout[10][16];
static shi gLife; //命数
static shi gScore; //记录得分
static shi gScore1; //记录小果地数目
static shi gScore5; //记录大果的数目
static shi g_Row,g_Col; //记录偷果者的位置
static shi gFairy1_Row,gFairy1_Col; //第一个精灵的位置
static shi gFairy2_Row,gFairy2_Col; //第二个精灵的位置
static shi gFairy3_Row,gFairy3_Col; //第三个精灵的位置
static shi gRet_Code; //标识符,1为赢,2为输
static shi gCount; //主程序循环次数,与gTimelevel一起控制循环周期
static shi gTimelevel = GTIME3; //主程序循环周期
//数组中的行、列和值对应位图的行、列和值。0-墙 1—小果 2-大果 3——小精灵 4-偷果者
static shi gSource[10][16]=
{
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, //1
{1,3,0,2,1,1,0,2,3,0,1,1,2,0,3,1}, //2
{0,1,0,1,0,1,0,0,1,0,1,0,1,0,1,0}, //3
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0}, //4
{0,1,0,0,0,0,1,0,0,1,0,0,0,0,1,0}, //5
{1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1}, //6
{0,1,0,0,1,0,1,1,1,1,0,1,0,0,1,0}, //7
{0,1,0,0,1,1,1,0,0,1,1,1,0,0,1,0}, //8
{0,2,1,1,1,0,1,1,4,1,0,1,1,1,2,0}, //9
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, //10
};
//游戏级别
char gLevel_Str[][5] = {"初级", "中级", "高级"};
void main(void)
{
shi level=1;
shi redraw=1;
shi keyvalue;
InitialGraph(); //图形方式初始化
while(1)
{
if( redraw ) //画出游戏界面
{
InitialGame();
ShowFace(level);
DrawBoard();
while(!_kbhit());
redraw = 0;
}
Sleep(LOOP_TIME); //CPU 休眠
gCount++; //循环计数
keyvalue=ReadKey();
switch(keyvalue)
{
case RESTART_KEY: // Restart;
redraw=1;
break;
case QUIT_KEY: //quit
return;
case LEVEL_KEY: //改变级别
if( level < 3 ) level++;
else level=1;
FillRec(440+40,420,16,20,(ushi)WHITE);
Print16zk(440+40,420,BLACK,(char *)gLevel_Str[level-1]);
switch(level)
{
case 1:
gTimelevel=GTIME3; break;
case 2:
gTimelevel=GTIME2; break;
case 3:
gTimelevel=GTIME1; break;
}
break;
}
if (keyvalue!=0)
SpecialGo(keyvalue); //移动偷果者
if( !(gCount%gTimelevel) ) FairyGo(); //移动小精灵
ShowInfo();
if( gLife<=0 ) // LOSE;
{
EndGame(level);
redraw=1;
}
if( (gScore1==0 ) && (gScore5==0) ) // WIN;
{
gRet_Code=1;
if(EndGame(level)&&level<3)
{
level++;
switch(level)
{
case 1:
gTimelevel=GTIME3; break;
case 2:
gTimelevel=GTIME2; break;
case 3:
gTimelevel=GTIME1; break;
}
}
redraw=1;
}
}
closegraph();
}
///////////////////////////////////////////////////////////////////////////
//画出游戏说明
void ShowFace(shi level)
{
FillRec(0,0,640,480,(ushi)WHITE);
Print16zk(40,410,BLACK, (char *)"ESC-- 退出");
Print16zk(40+100,410,BLACK, (char *)"R -- 重来");
Print16zk(40+100+100,410,BLACK, (char *)"L -- 级别");
Print16zk(40,430,BLACK, (char *)"→ -- 左移");
Print16zk(40+100,430,BLACK, (char *)"← -- 右移");
Print16zk(40,450,BLACK,(char *)"↑ -- 上移");
Print16zk(40+100,450,BLACK,(char *)"↓ -- 下移");
FillRec(0,0,640,400,(ushi)WHITE);
Print16zk(440,420,BLACK,(char *)"级别:");
Print16zk(440+40,420,BLACK,(char *)gLevel_Str[level-1]);
Print16zk(440,450,BLACK,"分数:");
Print16zk(440+40,450,BLACK," 0");
///////////////////////////////////////////////////////
}
/////////////////////////////////////////////////////////////////////////
void ShowInfo(void)
{
char stringnum[8];
FillRec(440+40, 450, 32, 14, (ushi)WHITE);
sprintf(stringnum,"%4d",gScore);
Print16zk(440+40,450,BLACK,stringnum);
}
//////////////////////////////////////////////////////////////////////////
void SpecialGo(shi keyvalue)
{
switch(keyvalue)
{
case UP_KEY:
g_Row--;
if( gLayout[g_Row][g_Col]!=0 )
{
if( gLayout[g_Row][g_Col]!=6 && gLayout[g_Row][g_Col]!=7
&&gLayout[g_Row][g_Col]!=8 )
MoveUp();
else
{
gRet_Code =2;
gLife--;
}
}
else
g_Row++;
break;
case DOWN_KEY:
g_Row++;
if( gLayout[g_Row][g_Col]!=0 )
{
if( gLayout[g_Row][g_Col]!=6 && gLayout[g_Row][g_Col]!=7
&&gLayout[g_Row][g_Col]!=8 )
MoveDown();
else
{
gRet_Code =2;
gLife--;
}
}
else
g_Row--;
break;
case LEFT_KEY:
g_Col--;
if( g_Col==-1)
{
g_Col=15;
MoveLeftBound();
break;
}
if( gLayout[g_Row][g_Col]!=0 )
{
if( gLayout[g_Row][g_Col]!=6 && gLayout[g_Row][g_Col]!=7
&&gLayout[g_Row][g_Col]!=8 )
MoveLeft();
else
{
gRet_Code =2;
gLife--;
}
}
else
g_Col++;
break;
case RIGHT_KEY:
g_Col++;
if( g_Col==16)
{
g_Col=0;
MoveRightBound();
break;
}
if( gLayout[g_Row][g_Col]!=0 )
{
if( gLayout[g_Row][g_Col]!=6 && gLayout[g_Row][g_Col]!=7
&&gLayout[g_Row][g_Col]!=8 )
MoveRight();
else
{
gRet_Code =2;
gLife--;
}
}
else
g_Col--;
break;
}
}
/////////////////////////////////////////////////////////////////////////////
shi ReadKey(void)
{
shi keyid = 0;
if (!kbhit()) return keyid;
keyid=getch();
if (keyid == 0xE0) keyid = getch();
return keyid;
}
/////////////////////////////////////////////////////////////////////////////
void MoveUp(void)
{
gLayout[g_Row+1][g_Col]=5;
DrawBlank(g_Col,g_Row+1);
DrawSpecial(g_Col,g_Row,UP_KEY);
if( gLayout[g_Row][g_Col]==1 )
{
gScore+=2; gScore1--;
}
if( gLayout[g_Row][g_Col]==2 )
{
gScore+=10; gScore5--;
}
gLayout[g_Row][g_Col]=4;
}
/////////////////////////////////////////////////////////////////////////////
void MoveDown(void)
{
gLayout[g_Row-1][g_Col]=5;
DrawBlank(g_Col,g_Row-1);
DrawSpecial(g_Col,g_Row,DOWN_KEY);
if( gLayout[g_Row][g_Col]==1 )
{
gScore+=2; gScore1--;
}
if( gLayout[g_Row][g_Col]==2 )
{
gScore+=10; gScore5--;
}
gLayout[g_Row][g_Col]=4;
}
////////////////////////////////////////////////////////////////////////////
void MoveLeft(void)
{
gLayout[g_Row][g_Col+1]=5;
DrawBlank(g_Col+1,g_Row);
DrawSpecial(g_Col,g_Row,LEFT_KEY);
if( gLayout[g_Row][g_Col]==1 )
{
gScore+=2; gScore1--;
}
if( gLayout[g_Row][g_Col]==2 )
{
gScore+=10; gScore5--;
}
gLayout[g_Row][g_Col]=4;
}
///////////////////////////////////////////////////////////////////////////
void MoveRight(void)
{
gLayout[g_Row][g_Col-1]=5;
DrawBlank(g_Col-1,g_Row);
DrawSpecial(g_Col,g_Row,RIGHT_KEY);
if( gLayout[g_Row][g_Col]==1 )
{
gScore+=2; gScore1--;
}
if( gLayout[g_Row][g_Col]==2 )
{
gScore+=10; gScore5--;
}
gLayout[g_Row][g_Col]=4;
}
/////////////////////////////////////////////////////////////////////////////
//移动到最左边的处理
void MoveLeftBound(void)
{
gLayout[g_Row][0]=5;
DrawBlank(0,g_Row);
DrawSpecial(g_Col,g_Row,LEFT_KEY);
if( gLayout[g_Row][g_Col]==1 )
{
gScore+=2; gScore1--;
}
if( gLayout[g_Row][g_Col]==2 )
{
gScore+=10; gScore5--;
}
gLayout[g_Row][g_Col]=4;
}
/////////////////////////////////////////////////////////////////////////////
//移动到最右边的处理
void MoveRightBound(void)
{
gLayout[g_Row][15]=5;
DrawBlank(15,g_Row);
DrawSpecial(g_Col,g_Row,RIGHT_KEY);
if( gLayout[g_Row][g_Col]==1 )
{
gScore+=2; gScore1--;
}
if( gLayout[g_Row][g_Col]==2 )
{
gScore+=10; gScore5--;
}
gLayout[g_Row][g_Col]=4;
}
////////////////////////////