#tetris #game #command-line #rust

app tetris-rs

terminal based tetris game written in rust

4 releases

0.1.4 Feb 7, 2023
0.1.3 Feb 3, 2023
0.1.2 Feb 3, 2023
0.1.0 Feb 3, 2023

#716 in Games

23 downloads per month

MIT license

34KB
872 lines

tetris-rs

Command line based Tetris in rust

A good example for beginners in Rust + Project architecture development

Project Structure

├── 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

cargo install tetris-rs

excute the compiled command tetris:

tetris

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

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

Config

Setting environment variables to customize

# defaults:
FEATURE_BRICK=true #bool

ACCELERATE_MODE=true #bool

WIDTH=13 #number

HEIGHT=20  #number

TEXTURE_FULL='#'  #char
TEXTURE_WALL='O' #char
TEXTURE_EMPTY=' ' #char
TEXTURE_SHADOW='+' #char

example:

TEXTURE_FULL='%' FEATURE_BRICK=false tetris 

Bricks

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

  • Eliminating one row, you get 200 scores.

  • 60 more points per combo.

  • You get 1 point for every time you accelerate.

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

    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

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()
            }
        }
    }
}

Dependencies

~3–11MB
~121K SLoC