Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - marksie

Pages: [1]
1
General / Re: Issues with Pong and Game Loop
« on: June 18, 2016, 05:33:47 pm »
Erdrick,

Thanks for this. However how do I do this?

I have tried the following and it does not work. Im a bit stumped tbh.

// Move the ball
                Ball.move(BallX, BallY);
                Ball.setPosition(BallPos.x,BallPos.y);

                //Move the computer controlled paddle

                //Move the paddle up
                if (BallPos.y < PaddlePos.y)
                {
                        PCrectangle.move(0, -1);
                        PCrectangle.setPosition(PaddlePos.x, PaddlePos.y);
                }

                //Move the paddle down
                if (BallPos.y > PaddlePos.y)
                {
                        PCrectangle.move(0, 1);
                        PCrectangle.setPosition(PaddlePos.x,PaddlePos.y);
                }

2
General / Issues with Pong and Game Loop
« on: June 18, 2016, 12:01:35 pm »
Hi,

I have started writing a Pong Game. I have started running into some minor issues and would gladly appreciate any kind of help. First of all, please excuse my sloppy code. Its a while since i have done any coding. I do not intend for this to be the final version, I am just trying to put together a basic game for learning purposes.

My issue, is the positioning of the computer controlled paddle. I have implemented code that checks its position and compared it to the position of the ball and then moves accordingly. This seems to work, until the Player Paddle hits the ball. The ball moves at an angle but the PC paddle does not move to hit it again. Im not sure if its because I used a rotation or the order in which I have put my code.

Any help would be appreciated:

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>


int main()
{
        //Code to draw the window
        sf::RenderWindow window(sf::VideoMode(800, 600), "My Window");
        window.setFramerateLimit(60); // call it once, after creating the window
       
        //Define Rectangle 1 - Player Paddle

        sf::RectangleShape rectangle(sf::Vector2f(25, 100)); //Defines a rectangle that is 25 x 100
        rectangle.setFillColor(sf::Color(100, 250, 40));        //Set the rectangle color to green
        rectangle.setPosition(30, 30);  //Set the position of the rectangle
       

        //Define rectangle 2 - AI Paddle

        sf::RectangleShape PCrectangle(sf::Vector2f(25, 100)); //Defines a rectangle that is 25 x 100
        PCrectangle.setFillColor(sf::Color(100, 250, 40));      //Set the rectangle color to green
        PCrectangle.setPosition(745, 30);       //Set the position of the rectangle

        int pad_move;

        //Define Ball
        sf::CircleShape Ball(20);
        Ball.setFillColor(sf::Color(255, 0, 0));
        Ball.setPosition(400, 300);

        int BallX = -3;         // Variable used for moving the ball - X Axis
        int BallY = 0;          // Variable used for moving the ball - Y Axis

        //Bounding Boxes Decalrations

        sf::FloatRect RectangleBox;
        sf::FloatRect PCRectangleBox;
        sf::FloatRect boundingBoxBall;

        sf::Vector2f BallPos;
        sf::Vector2f PaddlePos;

       

        //Define Border

       
        //Run the program as long as the window is open
       
       
        while (window.isOpen())
       
        {

                // Check Poistion of ball and paddles

                boundingBoxBall = Ball.getGlobalBounds();
                RectangleBox = rectangle.getGlobalBounds();
                PCRectangleBox = PCrectangle.getGlobalBounds();

                BallPos = Ball.getPosition();
                PaddlePos = PCrectangle.getPosition();

                //Check for ball paddle collisions

                if (boundingBoxBall.intersects(RectangleBox))
                {
                        BallX = 3;
                        Ball.setRotation(-100);
                }

                if (boundingBoxBall.intersects(PCRectangleBox))
                {
                        BallX = -3;
                        Ball.setRotation(+100);
                }


                // Move the ball
                Ball.move(BallX, BallY);

                //Move the computer controlled paddle

                //Move the paddle up
                if (BallPos.y < PaddlePos.y)
                {
                        PCrectangle.move(0, -1);
                }

                //Move the paddle down
                if (BallPos.y > PaddlePos.y)
                {
                        PCrectangle.move(0, 1);
                }








                //Draw everything on screen
                window.clear();
                window.draw(rectangle);
                window.draw(PCrectangle);
                window.draw(Ball);
                window.display();
               
               

               
                // Check all the windows events that were triggered since the last iteration of the loop
                sf::Event event;
               
                while (window.pollEvent(event))
                {
                        switch (event.type)
                        {
                               
                               
                                //Window Closed
                        case sf::Event::Closed:
                                window.close();
                                break;
                               
                                //Need to do Nested Switch Case for detecting key presses.
                               
                                //Up Key
                        case sf::Event::KeyPressed:
                                switch (event.key.code)
                                {
                                        //Up Key
                                case sf::Keyboard::Up:
                                        rectangle.move(0, -10);
                                        break;

                                        //Down Key
                                case sf::Keyboard::Down:
                                        rectangle.move(0, 10);
                                        break;

                                }
                       
                                //We do not process other events
                        default:
                                break;




                        }
                }
       
       
        }

        return 0;
}

 


3
Graphics / Boundry Boxes on Shapes
« on: June 18, 2016, 10:41:14 am »
Hi all,

Yesterday I download SFML 2.3 and started writing my first pong game. However I am having some issues with Bounding Boxes when it comes to collision detection.

I have been using the tutorials that are provided so far. I can create two paddles and a ball. However the player movable Paddle goes off the screen when in reality I want it to stop. Looking at the bounding box example, I cant see how I would use this with screen coordinates. Any help would be appreciated.

I can see how to look for collisions between object (I think). But the paddle going off the screen is baffling me.

// get the bounding box of the entity
sf::FloatRect boundingBox = entity.getGlobalBounds();

// check collision with a point
sf::Vector2f point = ...;
if (boundingBox.contains(point))
{
    // collision!
}




 

Pages: [1]