tetris-rs

Command line based Tetris in rust

A good example for beginners in Rust + Project architecture development

Project Structure

rs ├── Cargo.lock ├── Cargo.toml ├── readme.md //this document ├── src │ ├── bricks.rs │ ├── display.rs //display game core │ ├── env.rs // Environment Variable structure │ ├── game.rs //game core │ ├── main.rs //entry │ └── record.rs //score computing & statistics └── target ├── CACHEDIR.TAG ├── debug ├── package └── release

tetris-rs

Get Started

add the executable command to your path

sh cargo install tetris-rs

excute the compiled command tetris:

sh tetris

Or you can download the source file and compile manually, if you don't want tetris to be a globally recognizable executable command

sh git clone https://github.com/kunieone/tetris_rs && cd tetris_rs cargo run .

Config

Setting environment variables to customize

```toml

defaults:

FEATURE_BRICK=true #bool

ACCELERATE_MODE=true #bool

WIDTH=13 #number

HEIGHT=20 #number

TEXTUREFULL='#' #char TEXTUREWALL='O' #char TEXTUREEMPTY=' ' #char TEXTURESHADOW='+' #char

```

example:

sh TEXTURE_FULL='%' FEATURE_BRICK=false tetris

Bricks

rust pub enum BrickType { // 7 classic bricks I, O, T, S, Z, L, J, // FEATURE_BRICK to enable feature bricks // # Dot, // # # // ### Desk, // # // ## Angle, // # // ## // ## W, // ## Bean, }

Score Computation

```rust impl Record { pub fn compute(&mut self, rowsnum: usize) { if rowsnum == 0 { self.combo = 0; return; } for _ in 0..rowsnum { self.comboonce() } }

fn combo_once(&mut self) {
    self.score += 200 + (self.combo * 60) as i64;
    self.eliminate_rows += 1;
    self.combo += 1;
    // 计算历史最高连击
    if self.combo > self.high_combo {
        self.high_combo = self.combo
    }
}

} ```

Accelerate Mode

accelerate the update frequency based on your score

rust pub fn update_by(&mut self, counter: i32) { match self.cfg.accelerate { true => { let time = match self.record.score { 0..=5999 => 100, 6000..=9999 => 70, 10000..=24999 => 60, 25000..=39999 => 50, 40000..=59999 => 45, _ => 40, }; if counter % (time) == 0 { self.update() } } false => { if counter % (100) == 0 { self.update() } } } }