big bang
This commit is contained in:
+102
@@ -0,0 +1,102 @@
|
||||
#![allow(clippy::cast_precision_loss)]
|
||||
|
||||
use macroquad::prelude::*;
|
||||
|
||||
struct Matrix {
|
||||
cells: Vec<Vec<bool>>,
|
||||
width: usize,
|
||||
height: usize,
|
||||
visible: usize,
|
||||
}
|
||||
|
||||
impl Matrix {
|
||||
fn new(width: usize, height: usize, visible: usize) -> Self {
|
||||
Self {
|
||||
cells: vec![vec![false; width]; height],
|
||||
width,
|
||||
height,
|
||||
visible,
|
||||
}
|
||||
}
|
||||
|
||||
fn visible_cells(&self) -> &[Vec<bool>] {
|
||||
&self.cells[self.height - self.visible..]
|
||||
}
|
||||
}
|
||||
|
||||
// dry stands for do repeat yourself :3
|
||||
struct SideMatrix {
|
||||
cells: Vec<Vec<bool>>,
|
||||
width: usize,
|
||||
// height: usize,
|
||||
}
|
||||
|
||||
impl SideMatrix {
|
||||
fn new(width: usize, height: usize) -> Self {
|
||||
Self {
|
||||
cells: vec![vec![false; width]; height],
|
||||
width,
|
||||
// height,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_matrix(matrix: &Matrix, x: f32, y: f32, cell_size: f32) {
|
||||
for (i, row) in matrix.visible_cells().iter().enumerate() {
|
||||
for (j, _) in row.iter().enumerate() {
|
||||
let color = if (i + j) % 2 == 0 { RED } else { YELLOW };
|
||||
|
||||
draw_rectangle(
|
||||
x + j as f32 * cell_size,
|
||||
y + i as f32 * cell_size,
|
||||
cell_size,
|
||||
cell_size,
|
||||
color,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_side_matrix(matrix: &SideMatrix, x: f32, y: f32, cell_size: f32) {
|
||||
for (i, row) in matrix.cells.iter().enumerate() {
|
||||
for (j, _) in row.iter().enumerate() {
|
||||
let color = if (i + j) % 2 == 0 { RED } else { YELLOW };
|
||||
|
||||
draw_rectangle(
|
||||
x + j as f32 * cell_size,
|
||||
y + i as f32 * cell_size,
|
||||
cell_size,
|
||||
cell_size,
|
||||
color,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[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 margin_px: f32 = 20.0;
|
||||
let gap_cells: f32 = 1.0; // gap between hold, main, and preview matrix, in cells unit
|
||||
|
||||
loop {
|
||||
clear_background(BLACK);
|
||||
|
||||
let cell_px = (screen_height() - margin_px * 2.0) / 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 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);
|
||||
|
||||
next_frame().await;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user