mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-08 08:07:54 -06:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a0f65b551c | |||
| 8808a1aa3b |
78
src/game.rs
78
src/game.rs
@@ -6,6 +6,7 @@ use sdl2::render::{Texture, TextureCreator};
|
|||||||
use sdl2::ttf::{Font, FontStyle};
|
use sdl2::ttf::{Font, FontStyle};
|
||||||
use sdl2::video::WindowContext;
|
use sdl2::video::WindowContext;
|
||||||
use sdl2::{pixels::Color, render::Canvas, video::Window};
|
use sdl2::{pixels::Color, render::Canvas, video::Window};
|
||||||
|
use tracing::event;
|
||||||
|
|
||||||
use crate::constants::{MapTile, BOARD_HEIGHT, BOARD_WIDTH, RAW_BOARD};
|
use crate::constants::{MapTile, BOARD_HEIGHT, BOARD_WIDTH, RAW_BOARD};
|
||||||
use crate::direction::Direction;
|
use crate::direction::Direction;
|
||||||
@@ -20,7 +21,7 @@ pub struct Game<'a> {
|
|||||||
power_pellet_texture: Texture<'a>,
|
power_pellet_texture: Texture<'a>,
|
||||||
font: Font<'a, 'static>,
|
font: Font<'a, 'static>,
|
||||||
pacman: Pacman<'a>,
|
pacman: Pacman<'a>,
|
||||||
map: Rc<Map>,
|
map: Rc<std::cell::RefCell<Map>>,
|
||||||
debug: bool,
|
debug: bool,
|
||||||
score: u32,
|
score: u32,
|
||||||
}
|
}
|
||||||
@@ -31,7 +32,7 @@ impl Game<'_> {
|
|||||||
texture_creator: &'a TextureCreator<WindowContext>,
|
texture_creator: &'a TextureCreator<WindowContext>,
|
||||||
ttf_context: &'a sdl2::ttf::Sdl2TtfContext,
|
ttf_context: &'a sdl2::ttf::Sdl2TtfContext,
|
||||||
) -> Game<'a> {
|
) -> Game<'a> {
|
||||||
let map = Rc::new(Map::new(RAW_BOARD));
|
let map = Rc::new(std::cell::RefCell::new(Map::new(RAW_BOARD)));
|
||||||
let pacman_atlas = texture_creator
|
let pacman_atlas = texture_creator
|
||||||
.load_texture("assets/32/pacman.png")
|
.load_texture("assets/32/pacman.png")
|
||||||
.expect("Could not load pacman texture");
|
.expect("Could not load pacman texture");
|
||||||
@@ -73,9 +74,9 @@ impl Game<'_> {
|
|||||||
self.debug = !self.debug;
|
self.debug = !self.debug;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test score increase
|
// Reset game
|
||||||
if keycode == Keycode::S {
|
if keycode == Keycode::R {
|
||||||
self.add_score(10);
|
self.reset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,8 +84,70 @@ impl Game<'_> {
|
|||||||
self.score += points;
|
self.score += points;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn reset(&mut self) {
|
||||||
|
// Reset the map to restore all pellets
|
||||||
|
{
|
||||||
|
let mut map = self.map.borrow_mut();
|
||||||
|
map.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset the score
|
||||||
|
self.score = 0;
|
||||||
|
|
||||||
|
// Reset Pacman position (you might want to customize this)
|
||||||
|
// For now, we'll keep Pacman where he is, but you could add:
|
||||||
|
// self.pacman.position = Map::cell_to_pixel((1, 1));
|
||||||
|
|
||||||
|
event!(tracing::Level::INFO, "Game reset - map and score cleared");
|
||||||
|
}
|
||||||
|
|
||||||
pub fn tick(&mut self) {
|
pub fn tick(&mut self) {
|
||||||
self.pacman.tick();
|
self.pacman.tick();
|
||||||
|
self.check_pellet_eating();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_pellet_eating(&mut self) {
|
||||||
|
let cell_pos = self.pacman.cell_position();
|
||||||
|
|
||||||
|
// Check if there's a pellet at the current position
|
||||||
|
let tile = {
|
||||||
|
let map = self.map.borrow();
|
||||||
|
map.get_tile((cell_pos.0 as i32, cell_pos.1 as i32))
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(tile) = tile {
|
||||||
|
match tile {
|
||||||
|
MapTile::Pellet => {
|
||||||
|
// Eat the pellet and add score
|
||||||
|
{
|
||||||
|
let mut map = self.map.borrow_mut();
|
||||||
|
map.set_tile((cell_pos.0 as i32, cell_pos.1 as i32), MapTile::Empty);
|
||||||
|
}
|
||||||
|
self.add_score(10);
|
||||||
|
event!(
|
||||||
|
tracing::Level::DEBUG,
|
||||||
|
"Pellet eaten at ({}, {})",
|
||||||
|
cell_pos.0,
|
||||||
|
cell_pos.1
|
||||||
|
);
|
||||||
|
}
|
||||||
|
MapTile::PowerPellet => {
|
||||||
|
// Eat the power pellet and add score
|
||||||
|
{
|
||||||
|
let mut map = self.map.borrow_mut();
|
||||||
|
map.set_tile((cell_pos.0 as i32, cell_pos.1 as i32), MapTile::Empty);
|
||||||
|
}
|
||||||
|
self.add_score(50);
|
||||||
|
event!(
|
||||||
|
tracing::Level::DEBUG,
|
||||||
|
"Power pellet eaten at ({}, {})",
|
||||||
|
cell_pos.0,
|
||||||
|
cell_pos.1
|
||||||
|
);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(&mut self) {
|
pub fn draw(&mut self) {
|
||||||
@@ -112,6 +175,7 @@ impl Game<'_> {
|
|||||||
for y in 0..BOARD_HEIGHT {
|
for y in 0..BOARD_HEIGHT {
|
||||||
let tile = self
|
let tile = self
|
||||||
.map
|
.map
|
||||||
|
.borrow()
|
||||||
.get_tile((x as i32, y as i32))
|
.get_tile((x as i32, y as i32))
|
||||||
.unwrap_or(MapTile::Empty);
|
.unwrap_or(MapTile::Empty);
|
||||||
let mut color = None;
|
let mut color = None;
|
||||||
@@ -162,6 +226,7 @@ impl Game<'_> {
|
|||||||
for y in 0..BOARD_HEIGHT {
|
for y in 0..BOARD_HEIGHT {
|
||||||
let tile = self
|
let tile = self
|
||||||
.map
|
.map
|
||||||
|
.borrow()
|
||||||
.get_tile((x as i32, y as i32))
|
.get_tile((x as i32, y as i32))
|
||||||
.unwrap_or(MapTile::Empty);
|
.unwrap_or(MapTile::Empty);
|
||||||
|
|
||||||
@@ -187,9 +252,8 @@ impl Game<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn render_score(&mut self) {
|
fn render_score(&mut self) {
|
||||||
let score = 0;
|
|
||||||
let lives = 3;
|
let lives = 3;
|
||||||
let score_text = format!("{:02}", score);
|
let score_text = format!("{:02}", self.score);
|
||||||
|
|
||||||
let x_offset = 12;
|
let x_offset = 12;
|
||||||
let y_offset = 2;
|
let y_offset = 2;
|
||||||
|
|||||||
37
src/map.rs
37
src/map.rs
@@ -1,13 +1,14 @@
|
|||||||
use crate::constants::MapTile;
|
use crate::constants::MapTile;
|
||||||
use crate::constants::{BOARD_HEIGHT, BOARD_WIDTH};
|
use crate::constants::{BOARD_HEIGHT, BOARD_WIDTH, RAW_BOARD};
|
||||||
|
|
||||||
pub struct Map {
|
pub struct Map {
|
||||||
inner: [[MapTile; BOARD_HEIGHT as usize]; BOARD_WIDTH as usize],
|
current: [[MapTile; BOARD_HEIGHT as usize]; BOARD_WIDTH as usize],
|
||||||
|
default: [[MapTile; BOARD_HEIGHT as usize]; BOARD_WIDTH as usize],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Map {
|
impl Map {
|
||||||
pub fn new(raw_board: [&str; BOARD_HEIGHT as usize]) -> Map {
|
pub fn new(raw_board: [&str; BOARD_HEIGHT as usize]) -> Map {
|
||||||
let mut inner = [[MapTile::Empty; BOARD_HEIGHT as usize]; BOARD_WIDTH as usize];
|
let mut map = [[MapTile::Empty; BOARD_HEIGHT as usize]; BOARD_WIDTH as usize];
|
||||||
|
|
||||||
for y in 0..BOARD_HEIGHT as usize {
|
for y in 0..BOARD_HEIGHT as usize {
|
||||||
let line = raw_board[y];
|
let line = raw_board[y];
|
||||||
@@ -35,11 +36,23 @@ impl Map {
|
|||||||
_ => panic!("Unknown character in board: {}", character),
|
_ => panic!("Unknown character in board: {}", character),
|
||||||
};
|
};
|
||||||
|
|
||||||
inner[x as usize][y as usize] = tile;
|
map[x as usize][y as usize] = tile;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Map { inner: inner }
|
Map {
|
||||||
|
current: map,
|
||||||
|
default: map.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn reset(&mut self) {
|
||||||
|
// Restore the map to its original state
|
||||||
|
for x in 0..BOARD_WIDTH as usize {
|
||||||
|
for y in 0..BOARD_HEIGHT as usize {
|
||||||
|
self.current[x][y] = self.default[x][y];
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_tile(&self, cell: (i32, i32)) -> Option<MapTile> {
|
pub fn get_tile(&self, cell: (i32, i32)) -> Option<MapTile> {
|
||||||
@@ -50,7 +63,19 @@ impl Map {
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(self.inner[x][y])
|
Some(self.current[x][y])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_tile(&mut self, cell: (i32, i32), tile: MapTile) -> bool {
|
||||||
|
let x = cell.0 as usize;
|
||||||
|
let y = cell.1 as usize;
|
||||||
|
|
||||||
|
if x >= BOARD_WIDTH as usize || y >= BOARD_HEIGHT as usize {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.current[x][y] = tile;
|
||||||
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cell_to_pixel(cell: (u32, u32)) -> (i32, i32) {
|
pub fn cell_to_pixel(cell: (u32, u32)) -> (i32, i32) {
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use std::cell::RefCell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use sdl2::{
|
use sdl2::{
|
||||||
@@ -22,14 +23,18 @@ pub struct Pacman<'a> {
|
|||||||
pub direction: Direction,
|
pub direction: Direction,
|
||||||
pub next_direction: Option<Direction>,
|
pub next_direction: Option<Direction>,
|
||||||
pub stopped: bool,
|
pub stopped: bool,
|
||||||
map: Rc<Map>,
|
map: Rc<RefCell<Map>>,
|
||||||
speed: u32,
|
speed: u32,
|
||||||
modulation: SimpleTickModulator,
|
modulation: SimpleTickModulator,
|
||||||
sprite: AnimatedTexture<'a>,
|
sprite: AnimatedTexture<'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Pacman<'_> {
|
impl Pacman<'_> {
|
||||||
pub fn new<'a>(starting_position: (u32, u32), atlas: Texture<'a>, map: Rc<Map>) -> Pacman<'a> {
|
pub fn new<'a>(
|
||||||
|
starting_position: (u32, u32),
|
||||||
|
atlas: Texture<'a>,
|
||||||
|
map: Rc<RefCell<Map>>,
|
||||||
|
) -> Pacman<'a> {
|
||||||
Pacman {
|
Pacman {
|
||||||
position: Map::cell_to_pixel(starting_position),
|
position: Map::cell_to_pixel(starting_position),
|
||||||
direction: Direction::Right,
|
direction: Direction::Right,
|
||||||
@@ -70,6 +75,7 @@ impl Pacman<'_> {
|
|||||||
let proposed_next_cell = self.next_cell(self.next_direction);
|
let proposed_next_cell = self.next_cell(self.next_direction);
|
||||||
let proposed_next_tile = self
|
let proposed_next_tile = self
|
||||||
.map
|
.map
|
||||||
|
.borrow()
|
||||||
.get_tile(proposed_next_cell)
|
.get_tile(proposed_next_cell)
|
||||||
.unwrap_or(MapTile::Empty);
|
.unwrap_or(MapTile::Empty);
|
||||||
if proposed_next_tile != MapTile::Wall {
|
if proposed_next_tile != MapTile::Wall {
|
||||||
@@ -79,7 +85,7 @@ impl Pacman<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn internal_position_even(&self) -> (u32, u32) {
|
fn internal_position_even(&self) -> (u32, u32) {
|
||||||
let (x, y ) = self.internal_position();
|
let (x, y) = self.internal_position();
|
||||||
((x / 2u32) * 2u32, (y / 2u32) * 2u32)
|
((x / 2u32) * 2u32, (y / 2u32) * 2u32)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -115,7 +121,7 @@ impl Entity for Pacman<'_> {
|
|||||||
self.handle_requested_direction();
|
self.handle_requested_direction();
|
||||||
|
|
||||||
let next = self.next_cell(None);
|
let next = self.next_cell(None);
|
||||||
let next_tile = self.map.get_tile(next).unwrap_or(MapTile::Empty);
|
let next_tile = self.map.borrow().get_tile(next).unwrap_or(MapTile::Empty);
|
||||||
|
|
||||||
if !self.stopped && next_tile == MapTile::Wall {
|
if !self.stopped && next_tile == MapTile::Wall {
|
||||||
event!(tracing::Level::DEBUG, "Wall collision. Stopping.");
|
event!(tracing::Level::DEBUG, "Wall collision. Stopping.");
|
||||||
@@ -125,7 +131,7 @@ impl Entity for Pacman<'_> {
|
|||||||
self.stopped = false;
|
self.stopped = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !self.stopped && self.modulation.next() {
|
if !self.stopped && self.modulation.next() {
|
||||||
let speed = self.speed as i32;
|
let speed = self.speed as i32;
|
||||||
match self.direction {
|
match self.direction {
|
||||||
|
|||||||
Reference in New Issue
Block a user