Compare commits

...

2 Commits

Author SHA1 Message Date
Greg Shuflin
d030563e49 More work 2024-10-03 18:29:35 -07:00
Greg Shuflin
bdd464bde2 Start updating 2024-10-03 18:12:09 -07:00
3 changed files with 2789 additions and 1234 deletions

3900
iced-tetris/Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,14 +1,13 @@
[package]
name = "iced-tetris"
version = "0.1.0"
edition = "2018"
edition = "2021"
resolver = "2"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
iced = { git = "https://github.com/hecrj/iced", rev = "099981cfc2f61a1f37e84100592d65babb94fb82", features = ["tokio", "canvas"] }
iced_native = { git = "https://github.com/hecrj/iced", rev = "099981cfc2f61a1f37e84100592d65babb94fb82"}
iced = { version = "0.13.1", features = ["tokio"] }
chrono = "0.4.19"
rand = "0.8.4"
tetris-logic = { path = "../tetris-logic" }

View File

@ -1,17 +1,107 @@
/*
use iced::widget::canvas::{self, Path, Stroke, Text};
use iced::{
executor, keyboard, time, Application, Color, Command, Element, Length, Point, Rectangle,
Settings, Size, Subscription,
};
use iced_native::{event, subscription, Event};
use tetris_logic::{BlockGrid, MoveDirection, Tetromino};
*/
use iced::{
widget::{button, text},
Element, Subscription, Task,
};
use tetris_logic::{BlockGrid, MoveDirection, Tetromino};
fn main() -> iced::Result {
Tetris::run(Settings::default())
//Tetris::run(Settings::default())
iced::application(Tetris::title, update, view)
.subscription(subscription)
.run_with(|| (Tetris::new(), Task::none()))
}
#[derive(Debug, Clone)]
enum Message {
Up,
Down,
Left,
Right,
Pause,
Tick(chrono::DateTime<chrono::Local>),
}
fn update(state: &mut Tetris, message: Message) {
match message {
Message::Pause => state.paused = !state.paused,
Message::Tick(_) => {
if !state.paused {
state.ticks += 1;
}
}
Message::Up => {
state.blocks.rotate_active_piece();
}
Message::Down => {
state.blocks.move_active_piece(MoveDirection::HardDrop);
}
Message::Left => {
state.blocks.move_active_piece(MoveDirection::Left);
}
Message::Right => {
state.blocks.move_active_piece(MoveDirection::Right);
}
};
if state.blocks.piece_currently_active() {
if state.ticks % 10 == 0 && !state.paused {
state.blocks.move_active_piece(MoveDirection::SoftDrop);
}
} else {
let piece: Tetromino = rand::random();
state.blocks.drop_piece(piece);
}
let lines_removed = state.blocks.clear_pieces();
state.lines_removed += lines_removed;
}
fn subscription(_state: &Tetris) -> Subscription<Message> {
let keyboard_subscription = iced::keyboard::on_key_press(|key, _modifiers| {
use iced::keyboard::key::{Key, Named};
match key {
Key::Named(Named::ArrowUp) => Some(Message::Up),
Key::Named(Named::ArrowDown) => Some(Message::Down),
Key::Named(Named::ArrowLeft) => Some(Message::Left),
Key::Named(Named::ArrowRight) => Some(Message::Right),
Key::Named(Named::Space) => Some(Message::Pause),
_ => None,
}
});
/*
let keyboard_subscription = subscription::events_with(|event, status| {
if let event::Status::Captured = status {
return None;
}
match event {
Event::Keyboard(keyboard::Event::KeyPressed { key_code, .. }) => {
handle_keypress(key_code)
}
_ => None,
}
});
*/
let time_subscription = iced::time::every(std::time::Duration::from_millis(50))
.map(|_| Message::Tick(chrono::Local::now()));
Subscription::batch([time_subscription, keyboard_subscription])
}
fn view(_state: &Tetris) -> Element<Message> {
button(text("FOO")).on_press(Message::Up).into()
}
struct Tetris {
background_cache: canvas::Cache,
//background_cache: canvas::Cache,
blocks: BlockGrid,
ticks: usize,
paused: bool,
@ -19,9 +109,13 @@ struct Tetris {
}
impl Tetris {
fn title(&self) -> String {
String::from("Tetris - Iced")
}
fn new() -> Tetris {
Tetris {
background_cache: canvas::Cache::default(),
//background_cache: canvas::Cache::default(),
blocks: BlockGrid::new(),
ticks: 0,
paused: false,
@ -30,6 +124,7 @@ impl Tetris {
}
}
/*
impl Application for Tetris {
type Executor = executor::Default;
type Message = Message;
@ -105,7 +200,9 @@ impl Application for Tetris {
.into()
}
}
*/
/*
impl<'a> canvas::Program<Message> for Tetris {
fn draw(&self, bounds: Rectangle, _cursor: canvas::Cursor) -> Vec<canvas::Geometry> {
let game_width = bounds.width / 3.0;
@ -171,7 +268,9 @@ impl<'a> canvas::Program<Message> for Tetris {
vec![background, frame.into_geometry()]
}
}
*/
/*
fn handle_keypress(key_code: keyboard::KeyCode) -> Option<Message> {
use keyboard::KeyCode;
@ -184,13 +283,4 @@ fn handle_keypress(key_code: keyboard::KeyCode) -> Option<Message> {
_ => return None,
})
}
#[derive(Debug)]
enum Message {
Up,
Down,
Left,
Right,
Pause,
Tick(chrono::DateTime<chrono::Local>),
}
*/