【问题标题】:c++ tron Player lightcycle move in one directionc++ tron Player lightcycle 单向移动
【发布时间】:2013-11-17 23:53:23
【问题描述】:

我正在尝试让玩家光循环继续向一个方向移动而不会停止,直到玩家按下按钮将其向另一个方向移动。我不确定如何使用 kbhit 做到这一点,所以请给我一些建议!谢谢。

void Lightcycle(){
    if (kbhit()) {// get user key input
    char GetCh = getch(); // GetCh equal to the button the user presses
    if (GetCh == 'w'){PlayerX = PlayerX - 1;}
    else if (GetCh == 's'){PlayerX = PlayerX +1;}
    else if (GetCh == 'd'){PlayerY = PlayerY +1;}
    else if (GetCh == 'a'){PlayerY = PlayerY - 1;}
}// end kbhit
}// end function

【问题讨论】:

  • 与其让输入直接移动玩家,不如给玩家一个方向并让输入修改它,然后将玩家朝他们指向的方向移动。
  • 你能用代码展示一个例子吗?我不知道如何在实际代码中应用它

标签: c++ input getch kbhit


【解决方案1】:

我猜你需要一个名为 direction 的全局变量并更改它,例如:

if (GetCh == 'w'){direction=1;}
else if (GetCh == 's'){direction=2;}
else if (GetCh == 'd'){direction=3;}
else if (GetCh == 'a'){direction=4;}

然后你需要在你的游戏循环中的某个地方,不断地处理玩家的移动,比如:

while(gameRunning){
    // Random code handling game goes here
    ...
    if (direction== 1){PlayerX = PlayerX - 1;}
    else if (direction== 2){PlayerX = PlayerX +1;}
    else if (direction== 3){PlayerY = PlayerY +1;}
    else if (direction== 3){PlayerY = PlayerY - 1;}
    ...
    // Other code handling game goes here
}

【讨论】:

    猜你喜欢
    • 2015-02-07
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    相关资源
    最近更新 更多