#include <ggebase.h>
#include <ggesprite.h>
#include <ggetexture.h>
#include <ggeanimation.h>
#pragma comment(lib, "galaxy2d")
using namespace gge;
ggeTexture *Bgtex;
ggeSprite *Bgspr;
ggeTexture *Playertex;
ggeAnimation *PlayerAni;
int MoveGrpahLines[8]={0,1,2,3,4,5,6,7};
int Direction;
bool KeyUp = false;
bool KeyDown = false;
bool KeyLeft = false;
bool KeyRight = false;
bool KeyMove = false;
bool RoleState = false;
float MoveSpeed;
typedef struct
{
float x;
float y;
}NewPT;
NewPT pt;
NewPT RoleGrid;
NewPT EndGrid;
void Move();
//帧函数
bool Frame()
{
float dt = Timer_GetDelta ();
PlayerAni->Update(dt);
if (Input_IsKeyDown(GGEK_ESCAPE)) return true;
if(Input_IsKeyOn(GGEK_UP))
{
KeyUp = true;
}
else
{
KeyUp = false;
}
if(Input_IsKeyOn(GGEK_DOWN))
{
KeyDown = true;
}
else
{
KeyDown = false;
}
if(Input_IsKeyOn(GGEK_LEFT))
{
KeyLeft = true;
}
else
{
KeyLeft = false;
}
if(Input_IsKeyOn(GGEK_RIGHT))
{
KeyRight = true;
}
else
{
KeyRight = false;
}
if(KeyRight && KeyUp)
{
KeyMove = true;
PlayerAni->Update(dt);
Direction = MoveGrpahLines[5];
Move();
}
else
if(KeyRight && KeyDown)
{
KeyMove = true;
PlayerAni->Update(dt);
Direction = MoveGrpahLines[7];
Move();
}
else
if(KeyLeft && KeyDown)
{
KeyMove = true;
PlayerAni->Update(dt);
Direction = MoveGrpahLines[1];
Move();
}
else
if(KeyLeft && KeyUp)
{
KeyMove = true;
PlayerAni->Update(dt);
Direction = MoveGrpahLines[3];
Move();
}
else
if(KeyLeft)
{
KeyMove = true;
PlayerAni->Update(dt);
Direction = MoveGrpahLines[2];
Move();
}
else
if(KeyRight)
{
KeyMove = true;
PlayerAni->Update(dt);
Direction = MoveGrpahLines[6];
Move();
}
else
if(KeyUp)
{
KeyMove = true;
PlayerAni->Update(dt);
Direction = MoveGrpahLines[4];
Move();
}
else
if(KeyDown)
{
KeyMove = true;
PlayerAni->Update(dt);
Direction = MoveGrpahLines[0];
Move();
}
/*
if(Input_IsKeyUp(GGEK_UP)||Input_IsKeyUp(GGEK_DOWN)||Input_IsKeyUp(GGEK_LEFT)||Input_IsKeyUp(GGEK_RIGHT))
{
KeyMove = false;
PlayerAni->SetFrame(0);
PlayerAni->Stop();
}
*/
RoleGrid.x = (int)((pt.x+80)/40);
RoleGrid.y = (int)((pt.y+150)/40);
if(KeyMove)
{
EndGrid = RoleGrid;
}
if(KeyMove == false)
{
if((EndGrid.x > RoleGrid.x ) && (EndGrid.y < RoleGrid.y))
{
PlayerAni->Update(dt);
Direction = MoveGrpahLines[5];
Move();
}
else
if((EndGrid.x > RoleGrid.x ) && (EndGrid.y > RoleGrid.y))
{
PlayerAni->Update(dt);
Direction = MoveGrpahLines[7];
Move();
}
else
if((EndGrid.x < RoleGrid.x ) && (EndGrid.y < RoleGrid.y))
{
PlayerAni->Update(dt);
Direction = MoveGrpahLines[3];
Move();
}
else
if((EndGrid.x < RoleGrid.x ) && (EndGrid.y > RoleGrid.y))
{
PlayerAni->Update(dt);
Direction = MoveGrpahLines[1];
Move();
}
else
if(EndGrid.x > RoleGrid.x)
{
PlayerAni->Update(dt);
Direction = MoveGrpahLines[6];
Move();
}
else
if(EndGrid.y < RoleGrid.y)
{
PlayerAni->Update(dt);
Direction = MoveGrpahLines[4];
Move();
}
else
if(EndGrid.x < RoleGrid.x)
{
PlayerAni->Update(dt);
Direction = MoveGrpahLines[2];
Move();
}
else
if(EndGrid.y > RoleGrid.y)
{
PlayerAni->Update(dt);
Direction = MoveGrpahLines[0];
Move();
}
else
{
PlayerAni->SetFrame(0);
PlayerAni->Stop();
}
}
Graph_BeginScene();
Graph_Clear();
Bgspr->Render(0,0);
PlayerAni->Render(pt.x,pt.y);
Graph_EndScene();
return false;
}
void Move()
{
if(Direction == MoveGrpahLines[0] && pt.y <480)
{
RoleState = true;
int aniFrame = PlayerAni->GetFrame();
PlayerAni->SetTextureRect(0, Direction*PlayerAni->GetHeight(), PlayerAni->GetWidth(), PlayerAni->GetHeight());
PlayerAni->SetFrame(aniFrame);
PlayerAni->Play();
pt.y = pt.y + MoveSpeed;
}
else
if(Direction == MoveGrpahLines[1] && pt.x > -10)
{
RoleState = true;
int aniFrame = PlayerAni->GetFrame();
PlayerAni->SetTextureRect(0, Direction*PlayerAni->GetHeight(), PlayerAni->GetWidth(), PlayerAni->GetHeight());
PlayerAni->SetFrame(aniFrame);
PlayerAni->Play();
pt.x = pt.x - MoveSpeed;
pt.y = pt.y + MoveSpeed;
}
else
if(Direction == MoveGrpahLines[2] && pt.x > -10)
{
RoleState = true;
int aniFrame = PlayerAni->GetFrame();
PlayerAni->SetTextureRect(0, Direction*PlayerAni->GetHeight(), PlayerAni->GetWidth(), PlayerAni->GetHeight());
PlayerAni->SetFrame(aniFrame);
PlayerAni->Play();
pt.x = pt.x - MoveSpeed;
}
else
if(Direction == MoveGrpahLines[3] && pt.x > -10)
{
RoleState = true;
int aniFrame = PlayerAni->GetFrame();
PlayerAni->SetTextureRect(0, Direction*PlayerAni->GetHeight(), PlayerAni->GetWidth(), PlayerAni->GetHeight());
PlayerAni->SetFrame(aniFrame);
PlayerAni->Play();
pt.x = pt.x - MoveSpeed;
pt.y = pt.y - MoveSpeed;
}
else
if(Direction == MoveGrpahLines[4] && pt.y > -100)
{
RoleState = true;
int aniFrame = PlayerAni->GetFrame();
PlayerAni->SetTextureRect(0, Direction*PlayerAni->GetHeight(), PlayerAni->GetWidth(), PlayerAni->GetHeight());
PlayerAni->SetFrame(aniFrame);
PlayerAni->Play();
pt.y = pt.y - MoveSpeed;
}
else
if(Direction == MoveGrpahLines[5] && pt.x < 738)
{
RoleState = true;
int aniFrame = PlayerAni->GetFrame();
PlayerAni->SetTextureRect(0, Direction*PlayerAni->GetHeight(), PlayerAni->GetWidth(), PlayerAni->GetHeight());
PlayerAni->SetFrame(aniFrame);
PlayerAni->Play();
pt.x = pt.x + MoveSpeed;
pt.y = pt.y - MoveSpeed;
}
else
if(Direction == MoveGrpahLines[6] && pt.x < 738)
{
RoleState = true;
int aniFrame = PlayerAni->GetFrame();
PlayerAni->SetTextureRect(0, Direction*PlayerAni->GetHeight(), PlayerAni->GetWidth(), PlayerAni->GetHeight());
PlayerAni->SetFrame(aniFrame);
PlayerAni->Play();
pt.x = pt.x + MoveSpeed;
}
else
if(Direction == MoveGrpahLines[7] && pt.y < 480)
{
RoleState = true;
int aniFrame = PlayerAni->GetFrame();
PlayerAni->SetTextureRect(0, Direction*PlayerAni->GetHeight(), PlayerAni->GetWidth(), PlayerAni->GetHeight());
PlayerAni->SetFrame(aniFrame);
PlayerAni->Play();
pt.x = pt.x + MoveSpeed;
pt.y = pt.y + MoveSpeed;
}
}
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
//创建游戏引擎
if (!Engine_Create(GGE_VERSION)) return 0;
System_SetState(GGE_TITLE, "剑侠情缘DIY");
System_SetState(GGE_SCREENMODE,SCREEN_WINDOWED);
System_SetState(GGE_SCREENWIDTH,800);
System_SetState(GGE_SCREENHEIGHT,600);
System_SetState(GGE_SCREENBPP,16);
System_SetState(GGE_HIDEMOUSE,false);
//设置帧函数
System_SetState(GGE_FRAMEFUNC, Frame);
//设置帧率
System_SetState(GGE_FPS, 60);
System_SetState(GGE_LOGNAME,"GAMEPROCESS.LOG");
//系统初始化
if (System_Initiate())
{
Direction = MoveGrpahLines[0];
pt.x = 300;
pt.y = 200;
MoveSpeed = 1.5;
RoleGrid.x = (int)((pt.x+80)/40);
RoleGrid.y = (int)((pt.y+150)/40);
EndGrid = RoleGrid;
Bgtex = Texture_Load("背景.jpg");
Bgspr = Sprite_Create(Bgtex,0,0,800,600);
Playertex = Texture_Load("Player1.png");
PlayerAni = Animation_Create(Playertex,8,8,48,96,0,0);
//开始运行帧函数
System_Start();
}
SAFE_RELEASE(Bgtex);
SAFE_RELEASE(Bgspr);
SAFE_RELEASE(Playertex);
SAFE_RELEASE(PlayerAni);
//关闭引擎,释放资源
Engine_Release();
return 0;
}
|