HGE:Tutorials:Input


Input 函数

Input_GetKey() 返回按下的键盘码. 这个函数对于一次性操作很有用,例如:弹出菜单,或者退出程序等.

Input_GetKeyState() 返回true如果指定的键被按下,佛则返回false. 这个函数很适用与检测连续恩下的键码,如角色的移动等.

在此得到所有键码的列表: HGE key code list

下面的介绍,将实现用方向键移动playerSprite。

在程序的顶部加入:

float playerLocX = 200, playerLocY = 200;  //初始位置(变化值)

bool done = false//程序是否退出

在FrameFunc函数中,将如下函数:

playerSprite->Render(200, 200);  //render theplayer sprite

改为:

playerSprite->Render(playerLocX , playerLocY);  //render the player sprite

 

在 FrameFunc 函数中,将,

return false;

改为:

return done;

 

在FrameFunc函数中的 hge->Gfx_BeginScene();函数下面,加入如下:

 

//player movement

if(hge->Input_GetKeyState(HGEK_UP)) playerLocY -= 100*dt;:

if(hge->Input_GetKeyState(HGEK_DOWN)) playerLocY += 100*dt;

if(hge->Input_GetKeyState(HGEK_LEFT)) playerLocX -= 100*dt;

if(hge->Input_GetKeyState(HGEK_RIGHT)) playerLocX += 100*dt;

如果,用户按下ESC键,我们将终止程序:

if(hge->Input_GetKey()==HGEK_ESCAPE) done=true//quit when Esc is pressed

 

鼠标键码

鼠标键码的常量值为:

HGEK_LBUTTON : 左键

HGEK_RBUTTON :右键

HGEK_MBUTTON : 中建

获得鼠标的位置

使用 Input_GetMousePos() 函数获得当前鼠标的位置(x,y).

程序中先声明如下变量:

float mouseX, mouseY;  //coordinates of the mouse cursor

在FrameFunc函数中加入:

hge->Input_GetMousePos(&mouseX, &mouseY);   //get thecurrent mouse position

打印鼠标的位置:

font1->printf(5, 75, "Mouselocation: %.2f, %.2f", mouseX, mouseY);  //render the current mouse position

最后,在WinMain函数中设置鼠标的状态为显示:

hge->System_SetState(HGE_HIDEMOUSE,false);

整体代码如下:

bool FrameFunc()

{

 hge->Input_GetMousePos(&mouseX, &mouseY);  //get the current mouse position

 float dt=hge->Timer_GetDelta();  //get the timesince the last call to FrameFunc

 star->Update(dt);  //update theanimation

 

 //playermovement

 if(hge->Input_GetKeyState(HGEK_UP)) playerLocY -= 100*dt;

 if(hge->Input_GetKeyState(HGEK_DOWN)) playerLocY += 100*dt;

 if(hge->Input_GetKeyState(HGEK_LEFT)) playerLocX -= 100*dt;

 if(hge->Input_GetKeyState(HGEK_RIGHT)) playerLocX += 100*dt;

 

 if(hge->Input_GetKey()==HGEK_ESCAPE) done=true;   //quit when Esc is pressed

 

 hge->Gfx_BeginScene();

 hge->Gfx_Clear(0);  //clear thescreen, filling it with black

 bgSprite->RenderStretch(0, 0, 800, 600); //render the background sprite stretched

 playerSprite->Render(playerLocX , playerLocY);  //render the player sprite

 star->Render(400, 300); //render theanimation of a star

 

 font1->SetScale(1.0); //set text size to normal

 font1->SetColor(ARGB(255,0,0,0));  //set color oftext to black

 font1->Render(5, 5, "This is some text");  //render text atcoordinates 5, 5

 int someNumber = 50;

 font1->SetScale(2.0);  //set text sizeto twice its normal size

 font1->printf(5, 30, "Here is a number: %d", someNumber);  //render text using printf-style formatting

 font1->SetScale(1.0); //set text size to normal

 font1->printf(5, 75, "Mouse location: %.2f, %.2f", mouseX, mouseY);  //render the current mouse position

 

 hge->Gfx_EndScene();

 

 return done;

}

 

你可能感兴趣的:(list,input,UP,float,tutorials)