Compare commits

...

3 Commits

Author SHA1 Message Date
ccde1b6538 feat: current canvas, window & mainloop 2025-06-17 11:49:08 -05:00
d90785a61a docs: implementation document 2025-06-17 11:48:59 -05:00
227c603ffe chore: project name & add lazy_static 2025-06-17 11:48:53 -05:00
4 changed files with 84 additions and 33 deletions

15
Cargo.lock generated
View File

@@ -2,13 +2,6 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "Pac-Man"
version = "0.1.0"
dependencies = [
"sdl2",
]
[[package]]
name = "bitflags"
version = "1.3.2"
@@ -33,6 +26,14 @@ version = "0.2.147"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
[[package]]
name = "pacman"
version = "0.1.0"
dependencies = [
"lazy_static",
"sdl2",
]
[[package]]
name = "sdl2"
version = "0.35.2"

View File

@@ -1,9 +1,10 @@
[package]
name = "Pac-Man"
name = "pacman"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
lazy_static = "1.4.0"
sdl2 = { version = "0.35", features = ["image", "ttf", "mixer"] }

35
IMPLEMENTATION.md Normal file
View File

@@ -0,0 +1,35 @@
# Implementation
A document detailing the implementation the project from rendering, to game logic, to build systems.
## Rendering
1. Map
- May require procedural text generation later on (cacheable?)
2. Pacman
3. Ghosts
- Requires colors
4. Items
5. Interface
- Requires fonts
## Grid System
1. How does the grid system work?
The grid is 28 x 36 (although, the map texture is 28 x 37), and each cell is 24x24 (pixels).
Many of the walls in the map texture only occupy a portion of the cell, so some items are able to render across multiple cells.
24x24 assets include pellets, the energizer, and the map itself ()
2. What constraints must be enforced on Ghosts and PacMan?
3. How do movement transitions work?
All entities store a precise position, and a direction. This position is only used for animation, rendering, and collision purposes. Otherwise, a separate 'cell position' (which is 24 times less precise, owing to the fact that it is based on the entity's position within the grid).
When an entity is transitioning between cells, movement directions are acknowledged, but won't take effect until the next cell has been entered completely.
4. Between transitions, how does collision detection work?
It appears the original implementation used cell-level detection.
I worry this may be prone to division errors. Make sure to use rounding (50% >=).

View File

@@ -1,17 +1,22 @@
use crate::constants::{WINDOW_HEIGHT, WINDOW_WIDTH};
use sdl2::event::{Event, WindowEvent};
use sdl2::image::LoadTexture;
use crate::game::Game;
use crate::textures::TextureManager;
use sdl2::event::{Event};
use sdl2::keyboard::Keycode;
use sdl2::pixels::Color;
use sdl2::render::{Texture, Canvas};
use std::time::Duration;
use sdl2::render::{Canvas, Texture};
use std::time::{Duration, Instant};
#[cfg(target_os = "emscripten")]
pub mod emscripten;
mod board;
mod constants;
mod direction;
mod game;
mod pacman;
mod textures;
mod entity;
mod animation;
fn redraw(canvas: &mut Canvas<sdl2::video::Window>, tex: &Texture, i: u8) {
canvas.set_draw_color(Color::RGB(i, i, i));
@@ -28,7 +33,6 @@ pub fn main() {
let window = video_subsystem
.window("Pac-Man", WINDOW_WIDTH, WINDOW_HEIGHT)
.position_centered()
.resizable()
.build()
.expect("Could not initialize window");
@@ -36,22 +40,21 @@ pub fn main() {
.into_canvas()
.build()
.expect("Could not build canvas");
let texture_creator = canvas.texture_creator();
let map_texture = texture_creator
.load_texture("assets/map.png")
.expect("Could not load pacman texture");
canvas
.copy(&map_texture, None, None)
.expect("Could not render texture on canvas");
.set_logical_size(WINDOW_WIDTH, WINDOW_HEIGHT)
.expect("Could not set logical size");
let mut i = 0u8;
let texture_creator = canvas.texture_creator();
let mut game = Game::new(&mut canvas, TextureManager::new(&texture_creator));
let mut event_pump = sdl_context
.event_pump()
.expect("Could not get SDL EventPump");
game.draw();
game.tick();
let mut main_loop = || {
for event in event_pump.poll_iter() {
match event {
@@ -63,20 +66,31 @@ pub fn main() {
} => return false,
event @ Event::KeyDown { .. } => {
println!("{:?}", event);
},
Event::Window { win_event, .. } => {
if let WindowEvent::Resized(width, height) = win_event {
i = i.wrapping_add(1);
canvas.set_logical_size(width as u32, height as u32).unwrap();
redraw(&mut canvas, &map_texture, i);
}
},
}
_ => {}
}
}
canvas.present();
let tick_time = {
let start = Instant::now();
game.tick();
start.elapsed()
};
let draw_time = {
let start = Instant::now();
game.draw();
start.elapsed()
};
// Alert if tick time exceeds 10ms
if tick_time > Duration::from_millis(3) {
println!("Tick took: {:?}", tick_time);
}
if draw_time > Duration::from_millis(3) {
println!("Draw took: {:?}", draw_time);
}
::std::thread::sleep(Duration::from_millis(10));
true
};