Crates.io | blocks_lib |
lib.rs | blocks_lib |
version | 0.3.3 |
source | src |
created_at | 2024-02-16 00:46:25.397799 |
updated_at | 2024-02-28 20:31:05.423567 |
description | Gamelogic only library for a simple tetromino based falling blocks game |
homepage | https://github.com/jvanderberg/blocks-lib |
repository | https://github.com/jvanderberg/blocks-lib |
max_upload_size | |
id | 1141874 |
size | 44,113 |
⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
⬜🟨🟨⬜⬜🟪⬜⬜⬜⬜⬜⬜⬜⬜⬜🟧⬜🟦⬜⬜⬜⬜⬜🟩🟩⬜🟥🟥⬜⬜
⬜🟨🟨⬜🟪🟪🟪⬜🟫🟫🟫🟫⬜🟧🟧🟧⬜🟦🟦🟦🟦⬜🟩🟩⬜⬜⬜🟥🟥⬜
⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
Blocks-lib is a game-logic only version of a simple tetromino based falling blocks game.
The game sticks roughly to 'official' piece dynamics but does not implement 'kicking' off the walls. It is entirely single threaded, with no async.
cargo add blocks_lib
To create a game, you call the gamestate constructor, add an event handler, and then call start().
let gs = GameState::new(
width,
height,
hide_next_piece,
difficulty
);
let ev = |ge: &GameEvent, gs: &GameState| match ge {
... Do something with the GameEvent to update the presentation ...
};
gs.add_event_handler(&ev);
gs.start();
The GameState structure has a number of methods to move the game forward and control pieces:
The event handler gets called with significant game events:
A visual implementation will respond to these events and interrogate the GameState object passed to the event handler to update the presentation layer.
GameState::get_board() returns the current pieces on the board. Each entry in the board.cells nested vector holds a PieceColor enum variant: Wall, Empty, Red, Green, Blue, Yellow, Cyan, Magenta, Orange, Tracer. The color of Wall and Tracer are up to the implementation, but it's suggested to use the actual color names for the rest. Empty is just an empty space in the board.
Here's an example drawing loop:
for y in 0..board.height {
for x in 0..board.width {
draw_square(x, y, board.cells[x as usize][y as usize]);
}
}