done, just need to add some documentation and clean up code a bit
This commit is contained in:
parent
351ae425ec
commit
351e795f29
106
falling.rs
106
falling.rs
@ -1,10 +1,10 @@
|
||||
use rand::Rng;
|
||||
use std::io::{stdin,stdout,Write};
|
||||
use console::Term;
|
||||
|
||||
fn output_levels(starting_level: u64, ending_level: u64, character_x_coord: u8, character_icon: char, levels: &Vec<Vec<u8>>) {
|
||||
print!("{}[2J", 27 as char);
|
||||
for i in starting_level..ending_level {
|
||||
print!("{} ", i);
|
||||
for j in 0..levels[i as usize].len() {
|
||||
let object = levels[i as usize][j as usize];
|
||||
if i == starting_level && j == character_x_coord as usize { print!("{}",character_icon) }
|
||||
@ -14,29 +14,64 @@ fn output_levels(starting_level: u64, ending_level: u64, character_x_coord: u8,
|
||||
else if object == 1 { print!("/") }
|
||||
else { print!("|") }
|
||||
}
|
||||
println!();
|
||||
println!(" {}",i);
|
||||
}
|
||||
}
|
||||
fn generate_level(level_to_generate: u64, difficulty: u8, mut left_wall: u8, mut right_wall: u8, levels: &mut Vec<Vec<u8>>) {
|
||||
fn generate_level(level_to_generate: u64, difficulty: u8, left_wall: &mut i8, right_wall: &mut i8, screen_width: u8, levels: &mut Vec<Vec<u8>>) {
|
||||
let mut new_level: Vec<u8> = Vec::new();
|
||||
|
||||
let left_wall_change = rand::thread_rng().gen_range(-2..2);
|
||||
let right_wall_change = rand::thread_rng().gen_range(-1..1);
|
||||
let mut left_wall_temp = left_wall as i8 + left_wall_change;
|
||||
if left_wall_temp >= 0 { left_wall = left_wall_temp as u8 }
|
||||
right_wall = (right_wall as i8 + right_wall_change) as u8;
|
||||
let left_wall_change = rand::thread_rng().gen_range(-1..2);
|
||||
let right_wall_change = rand::thread_rng().gen_range(-1..2);
|
||||
//println!("lwc: {}, rwc: {}", left_wall_change, right_wall_change);
|
||||
*left_wall = *left_wall + left_wall_change;
|
||||
*right_wall = *right_wall + right_wall_change;
|
||||
if *left_wall <= 0 {
|
||||
*left_wall = 1;
|
||||
}
|
||||
if *right_wall >= screen_width.try_into().unwrap() {
|
||||
*right_wall = screen_width as i8-1
|
||||
}
|
||||
//println!("lw: {}, rw: {}", left_wall, right_wall);
|
||||
if (*right_wall-*left_wall).abs() < 8 {
|
||||
println!("VERY CLOSE");
|
||||
*left_wall-=2;
|
||||
*right_wall+=2;
|
||||
}
|
||||
//let mut left_wall_temp = left_wall as i8 + left_wall_change;
|
||||
//if left_wall_temp >= 0 { left_wall = left_wall_temp as u8 }
|
||||
//right_wall = (right_wall as i8 + right_wall_change) as u8;
|
||||
|
||||
for i in 0..right_wall+1 {
|
||||
for i in 0..screen_width {
|
||||
new_level.push(4)
|
||||
}
|
||||
|
||||
new_level[left_wall as usize] = 0;
|
||||
for i in left_wall+1..right_wall-1 {
|
||||
for i in *left_wall+1..*right_wall-1 {
|
||||
let mut rng = rand::thread_rng();
|
||||
let object: u8 = rand::thread_rng().gen_range(3..difficulty);
|
||||
new_level[i as usize] = object;
|
||||
}
|
||||
new_level[right_wall as usize] = 0;
|
||||
for i in 0..*left_wall {
|
||||
if i == *left_wall-1 && left_wall_change > 0 {
|
||||
new_level[i as usize] = 2;
|
||||
}
|
||||
else if i == *left_wall-1 && left_wall_change < 0 {
|
||||
new_level[i as usize] = 1;
|
||||
}
|
||||
else {
|
||||
new_level[i as usize] = 0;
|
||||
}
|
||||
}
|
||||
for i in *right_wall..screen_width as i8 {
|
||||
if i == *right_wall && right_wall_change > 0 {
|
||||
new_level[i as usize] = 2;
|
||||
}
|
||||
else if i == *right_wall && right_wall_change < 0 {
|
||||
new_level[i as usize] = 1;
|
||||
}
|
||||
else {
|
||||
new_level[i as usize] = 0;
|
||||
}
|
||||
}
|
||||
levels.push(new_level);
|
||||
}
|
||||
|
||||
@ -52,39 +87,50 @@ fn input() -> String{
|
||||
}
|
||||
return s;
|
||||
}
|
||||
fn check_if_alive(levels: &Vec<Vec<u8>>, level: usize, x_coord: usize) -> bool {
|
||||
println!("{}",levels[level][x_coord]);
|
||||
if levels[level][x_coord] == 0 || levels[level][x_coord] == 1 || levels[level][x_coord] == 2 || levels[level][x_coord] == 3 {
|
||||
println!("GAME OVER");
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut levels: Vec<Vec<u8>> = Vec::new();
|
||||
|
||||
let screen_height: u8 = 20;
|
||||
let screen_width: u8 = 11;
|
||||
let screen_height: u8 = 70;
|
||||
let screen_width: u8 = 100;
|
||||
let difficulty: u8 = 10;
|
||||
|
||||
let stdout = Term::buffered_stdout();
|
||||
|
||||
let mut current_level: u64 = 0;
|
||||
let character_icon: char = 'µ';
|
||||
let mut x_coord: u8 = (screen_width as f32/ 2.0) as u8; // Distance from left wall
|
||||
let mut left_wall: u8 = 0;
|
||||
let mut right_wall: u8 = screen_width;
|
||||
|
||||
let mut left_wall: i8 = (screen_width as f32/ 4.0) as i8;
|
||||
let mut right_wall: i8 = ((screen_width as f32 * 3.0)/ 4.0) as i8;
|
||||
let mut alive: bool = true;
|
||||
|
||||
for i in 1..screen_height {
|
||||
generate_level(i as u64, difficulty, left_wall, right_wall, &mut levels)
|
||||
generate_level(i as u64, difficulty, &mut left_wall, &mut right_wall, screen_width, &mut levels)
|
||||
};
|
||||
loop {
|
||||
generate_level(current_level, difficulty, left_wall, right_wall, &mut levels);
|
||||
'game_loop: loop {
|
||||
generate_level(current_level, difficulty, &mut left_wall, &mut right_wall, screen_width, &mut levels);
|
||||
output_levels(current_level, current_level + screen_height as u64, x_coord, character_icon, &levels);
|
||||
let direction = input();
|
||||
if direction == "r" {
|
||||
if x_coord < screen_width-1 {
|
||||
x_coord += 1
|
||||
if let Ok(character) = stdout.read_char() {
|
||||
match character {
|
||||
'd' => if x_coord < screen_width-1 { x_coord += 1 },
|
||||
'a' => if x_coord > 1 { x_coord -= 1 },
|
||||
'q' => break 'game_loop,
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
else if direction == "l" {
|
||||
if x_coord > 1 {
|
||||
x_coord -= 1
|
||||
}
|
||||
current_level+=1;
|
||||
alive = check_if_alive(&levels, current_level as usize, x_coord as usize);
|
||||
if alive == false {
|
||||
break 'game_loop
|
||||
}
|
||||
current_level+=1
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user