我正在使用 libtcod 和 c 并且我的移动函数不响应输入,这是函数在被调用时的样子
TCOD_key_t key;
move_entity(player.x, player.y, key);
这是实际的源代码
void move_entity(int x, int y, TCOD_key_t key){
TCOD_sys_check_for_event(TCOD_EVENT_KEY_PRESS, &key, NULL);
switch(key.vk){
case TCODK_UP : y--; break;
case TCODK_DOWN : y++; break;
case TCODK_RIGHT : x++; break;
case TCODK_LEFT : x--; break;
default:break;
}
}
奇怪的是,当 move_entity 内部的代码被复制到程序响应的主函数中时,这里是主循环
#include "libtcod.h"
#include "move.h"
#include "entity.h"
int main(){
struct Entity player = {40, 25, '@', 100, TCOD_COLOR_LIME};
struct Entity enemy = {20, 35, '&', 50, TCOD_COLOR_RED};
TCOD_console_set_custom_font("terminal12x12_gs_ro.png", TCOD_FONT_LAYOUT_ASCII_INROW, 16, 16);
TCOD_console_init_root(80, 50, "Placeholder title", false, TCOD_RENDERER_SDL);
while(!TCOD_console_is_window_closed()){
TCOD_key_t key;
move_entity(player.x, player.y, key);
TCOD_console_clear(NULL);
TCOD_console_print(NULL, 1, 1, "Player Health:%d", player.health);
TCOD_console_print(NULL, 1, 2, "Enemy Health:%d", enemy.health);
entity_render(player.x, player.y, player.ch, player.forcol);
entity_render(enemy.x, enemy.y, enemy.ch, enemy.forcol);
TCOD_console_flush(NULL);
}
return 0;
}
我敢肯定这只是我忽略的一些愚蠢的事情,但它真的让我大吃一惊,我很感激你的帮助:)
*编辑销售商的建议是我编辑的代码
在主循环中调用的代码
move_entity(&player.x, &player.y);
以及函数中的代码
void move_entity(int *x, int *y){
TCOD_key_t key;
TCOD_sys_wait_for_event(TCOD_EVENT_KEY_PRESS, &key, NULL, false);
switch(key.vk){
case TCODK_UP : *y--; break;
case TCODK_DOWN : *y++; break;
case TCODK_RIGHT : *x++; break;
case TCODK_LEFT : *x--; break;
default:break;
}
}