void Game::paint() { // Show the title screen if(m_ShowTitle) { OpenGLRenderer::getInstance()->drawTexture(m_Textures[21], 15.0f, 0.0f); return; } // If the game is over if(m_GameOver) { // Draw the game over texture OpenGLRenderer::getInstance()->drawTexture(m_Textures[19], ScreenManager::getInstance()->getScreenWidth() / 2 - m_Textures[19]->getSourceWidth() / 2, ScreenManager::getInstance()->getScreenHeight() / 1.5); return; } // Draw the texture for the game background Paddle* paddle = (Paddle*)getGameObjectByType(GAME_PADDLE_TYPE); if(paddle != NULL) { // Move the textures as the paddle moves // Draw the background texture OpenGLRenderer::getInstance()->drawTexture(m_Textures[0], (paddle->getX() / 8) - (m_Textures[0]->getTextureWidth() / 4), 0.0f); // Only draw when player has lives if(m_Lives > 0) { // draws the HP texture OpenGLRenderer::getInstance()->drawTexture(m_Textures[10], paddle->getX() + paddle->getWidth() / 8, paddle->getY() + paddle->getHeight() * 1.55); OpenGLRenderer::getInstance()->drawTexture(m_Textures[m_Lives], paddle->getX() + paddle->getWidth() / 3.5, paddle->getY() + paddle->getHeight() * 1.25); } // Only draw when within the maximum number of rounds if(m_Round <= GAME_MAX_ROUNDS) { // draws the round texture OpenGLRenderer::getInstance()->drawTexture(m_Textures[11], paddle->getX() + paddle->getWidth() / 1.5, paddle->getY() + paddle->getHeight() * 1.55); OpenGLRenderer::getInstance()->drawTexture(m_Textures[m_Round + 1], paddle->getX() + paddle->getWidth() / 1.25, paddle->getY() + paddle->getHeight() * 1.25); } } //Cycle through and draw all the game objects for(int i = 0; i < m_GameObjects.size(); i++) { if(m_GameObjects.at(i)->getIsActive() == true) { // Painting bricks if(m_GameObjects.at(i)->getType() == GAME_BRICK_TYPE) { // Paints a different texture for each round m_GameObjects.at(i)->paint(m_Textures[12 + m_Round]); } // Painting balls else if(m_GameObjects.at(i)->getType() == GAME_BALL_TYPE) { m_GameObjects.at(i)->paint(m_Textures[18]); } else { m_GameObjects.at(i)->paint(); } } } //Draw the outer white walls OpenGLRenderer::getInstance()->setForegroundColor(OpenGLColorWhite()); OpenGLRenderer::getInstance()->setLineWidth(4.0f); OpenGLRenderer::getInstance()->drawLine(1.0f, 0.0f, 1.0f, getHeight()); OpenGLRenderer::getInstance()->drawLine(0.0f, 1.0f, getWidth(), 1.0f); OpenGLRenderer::getInstance()->drawLine(getWidth() - 1, 0.0f, getWidth() - 1, getHeight()); OpenGLRenderer::getInstance()->setLineWidth(1.0f); // If the game is paused if(m_Pause) { // Draw the pause texture OpenGLRenderer::getInstance()->drawTexture(m_Textures[9], ScreenManager::getInstance()->getScreenWidth() / 2 - m_Textures[9]->getSourceWidth() / 2, ScreenManager::getInstance()->getScreenHeight() / 1.5); } }
void detectCollisions(Paddle& paddle) { int startX = paddle.getBitmapX(); int startY = paddle.getBitmapY(); int paddleWidth = paddle.getWidth(); int paddleHeight = paddle.getHeight(); if (paddle.isHorizontal) { if (paddle.choosen) //down { if (x + moveX >= startX && y + moveY >= startY && x + moveX <= startX + paddleWidth) { double position = paddle.getX(); double BALL_RADIUS = Ball::getWidth() / 2; double radius = paddle.getWidth() / 2; double velocity = sqrt((moveX*moveX) + (moveY*moveY)); double kx = ((position - getX()) / ((BALL_RADIUS + radius) / velocity)); double ky = sqrt((velocity * velocity) - (kx * kx)); moveX = -kx; moveY = -fabs(ky); } } else //up { if (x + moveX >= startX && y + moveY <= startY + paddleHeight && x + moveX <= startX + paddleWidth) { double position = paddle.getX(); double BALL_RADIUS = Ball::getWidth() / 2; double radius = paddle.getWidth() / 2; double velocity = sqrt((moveX*moveX) + (moveY*moveY)); double kx = ((position - getX()) / ((BALL_RADIUS + radius) / velocity)); double ky = sqrt((velocity * velocity) - (kx * kx)); moveX = -kx; moveY = fabs(ky); } } } else { startX = paddle.getX() - paddle.getHeight() /2; startY = paddle.getY() - paddle.getWidth() /2; int paddleWidthN = paddle.getHeight(); int paddleHeightN = paddle.getWidth(); if (paddle.choosen) // left { if (y + moveY >= startY && x + moveX <= startX + paddleWidthN && y + moveY <= startY + paddleHeightN) { double position = paddle.getY(); double BALL_RADIUS = Ball::getWidth() / 2; double radius = paddle.getWidth() / 2; double velocity = sqrt((moveX*moveX) + (moveY*moveY)); double ky = ((position - getY()) / ((BALL_RADIUS + radius) / velocity)); double kx = sqrt((velocity * velocity) - (ky * ky)); moveX = fabs(kx); moveY = -ky; } } else // right { if (y + moveY >= startY && x + moveX >= startX && y + moveY <= startY + paddleHeightN) { double position = paddle.getY(); double BALL_RADIUS = Ball::getWidth() / 2; double radius = paddle.getWidth() / 2; double velocity = sqrt((moveX*moveX) + (moveY*moveY)); double ky = ((position - getY()) / ((BALL_RADIUS + radius) / velocity)); double kx = sqrt((velocity * velocity) - (ky * ky)); moveX = -fabs(kx); moveY = -ky; } } } }