我正在使用 Allegro 4 和 C++ 制作一个简单版本的俄罗斯方块。我已经准备好大部分代码,除了用户输入之外一切正常。游戏仅在少数随机情况下检测到按键。我担心这是一个时间问题,但我似乎无法抓住它。这是相关的代码位...
//Tetris.h
//Tetris class definition...
volatile long tet_counter = 0;
void increment_tet_counter()
{
tet_counter++;
}
END_OF_FUNCTION(increment_tet_counter);
//this function gets called by the menu class when the user selects the Tetris option
//contains main loop, calls all other functions etc.
void Tetris::show(BITMAP* buffer, int& scr)
{
LOCK_VARIABLE(tet_counter);
LOCK_FUNCTION(increment_tet_counter);
install_int_ex(increment_tet_counter, BPS_TO_TIMER(120));
while(tet_counter > 0)
{
if(tet_scr == 1) //welcome screen logic
{
if(key[KEY_E])
{
game_init(); //initialize game variables, etc.
tet_scr = 2; // go to main game screen
}
else if(key[KEY_I])
{
tet_scr = 3; //go to instructions screen
}
else if(key[KEY_Q]) //quit tetris
{
scr = 5; //reference to a variable in the main.cpp, basically reverting the
//main screen to the menu screen upon return
return;
}
}
else if(tet_scr == 2) //main game screen
{
if(cannot_move()) //managing auto-block movement, making the last line of blocks
{ //permanent, etc... all of this works fine
put_block();
update_score(remove_filled_line());
get_next_block();
}
move_block(); //handles auto-block movement and user input
if(is_game_over())
{textprintf_ex(screen, font, 500, 300, WHITE, -1, "GAME OVER!");
rest(2000);
tet_scr = 5;
}
if(key[KEY_P]) //pause game
tet_scr = 4;
if(key[KEY_Q]) //quit game
tet_scr = 5;
}
//else ifs for other screens of the game... all work fine
tet_counter--;
}
//drawing functions ... work fine
}
//handles block movement... CPU-generated movement works, user input not detected mot of the time
void Tetris::move_block()
{
timer = (timer+1) % time_factor;
if(timer == time_factor-1) //move block every couple of seconds
block_y += 20;
int k;
if(keypressed()) //this is where the problem is (i think)
{ //the game only picks up the keypresses some of the times
k = readkey(); //and sometimes when I keep the keypressed for a long time it work
if(k >> 8 == KEY_DOWN) //but it's very buggy and erratic and no way to predict
block_y+=20; //when it'll work and when it won't
else if(k >> 8 == KEY_LEFT)
block_x-=20;
else if(k >> 8 == KEY_RIGHT)
block_x+=20;
else if(k >> 8 == KEY_UP)
rotate_block();
}
if(block_x < BOARD_X)
block_x = BOARD_X;
if(block_x > x_lim)
block_x = x_lim;
if(block_y < BOARD_Y)
block_y = BOARD_Y;
if(block_y > y_lim)
block_y = y_lim;
}
//various other functions handling game logic/drawing etc are all working fine
我之前以完全相同的方式实现了俄罗斯方块,但没有任何总体菜单类。只需运行 .exe,您就会遇到俄罗斯方块游戏。那个时候没有这样的问题。因此,Tetris.h 文件之外的代码显然与游戏有关。我使用'keypressed()'的唯一其他地方主要是这样......
while (counter > 0)
{
if(keypressed())
{
switch(readkey() >> 8)
{
case KEY_DOWN:
option++;
break;
case KEY_UP:
option--;
break;
}
}
if (option < 0)
option = 5;
else if (option > 5)
option = 0;
screen_handler(option, done); //updates a variable using which the appropriate game is called in show_screen(), pong, breakout, snake are the other games
counter--;
}
show_screen(); //this is where Tetris::show() is called
blit(main_buffer, screen, 0, 0, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
clear_bitmap(main_buffer);
任何和所有的帮助来解决这个混乱将不胜感激。谢谢。