falling
This commit is contained in:
commit
351ae425ec
90
falling.rs
Normal file
90
falling.rs
Normal file
@ -0,0 +1,90 @@
|
||||
use rand::Rng;
|
||||
use std::io::{stdin,stdout,Write};
|
||||
|
||||
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) }
|
||||
else if object >= 4 { print!(" ") }
|
||||
else if object == 3 { print!("¯") }
|
||||
else if object == 2 { print!("\\") }
|
||||
else if object == 1 { print!("/") }
|
||||
else { print!("|") }
|
||||
}
|
||||
println!();
|
||||
}
|
||||
}
|
||||
fn generate_level(level_to_generate: u64, difficulty: u8, mut left_wall: u8, mut right_wall: 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;
|
||||
|
||||
for i in 0..right_wall+1 {
|
||||
new_level.push(4)
|
||||
}
|
||||
|
||||
new_level[left_wall as usize] = 0;
|
||||
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;
|
||||
levels.push(new_level);
|
||||
}
|
||||
|
||||
fn input() -> String{
|
||||
let mut s=String::new();
|
||||
let _=stdout().flush();
|
||||
stdin().read_line(&mut s).expect("Did not enter a correct string");
|
||||
if let Some('\n')=s.chars().next_back() {
|
||||
s.pop();
|
||||
}
|
||||
if let Some('\r')=s.chars().next_back() {
|
||||
s.pop();
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut levels: Vec<Vec<u8>> = Vec::new();
|
||||
|
||||
let screen_height: u8 = 20;
|
||||
let screen_width: u8 = 11;
|
||||
let difficulty: u8 = 10;
|
||||
|
||||
|
||||
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;
|
||||
|
||||
|
||||
for i in 1..screen_height {
|
||||
generate_level(i as u64, difficulty, left_wall, right_wall, &mut levels)
|
||||
};
|
||||
loop {
|
||||
generate_level(current_level, difficulty, left_wall, right_wall, &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
|
||||
}
|
||||
}
|
||||
else if direction == "l" {
|
||||
if x_coord > 1 {
|
||||
x_coord -= 1
|
||||
}
|
||||
}
|
||||
current_level+=1
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user