My favourites are C++ and C, and then Python. But i'm using Java in my current job.
I still have the source code of my first successful project, it was a 'Pong' game:
Code:
#include < SFML/Window.hpp >
#include < SFML/Graphics.hpp >
//------------------------------------------------------------------
class Ball : public sf::CircleShape
{
sf::Vector2f m_velocity;
const float Velocity_Rate { 4.5f };
public:
Ball() : m_velocity(4.5f, 4.5f)
{
setRadius(5.f);
setFillColor(sf::Color::Green);
}
void invertDirectionX()
{
m_velocity.x = -m_velocity.x;
}
void invertDirectionY()
{
m_velocity.y = -m_velocity.y;
}
void resetVelocity()
{
m_velocity.y = Velocity_Rate;
m_velocity.x = Velocity_Rate;
}
void incrementVelocity()
{
m_velocity.y += 0.1f;
m_velocity.x += 0.1f;
}
void run()
{
move(m_velocity);
}
float size() const { return radius()*2.f; }
float radius() const { return getRadius(); }
float x() const { return getPosition().x; }
float y() const { return getPosition().y; }
float left() { return x() - getRadius(); }
float right() { return x() + getRadius(); }
float top() { return y() - getRadius(); }
float bottom() { return y() + getRadius(); }
sf::Vector2f velocity() const { return m_velocity; }
};
//------------------------------------------------------------------
//------------------------------------------------------------------
class Paddle : public sf::RectangleShape
{
sf::Vector2f m_velocity;
public:
Paddle()
{
setSize({10.f, 50.f});
setFillColor(sf::Color::Green);
}
void setVerticalVelocity(float rate)
{
m_velocity.y = rate;
}
void moveVertical()
{
move(m_velocity);
}
float height() const { return getSize().y; }
float width() const { return getSize().x; }
float x() const { return getPosition().x; }
float y() const { return getPosition().y; }
float left() { return x() - width(); }
float right() { return x() + width(); }
float top() { return y() - height(); }
float bottom() { return y() + height(); }
sf::Vector2f velocity() const { return m_velocity; }
};
//------------------------------------------------------------------
//------------------------------------------------------------------
class Pong
{
const unsigned int Width { 600 };
const unsigned int Height { 400 };
sf::RenderWindow m_win;
sf::Event m_event;
int m_playerScore = 0;
int m_enemyScore = 0;
Paddle m_player;
Paddle m_enemy;
Ball m_ball;
public:
Pong() : m_win({Width, Height}, "Pong - 0 x 0")
{
m_win.setFramerateLimit(60);
reset();
}
void init()
{
while (m_win.isOpen())
{
handleInput();
update();
render();
}
}
private:
void reset()
{
m_player.setPosition(10.f,
(Height-m_player.height())/2.f);
m_enemy.setPosition(Width-(m_enemy.width()*2.f),
(Height-m_player.height())/2.f);
m_ball.setPosition((Width-m_ball.radius()*2.f)/2.f,
Height-m_ball.size()/2.f);
}
void updatePlayer()
{
m_player.moveVertical();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && m_player.y() != 0.f)
{
m_player.setVerticalVelocity(-5.f);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && m_player.y() != (Height - m_player.height()))
{
m_player.setVerticalVelocity(5.f);
}
else
{
m_player.setVerticalVelocity(0.f);
}
}
void updateEnemy()
{
m_enemy.moveVertical();
if (((m_enemy.y() + m_enemy.height()/2.f) > (m_ball.y() + m_ball.size()/2.f))
&& m_enemy.y() >= 0.f)
{
m_enemy.setVerticalVelocity(-3.6f);
}
else if ((m_enemy.y() + m_enemy.height()/2.f) < (m_ball.y() + m_ball.size()/2.f)
&& (m_player.y() <= (Height - m_player.height())))
{
m_enemy.setVerticalVelocity(3.6f);
}
}
void updateBall()
{
m_ball.run();
if (m_ball.left() < 0.f)
{
++m_enemyScore;
updateScore();
//m_ball.resetVelocity();
reset();
}
else if (m_ball.right() > Width)
{
++m_playerScore;
updateScore();
//m_ball.resetVelocity();
reset();
}
if (m_ball.top() < 0.f)
{
//m_ball.incrementVelocity();
m_ball.invertDirectionY();
}
else if (m_ball.bottom() > Height)
{
//m_ball.incrementVelocity();
m_ball.invertDirectionY();
}
}
void updateScore()
{
static char scores[17];
sprintf(scores, "Pong - %d x %d", m_playerScore, m_enemyScore);
m_win.setTitle(scores);
}
template < class T1, class T2 >
bool isInstersecting(T1& a, T2& b)
{
return a.right() >= b.left() &&
a.left() <= b.right() &&
a.bottom() >= b.top() &&
a.top() <= b.bottom();
}
void checkPaddleCollision()
{
if (isInstersecting(m_ball, m_player) || isInstersecting(m_ball, m_enemy))
{
if (m_ball.x() < m_player.x())
{
m_ball.invertDirectionX();
//m_ball.incrementVelocity();
}
if (m_ball.x() < m_enemy.x())
{
m_ball.invertDirectionX();
//m_ball.incrementVelocity();
}
}
}
void handleInput()
{
while (m_win.pollEvent(m_event))
{
switch (m_event.type)
{
case sf::Event::Closed:
m_win.close();
break;
}
}
}
void update()
{
updatePlayer();
updateBall();
updateEnemy();
checkPaddleCollision();
}
void render()
{
m_win.clear(sf::Color::Black);
m_win.draw(m_player);
m_win.draw(m_enemy);
m_win.draw(m_ball);
m_win.display();
}
};
//------------------------------------------------------------------
//------------------------------------------------------------------
int main()
{
Pong p;
p.init();
return 0;
}
//------------------------------------------------------------------
Bookmarks