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

Author Topic: Issues with Pong and Game Loop  (Read 2918 times)

0 Members and 1 Guest are viewing this topic.

marksie

  • Newbie
  • *
  • Posts: 3
    • View Profile
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;
}

 


Erdrick

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
    • Email
Re: Issues with Pong and Game Loop
« Reply #1 on: June 18, 2016, 04:17:02 pm »
Hi,

After you move the ball you should store its position again right away since its used in a condition immediately afterwards, but you didn't update the position variable.

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

        //Move the paddle up
        if (BallPos.y < PaddlePos.y)
 

Erdrick

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
    • Email
Re: Issues with Pong and Game Loop
« Reply #2 on: June 18, 2016, 04:19:54 pm »
Hi,

After you move the ball you should store its position again right away since its used in a condition immediately afterwards, but you didn't update the position variable.

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

        //Move the paddle up
        if (BallPos.y < PaddlePos.y)
 

same for the paddle

marksie

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Issues with Pong and Game Loop
« Reply #3 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);
                }

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: Issues with Pong and Game Loop
« Reply #4 on: June 18, 2016, 11:16:31 pm »
i'm afraid this is not correct way to make pong.

btw, in SFML folder, there is a pong example under examples folder.

based on that example. you need to implement the physics that is needed for pong game, it is quite easy only thing you need is the speed formula which is

distance = speed x time

this might help you alot for smooth movement and collision detection.


Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Issues with Pong and Game Loop
« Reply #5 on: June 19, 2016, 02:18:59 am »
                Ball.move(BallX, BallY);
                Ball.setPosition(BallPos.x,BallPos.y);
The "move" here is effectively ignored due to the absolute position being set immediately afterwards. Same applies to the move-setPosition pairs for PCrectangle.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything