Skip to content

Instantly share code, notes, and snippets.

@rgaidot
Created November 21, 2023 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgaidot/2d519fd01e1d8a88cba9c868531ad6de to your computer and use it in GitHub Desktop.
Save rgaidot/2d519fd01e1d8a88cba9c868531ad6de to your computer and use it in GitHub Desktop.
tetris
use std::fmt;
use std::time::Duration;
fn main() {
// Set up the game board
let mut board = vec![];
for _ in 0..10 {
board.push(vec![0; 10]);
}
// Create the Tetrominoes
let tetrominoes: Vec<Tetromino> = vec![
Tetromino::Block(Rectangle::new(10, 10)),
Tetromino::Ice(Rectangle::new(10, 10)),
Tetromino::Magnet { x: 5, y: 5 },
Tetromino::Roof(Rectangle::new(10, 10)),
Tetromino::Wall(Rectangle::new(10, 10)),
];
// Set up the game loop
let mut score = 0;
while true {
// Get user input for the next move
let move = get_move();
// Move the Tetromino to the correct position
tetrominoes[move as usize].position = Position::new(board[move.y][move.x]);
// Check if the Tetromino has reached the bottom of the screen
if tetrominoes[move as usize].position.y + tetrominoes[move as usize].size > board.len() - 10 {
break;
}
// Update the score and display the current state of the game
score += 1;
print_board(board);
println!("Score: {}", score);
// Wait for the user to input a move again
let _ = std::time::sleep(Duration::from_secs(0.5));
}
}
fn get_move() -> Tetromino {
// Get user input for the next move
println!("Select a Tetromino:");
println!("1. Block");
println!("2. Ice");
println!("3. Magnet");
println!("4. Roof");
println!("5. Wall");
let mut user_input = String::new();
io::stdin().read_line(&mut user_input).unwrap();
match user_input.trim() {
"1" => Tetromino::Block(Rectangle::new(10, 10)),
"2" => Tetromino::Ice(Rectangle::new(10, 10)),
"3" => Tetromino::Magnet { x: 5, y: 5 },
"4" => Tetromino::Roof(Rectangle::new(10, 10)),
"5" => Tetromino::Wall(Rectangle::new(10, 10)),
_ => panic!("Invalid input"),
}
}
fn print_board(board: Vec<Vec<u8>>) {
for row in board {
println!("Row {}", row);
for cell in row {
println!("{}", cell);
}
println!();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment