/* Callback handler for window re-paint event */ void display() { glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer glMatrixMode(GL_MODELVIEW); // To operate on the model-view matrix glLoadIdentity(); // Reset model-view matrix paddleRight.move(); ball.draw(); paddleRight.draw(); paddleLeft.draw(); glutSwapBuffers(); // Swap front and back buffers (of double buffered mode) // Animation Control - compute the location for the next refresh ball.contactLeft(paddleLeft); ball.contactRight(paddleRight); ball.move(); ai.AIPaddleMove(ball); //ai2.AIPaddleMove(ball); if (ball.didRoundEnd() == 'R') { ++scoreRight; ball.resetBallPos(); } else if (ball.didRoundEnd() == 'L') { ++scoreLeft; ball.resetBallPos(); } else return; }
void PowerUp::onPaddleHit(Paddle& paddle) { switch (m_type) { case SMALL_PADDLE: paddle.shrink(); break; case LARGE_PADDLE: paddle.grow(); break; case STICKY_PADDLE: paddle.activeSticky(); break; case LASER_PADDLE: paddle.activeLaser(); break; case TRIPLE_BALL: getManager()->createBall(); getManager()->createBall(); break; case POWER_BALL: getManager()->applyOnEachBall(&Ball::enablePowerBall); break; case EXTRA_LIFE: getManager()->addPlayerLife(); break; case SPEED_RESET: getManager()->applyOnEachBall(&Ball::resetSpeed); break; } kill(); SoundSystem::playSound("power-up.ogg"); }
bool Ball::isCollided(Paddle &paddle) { const sf::Vector2f polarPos = toPolar(getPosition() - Settings::getScreenCenter()); const float ballRad = ((sf::CircleShape*)shape)->getRadius(); const float closeRad = 0.01 * Settings::getZoneRadius(); if ((polarPos.x + ballRad) > (paddle.getRadius() - closeRad)) { const float ballRadAngle = toPolar(sf::Vector2f(-ballRad, polarPos.x)).y; const float closeAngle = 1.0; const float paddleCenter = paddle.getCenterAngle(); if ( ((polarPos.y + ballRadAngle) > (paddleCenter - paddle.getAngularLength() - closeAngle)) && ((polarPos.y - ballRadAngle) < (paddleCenter + paddle.getAngularLength() + closeAngle)) ) { // ball is close to paddle, so bother to do checks const int NUM_POINTS = ((sf::CircleShape*)shape)->getPointCount(); for (int i = 0; i<NUM_POINTS; i++) { // check for every point on the circle shape, in global coords sf::Vector2f localPoint = ((sf::CircleShape*)shape)->getPoint(i); sf::Transform transform = shape->getTransform(); sf::Vector2f globalPoint = transform.transformPoint(localPoint); if (paddle.isWithin(globalPoint)) { return true; } } } } return false; }
// Il metodo `run` farà partire il game loop. void run() { while(true) { window.clear(sf::Color::Black); if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) break; // Il tasto `P` gestirà la pausa. if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::P)) { // Prima di mettere/togliere la pausa, controlliamo // se il tasto era già stato pressato. if(!pausePressedLastFrame) { if(state == State::Paused) state = State::InProgress; else if(state == State::InProgress) state = State::Paused; } pausePressedLastFrame = true; } else pausePressedLastFrame = false; // Il tasto `R` farà ricominciare il gioco. if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::R)) restart(); // Se il gioco è in pausa, non aggiorneremo i game // object. if(state != State::Paused) { ball.update(); paddle.update(); for(auto& brick : bricks) { brick.update(); solveBrickBallCollision(brick, ball); } bricks.erase( std::remove_if(std::begin(bricks), std::end(bricks), [](const auto& mBrick) { return mBrick.destroyed; }), std::end(bricks)); solvePaddleBallCollision(paddle, ball); } ball.draw(window); paddle.draw(window); for(auto& brick : bricks) brick.draw(window); window.display(); } }
Paddle* Paddle::paddleWithTexture(CCTexture2D* aTexture) { Paddle* pPaddle = new Paddle(); pPaddle->initWithTexture( aTexture ); pPaddle->autorelease(); return pPaddle; }
bool Power::collisionWithPaddle(Paddle& paddle, Ball &ball, Bricks &bricks) { if(ball.gameStatus== CONTINUE) { if(position[1]- width/2<= paddle.position[1]+ paddle.width/2) { if(position[0]+ length/2>= paddle.position[0]- paddle.length/2 && position[0]- length/2<= paddle.position[0]+ paddle.length/2) { if(type== EXPAND) { engine1->play2D("media/powerGood.wav"); paddle.enlarge(); return true; } else if(type== SHORTEN) {engine1->play2D("media/powerBad.wav"); paddle.shorten(); return true; } else if(type== GLUE) {engine1->play2D("media/powerGood.wav"); ball.isGlue= 1; return true; } else if(type== SHOOT) {engine1->play2D("media/powerGood.wav"); paddle.shoot= 1; return true; } else if(type== UNGLUE) {engine1->play2D("media/powerBad.wav"); ball.isGlue= 0; return true; } else if(type== NOSHOOT) {engine1->play2D("media/powerBad.wav"); paddle.shoot= 0; return true; } else if(type== ONEUP) {engine1->play2D("media/powerGood.wav"); ball.lives++; return true; } } } } return false; }
void WorldManager::updateLeftPlayer(PaddleMove direction) { Paddle *leftPlayer = mWorld->getLeftPlayer(); if (direction == MOVE_DOWN && !isLeftPlayerHitBottomWall()) { leftPlayer->setDirection(PADDLE_DIRECTION_DOWN); } else if (direction == MOVE_UP && !isLeftPlayerHitTopWall()) { leftPlayer->setDirection(PADDLE_DIRECTION_UP); } else { leftPlayer->stop(); } }
void WorldManager::putBallAtLeftPaddleEdge() { Paddle *leftPaddle = mWorld->getLeftPaddle(); Ball *ball = mWorld->getBall(); Ogre::Vector3 leftPaddlePosition = leftPaddle->getPosition(); Ogre::Vector3 leftPaddleDimension = leftPaddle->getDimension(); Ogre::Vector3 ballPosition = ball->getPosition(); Ogre::Vector3 ballDimension = ball->getDimension(); ballPosition.x = leftPaddlePosition.x + (leftPaddleDimension.x + ballDimension.x)/2; ball->setPosition(ballPosition); }
void WorldManager::putBallAtRightPaddleEdge() { Paddle *rightPaddle = mWorld->getRightPaddle(); Ball *ball = mWorld->getBall(); Ogre::Vector3 rightPaddlePosition = rightPaddle->getPosition(); Ogre::Vector3 rightPaddleDimension = rightPaddle->getDimension(); Ogre::Vector3 ballPosition = ball->getPosition(); Ogre::Vector3 ballDimension = ball->getDimension(); ballPosition.x = rightPaddlePosition.x - (rightPaddleDimension.x + ballDimension.x)/2; ball->setPosition(ballPosition); }
void WorldManager::updateRightPlayer(PaddleMove direction) { Paddle *rightPlayer = mWorld->getRightPlayer(); if (direction == MOVE_DOWN && !isRightPlayerHitBottomWall()) { rightPlayer->setDirection(Ogre::Vector3(0, -1, 0)); } else if (direction == MOVE_UP && !isRightPlayerHitTopWall()) { rightPlayer->setDirection(Ogre::Vector3(0, 1, 0)); } else { rightPlayer->stop(); } }
bool GameWorld::update() { Ball * ball = dynamic_cast<Ball*>(worldObjects.at(1)); ball->updatePosition(); Paddle * paddle = dynamic_cast<Paddle*>(worldObjects.at(0)); paddle->CheckBallCollision(); if (worldObjects.size() == 2 || life == 0 || curLevel == 10){ return false; } return true; }
/**************************************************************************//** * @author Paul Blasi * * @par Description: * Stores the state of the pressed key and does an initial update of the left * player. Also handles the key presses for space, which pauses, escape, which * exits the game, and +/-, which incresses and decreases the speed of the ball * respectively. * * @param[in] key - the key that was pressed * @param[in] x - the x position of the mouse when the key was pressed * @param[in] y - the y position of the mouse when the key was pressed * *****************************************************************************/ void Keyboard(unsigned char key, int x, int y) { KEYSTATE[key] = true; if ((KEYSTATE['w'] || KEYSTATE['W']) && !PAUSED) { LEFT_PLAYER.ChangeYSpeed(PADDLE_INC); } if ((KEYSTATE['a'] || KEYSTATE['A']) && !PAUSED) { LEFT_PLAYER.ChangeXSpeed((-1)*PADDLE_INC); } if ((KEYSTATE['s'] || KEYSTATE['S']) && !PAUSED) { LEFT_PLAYER.ChangeYSpeed(-1 * PADDLE_INC); } if ((KEYSTATE['d'] || KEYSTATE['D']) && !PAUSED) { LEFT_PLAYER.ChangeXSpeed(PADDLE_INC); } if (KEYSTATE['+'] && !PAUSED) { BALL.IncreaseMinVelocity(); } if (KEYSTATE['-'] && !PAUSED) { BALL.DecreaseMinVelocity(); } if (KEYSTATE[' ']) { PAUSED = !PAUSED; if (PAUSED) { glutPostRedisplay(); } else { glutTimerFunc(30, Animate, 0); } } if (KEYSTATE[ESCAPE_KEY]) { glutExit(); } }
Paddle* Paddle::createWithSide(Paddle::Side side) { Paddle *paddle = new (std::nothrow) Paddle(); CCASSERT(paddle, "You must unlearn, what you have learned"); if (paddle->initWithSide(side)) { paddle->autorelease(); return paddle; } CC_SAFE_DELETE(paddle); return nullptr; }
Paddle * Paddle::create () { Paddle * paddle = new Paddle(); if (paddle && paddle->initWithFile("blank.png")) { paddle->setTextureRect(CCRectMake(0, 0, paddle->_paddleWidth, paddle->_paddleHalfWidth)); paddle->setColor(ccc3(124,252,0)); //paddle->initPaddle(); paddle->autorelease(); return paddle; } CC_SAFE_DELETE(paddle); return NULL; }
void ExtraBallItem::Touch(Paddle& paddle, TouchInfo info) { SetPhysicsModel(PHY_GHOST); SetCollisionModel(PHY_GHOST); Die(); entPtr_t ball = entPtr_t(new ENT::Ball(GetRules())); ball->SetPosition(paddle.GetPosition() + MATH::Vector2((paddle.GetBBox().lowerRight.x / 2.0f) - (BALL_SIZE / 2.0f), -(BALL_SIZE + 1.0f))); Entity::LinkToWorld(ball); paddle.AttachEntity(ball); }
/**************************************************************************//** * @author Paul Blasi * * @par Description: * The action the right paddle is to take when it collides with any wall. Keeps * the paddles from travelling through walls. * * @param[in] col - the type of collision that occurred * *****************************************************************************/ void RightCollideWithWall(CollisionTypeEnum col) { if (col == RIGHT_COLLISION) { if (RIGHT_PLAYER.X_Vel() > 0) { RIGHT_PLAYER.ResetXSpeed(); } } if (col == LEFT_COLLISION) { if (RIGHT_PLAYER.X_Vel() < 0) { RIGHT_PLAYER.ResetXSpeed(); } } if (col == TOP_COLLISION) { if (RIGHT_PLAYER.Y_Vel() > 0) { RIGHT_PLAYER.ResetYSpeed(); } } if (col == BOTTOM_COLLISION) { if (RIGHT_PLAYER.Y_Vel() < 0) { RIGHT_PLAYER.ResetYSpeed(); } } }
Paddle* Application::createPaddle(Ogre::String nme, GameObject::objectType tp, Ogre::String meshName, int x, int y, int z, Ogre::Real scale, Ogre::SceneManager* scnMgr, GameManager* ssm, Ogre::Real mss, Ogre::Real rest, Ogre::Real frict, bool kinematic, Simulator* mySim) { createRootEntity(nme, meshName, x, y, z); Ogre::SceneNode* sn = mSceneManager->getSceneNode(nme); Ogre::Entity* ent = SceneHelper::getEntity(mSceneManager, nme, 0); sn->setScale(scale,scale,scale); const btTransform pos; OgreMotionState* ms = new OgreMotionState(pos, sn); Paddle* obj = new Paddle(nme, tp, mSceneManager, ssm, sn, ent, ms, mySim, mss, rest, frict, scale, kinematic); obj->addToSimulator(); return obj; }
void solvePaddleBallCollision(const Paddle& mPaddle, Ball& mBall) noexcept { if(!isIntersecting(mPaddle, mBall)) return; auto newY(mPaddle.top() - mBall.shape.getRadius() * 2.f); mBall.shape.setPosition(mBall.x(), newY); auto paddleBallDiff(mBall.x() - mPaddle.x()); auto posFactor(paddleBallDiff / mPaddle.width()); auto velFactor(mPaddle.velocity.x * 0.05f); sf::Vector2f collisionVec{posFactor + velFactor, -2.f}; mBall.velocity = getReflected(mBall.velocity, getNormalized(collisionVec)); }
/* Callback handler for mouse event */ void mouse(int button, int state, int x, int y) { if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { // Pause/resume paused = !paused; // Toggle state if (paused) { ball.pause(); paddleLeft.pause(); paddleRight.pause(); } else { ball.unpause(); paddleLeft.unpause(); paddleRight.unpause(); } } }
void Game::mouseMovementEvent(float aDeltaX, float aDeltaY, float aPositionX, float aPositionY) { // If the game isn't paused if(!m_Pause) { //Set the paddle to the x position of the mouse Paddle* paddle = (Paddle*)getGameObjectByType(GAME_PADDLE_TYPE); //Safety check, paddle could be NULL if(paddle != NULL) { paddle->setX(aPositionX - (paddle->getWidth() / 2.0f)); } } }
void createPaddles() { player_one.setLength(3); player_one.setPosition(rows/2 - 2, columns - 1); for(int i = 0; i < player_one.length; i++) { board[player_one.x_position + i][player_one.y_position] = '|'; } player_two.setLength(3); player_two.setPosition(rows/2 - 2, 0); for(int i = 0; i < player_two.length; i++) { board[player_two.x_position + i][player_two.y_position] = '|'; } }
void Render(Paddle p1, Paddle p2, Ball b, GLuint wins){ glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); p1.Draw(); p2.Draw(); b.Draw(); if (b.x + b.size < p1.x - p1.w){ DrawSprite(wins, 1.1, 0.8, 0.1, 0.08, 0); } else if (b.x - b.size > p2.x + p2.w){ DrawSprite(wins, -1.1, 0.8, 0.1, 0.08, 0); } }
void testCollision(Paddle& mPaddle, Ball& mBall) { if(!isIntersecting(mPaddle, mBall)) return; mBall.velocity.y = -ballVelocity; if(mBall.x() < mPaddle.x()) mBall.velocity.x = -ballVelocity; else mBall.velocity.x = ballVelocity; }
//------------------------------------------------------------------ // // PongLayer // //------------------------------------------------------------------ PongLayer::PongLayer() { m_ballStartingVelocity = CGPointMake(20.0f, -100.0f); m_ball = Ball::ballWithTexture( CCTextureCache::sharedTextureCache()->addImage(s_Ball) ); m_ball->setPosition( CGPointMake(160.0f, 240.0f) ); m_ball->setVelocity( m_ballStartingVelocity ); addChild( m_ball ); m_ball->retain(); CCTexture2D* paddleTexture = CCTextureCache::sharedTextureCache()->addImage(s_Paddle); NSMutableArray<NSObject *> *paddlesM = new NSMutableArray<NSObject *>(4); Paddle* paddle = Paddle::paddleWithTexture(paddleTexture); paddle->setPosition( CGPointMake(160, 15) ); paddlesM->addObject( paddle ); paddle = Paddle::paddleWithTexture( paddleTexture ); paddle->setPosition( CGPointMake(160, 480 - kStatusBarHeight - 15) ); paddlesM->addObject( paddle ); paddle = Paddle::paddleWithTexture( paddleTexture ); paddle->setPosition( CGPointMake(160, 100) ); paddlesM->addObject( paddle ); paddle = Paddle::paddleWithTexture( paddleTexture ); paddle->setPosition( CGPointMake(160, 480 - kStatusBarHeight - 100) ); paddlesM->addObject( paddle ); m_paddles = paddlesM->copy(); NSMutableArray<NSObject *>::NSMutableArrayIterator it; for(it = m_paddles->begin(); it != m_paddles->end(); it++) { paddle = (Paddle*)(*it); if(!paddle) break; addChild(paddle); } paddlesM->release(); schedule( schedule_selector(PongLayer::doStep) ); }
void solvePaddleBallCollision(const Paddle& mPaddle, Ball& mBall) noexcept { if(!isIntersecting(mPaddle, mBall)) return; mBall.velocity.y = -Ball::defVelocity; mBall.velocity.x = mBall.x() < mPaddle.x() ? -Ball::defVelocity : Ball::defVelocity; }
void Game::start(){ Paddle* paddle = new Paddle(); paddle->setPos(150,460); scene->addItem(paddle); paddle->grabKeyboard(); Ball* ball = new Ball(); ball->setPos(200,440); scene->addItem(ball); creatBlockGrid(); // play background music //QMediaPlayer * music = new QMediaPlayer(); //music->setMedia(QUrl("qrc:/sound/under.ogg")); //music->play(); }
int main() { window.create(sf::VideoMode(800, 600), "Breakfree"); window.setVerticalSyncEnabled(true); //, sf::Style::Close|sf::Style::Titlebar); if (!font.loadFromFile("Nirmala.ttf")) return EXIT_FAILURE; Levels lv; Paddle pad; InputManager input; while (window.isOpen()) { switch (level) { case 0: lv.loadLv0(); break; } while (playing) { input.ExecuteEvents(window); pad.Update(); window.clear(); // ------draw graphics here------- sf::Text text("Score: " + std::to_string(score), font, 20); text.setPosition(660, 220); window.draw(text); lv.Draw(window); window.draw(pad.sprite); window.draw(pad.ball); window.display(); // ----------------------- } } return 0; }
/**************************************************************************//** * @author Paul Blasi, Caitlin Taggart * * @par Description: * The display callback. Draws each of the objects and displays score for each * player. If the game is currently paused, it also draws a translucent box * over the play area and the text "Paused". * *****************************************************************************/ void Display(void) { int length; glClear(GL_COLOR_BUFFER_BIT); BALL.Draw(); LEFT_PLAYER.Draw(); RIGHT_PLAYER.Draw(); LEFT_WALL.Draw(); RIGHT_WALL.Draw(); TOP_WALL.Draw(); BOTTOM_WALL.Draw(); NET.Draw(); string right_score = to_string(RIGHT_PLAYER.Score); string left_score = to_string(LEFT_PLAYER.Score); glColor4fv(WHITE); length = glutBitmapLength(GLUT_BITMAP_TIMES_ROMAN_24, (const unsigned char*)left_score.c_str()); glRasterPos2i(50 - (200.0 / SCREEN_WIDTH * length / 2.0), 90); glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, (const unsigned char*)left_score.c_str()); length = glutBitmapLength(GLUT_BITMAP_TIMES_ROMAN_24, (const unsigned char*)right_score.c_str()); glRasterPos2i(150 - (200.0 / SCREEN_WIDTH * length / 2.0), 90); glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, (const unsigned char*)right_score.c_str()); if (PAUSED) { float alphaBlack[4] = { 0.0, 0.0, 0.0, .60 }; Paddle temp; temp = Paddle(Point(100, 50), 200, 100, alphaBlack); temp.Draw(); glColor4fv(WHITE); length = glutBitmapLength(GLUT_BITMAP_TIMES_ROMAN_24, (const unsigned char*)"PAUSED"); glRasterPos2i(100 - (200.0/SCREEN_WIDTH * length / 2.0), 50); glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, (const unsigned char*)"PAUSED"); } //glFlush(); glutSwapBuffers(); }
/**************************************************************************//** * @author Paul Blasi, Caitlin Taggart * * @par Description: * The action a ball is to take when it collides with the left paddle * * @param[in] col - the type of collision that occurred * *****************************************************************************/ void BallCollideWithLeftPaddle(CollisionTypeEnum col) { BALL.BounceOffPaddle(col); float ratio = ((LEFT_PLAYER.Center.Y - BALL.Center.Y) / (LEFT_PLAYER.Center.Y - LEFT_PLAYER.Y_Max())); BALL.BounceOffPaddle(ratio); if (LEFT_PLAYER.X_Vel() >= 0) { if (LEFT_PLAYER.Velocity() > BALL.Velocity()) { BALL.IncreaseSpeed((LEFT_PLAYER.Velocity() - BALL.Velocity()) / 3); } else { BALL.DecreaseSpeed(-1 * (LEFT_PLAYER.Velocity() - BALL.Velocity()) / 3); } } if (LAST_PADDLE_COLLISION != "Left") { LEFT_PLAYER.ReduceSize(); RIGHT_PLAYER.ReduceSize(); } LAST_PADDLE_COLLISION = "Left"; }
Ball::Ball(sf::Texture & texture, float sp, Paddle & paddle) : Collidable(CBCircle(sf::Vector2f(0,0),BALLRAD), 0) { /* place ball above center of paddle */ setPosition(paddle.getCenter() - sf::Vector2f(0,(float) BALLRAD+paddle.getSize().y/2)); sprite.setTexture(texture); sprite.setOrigin(BALLRAD, BALLRAD); primaryPaddle = &paddle; speed = sp; currentState = 0; changeState(LAUNCH); bounceSound.setBuffer(bounceBuffer); deadSound.setBuffer(deadBuffer); launchBallSound.setBuffer(launchBallBuffer); tileBounceSound.setBuffer(tileBounceBuffer); }