From bc01ee65a756f68161717ef15c167c4ce21d2382 Mon Sep 17 00:00:00 2001 From: mii8080 <39086319+morioka22@users.noreply.github.com> Date: Sat, 2 Oct 2021 09:41:36 +0900 Subject: [PATCH 1/6] Create README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..25ae916 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# rustris +文化祭のために約 1 日で作成したテトリス風のゲーム。 From 760c51b8c45850a8288789e63fa735fbe26f9037 Mon Sep 17 00:00:00 2001 From: mii8080 <39086319+morioka22@users.noreply.github.com> Date: Sat, 2 Oct 2021 00:51:10 +0000 Subject: [PATCH 2/6] add score --- src/game_data.rs | 6 ++++-- src/main.rs | 11 ++++++++++- src/rustris.rs | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/game_data.rs b/src/game_data.rs index 05173a3..629cf43 100644 --- a/src/game_data.rs +++ b/src/game_data.rs @@ -14,7 +14,8 @@ pub struct GameData { pub mino_rotation: MinoRotation, pub field_size: (usize, usize), pub show_ghost: bool, - pub hold_mino: Option + pub hold_mino: Option, + pub score: i64, } impl GameData { @@ -91,7 +92,8 @@ impl GameData { mino_rotation: MinoRotation::Up, field_size, show_ghost, - hold_mino: None + hold_mino: None, + score: 0, } } } diff --git a/src/main.rs b/src/main.rs index 5bc60c0..e39fef6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -270,6 +270,7 @@ fn main() { { let mut rustris = rustris.lock().unwrap(); + let mut clear_lines = 0; for x in 0..(rustris.game_data.field_size.1) { let mut air = false; for y in 0..(rustris.game_data.field_size.0) { @@ -279,6 +280,7 @@ fn main() { } if !air { + clear_lines += 1; for y in 0..(rustris.game_data.field_size.0) { rustris.game_data.field[x][y] = Block::Air; } @@ -293,6 +295,8 @@ fn main() { } } } + + rustris.game_data.score += clear_lines * 100; } { @@ -314,6 +318,11 @@ fn main() { cursor::MoveTo(console_size.0 / 2, console_size.1 / 2), Print("Game Over") ).unwrap(); + execute!( + stdout, + cursor::MoveTo(console_size.0 / 2, console_size.1 / 2 + 1), + Print(format!("Score : {}", rustris.game_data.score)) + ).unwrap(); thread::sleep(Duration::from_secs(3)); @@ -340,7 +349,7 @@ fn main() { } *exit_flag.lock().unwrap() = true; - frame_thread.join(); + frame_thread.join().unwrap(); main(); } diff --git a/src/rustris.rs b/src/rustris.rs index c0fc0f2..f92379b 100644 --- a/src/rustris.rs +++ b/src/rustris.rs @@ -205,7 +205,7 @@ impl Rustris { print_buffer += "\n"; } } - print_buffer += "     Z ホールド, X 左回転, C 右回転\n"; + print_buffer += &format!("     Z ホールド, X 左回転, C 右回転 score: {}\n", self.game_data.score); for _ in 0..(if (console_size.1 - print_buffer.lines().count() as u16) > 0 { console_size.1 - print_buffer.lines().count() as u16 - 1 } else { console_size.1 - print_buffer.lines().count() as u16 }) { print_buffer += "\n"; From 53d133260f8530a9ee4f440e00ce1d378c152942 Mon Sep 17 00:00:00 2001 From: mii8080 <39086319+morioka22@users.noreply.github.com> Date: Sat, 2 Oct 2021 01:25:22 +0000 Subject: [PATCH 3/6] move clear line function --- src/main.rs | 28 +--------------------------- src/rustris.rs | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 29 deletions(-) diff --git a/src/main.rs b/src/main.rs index e39fef6..347ce08 100644 --- a/src/main.rs +++ b/src/main.rs @@ -270,33 +270,7 @@ fn main() { { let mut rustris = rustris.lock().unwrap(); - let mut clear_lines = 0; - for x in 0..(rustris.game_data.field_size.1) { - let mut air = false; - for y in 0..(rustris.game_data.field_size.0) { - if let Block::Air = rustris.game_data.field[x][y] { - air = true; - } - } - - if !air { - clear_lines += 1; - for y in 0..(rustris.game_data.field_size.0) { - rustris.game_data.field[x][y] = Block::Air; - } - - for x2 in (0..(rustris.game_data.field_size.1)).rev() { - if x2 < x && x2 < 20 { - for y2 in 0..(rustris.game_data.field_size.0) { - rustris.game_data.field[x2 + 1][y2] = rustris.game_data.field[x2][y2]; - rustris.game_data.field[x2][y2] = Block::Air; - } - } - } - } - } - - rustris.game_data.score += clear_lines * 100; + rustris.check_clear(); } { diff --git a/src/rustris.rs b/src/rustris.rs index f92379b..6041ecd 100644 --- a/src/rustris.rs +++ b/src/rustris.rs @@ -6,12 +6,43 @@ use crate::{block::Block, game_data::*, game_status::GameStatus, mino::Mino, min #[derive(Debug)] pub struct Rustris { pub game_data: GameData, - pub game_status: GameStatus + pub game_status: GameStatus, + pub t_spin: bool, } impl Rustris { pub fn new(game_data: GameData) -> Rustris { - Rustris { game_data, game_status: GameStatus::Playing } + Rustris { game_data, game_status: GameStatus::Playing, t_spin: false } + } + + pub fn check_clear(&mut self) { + let mut clear_lines = 0; + for x in 0..(self.game_data.field_size.1) { + let mut air = false; + for y in 0..(self.game_data.field_size.0) { + if let Block::Air = self.game_data.field[x][y] { + air = true; + } + } + + if !air { + clear_lines += 1; + for y in 0..(self.game_data.field_size.0) { + self.game_data.field[x][y] = Block::Air; + } + + for x2 in (0..(self.game_data.field_size.1)).rev() { + if x2 < x && x2 < 20 { + for y2 in 0..(self.game_data.field_size.0) { + self.game_data.field[x2 + 1][y2] = self.game_data.field[x2][y2]; + self.game_data.field[x2][y2] = Block::Air; + } + } + } + } + } + + self.game_data.score += clear_lines * 100; } pub fn get_next_mino(&mut self) -> Mino { From 9d4c0ec9ff135466aecab035309d9e3f22850681 Mon Sep 17 00:00:00 2001 From: mii8080 <39086319+morioka22@users.noreply.github.com> Date: Sat, 2 Oct 2021 10:54:20 +0900 Subject: [PATCH 4/6] Create LICENSE --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..6adfbe2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 mii8080 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From e4e6bc9e246882c912bd99999d511cffeae7f548 Mon Sep 17 00:00:00 2001 From: mii8080 <39086319+morioka22@users.noreply.github.com> Date: Sat, 2 Oct 2021 02:01:13 +0000 Subject: [PATCH 5/6] add game speed --- src/game_data.rs | 2 ++ src/main.rs | 10 ++++++++-- src/rustris.rs | 1 + 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/game_data.rs b/src/game_data.rs index 629cf43..829f3bb 100644 --- a/src/game_data.rs +++ b/src/game_data.rs @@ -16,6 +16,7 @@ pub struct GameData { pub show_ghost: bool, pub hold_mino: Option, pub score: i64, + pub game_speed: f32, } impl GameData { @@ -94,6 +95,7 @@ impl GameData { show_ghost, hold_mino: None, score: 0, + game_speed: 0.5, } } } diff --git a/src/main.rs b/src/main.rs index 347ce08..3bf70aa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -147,7 +147,12 @@ fn main() { if *exit_flag_rc.lock().unwrap() { break; } - thread::sleep(Duration::from_secs_f32(0.5)); + let mut game_speed = 0.5f32; + { + let rustris_rc = rustris_rc.lock().unwrap(); + game_speed = rustris_rc.game_data.game_speed; + } + thread::sleep(Duration::from_secs_f32(game_speed)); { let mut rustris_rc = rustris_rc.lock().unwrap(); if !rustris_rc.move_mino(1, 0) { @@ -169,8 +174,9 @@ fn main() { } } { + let mut rustris_rc = rustris_rc.lock().unwrap(); let mut stdout = stdout_rc.lock().unwrap(); - let buf = rustris_rc.lock().unwrap().show(); + let buf = rustris_rc.show(); execute!(stdout, Print(buf)).unwrap() } } diff --git a/src/rustris.rs b/src/rustris.rs index 6041ecd..8373850 100644 --- a/src/rustris.rs +++ b/src/rustris.rs @@ -43,6 +43,7 @@ impl Rustris { } self.game_data.score += clear_lines * 100; + self.game_data.game_speed += 0.01 * clear_lines as f32; } pub fn get_next_mino(&mut self) -> Mino { From 0263237729337a1dbebbdbe1b9aa181464e1605f Mon Sep 17 00:00:00 2001 From: mii8080 <39086319+morioka22@users.noreply.github.com> Date: Sun, 3 Oct 2021 00:22:29 +0000 Subject: [PATCH 6/6] fix check clear --- src/main.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3bf70aa..f5c5be0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,9 +5,9 @@ mod rustris; mod mino_rotation; mod game_status; mod super_rotation; -use std::{io::stdout, process::exit, sync::{Arc, Mutex}, thread, time::Duration}; +use std::{io::stdout, sync::{Arc, Mutex}, thread, time::Duration}; -use crossterm::{cursor, event::{Event, KeyCode, KeyEvent, KeyModifiers, read}, execute, style::{Color, Print, ResetColor, SetBackgroundColor, SetForegroundColor}, terminal::{self, Clear, ClearType, enable_raw_mode}}; +use crossterm::{cursor, event::{Event, KeyCode, KeyEvent, KeyModifiers, read}, execute, style::{Color, Print, ResetColor, SetBackgroundColor}, terminal::{self, Clear, ClearType, enable_raw_mode}}; use game_data::GameData; use game_status::GameStatus; use mino_rotation::MinoRotation; @@ -164,6 +164,8 @@ fn main() { } else { rustris_rc.place_control_mino(Block::Block); rustris_rc.next_mino(); + rustris_rc.check_clear(); + ground_flag = false; *control_count_rc.lock().unwrap() = 0; before_control_count = 0; @@ -235,6 +237,7 @@ fn main() { modifiers: KeyModifiers::NONE }) => { let mut rustris = rustris.lock().unwrap(); + if let Some(holding) = rustris.game_data.hold_mino.clone() { let control_tmp = rustris.game_data.control_mino.clone(); rustris.game_data.control_mino = Some(holding);