Allegro 4와 C++를 사용하여 Pong의 매우 기본적인 버전을 구현하려고합니다. 그러나 rest() 호출과 함께 타이밍 메커니즘을 사용할 때 문제가 발생합니다. 내 게임은 2 명의 플레이어를 위해 설정되고 플레이어는 7 점을 얻은 후 세트를 얻습니다. 둘 다 0에서 시작합니다. 처음으로 2 세트를 얻는 플레이어가 승리합니다. 세트가 획득 된 후, 플레이어가 메시지를 볼 수 있도록 승자와 통화 나머지 (2000)의 이름을 표시합니다. 그러나이 후에 볼은 아무데도 나오지 않아 세트 시작시 플레이어 중 한 명에게 자동 포인트가 생깁니다. 그 후, 그것은 예상했던대로 센터에서 재개됩니다. 이것은 타이밍 메커니즘이나 rest() 호출을 제거 할 때 발생하지 않습니다.Pong을 구현하는 동안 Allegro의 타이밍 문제
나는 점수의 업데이트와 타이밍 루프 밖의 세트를 움직이려고했지만 작동하지 않습니다. 또한 rest() 호출 직후에 볼의 init() 함수를 호출하지도 않습니다. 문제를 해결하는 방법에 대해 약간의 의견을 정말로 고맙게 생각합니다.
다음은 코드입니다. 나는 문제에 영향을 미치지 않는 기본 비트와 포함을 생략했다. //includes...
Paddle p1, p2; //paddles for player 1, player 2
Ball b; //game ball
int main()
{
setupGame(); //setup allegro and bitmaps
init(); //initialize paddles and ball
bool done = false;
int win, game = 0;
//game loop
while(!done)
{
if(key[KEY_X]) //quick exit for debugging
done = true;
//timing loop
while(counter > 0) {
movePaddles();
moveAndCollideBall();
game = checkCondition(b, p1, p2); //check if either player 1 or player 2 has won a point
updateScore(game); //update score
updateSets(); //update set if score of either equals 7
counter --;
}
draw(); //draw paddles and ball
displayScore();
blit(buffer, screen, 0, 0, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); //draw buffer to screen
clear_bitmap(buffer); //clear buffer for next iteration
checkWinner(done); //check if either player has won 2 sets, exit loop if so
}
exitGame(); //clear bitmaps and exit allegro
return 0;
}
END_OF_MAIN()
//Returns 1 if player 1 has won a point, 2 for player 2
int checkCondition(Ball b, Paddle p1, Paddle p2)
{
if(b.x < p1.x)
return 2;
else if(b.x > p2.x+PADDLE_WIDTH)
return 1;
return 0;
}
//setup allegro and timing variables, load bitmaps
void setupGame()
{
//allegro, screen setup, etc.
//timing mechanism
LOCK_VARIABLE(counter);
LOCK_FUNCTION(increment_counter);
install_int_ex(increment_counter, BPS_TO_TIMER(240));
srand(time(0));
//other setup stuff
}
//initialize paddles and ball
void init()
{
p1.init(10, 210);
p2.init(620, 210);
b.init();
}
//output score to buffer
void displayScore()
{
textprintf_ex(buffer, gamefont, SCREEN_WIDTH/4, 10, WHITE, -1, "%i", p1.score);
textprintf_ex(buffer, gamefont, SCREEN_WIDTH/2 - SCREEN_WIDTH/3, 450, WHITE, -1, "Sets: %i", p1.sets);
textprintf_ex(buffer, gamefont, SCREEN_WIDTH/2 + SCREEN_WIDTH/4, 10, WHITE, -1, "%i", p2.score);
textprintf_ex(buffer, gamefont, SCREEN_WIDTH/2 + SCREEN_WIDTH/6, 450, WHITE, -1, "Sets: %i", p2.sets);
}
//taking the winner of the point as parameter, update the corresponding player's score by 1, reset ball
void updateScore(int game)
{
if(game > 0)
{game == 1 ? ++p1.score : ++p2.score;
b.init();
game = 0;
}
}
//update no of sets won if either player has score of 7, reset scores
void updateSets()
{
if(p1.score == 7 || p2.score == 7)
{
if(p1.score == 7)
{p1.sets++;
textprintf_ex(screen, gamefont, SCREEN_WIDTH/5, SCREEN_HEIGHT/2,WHITE, -1, "Player 1 wins the set!");
}
else
{p2.sets++;
textprintf_ex(screen, gamefont, SCREEN_WIDTH/5, SCREEN_HEIGHT/2,WHITE, -1, "Player 2 wins the set!");
}
p2.score = 0;
p1.score = 0;
rest(2000); //THIS SEEMS TO CAUSE THE PROBLEM, AFTER THE REST, THE BALL INSTEAD OF COMING FROM THE CENTER, QUICKLY COMES FROM SOMEWHERE CLOSE TO THE CORNER
}
}
//check if either player has won 2 sets, if so set done to true
void checkWinner(bool& done)
{
if(p1.sets == 2)
{textprintf_ex(screen, gamefont, SCREEN_WIDTH/4, SCREEN_HEIGHT/2 + 30,WHITE, -1, "Player 1 wins!!");
rest(2000);
done = true;
}
else if(p2.sets == 2)
{textprintf_ex(screen, gamefont, SCREEN_WIDTH/4, SCREEN_HEIGHT/2 + 30,WHITE, -1, "Player 2 wins!!");
rest(2000);
done = true;
}
}
Allegro 4는 거의 사용되지 않습니다. 이전 API가 ... 더 오래되고 실제로는 더 현대적인 접근 방식을 반영하지 않았기 때문에 더 이상 적극적으로 개발되지 않았습니다. 알레그로 5를 사용하는 것이 좋습니다. – Cubic
그래,하지만 알레그로 4에 대해 이미 잘 알고있어 알레그로 5 주변에서 머리를 쓰기 시작하기 전에 게임을 끝내고 싶습니다. – theSuperiorVenacava