tetris_core

Crates.iotetris_core
lib.rstetris_core
version0.2.1
sourcesrc
created_at2019-06-16 12:54:38.044758
updated_at2022-11-22 22:32:25.11034
descriptionSimple Tetris game model with no UI or Game engine
homepagehttps://github.com/etoledom/tetris_core
repositoryhttps://github.com/etoledom/tetris_core
max_upload_size
id141533
size49,080
(etoledom)

documentation

https://github.com/etoledom/tetris_core

README

tetris_core

Simple Tetris game logic (with no interface)

WARNING: This project was created on an early stage of learning RUST. It might lack many good practices.

Usage:

  • Use any drawing library. (i.e: piston_window)
  • Use any randomizer library or method (i.e: rand)

As an example of implementation, you can check https://github.com/etoledom/rust_practice/blob/master/07_tetris/src/main.rs

Implement the Randomizer trait

struct Rand;
impl Randomizer for Rand {
    fn random_between(&self, lower: i32, higher: i32) -> i32 {
        let mut rng = rand::thread_rng();
        return rng.gen_range(lower, higher);
    }
}

Instantiate a Tetris Game instance using an instance or your randomizer struct and the desired board size:

let game_size = Size {
    height: 20,
    width: 10,
};
let mut game = Game::new(&game_size, Box::new(rand));

update(&mut self, delta_time: f64)

Call game.update(delta_time); on every game loop.

draw(&self) -> Vec<Block>

Get the board model to be drawn:

let game_blocks = game.draw();

A block is a structure that specifies the block's position, size and color in rgba. Position and size are unitary, you can give it the unit and size you want.

struct Block {
	pub rect: Rect,
	pub color: Color,
}

perform(&mut self, action: Action)

Perform movement and rotation actions

game.perform(Action::Rotate);

is_game_over(&self) -> bool

Checks if is game over.

get_score(&self) -> u64

Gets the current score.

Commit count: 13

cargo fmt