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;
}