tick and crooked piece

This commit is contained in:
2026-03-23 21:53:24 +07:00
parent 973a01fc09
commit 3c32aeaea6
+91 -10
View File
@@ -73,29 +73,110 @@ fn draw_side_matrix(matrix: &SideMatrix, x: f32, y: f32, cell_size: f32) {
}
}
struct Piece {
x: i32,
y: i32,
}
impl Piece {
fn new(buffer: i32) -> Self {
Self { x: 4, y: buffer } // spawn at top center
}
}
fn draw_piece(piece: &Piece, matrix_x: f32, matrix_y: f32, cell_size: f32, visible_offset: i32) {
let draw_y = piece.y - visible_offset;
if draw_y < 0 { return; }
draw_rectangle(
matrix_x + piece.x as f32 * cell_size,
matrix_y + draw_y as f32 * cell_size,
cell_size,
cell_size,
BLUE,
);
}
struct Game {
matrix: Matrix,
hold: SideMatrix,
preview: SideMatrix,
piece: Piece, // current
drop_timer: f32,
drop_interval: f32, // in seconds
}
impl Game {
fn new() -> Self {
Self {
matrix: Matrix::new(10, 40, 20),
hold: SideMatrix::new(4, 4),
preview: SideMatrix::new(4, 20),
piece: Piece::new(20),
drop_timer: 0.0,
drop_interval: 1.0,
}
}
fn update(&mut self, dt: f32) {
self.drop_timer += dt;
if self.drop_timer >= self.drop_interval {
self.drop_timer = 0.0;
self.drop_piece();
}
}
fn drop_piece(&mut self) {
let next_y = self.piece.y + 1;
if next_y >= self.matrix.height as i32 {
self.lock_piece();
self.piece = Piece::new(20);
} else {
self.piece.y = next_y;
}
}
fn lock_piece(&mut self) {
self.matrix.cells[self.piece.y as usize][self.piece.x as usize] = true;
}
}
#[macroquad::main("tetrus")]
async fn main() {
let matrix = Matrix::new(10, 40, 20);
let hold = SideMatrix::new(4, 4);
let preview = SideMatrix::new(4, 20);
let mut game = Game::new();
let margin_px: f32 = 20.0;
let gap_cells: f32 = 1.0; // gap between hold, main, and preview matrix, in cells unit
loop {
game.update(get_frame_time());
clear_background(BLACK);
let cell_px = (screen_height() - margin_px * 2.0) / matrix.visible as f32;
let cell_px = (screen_height() - margin_px * 2.0) / game.matrix.visible as f32;
let gap_px = gap_cells * cell_px;
let hold_x = margin_px;
let main_x = hold_x + hold.width as f32 * cell_px + gap_px;
let preview_x = main_x + matrix.width as f32 * cell_px + gap_px;
let main_x = hold_x + game.hold.width as f32 * cell_px + gap_px;
let preview_x = main_x + game.matrix.width as f32 * cell_px + gap_px;
let top_y = margin_px;
draw_side_matrix(&hold, hold_x, top_y, cell_px);
draw_matrix(&matrix, main_x, top_y, cell_px);
draw_side_matrix(&preview, preview_x, top_y, cell_px);
draw_side_matrix(&game.hold, hold_x, top_y, cell_px);
draw_matrix(&game.matrix, main_x, top_y, cell_px);
draw_piece(
&game.piece,
main_x,
top_y,
cell_px,
(game.matrix.height - game.matrix.visible) as i32,
);
draw_side_matrix(&game.preview, preview_x, top_y, cell_px);
next_frame().await;
}