add: basic game

This commit is contained in:
Charles
2024-12-21 00:38:36 -08:00
parent d1e2668bf8
commit d6853a1f37
60 changed files with 354 additions and 3 deletions
Generated
+56
View File
@@ -2,6 +2,62 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "kids-alphabet" name = "kids-alphabet"
version = "0.1.0" version = "0.1.0"
dependencies = [
"sdl2",
]
[[package]]
name = "lazy_static"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]]
name = "libc"
version = "0.2.169"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
[[package]]
name = "sdl2"
version = "0.37.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b498da7d14d1ad6c839729bd4ad6fc11d90a57583605f3b4df2cd709a9cd380"
dependencies = [
"bitflags",
"lazy_static",
"libc",
"sdl2-sys",
]
[[package]]
name = "sdl2-sys"
version = "0.37.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "951deab27af08ed9c6068b7b0d05a93c91f0a8eb16b6b816a5e73452a43521d3"
dependencies = [
"cfg-if",
"libc",
"version-compare",
]
[[package]]
name = "version-compare"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "579a42fc0b8e0c63b76519a339be31bed574929511fa53c1a3acae26eb258f29"
+1
View File
@@ -4,3 +4,4 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
sdl2 = { version = "0.37.0", features = ["image"]}
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 110 KiB

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 148 KiB

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 5.6 KiB

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 5.0 KiB

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
View File
Binary file not shown.
+1 -1
View File
@@ -14,7 +14,7 @@
{ {
defaultPackage = naersk-lib.buildPackage ./.; defaultPackage = naersk-lib.buildPackage ./.;
devShell = with pkgs; mkShell { devShell = with pkgs; mkShell {
buildInputs = [ cargo rustc rustfmt pre-commit rustPackages.clippy ]; buildInputs = [ cargo rustc rustfmt pre-commit rustPackages.clippy SDL2 SDL2_image ];
RUST_SRC_PATH = rustPlatform.rustLibSrc; RUST_SRC_PATH = rustPlatform.rustLibSrc;
}; };
} }
+296 -2
View File
@@ -1,3 +1,297 @@
fn main() { extern crate sdl2;
println!("Hello, world!");
use sdl2::event::Event;
use sdl2::image::{InitFlag, LoadTexture};
use sdl2::keyboard::Keycode;
use sdl2::pixels::Color;
use sdl2::render::Texture;
use std::collections::HashMap;
use std::time::Duration;
pub fn main() -> Result<(), String> {
let sdl_context = sdl2::init()?;
let video_subsystem = sdl_context.video()?;
let _image = sdl2::image::init(InitFlag::PNG);
let window = video_subsystem
.window("rust-sdl2 demo: Video", 1024, 600)
.position_centered()
.opengl()
.build()
.map_err(|e| e.to_string())?;
let mut canvas = window.into_canvas().build().map_err(|e| e.to_string())?;
let texture_creator = canvas.texture_creator();
let mut textures: HashMap<&str, Texture> = HashMap::default();
textures.insert(
"a",
texture_creator
.load_texture_bytes(include_bytes!("../assets/a.png"))
.unwrap(),
);
textures.insert(
"b",
texture_creator
.load_texture_bytes(include_bytes!("../assets/b.png"))
.unwrap(),
);
textures.insert(
"c",
texture_creator
.load_texture_bytes(include_bytes!("../assets/c.png"))
.unwrap(),
);
textures.insert(
"d",
texture_creator
.load_texture_bytes(include_bytes!("../assets/d.png"))
.unwrap(),
);
textures.insert(
"e",
texture_creator
.load_texture_bytes(include_bytes!("../assets/e.png"))
.unwrap(),
);
textures.insert(
"f",
texture_creator
.load_texture_bytes(include_bytes!("../assets/f.png"))
.unwrap(),
);
textures.insert(
"g",
texture_creator
.load_texture_bytes(include_bytes!("../assets/g.png"))
.unwrap(),
);
textures.insert(
"h",
texture_creator
.load_texture_bytes(include_bytes!("../assets/h.png"))
.unwrap(),
);
textures.insert(
"i",
texture_creator
.load_texture_bytes(include_bytes!("../assets/i.png"))
.unwrap(),
);
textures.insert(
"j",
texture_creator
.load_texture_bytes(include_bytes!("../assets/j.png"))
.unwrap(),
);
textures.insert(
"k",
texture_creator
.load_texture_bytes(include_bytes!("../assets/k.png"))
.unwrap(),
);
textures.insert(
"l",
texture_creator
.load_texture_bytes(include_bytes!("../assets/l.png"))
.unwrap(),
);
textures.insert(
"m",
texture_creator
.load_texture_bytes(include_bytes!("../assets/m.png"))
.unwrap(),
);
textures.insert(
"n",
texture_creator
.load_texture_bytes(include_bytes!("../assets/n.png"))
.unwrap(),
);
textures.insert(
"o",
texture_creator
.load_texture_bytes(include_bytes!("../assets/o.png"))
.unwrap(),
);
textures.insert(
"p",
texture_creator
.load_texture_bytes(include_bytes!("../assets/p.png"))
.unwrap(),
);
textures.insert(
"q",
texture_creator
.load_texture_bytes(include_bytes!("../assets/q.png"))
.unwrap(),
);
textures.insert(
"r",
texture_creator
.load_texture_bytes(include_bytes!("../assets/r.png"))
.unwrap(),
);
textures.insert(
"s",
texture_creator
.load_texture_bytes(include_bytes!("../assets/s.png"))
.unwrap(),
);
textures.insert(
"t",
texture_creator
.load_texture_bytes(include_bytes!("../assets/t.png"))
.unwrap(),
);
textures.insert(
"u",
texture_creator
.load_texture_bytes(include_bytes!("../assets/u.png"))
.unwrap(),
);
textures.insert(
"v",
texture_creator
.load_texture_bytes(include_bytes!("../assets/v.png"))
.unwrap(),
);
textures.insert(
"w",
texture_creator
.load_texture_bytes(include_bytes!("../assets/w.png"))
.unwrap(),
);
textures.insert(
"x",
texture_creator
.load_texture_bytes(include_bytes!("../assets/x.png"))
.unwrap(),
);
textures.insert(
"y",
texture_creator
.load_texture_bytes(include_bytes!("../assets/y.png"))
.unwrap(),
);
textures.insert(
"z",
texture_creator
.load_texture_bytes(include_bytes!("../assets/z.png"))
.unwrap(),
);
// Special words
textures.insert(
"cat",
texture_creator
.load_texture_bytes(include_bytes!("../assets/cat.png"))
.unwrap(),
);
textures.insert(
"dog",
texture_creator
.load_texture_bytes(include_bytes!("../assets/dog.png"))
.unwrap(),
);
let opts: Vec<&str> = textures.keys().map(|s| *s).collect();
canvas.set_draw_color(Color::RGB(255, 0, 0));
canvas.clear();
canvas.present();
let mut event_pump = sdl_context.event_pump()?;
let mut line = String::new();
'running: loop {
for event in event_pump.poll_iter() {
match event {
Event::KeyDown {
keycode: Some(kc), ..
} => {
line.extend(
match kc {
Keycode::A => "a",
Keycode::B => "b",
Keycode::C => "c",
Keycode::D => "d",
Keycode::E => "e",
Keycode::F => "f",
Keycode::G => "g",
Keycode::H => "h",
Keycode::I => "i",
Keycode::J => "j",
Keycode::K => "k",
Keycode::L => "l",
Keycode::M => "m",
Keycode::N => "n",
Keycode::O => "o",
Keycode::P => "p",
Keycode::Q => "q",
Keycode::R => "r",
Keycode::S => "s",
Keycode::T => "t",
Keycode::U => "u",
Keycode::V => "v",
Keycode::W => "w",
Keycode::X => "x",
Keycode::Y => "y",
Keycode::Z => "z",
Keycode::Escape => break 'running,
_ => "",
}
.chars(),
);
}
Event::Quit { .. } => break 'running,
_ => {}
}
}
let mut matches = vec![];
let mut skip_chars = 0;
let mut chars = line.chars();
for i in 0..line.len() {
let part_line = find_matches(chars.as_str(), &opts);
for part in part_line {
matches.push((i, part));
}
if matches.len() == 0 {
// Advance the line so we don't leak chars
skip_chars = i;
}
chars.next();
}
let matches: Vec<&str> = matches.into_iter().map(|(_, s)| s).collect();
let show = if matches.len() > 0 { matches[0] } else { "" };
// Discard extra characters from line
let (_, tline) = line.split_at(skip_chars);
line = String::from(tline);
canvas.clear();
if let Some(tex) = textures.get(show) {
canvas.copy(tex, None, None).unwrap();
}
canvas.present();
::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 30));
// The rest of the game loop goes here...
}
Ok(())
}
fn find_matches<'a, 'b>(word: &'b str, opts: &'a [&'a str]) -> Vec<&'a str> {
if word.len() == 0 {
return vec![];
}
let mut ret = vec![];
for opt in opts {
if &word == opt {
ret.push(*opt)
}
}
ret
} }