xtd 0.2.0
console_firework.cpp

Shows how to use some xtd::console methods.

#include <xtd/threading/thread>
#include <xtd/console>
#include <xtd/is>
#include <xtd/random>
#include <xtd/startup>
#include <memory>
using namespace xtd;
using namespace xtd::collections::generic;
namespace console_firework_example {
class firework abstract_ {
public:
firework(int x, int y, console_color color, int delay_) : x_(x), y_(y), color_(color), delay_(delay_) {}
firework(const firework& firework) : x_(firework.x_), y_(firework.y_), color_(firework.color_), delay_(firework.delay_) {}
int delay() {return delay_;}
virtual void paint() const = 0;
protected:
static void write(int x, int y, const string& str) {
}
int x_ = 0;
int y_ = 0;
int delay_ = 0;
};
class firework_start : public firework {
public:
firework_start(int x, int y, console_color color, int speed) : console_firework_example::firework(x, y, color, speed) {}
explicit firework_start(const firework& firework) : console_firework_example::firework(firework) {}
void paint() const override {
write(x_, y_, "+");
}
};
class firework_exploded : public firework {
public:
firework_exploded(int x, int y, console_color color, int speed) : console_firework_example::firework(x, y, color, speed) {}
explicit firework_exploded(const firework& firework) : console_firework_example::firework(firework) {}
void paint() const override {
write(x_ - 1, y_ - 1, " - ");
write(x_ - 1, y_, "-+-");
write(x_ - 1, y_ + 1, " - ");
}
};
class firework_start_expanded1 : public firework {
public:
firework_start_expanded1(int x, int y, console_color color, int speed) : console_firework_example::firework(x, y, color, speed) {}
explicit firework_start_expanded1(const firework& firework) : console_firework_example::firework(firework) {}
void paint() const override {
write(x_ - 2, y_ - 2, " --- ");
write(x_ - 2, y_ - 1, "-+++-");
write(x_ - 2, y_, "-+#+-");
write(x_ - 2, y_ + 1, "-+++-");
write(x_ - 2, y_ + 2, " --- ");
}
};
class firework_start_expanded2 : public firework {
public:
firework_start_expanded2(int x, int y, console_color color, int speed) : console_firework_example::firework(x, y, color, speed) {}
explicit firework_start_expanded2(const firework& firework) : console_firework_example::firework(firework) {}
void paint() const override {
write(x_ - 2, y_ - 2, " +++ ");
write(x_ - 2, y_ - 1, "++#++");
write(x_ - 2, y_, "-# #-");
write(x_ - 2, y_ + 1, "++#++");
write(x_ - 2, y_ + 2, " +++ ");
}
};
class firework_start_expanded3 : public firework {
public:
firework_start_expanded3(int x, int y, console_color color, int speed) : console_firework_example::firework(x, y, color, speed) {}
explicit firework_start_expanded3(const firework& firework) : console_firework_example::firework(firework) {}
void paint() const override {
write(x_ - 2, y_ - 2, " # ");
write(x_ - 2, y_ - 1, "## ##");
write(x_ - 2, y_, "# #");
write(x_ - 2, y_ + 1, "## ##");
write(x_ - 2, y_ + 2, " # ");
}
};
class firework_start_expanded4 : public firework {
public:
firework_start_expanded4(int x, int y, console_color color, int speed) : console_firework_example::firework(x, y, color, speed) {}
explicit firework_start_expanded4(const firework& firework) : console_firework_example::firework(firework) {}
void paint() const override {
write(x_ - 2, y_ - 2, " # # ");
write(x_ - 2, y_ - 1, "# #");
write(x_ - 2, y_, " ");
write(x_ - 2, y_ + 1, "# #");
write(x_ - 2, y_ + 2, " # # ");
}
};
class firework_end : public firework {
public:
firework_end(int x, int y, console_color color, int speed) : console_firework_example::firework(x, y, color, speed) {}
explicit firework_end(const firework& firework) : console_firework_example::firework(firework) {}
void paint() const override {
write(x_ - 2, y_ - 2, " ");
write(x_ - 2, y_ - 1, " ");
write(x_ - 2, y_, " ");
write(x_ - 2, y_ + 1, " ");
write(x_ - 2, y_ + 2, " ");
}
};
class program static_ {
public:
static void main() {
console::write_line("Adjuste window size and pres any key when ready to start...");
auto console_height = console::window_height();
auto console_width = console::window_width();
auto rand = xtd::random {};
auto fireworks = std::list<ptr<firework>> {};
fireworks.push_back(new_ptr<firework_start>(rand.next(2, console_width - 2), rand.next(2, console_height - 2), colors[rand.next(colors.size())], rand.next(1, 5)));
auto fireworks_to_removed = list<ptr<firework>> {};
for (auto& firework : fireworks) {
if (is<firework_end>(firework)) fireworks_to_removed.push_back(firework);
explode(firework);
}
for (auto& firework : fireworks_to_removed)
fireworks.remove(firework);
}
}
private:
static void explode(ptr<firework>& firework) {
firework->paint();
if (is<firework_start_expanded4>(firework)) firework = new_ptr<firework_end>(*firework);
if (is<firework_exploded>(firework)) firework = new_ptr<firework_start_expanded1>(*firework);
if (date_time::now().ticks() % firework->delay() == 0 && is<firework_start>(firework)) firework = new_ptr<firework_exploded>(*firework);
}
};
}
startup_(console_firework_example::program::main);
// This code can produce the following output with colors :
//
//
// # #
// # # -
// -+-
// # # -
// # #
//
// +++
// ++#++
// -# #-
// ++#++
// --- +++
// -+++-
// -+#+-
// -+++-
// ---
//
// ---
// -+++-
// -+#+-
// -+++-
// + ---
//
//
//
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:79
static int32 window_height()
Gets the height of the console window area.
static void write(arg_t &&value)
Writes the text representation of the specified value to the standard output stream.
Definition console.hpp:462
static console_color foreground_color()
Gets the foreground color of the console.
static void clear()
Clears the console buffer and corresponding console window of display information.
static bool reset_color()
Sets the foreground and background console colors to their defaults.
static console_key_info read_key()
Obtains the next character or function key pressed by the user. The pressed key is displayed in the c...
static void set_cursor_position(int32 left, int32 top)
Sets the position of the cursor.
static bool key_available()
Gets a value indicating whether a key press is available in the input stream.
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
static int32 window_width()
Gets the width of the console window area.
static bool cursor_visible()
Gets a value indicating whether the cursor is visible.
static date_time now() noexcept
Gets a xtd::date_time object that is set to the current date and time on this computer,...
Represents a pseudo-random number generator, a device that produces a sequence of numbers that meet c...
Definition random.hpp:44
static void sleep(int32 milliseconds_timeout)
Suspends the current thread for a specified time.
#define static_
This keyword is use to represent a static object. A static object can't be instantiated (constructors...
Definition static.hpp:37
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.hpp:167
xtd::sptr< type_t > ptr
The xtd::ptr object is a shared pointer.
Definition ptr.hpp:27
std::chrono::duration< int64, tick > ticks
Represents a tick duration.
Definition ticks.hpp:21
ptr< type_t > new_ptr(args_t &&... args)
The xtd::new_ptr operator creates a xtd::ptr object.
Definition new_ptr.hpp:24
bool is(xtd::any value)
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:485
console_color
Specifies constants that define foreground and background colors for the console.
Definition console_color.hpp:20
@ black
The color black.
Definition console_color.hpp:24
@ blue
The color blue.
Definition console_color.hpp:42
@ magenta
The color magenta (purplish red).
Definition console_color.hpp:50
@ cyan
The color cyan (blue-green).
Definition console_color.hpp:46
@ green
The color green.
Definition console_color.hpp:44
@ red
The color red.
Definition console_color.hpp:48
@ yellow
The color yellow.
Definition console_color.hpp:52
@ white
The color white.
Definition console_color.hpp:54
@ y
The Y key.
Definition console_key.hpp:136
@ x
The X key.
Definition console_key.hpp:134
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:16
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8