- Give your flappy bird a name and start the game:
- Start playing:
- Restart when you lose:
- Close game and see your achievements:
- Graphical Interface (SDL, SFML, Qt, etc.) - C++ API
- RTTI (Run-time type information)
Bird* bird = dynamic_cast<Bird*>(_bird);
- Abstract Classes
class Object
{
...
virtual void draw(sf::RenderWindow&) = 0;
virtual void manageExceptions() = 0;
virtual ~Object(){}
};
- Overloading Operators
std::istream& operator >>(std::istream& in, Bird& bird)
{
std::cout<< "Enter your bird's name and hit enter: ";
in >> bird.name;
return in;
}
And many more!
- Heap Memory Allocation
ObjectFactory* factory = new ObjectFactory;
- Exceptions
class Exception : public std::exception
{
...
};
class file_not_found_error : public Exception
{
...
};
try {
if (!std::filesystem::exists(fileName)) throw file_not_found_error(fileName);
else throw 1;
}
catch (const file_not_found_error& err) {
std::cout << err.what();
}
catch (...) {
f.loadFromFile(fileName);
t.setPosition(x, y);
t.setFont(f);
t.setOutlineThickness(thickness);
t.setCharacterSize(size);
t.setString(s);
}
- STL
std::priority_queue <int> allScores;
- Lambda expressions
auto tooLow = [&]() { return bird->getY() > 700 && bird->isAlive(); };
auto tooHigh = [&]() { return bird->getY() < -10 && bird->isAlive(); };
...
if(tooLow() || tooHigh())
{
score->insertScore();
bird->die();
}
- Templates
template <class T>
class Text
{
...
};
- Smart pointers
std::unique_ptr <Score> score = std::make_unique <Score>();
- Design patterns
//Singleton
class Bird : public Object
{
private:
...
static Bird* instance;
Bird();
Bird(const Bird&) = delete;
Bird(Bird &&) noexcept = delete;
Bird& operator =(const Bird&) = delete;
Bird &operator=(Bird &&bird) = delete;
public:
static Bird* GetInstance();
...
};
//Factory
class ObjectFactory
{
public:
Object* Create(const char objName[])
{
if (objName == "bird") return Bird::GetInstance();
else if(objName == "pipe") return new Pipe;
else
{
std::cout << "Obiect invalid!\n";
return nullptr;
}
}
};
//Facade
class Collision
{
private:
Bird &bird;
Pipe &first, &second, &third;
Score &score;
sf::SoundBuffer buffer;
sf::Sound coin_sound;
public:
Collision(Bird &b, Pipe &p1, Pipe &p2, Pipe &p3, Score &s);
void isCollide();
void update();
};
- Features of C++17/20 (constexpr, consteval, constinit, fold expressions, init statement for if/switch, etc.)
if (!std::filesystem::exists(fileName)) throw file_not_found_error(fileName);
- Move Semantics
Pipe& Pipe::operator =(Pipe&& other)
{
if (this == &other) return *this;
x = other.x;
y = other.y;
file_up = other.file_up;
file_down = other.file_down;
manageExceptions();
other.x = other.y = 0;
other.file_up = other.file_down = "";
return *this;
}
...
// in main
*third = *first + 1000;