gpt4 book ai didi

c - 如果已经按下了向下+向右或向上+向左,则没有空格键的 SDL 事件

转载 作者:行者123 更新时间:2023-12-05 03:08:25 28 4
gpt4 key购买 nike

我正在 SDL 的 2D 游戏中试验 Action ,效果很好。但是,当我通过按 SPACE 添加角色射击的可能性时,我遇到了一个奇怪的错误:如果已经按下了向下和向右键或向上和向左键,则按下 时不会创建任何事件>空间。在其他情况下,事件会按原样创建。

这是我用来获取一些事件的循环:

while (on == 1)
{
SDL_PollEvent(&event);
switch (event.type)
{
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
on = 0;
break;
case SDLK_UP:
shooter1.dir[0] = 1; // shooter1 is an instance of a structure that
// contains a position and an array representing
// 4 directions (down, up, left, right)
break;
case SDLK_DOWN:
shooter1.dir[1] = 1;
break;
case SDLK_LEFT:
shooter1.dir[2] = 1;
break;
case SDLK_RIGHT:
shooter1.dir[3] = 1;
break;
case SDLK_SPACE: // Eventually, the program does not go in this
// case, even though the SPACE key is pressed
fprintf(stderr, "SPACE PRESSED\n");
break;
default:
break;
}
break;
case SDL_KEYUP:
switch (event.key.keysym.sym)
{
case SDLK_UP:
shooter1.dir[0] = 0;
break;
case SDLK_DOWN:
shooter1.dir[1] = 0;
break;
case SDLK_LEFT:
shooter1.dir[2] = 0;
break;
case SDLK_RIGHT:
shooter1.dir[3] = 0;
break;
default:
break;
}
break;
default:
break;
}
event.type = 0;
get_new_positions(&shooter1);
SDL_BlitSurface(background, NULL, screen, &background_pos);
blit_shooter(screen, &shooter1);
SDL_Flip(screen);
}

知道这种行为的原因吗?

最佳答案

这几乎可以肯定是硬件限制。引自 the relevant Wikipedia page , 强调:

Certain high-end keyboards have "n-key rollover". This means that each key is scanned completely independently by the keyboard hardware, so that each keypress is correctly detected regardless of how many other keys are being pressed or held down at the time. [...]

However, to reduce cost and design complexity, most computer keyboards do not isolate all keys in this way. Instead, they use a matrix of key switches, without any isolation diodes, that assumes that only a limited number of keys will be held down at any given time. With these keyboards, pressing as few as three keys can cause ghosting effects, although care is taken when laying out the matrix arrangement that this does not happen for common modifier key combinations.

关于c - 如果已经按下了向下+向右或向上+向左,则没有空格键的 SDL 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45331316/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com