mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-06 15:15:48 -06:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4a4e6e40a9 | |||
| ccde1b6538 | |||
| d90785a61a | |||
| 227c603ffe |
15
Cargo.lock
generated
15
Cargo.lock
generated
@@ -2,13 +2,6 @@
|
|||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 3
|
version = 3
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "Pac-Man"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"sdl2",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bitflags"
|
name = "bitflags"
|
||||||
version = "1.3.2"
|
version = "1.3.2"
|
||||||
@@ -33,6 +26,14 @@ version = "0.2.147"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
|
checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pacman"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"lazy_static",
|
||||||
|
"sdl2",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "sdl2"
|
name = "sdl2"
|
||||||
version = "0.35.2"
|
version = "0.35.2"
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "Pac-Man"
|
name = "pacman"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
lazy_static = "1.4.0"
|
||||||
sdl2 = { version = "0.35", features = ["image", "ttf", "mixer"] }
|
sdl2 = { version = "0.35", features = ["image", "ttf", "mixer"] }
|
||||||
|
|||||||
35
IMPLEMENTATION.md
Normal file
35
IMPLEMENTATION.md
Normal 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% >=).
|
||||||
127
src/constants.rs
127
src/constants.rs
@@ -1,44 +1,95 @@
|
|||||||
|
use lazy_static::lazy_static;
|
||||||
|
|
||||||
pub const BOARD_WIDTH: u32 = 28;
|
pub const BOARD_WIDTH: u32 = 28;
|
||||||
pub const BOARD_HEIGHT: u32 = 36;
|
pub const BOARD_HEIGHT: u32 = 37; // Adjusted to fit map texture?
|
||||||
pub const BLOCK_SIZE_24: u32 = 24;
|
pub const CELL_SIZE: u32 = 24;
|
||||||
pub const BLOCK_SIZE_32: u32 = 32;
|
|
||||||
|
|
||||||
pub const WINDOW_WIDTH: u32 = BLOCK_SIZE_24 * BOARD_WIDTH;
|
pub const WINDOW_WIDTH: u32 = CELL_SIZE * BOARD_WIDTH;
|
||||||
pub const WINDOW_HEIGHT: u32 = BLOCK_SIZE_24 * BOARD_HEIGHT;
|
pub const WINDOW_HEIGHT: u32 = CELL_SIZE * BOARD_HEIGHT;
|
||||||
|
|
||||||
pub const RAW_BOARD: &str = r###"
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
||||||
|
pub enum MapTile {
|
||||||
|
Empty,
|
||||||
|
Wall,
|
||||||
|
Pellet,
|
||||||
|
PowerPellet,
|
||||||
|
StartingPosition(u8),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const RAW_BOARD: [&str; BOARD_HEIGHT as usize] = [
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
"############################",
|
||||||
|
"#............##............#",
|
||||||
|
"#.####.#####.##.#####.####.#",
|
||||||
|
"#o####.#####.##.#####.####o#",
|
||||||
|
"#.####.#####.##.#####.####.#",
|
||||||
|
"#..........................#",
|
||||||
|
"#.####.##.########.##.####.#",
|
||||||
|
"#.####.##.########.##.####.#",
|
||||||
|
"#......##....##....##......#",
|
||||||
|
"######.##### ## #####.######",
|
||||||
|
" #.##### ## #####.# ",
|
||||||
|
" #.## 1 ##.# ",
|
||||||
|
" #.## ###==### ##.# ",
|
||||||
|
"######.## # # ##.######",
|
||||||
|
" . #2 3 4 # . ",
|
||||||
|
"######.## # # ##.######",
|
||||||
|
" #.## ######## ##.# ",
|
||||||
|
" #.## ##.# ",
|
||||||
|
" #.## ######## ##.# ",
|
||||||
|
"######.## ######## ##.######",
|
||||||
|
"#............##............#",
|
||||||
|
"#.####.#####.##.#####.####.#",
|
||||||
|
"#.####.#####.##.#####.####.#",
|
||||||
|
"#o..##.......0 .......##..o#",
|
||||||
|
"###.##.##.########.##.##.###",
|
||||||
|
"###.##.##.########.##.##.###",
|
||||||
|
"#......##....##....##......#",
|
||||||
|
"#.##########.##.##########.#",
|
||||||
|
"#.##########.##.##########.#",
|
||||||
|
"#..........................#",
|
||||||
|
"############################",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
];
|
||||||
|
|
||||||
############################
|
lazy_static! {
|
||||||
#............##............#
|
pub static ref BOARD: [[MapTile; BOARD_HEIGHT as usize]; BOARD_HEIGHT as usize] = {
|
||||||
#.####.#####.##.#####.####.#
|
let mut board = [[MapTile::Empty; BOARD_HEIGHT as usize]; BOARD_HEIGHT as usize];
|
||||||
#o####.#####.##.#####.####o#
|
|
||||||
#.####.#####.##.#####.####.#
|
|
||||||
#..........................#
|
|
||||||
#.####.##.########.##.####.#
|
|
||||||
#.####.##.########.##.####.#
|
|
||||||
#......##....##....##......#
|
|
||||||
######.##### ## #####.######
|
|
||||||
#.##### ## #####.#
|
|
||||||
#.## 1 ##.#
|
|
||||||
#.## ###==### ##.#
|
|
||||||
######.## # # ##.######
|
|
||||||
. #2 3 4 # .
|
|
||||||
######.## # # ##.######
|
|
||||||
#.## ######## ##.#
|
|
||||||
#.## ##.#
|
|
||||||
#.## ######## ##.#
|
|
||||||
######.## ######## ##.######
|
|
||||||
#............##............#
|
|
||||||
#.####.#####.##.#####.####.#
|
|
||||||
#.####.#####.##.#####.####.#
|
|
||||||
#o..##.......0 .......##..o#
|
|
||||||
###.##.##.########.##.##.###
|
|
||||||
###.##.##.########.##.##.###
|
|
||||||
#......##....##....##......#
|
|
||||||
#.##########.##.##########.#
|
|
||||||
#.##########.##.##########.#
|
|
||||||
#..........................#
|
|
||||||
############################
|
|
||||||
|
|
||||||
"###;
|
for y in 0..BOARD_HEIGHT as usize {
|
||||||
|
let line = RAW_BOARD[y];
|
||||||
|
|
||||||
|
for x in 0..BOARD_WIDTH as usize {
|
||||||
|
if x >= line.len() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
let i = (y * (BOARD_WIDTH as usize) + x) as usize;
|
||||||
|
let character = line
|
||||||
|
.chars()
|
||||||
|
.nth(x as usize)
|
||||||
|
.unwrap_or_else(|| panic!("Could not get character at {} = ({}, {})", i, x, y));
|
||||||
|
|
||||||
|
let tile = match character {
|
||||||
|
'#' => MapTile::Wall,
|
||||||
|
'.' => MapTile::Pellet,
|
||||||
|
'o' => MapTile::PowerPellet,
|
||||||
|
' ' => MapTile::Empty,
|
||||||
|
c @ '0' | c @ '1' | c @ '2' | c @ '3' | c @ '4' => {
|
||||||
|
MapTile::StartingPosition(c.to_digit(10).unwrap() as u8)
|
||||||
|
},
|
||||||
|
'=' => MapTile::Empty,
|
||||||
|
_ => panic!("Unknown character in board: {}", character),
|
||||||
|
};
|
||||||
|
|
||||||
|
board[x as usize][y as usize] = tile;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
board
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
64
src/main.rs
64
src/main.rs
@@ -1,17 +1,22 @@
|
|||||||
use crate::constants::{WINDOW_HEIGHT, WINDOW_WIDTH};
|
use crate::constants::{WINDOW_HEIGHT, WINDOW_WIDTH};
|
||||||
use sdl2::event::{Event, WindowEvent};
|
use crate::game::Game;
|
||||||
use sdl2::image::LoadTexture;
|
use crate::textures::TextureManager;
|
||||||
|
use sdl2::event::{Event};
|
||||||
use sdl2::keyboard::Keycode;
|
use sdl2::keyboard::Keycode;
|
||||||
use sdl2::pixels::Color;
|
use sdl2::pixels::Color;
|
||||||
use sdl2::render::{Texture, Canvas};
|
use sdl2::render::{Canvas, Texture};
|
||||||
use std::time::Duration;
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
#[cfg(target_os = "emscripten")]
|
#[cfg(target_os = "emscripten")]
|
||||||
pub mod emscripten;
|
pub mod emscripten;
|
||||||
|
|
||||||
mod board;
|
|
||||||
mod constants;
|
mod constants;
|
||||||
|
mod direction;
|
||||||
mod game;
|
mod game;
|
||||||
|
mod pacman;
|
||||||
|
mod textures;
|
||||||
|
mod entity;
|
||||||
|
mod animation;
|
||||||
|
|
||||||
fn redraw(canvas: &mut Canvas<sdl2::video::Window>, tex: &Texture, i: u8) {
|
fn redraw(canvas: &mut Canvas<sdl2::video::Window>, tex: &Texture, i: u8) {
|
||||||
canvas.set_draw_color(Color::RGB(i, i, i));
|
canvas.set_draw_color(Color::RGB(i, i, i));
|
||||||
@@ -28,7 +33,6 @@ pub fn main() {
|
|||||||
let window = video_subsystem
|
let window = video_subsystem
|
||||||
.window("Pac-Man", WINDOW_WIDTH, WINDOW_HEIGHT)
|
.window("Pac-Man", WINDOW_WIDTH, WINDOW_HEIGHT)
|
||||||
.position_centered()
|
.position_centered()
|
||||||
.resizable()
|
|
||||||
.build()
|
.build()
|
||||||
.expect("Could not initialize window");
|
.expect("Could not initialize window");
|
||||||
|
|
||||||
@@ -36,22 +40,21 @@ pub fn main() {
|
|||||||
.into_canvas()
|
.into_canvas()
|
||||||
.build()
|
.build()
|
||||||
.expect("Could not build canvas");
|
.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
|
canvas
|
||||||
.copy(&map_texture, None, None)
|
.set_logical_size(WINDOW_WIDTH, WINDOW_HEIGHT)
|
||||||
.expect("Could not render texture on canvas");
|
.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
|
let mut event_pump = sdl_context
|
||||||
.event_pump()
|
.event_pump()
|
||||||
.expect("Could not get SDL EventPump");
|
.expect("Could not get SDL EventPump");
|
||||||
|
|
||||||
|
game.draw();
|
||||||
|
game.tick();
|
||||||
|
|
||||||
let mut main_loop = || {
|
let mut main_loop = || {
|
||||||
for event in event_pump.poll_iter() {
|
for event in event_pump.poll_iter() {
|
||||||
match event {
|
match event {
|
||||||
@@ -63,20 +66,31 @@ pub fn main() {
|
|||||||
} => return false,
|
} => return false,
|
||||||
event @ Event::KeyDown { .. } => {
|
event @ Event::KeyDown { .. } => {
|
||||||
println!("{:?}", event);
|
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));
|
::std::thread::sleep(Duration::from_millis(10));
|
||||||
true
|
true
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user