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;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小涵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值