This code is from somebody's Pong game. I do not own it, I just want to understand it, as it has no comments explaining to me about it.
So if any one can explain it to me a bit, it would be much appreciated.
Thank You.
Here is the code.
//Pong game yet to finish
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
const int ScreenWidth = 550;
const int ScreenHeight = 450;
enum Player {Player1, Player2, Computer};
class Paddle
{
public:
Paddle(const Player& play) :
player(play), rect(sf::Shape::Rectangle(0,0,10,100, sf::Color(200,200,200)))
{
rect.SetPosition(0, (ScreenHeight / 2) - 50);
if(player == Player2 || player == Computer)
rect.SetPosition(ScreenWidth - 10, rect.GetPosition().y);
}
void Move(const sf::RenderWindow& win)
{
const float delta = Speed * win.GetFrameTime();
//Collision with walls
if(rect.GetPosition().y < 0)
rect.SetPosition(rect.GetPosition().x, 0);
if(rect.GetPosition().y > ScreenHeight - 100)
rect.SetPosition(rect.GetPosition().x, ScreenHeight - 100);
switch(player)
{
case Player1:
if(win.GetInput().IsKeyDown(sf::Key::Down)) rect.Move(0, delta);
if(win.GetInput().IsKeyDown(sf::Key::Up)) rect.Move(0, -delta);
break;
case Player2:
if(win.GetInput().IsKeyDown(sf::Key::S)) rect.Move(0, delta);
if(win.GetInput().IsKeyDown(sf::Key::W)) rect.Move(0, -delta);
break;
case Computer:
break;
}
}
void Draw(sf::RenderWindow& win) const
{
win.Draw(rect);
}
sf::Vector2f Position() const
{
return rect.GetPosition();
}
private:
sf::Shape rect;
Player player;
enum { Speed = 200 };
};
class Ball
{
public:
Ball(): circle(sf::Shape::Circle(ScreenWidth / 2,
ScreenHeight / 2, 10, sf::Color(200,200,200)))
{
}
void Draw(sf::RenderWindow& window)
{
window.Draw(circle);
//The differente between the position and width
//float diX = circle.GetPosition().x + ScreenWidth / 2;
//float diY = circle.GetPosition().y + ScreenHeight / 2;
}
private:
sf::Shape circle;
};
int main()
{
sf::RenderWindow window(sf::VideoMode(ScreenWidth,ScreenHeight), "Pong Game");
Paddle player1(Player1);
Paddle player2(Player2);
Ball ball;
while(window.IsOpened())
{
sf::Event event;
while(window.GetEvent (event))
if(event.Type == sf::Event::Closed)
window.Close();
if(window.GetInput().IsKeyDown(sf::Key::Escape))
window.Close();
window.Clear(sf::Color(100,100,100));
player1.Move(window);
player2.Move(window);
ball.Draw(window);
player1.Draw(window);
player2.Draw(window);
window.Display();
}
}