Skip to content

Commit

Permalink
Merge pull request #7 from jako4295/mvp-game
Browse files Browse the repository at this point in the history
Mvp game
  • Loading branch information
jako4295 authored Jul 4, 2024
2 parents d9aef41 + 066c720 commit 640d3ed
Show file tree
Hide file tree
Showing 21 changed files with 487 additions and 115 deletions.
146 changes: 146 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ rand = "0.8.5"
color-eyre = "0.6.3"
crossterm = "0.27.0"
ratatui = "0.26.3"
chrono = "0.4.38"
Binary file added ideas/game.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ideas/menu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ideas/overview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ideas/settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 18 additions & 17 deletions src/char_lib/load_chars.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dict::{Dict, DictIface};
use dict::Dict;
use rand::seq::SliceRandom;
use std::fs::read_to_string;
use std::str;
Expand All @@ -24,19 +24,20 @@ pub fn chose_random(char_vec: Vec<char>) -> char {
output_char.to_owned()
}

fn main() {
let mut dict: Dict<bool> = Dict::<bool>::new();
dict.add("letters".to_string(), true);
dict.add("cap_letters".to_string(), true);
dict.add("numbers".to_string(), true);
let char_vec: Vec<char> = load_files_to_vec(dict);
println!(
"{:?}",
char_vec
.iter()
.map(|x| x.to_string() + " ")
.collect::<String>()
);
let rand = chose_random(char_vec);
println!("{rand}");
}
// use dict::DictIface;
// fn main() {
// let mut dict: Dict<bool> = Dict::<bool>::new();
// dict.add("letters".to_string(), true);
// dict.add("cap_letters".to_string(), true);
// dict.add("numbers".to_string(), true);
// let char_vec: Vec<char> = load_files_to_vec(dict);
// println!(
// "{:?}",
// char_vec
// .iter()
// .map(|x| x.to_string() + " ")
// .collect::<String>()
// );
// let rand = chose_random(char_vec);
// println!("{rand}");
// }
15 changes: 15 additions & 0 deletions src/crabtype_icon.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
" ",
" ",
" ",
" ",
" ",
" ████████ ████████████████ ",
" ████████ ██████████ ",
" ████ ███████████████████████ █████████████ ",
" ████ ██ █ ██ █ ██ ████████ ████ ",
" ████ ██████ ██████████████████ ██████████████ ",
" ██████████ ██ █ ██ ██████ ██ ███ ",
" ██████████ ██████ ██████████████ ██ █████████ ",
" ",
" ",
" ",
117 changes: 117 additions & 0 deletions src/game/game_page.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use crate::char_lib::load_chars;
use chrono::{DateTime, Duration, Local};
use dict::{Dict, DictIface};

use ratatui::{
prelude::*,
symbols::border,
widgets::{block::*, *},
};

pub fn get_dict() -> Dict<bool> {
let mut dict: Dict<bool> = Dict::<bool>::new();
dict.add("letters".to_string(), true);
dict.add("cap_letters".to_string(), false);
dict.add("numbers".to_string(), false);
dict
}

#[derive(Debug)]
pub struct GameLogic {
pub time: Duration,
pub start_time: DateTime<Local>,
pub random_char: char,
pub char_vec: Vec<char>,
pub score: u32,
pub play: bool,
pub current_char: Option<char>,
}

impl Default for GameLogic {
fn default() -> GameLogic {
let dict: Dict<bool> = get_dict();
let start_t = Local::now();
let load_char = load_chars::load_files_to_vec(dict);

GameLogic {
time: Local::now().signed_duration_since(start_t),
start_time: start_t,
random_char: load_chars::chose_random(load_char.to_owned()),
char_vec: load_char,
score: 0,
play: true,
current_char: None,
}
}
}

impl GameLogic {
pub fn get_time(&mut self) {
let time_now = Local::now();
self.time = time_now.signed_duration_since(self.start_time)
}
pub fn reset(&mut self) {
self.start_time = Local::now();
self.score = 0;
self.play = true;
}
pub fn compare_pressed_char(&mut self, character: char) {
if self.time >= Duration::seconds(30) {
self.play = false
}
if self.random_char == character {
self.random_char = load_chars::chose_random(self.char_vec.to_owned());
self.score += 1;
}
}

pub fn render(&self, area: Rect, buf: &mut Buffer) {
let title = Title::from(" CrabType ".bold());
// let instructions = Title::from(Line::from(vec![
// " Decrement ".into(),
// "<Left>".blue().bold(),
// " Increment ".into(),
// "<Right>".blue().bold(),
// " Quit ".into(),
// "<Q> ".blue().bold(),
// ]));
let block = Block::default()
.title(title.alignment(Alignment::Center))
// .title(
// instructions
// .alignment(Alignment::Center)
// .position(Position::Bottom),
// )
.borders(Borders::ALL)
.border_set(border::THICK);

let text = vec![
text::Line::from(vec![
Span::from("Timer: "),
Span::from(self.time.num_seconds().to_string()),
Span::from("sec / 30 sec"),
]),
text::Line::from(" "),
text::Line::from(vec![
Span::from("Value: "),
self.random_char.to_string().yellow(),
]),
];
let text2 = vec![
text::Line::from(vec![Span::from("You score is: ")]),
text::Line::from(" "),
text::Line::from(vec![Span::from(self.score.to_string())]),
];
if self.play {
Paragraph::new(text)
.centered()
.block(block)
.render(area, buf);
} else {
Paragraph::new(text2)
.centered()
.block(block)
.render(area, buf);
}
}
}
1 change: 1 addition & 0 deletions src/game/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod game_page;
1 change: 0 additions & 1 deletion src/lib.rs

This file was deleted.

Loading

0 comments on commit 640d3ed

Please sign in to comment.