Skip to content

Commit

Permalink
Added confettii
Browse files Browse the repository at this point in the history
  • Loading branch information
csherratt committed Aug 24, 2020
1 parent a52c8b3 commit 4de7ee7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "cargo-fun"
version = "0.1.1"
version = "0.2.0"
authors = ["Cora Sherratt <cora.sherratt@gmail.com>"]
edition = "2018"
license = "MIT"
Expand Down
78 changes: 54 additions & 24 deletions src/main.rs
Expand Up @@ -18,7 +18,7 @@ struct Drawable {

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
enum DrawableType {
Streamer, Balloon
Streamer, Balloon, Confetti
}

const BALLOON: &'static str = " .───.
Expand All @@ -34,37 +34,50 @@ const BALLOON: &'static str = " .───.
";

const COLORS: &'static [Color] = &[Red, Green, Yellow, Blue, Magenta, Cyan];
const CONFETTI: &'static [char] = &['\\', '/', '-', '|'];

impl Drawable {
fn draw(&self, height: i32, into: &mut std::io::Stdout) {
let mut rng = rand::thread_rng();

if self.what == DrawableType::Streamer {
for y in (self.y as i32 - 4)..(self.y as i32) {
if y < 0 || y > height {
continue;
match self.what {
DrawableType::Streamer => {
for y in (self.y as i32 - 4)..(self.y as i32) {
if y < 0 || y > height {
continue;
}
into.queue(cursor::MoveTo(self.x as u16, y as u16)).unwrap();
if rng.gen_bool(0.5) {
into.queue(PrintStyledContent(style("(").with(self.color))).unwrap();
} else {
into.queue(PrintStyledContent(style(")").with(self.color))).unwrap();
}
}
into.queue(cursor::MoveTo(self.x as u16, y as u16)).unwrap();
if rng.gen_bool(0.5) {
into.queue(PrintStyledContent(style("(").with(self.color))).unwrap();
} else {
into.queue(PrintStyledContent(style(")").with(self.color))).unwrap();
}
DrawableType::Balloon => {
for (i, line) in BALLOON.lines().enumerate() {
let y = i as i32 + self.y as i32;
if y < 0 || y > height {
continue;
}
into.queue(cursor::MoveTo(self.x as u16, y as u16)).unwrap();
for char in line.chars() {
if char == ' ' {
into.queue(cursor::MoveRight(1)).unwrap();
} else {
into.queue(PrintStyledContent(style(char).with(self.color))).unwrap();
}
}
}
}
} else {
for (i, line) in BALLOON.lines().enumerate() {
let y = i as i32 + self.y as i32;
DrawableType::Confetti => {
let y = self.y as i32;
if y < 0 || y > height {
continue;
return;
}
let index = rng.gen_range(0, CONFETTI.len());
into.queue(cursor::MoveTo(self.x as u16, y as u16)).unwrap();
for char in line.chars() {
if char == ' ' {
into.queue(cursor::MoveRight(1)).unwrap();
} else {
into.queue(PrintStyledContent(style(char).with(self.color))).unwrap();
}
}
into.queue(PrintStyledContent(style(CONFETTI[index]).with(self.color))).unwrap();
}
}
}
Expand Down Expand Up @@ -115,12 +128,28 @@ fn add_balloons(width: u16, height: u16, drawables: &mut Vec<Drawable>) {
}
}

fn add_confetti(width: u16, height: u16, drawables: &mut Vec<Drawable>) {
let mut rng = rand::thread_rng();

for _ in 0..256 {
let x = rng.gen_range(0, width);
let y = rng.gen_range(-(height as f32) * 0.75, 0f32);
let color = COLORS[rng.gen_range(0, COLORS.len())];
drawables.push(Drawable {
x: x as f32,
y,
color,
what: DrawableType::Confetti
});
}
}

fn move_everything(drawables: &mut Vec<Drawable>) {
let count = drawables.len() as f32;
for (i, drawable) in drawables.iter_mut().enumerate() {
let rate = 0.5f32 + 2.0 * (i as f32) / count;
match drawable.what {
DrawableType::Streamer => {
DrawableType::Streamer | DrawableType::Confetti => {
drawable.y += rate;
},
DrawableType::Balloon => {
Expand All @@ -136,13 +165,14 @@ fn main() {

add_streamers(width, height, &mut drawables);
add_balloons(width, height, &mut drawables);
add_confetti(width, height, &mut drawables);

drawables.shuffle(&mut rand::thread_rng());

for _ in 0..100 {
for _ in 0..50 {
render(width, height, &drawables).unwrap();
move_everything(&mut drawables);
std::thread::sleep(Duration::from_millis(50));
std::thread::sleep(Duration::from_millis(60));
}

stdout().execute(terminal::Clear(terminal::ClearType::All)).unwrap();
Expand Down

0 comments on commit 4de7ee7

Please sign in to comment.