Example #1
0
	void updatePlayer() {
		if (Keyboard::isKeyPressed(Keyboard::Left)) {
			player.move(-moveDistance * updateTime/50.0, 0);
		} else if (Keyboard::isKeyPressed(Keyboard::Right)) {
			player.move(moveDistance * updateTime/50.0, 0);
		}
	}
        void update() {
            shape.move(velocity);

            if(Keyboard::isKeyPressed(Keyboard::Key::Left) && left() > 0) { velocity.x = -paddleVelocity; }
            else if(Keyboard::isKeyPressed(Keyboard::Key::Right) && right() < windowWidth) { velocity.x = paddleVelocity; }
            else velocity.x = 0;
        }
Example #3
0
	void update() 
	{ 
		shape.move(velocity); 		

		// To move the paddle, we check if the user is pressing
		// the left or right arrow key: if so, we change the velocity.		

		// To keep the paddle "inside the window", we change the velocity
		// only if its position is inside the window.
		if(Keyboard::isKeyPressed(Keyboard::Key::Left) 
			&& left() > 0) velocity.x = -paddleVelocity;
		else if(Keyboard::isKeyPressed(Keyboard::Key::Right) 
			&& right() < windowWidth) velocity.x = paddleVelocity;

		// If the user isn't pressing anything, stop moving.
		else velocity.x = 0;
	}	
Example #4
0
int main()
{
    //-----------------------------
    RenderWindow window; //creates the main window
    window.create(VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "SFML Works!"); //gives the main window attributes such as width, height, and a name
    //-----------------------------

    //-----------------------------
    window.setFramerateLimit(60);//sets frame rate (makes game speed faster/slower)
    //-----------------------------

    //-----------------------------
    RectangleShape mybox; //A rectangle shape
    mybox.setFillColor(Color::Red); //sets color
    mybox.setSize(Vector2f(20,30)/*an xy coordinate using float*/); //sets size
    mybox.setPosition(0,0); //sets position using the upper left corner of the box by default
    //-----------------------------


    //-----------------------------
    while (window.isOpen()) //the almighty gameloop (Keeps game running until gameover)
    {
        Event event; //creates an event, so we have access to it's many wonderful functions
        while (window.pollEvent(event))// event loop (constantly checks for user inputs and queues the actions)
        {
            switch (event.type)// checks the type of the event...
            {
            case Event::Closed: //closed event, triggered by clicking the X button
                window.close(); //closes the window
                break;

                // key pressed
            case Event::KeyPressed: //KeyPressed event, triggered by pressing any key
                switch (event.key.code) //takes in user inputs and performs actions
                {
                case Keyboard::Escape: //if esc button is pressed...
                    window.close();//close the window
                    break;

                case Keyboard::W://if W is pressed...
                    mybox.move(Vector2f(0,-5));//move the box up
                    break;

                case Keyboard::A://if A is pressed...
                    mybox.move(Vector2f(-5,0));//move the box left
                    break;

                case Keyboard::S://if S is pressed...
                    mybox.move(Vector2f(0,5));//move the box down
                    break;

                case Keyboard::D://if D is pressed...
                    mybox.move(Vector2f(5,0));//move the box right
                    break;

                default:
                    break;
                }//end of switch (event.key.code)
                break;

            default:
                break;
            }//end of switch (event.type)
        }//end of while (window.pollEvent(event))


        //~~~~~~~~~~~~~~~
        process();
        //~~~~~~~~~~~~~~~


        //~~~~~~~~~~~~~~~
        window.clear();//clears the screen
        window.draw(mybox);//draws your box shape on the screen (note: invisible unless displayed)
        //window.display();//displays what ever you drew.
        //~~~~~~~~~~~~~~~

    }//end of while (window.isOpen())

    //-----------------------------

}//end of int main()
Example #5
0
	void checkCollisions() {
		if (intersects(player, left) || intersects(player, right)) {
			FloatRect l = left.getGlobalBounds();
			FloatRect r = right.getGlobalBounds();
			Vector2f p = player.getPosition();
			p.x = clamp(p.x, l.left + l.width + 5, r.left - player.getGlobalBounds().width - 5);
			player.setPosition(p);
			blap.play();
		}

		if (intersects(ball, top)) {
			FloatRect t = top.getGlobalBounds();
			FloatRect b = ball.getGlobalBounds();
			ballSpeed.y = abs(ballSpeed.y);
			int u = t.top + t.height - b.top;
			ball.move(0, 2*u);
			blop.play();
		}

		if (intersects(ball, left)) {
			FloatRect l = left.getGlobalBounds();
			FloatRect b = ball.getGlobalBounds();
			ballSpeed.x = abs(ballSpeed.x);
			int u = l.left + l.width - b.left;
			b.left = l.left + l.width + u;
			ball.setPosition(b.left, b.top);
			blop.play();
		}

		if (intersects(ball, right)) {
			FloatRect r = right.getGlobalBounds();
			FloatRect b = ball.getGlobalBounds();
			ballSpeed.x = -abs(ballSpeed.x);
			int u = b.left + b.width - r.left;
			b.left = r.left - b.width - u;
			ball.setPosition(b.left, b.top);
			blop.play();
		}

		if (intersects(ball, bottom)) {
			blam.play();
			playerLives--;
			stringstream str;
			str<<playerLives;
			lives.setString(str.str());
			lives.setPosition(width/2 - lives.getGlobalBounds().width/2, height - 60);
			resetBall();
		}

		if (intersects(ball, player)) {
			FloatRect p = player.getGlobalBounds();
			FloatRect b = ball.getGlobalBounds();

			Vector2f o(p.left + p.width/2, p.top + p.height/2);
			Vector2f om(b.left + p.width/2 - o.x, b.top + b.width/2 - o.y);
			om.x /= p.width;
			om.y /= p.height;
			float angle = atan2(om.y, om.x);
			if (angle <= -PI/4 && angle >= -3*PI/4) {
				ballSpeed.y = -abs(ballSpeed.y);
				ballSpeed.x = (b.left + b.width/2 - p.left - p.width/2) / 200;
				int u = b.top + b.height - p.top;
				b.top = p.top - b.height - u;
				ball.setPosition(b.left, b.top);
				ballSpeed.x *= 1.02f;
				ballSpeed.y *= 1.02f;
				blip.play();
			}
		}

		if (grid.collide(ball, ballSpeed, playerScore)) {
			blap.play();
			playerScore += 200;
			stringstream str;
			str<<playerScore<<" pts ";
			score.setString(str.str());
			score.setPosition(width/2 + score.getGlobalBounds().width/2 - margin, height - 60);
		}

		if (grid.isGameWon())
			gameState = GAME_WON;
		if (playerLives <= 0)
			gameState = GAME_LOST;
	}
Example #6
0
	void updateBall() {
		ball.move(ballSpeed.x * updateTime, ballSpeed.y * updateTime);
	}