diff options
| author | omagdy7 <omar.professional8777@gmail.com> | 2022-09-12 16:42:17 +0200 |
|---|---|---|
| committer | omagdy7 <omar.professional8777@gmail.com> | 2022-09-12 16:42:17 +0200 |
| commit | 36a7260f908de87b021d65641d5faa307b7d9652 (patch) | |
| tree | 71bafc0969c3f97f38293f1a3a7f8061b3d0cd47 /src/gameloop.rs | |
| parent | d608f1749f26279772cfde92d8224c46d0c6e556 (diff) | |
| download | gof-rs-36a7260f908de87b021d65641d5faa307b7d9652.tar.xz gof-rs-36a7260f908de87b021d65641d5faa307b7d9652.zip | |
Refactored the code in seperate files
Diffstat (limited to 'src/gameloop.rs')
| -rw-r--r-- | src/gameloop.rs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/gameloop.rs b/src/gameloop.rs new file mode 100644 index 0000000..d573868 --- /dev/null +++ b/src/gameloop.rs @@ -0,0 +1,70 @@ +use crate::generation::*; + +use colored::Colorize; +use crossterm::{ + cursor::{Hide, MoveTo, Show}, + event, + event::{poll, Event, KeyCode, KeyEvent}, + style::Stylize, + terminal, + terminal::{EnterAlternateScreen, LeaveAlternateScreen}, + ExecutableCommand, QueueableCommand, +}; +use rand::{thread_rng, Rng}; +use std::{ + io::{self, Write}, + thread::sleep, + time::Duration, +}; + + +pub fn run() { + let mut stdout = io::stdout(); + let mut frame: Gen = new_gen(); + let mut nxt; + + terminal::enable_raw_mode().unwrap(); + stdout.execute(EnterAlternateScreen).unwrap(); + stdout.execute(Hide).unwrap(); + + 'gameloop: loop { + while event::poll(Duration::default()).unwrap() { + if let Event::Key(key_event) = event::read().unwrap() { + match key_event.code { + KeyCode::Esc | KeyCode::Char('q') => { + break 'gameloop; + } + KeyCode::Char('s') => { + frame = new_gen(); + render_gen(&mut stdout, &frame) + } + + KeyCode::Char('n') => { + nxt = next_gen(&mut frame); + render_gen(&mut stdout, &nxt) + } + KeyCode::Char('a') => 'animate: loop { + nxt = next_gen(&mut frame); + render_gen(&mut stdout, &nxt); + sleep(Duration::from_millis(16)); + if (poll(Duration::from_millis(1))).unwrap() { + if let Event::Key(k) = event::read().unwrap() { + match k.code { + KeyCode::Char('q') => break 'animate, + _ => {} + } + } + } else { + } + }, + _ => {} + } + } + } + } + + stdout.execute(Show).unwrap(); + stdout.execute(LeaveAlternateScreen).unwrap(); + terminal::disable_raw_mode().unwrap(); + +} |
