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

Author Topic: Can somebody help me understand this code?  (Read 2450 times)

0 Members and 1 Guest are viewing this topic.

twigz

  • Newbie
  • *
  • Posts: 4
    • MSN Messenger - Jerry_g16@hotmail.com
    • View Profile
Can somebody help me understand this code?
« on: July 16, 2011, 04:48:04 am »
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.


Code: [Select]
//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();
}
}

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Can somebody help me understand this code?
« Reply #1 on: July 16, 2011, 05:25:20 am »
Hey there. It would be more simple if you pointed out which parts you don't understand.
Just copy-pasting a code makes it too vague and difficult to answer.

Also, warp your source codes with the code button on your post as it makes it easier to understand.
Like so:
Code: [Select]
struct WindowSettings
{
sf::VideoMode videomode;
};

twigz

  • Newbie
  • *
  • Posts: 4
    • MSN Messenger - Jerry_g16@hotmail.com
    • View Profile
Can somebody help me understand this code?
« Reply #2 on: July 16, 2011, 05:33:01 am »
Oh ok then, Sorry I'm kinda new to the forum didn't know.

To be honest i'm lost with the entire paddle class.

1)
Code: [Select]
Paddle(const Player& play) :
      player(play)

2)
Code: [Select]
rect.SetPosition(ScreenWidth - 10,  rect.GetPosition().y);
   


3)
Code: [Select]
void Move(const sf::RenderWindow& win)

4
Code: [Select]
)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);


5)
Code: [Select]
sf::Vector2f Position() const
   {
      return rect.GetPosition();
   }
private:
   sf::Shape rect;
   Player player;
   enum { Speed = 200 };
};

Those are the parts where i'm lost, I'm going back and fort by the tutorial section, but i still can't seem to understand their use right in this program.

Can u help me to understand those parts.[/quote]

 

anything