int main(int argc, char *argv[]) { Setup(); bool done = false; SDL_Event event; PreProcesses(); const Uint8* keys = SDL_GetKeyboardState(NULL); Paddle p1(-0.9); Paddle p2(0.9); float angle[] = { 45, 135, 225, 315 }; int choice = floor(rand() % 4); Ball ball(0.0005, angle[choice]); GLuint wins = LoadTexture("wins.png", GL_RGBA); while (!done) { while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) { done = true; } else if (event.type == SDL_KEYDOWN){ if (event.key.keysym.scancode == SDL_SCANCODE_SPACE){ p1.Reset(-0.9); p2.Reset(0.9); choice = floor(rand() % 4); ball.Reset(angle[choice]); } } } if (CollisionX(ball)){ ball.d_y = -ball.d_y; } if (CollisionY(ball, p1, p2)){ if (ball.x > 0){ ball.d_x = -abs(ball.d_x); } else { ball.d_x = abs(ball.d_x); } } PaddleMovement(keys, p1.y, p2.y); Update(ball); Render(p1, p2, ball, wins); SDL_GL_SwapWindow(displayWindow); } SDL_Quit(); return 0; }
// Updates functions and positions every frame // void UpdateGame(bool &play, DynamObject &redPaddle, DynamObject &bluePaddle, DynamObject &ball, int &redScore, int &blueScore, float &ballInitX, float &ballInitY, int &ballDir, bool &winLose, bool &quit) { if(winLose == false) { StartBall(play, ball, ballDir); BallCollisionWalls(ball, redScore, blueScore, ballInitX, ballInitY, play, ballDir); BallCollisionPaddles(ball, redPaddle, bluePaddle, play); PaddleMovement(redPaddle); AIBehaviour(bluePaddle, ball); UpdateBall(ball); } WinLose(redScore, blueScore, winLose, quit); }