mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-08 14:07:47 -06:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 274404b9ea | |||
| 2214a5541f | |||
| 64de5fb732 | |||
| 4a4e6e40a9 |
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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
6
src/direction.rs
Normal file
6
src/direction.rs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
pub enum Direction {
|
||||||
|
Up,
|
||||||
|
Down,
|
||||||
|
Left,
|
||||||
|
Right,
|
||||||
|
}
|
||||||
8
src/entity.rs
Normal file
8
src/entity.rs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
pub trait Entity {
|
||||||
|
// Returns true if the entity is colliding with the other entity
|
||||||
|
fn is_colliding(&self, other: &dyn Entity) -> bool;
|
||||||
|
// Returns the absolute position of the entity
|
||||||
|
fn position(&self) -> (i32, i32);
|
||||||
|
// Returns the cell position of the entity (XY position within the grid)
|
||||||
|
fn cell_position(&self) -> (u32, u32);
|
||||||
|
}
|
||||||
66
src/game.rs
66
src/game.rs
@@ -1,11 +1,63 @@
|
|||||||
pub struct Game {}
|
use sdl2::{pixels::Color, render::Canvas, video::Window};
|
||||||
|
|
||||||
impl Game {
|
use crate::constants::{MapTile, BOARD, BOARD_HEIGHT, BOARD_WIDTH};
|
||||||
pub fn new() -> Game {
|
use crate::pacman::Pacman;
|
||||||
Game {}
|
use crate::textures::TextureManager;
|
||||||
|
|
||||||
|
pub struct Game<'a> {
|
||||||
|
pub textures: TextureManager<'a>,
|
||||||
|
canvas: &'a mut Canvas<Window>,
|
||||||
|
pacman: Pacman<'a>,
|
||||||
|
debug: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Game<'_> {
|
||||||
|
pub fn new<'a>(
|
||||||
|
canvas: &'a mut Canvas<Window>,
|
||||||
|
texture_manager: TextureManager<'a>,
|
||||||
|
) -> Game<'a> {
|
||||||
|
let pacman = Pacman::new(None, &texture_manager.pacman);
|
||||||
|
|
||||||
|
Game {
|
||||||
|
canvas,
|
||||||
|
textures: texture_manager,
|
||||||
|
pacman: pacman,
|
||||||
|
debug: true,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn tick() {}
|
pub fn tick(&mut self) {}
|
||||||
|
|
||||||
pub fn draw() {}
|
pub fn draw(&mut self) {
|
||||||
}
|
// Clear the screen (black)
|
||||||
|
self.canvas.set_draw_color(Color::RGB(0, 0, 0));
|
||||||
|
self.canvas.clear();
|
||||||
|
|
||||||
|
self.canvas
|
||||||
|
.copy(&self.textures.map, None, None)
|
||||||
|
.expect("Could not render texture on canvas");
|
||||||
|
|
||||||
|
// Draw a grid
|
||||||
|
for x in 0..BOARD_WIDTH {
|
||||||
|
for y in 0..BOARD_HEIGHT {
|
||||||
|
let tile = BOARD[x as usize][y as usize];
|
||||||
|
let color = match tile {
|
||||||
|
MapTile::Empty => None,
|
||||||
|
MapTile::Wall => Some(Color::BLUE),
|
||||||
|
MapTile::Pellet => Some(Color::RED),
|
||||||
|
MapTile::PowerPellet => Some(Color::MAGENTA),
|
||||||
|
MapTile::StartingPosition(_) => Some(Color::GREEN),
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(color) = color {
|
||||||
|
self.canvas.set_draw_color(color);
|
||||||
|
self.canvas
|
||||||
|
.draw_rect(sdl2::rect::Rect::new(x as i32 * 24, y as i32 * 24, 24, 24))
|
||||||
|
.expect("Could not draw rectangle");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.canvas.present();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
29
src/textures.rs
Normal file
29
src/textures.rs
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
|
||||||
|
use sdl2::{
|
||||||
|
image::LoadTexture,
|
||||||
|
render::{Texture, TextureCreator},
|
||||||
|
video::WindowContext,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct TextureManager<'a> {
|
||||||
|
pub map: Texture<'a>,
|
||||||
|
pub pacman: Texture<'a>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> TextureManager<'a> {
|
||||||
|
pub fn new(texture_creator: &'a TextureCreator<WindowContext>) -> Self {
|
||||||
|
let map_texture = texture_creator
|
||||||
|
.load_texture("assets/map.png")
|
||||||
|
.expect("Could not load pacman texture");
|
||||||
|
|
||||||
|
let pacman_atlas = texture_creator
|
||||||
|
.load_texture("assets/pacman.png")
|
||||||
|
.expect("Could not load pacman texture");
|
||||||
|
|
||||||
|
|
||||||
|
TextureManager {
|
||||||
|
map: map_texture,
|
||||||
|
pacman: pacman_atlas,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user