mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-08 18:08:05 -06:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 443afb1223 | |||
| 724878dc17 | |||
| 274404b9ea | |||
| 2214a5541f | |||
| 64de5fb732 |
53
src/animation.rs
Normal file
53
src/animation.rs
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
use sdl2::{
|
||||||
|
rect::Rect,
|
||||||
|
render::{Canvas, Texture},
|
||||||
|
video::Window,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct AnimatedTexture<'a> {
|
||||||
|
raw_texture: &'a Texture<'a>,
|
||||||
|
current_frame: u32,
|
||||||
|
frame_count: u32,
|
||||||
|
frame_width: u32,
|
||||||
|
frame_height: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> AnimatedTexture<'a> {
|
||||||
|
pub fn new(
|
||||||
|
texture: &'a Texture<'a>,
|
||||||
|
frame_count: u32,
|
||||||
|
frame_width: u32,
|
||||||
|
frame_height: u32,
|
||||||
|
) -> Self {
|
||||||
|
AnimatedTexture {
|
||||||
|
raw_texture: texture,
|
||||||
|
current_frame: 0,
|
||||||
|
frame_count,
|
||||||
|
frame_width,
|
||||||
|
frame_height,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn next_frame(&mut self) {
|
||||||
|
self.current_frame = (self.current_frame + 1) % self.frame_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_frame_rect(&self) -> Rect {
|
||||||
|
Rect::new(
|
||||||
|
(self.current_frame * self.frame_width) as i32,
|
||||||
|
0,
|
||||||
|
self.frame_width,
|
||||||
|
self.frame_height,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn render(&mut self, canvas: &mut Canvas<Window>, position: (i32, i32)) {
|
||||||
|
let frame_rect = self.get_frame_rect();
|
||||||
|
let position_rect = Rect::new(position.0, position.1, self.frame_width, self.frame_height);
|
||||||
|
canvas
|
||||||
|
.copy(&self.raw_texture, frame_rect, position_rect)
|
||||||
|
.expect("Could not render sprite on canvas");
|
||||||
|
|
||||||
|
self.next_frame();
|
||||||
|
}
|
||||||
|
}
|
||||||
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
41
src/pacman.rs
Normal file
41
src/pacman.rs
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
use sdl2::{render::{Canvas, Texture}, video::Window};
|
||||||
|
|
||||||
|
use crate::{direction::Direction, entity::Entity, animation::AnimatedTexture};
|
||||||
|
|
||||||
|
pub struct Pacman<'a> {
|
||||||
|
// Absolute position on the board (precise)
|
||||||
|
pub position: (i32, i32),
|
||||||
|
pub direction: Direction,
|
||||||
|
sprite: AnimatedTexture<'a>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Pacman<'_> {
|
||||||
|
pub fn new<'a>(starting_position: Option<(i32, i32)>, atlas: &'a Texture<'a>) -> Pacman<'a> {
|
||||||
|
Pacman {
|
||||||
|
position: starting_position.unwrap_or((0i32, 0i32)),
|
||||||
|
direction: Direction::Right,
|
||||||
|
sprite: AnimatedTexture::new(atlas, 2, 24, 24),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn render(&mut self, canvas: &mut Canvas<Window>) {
|
||||||
|
self.sprite.render(canvas, self.position);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Entity for Pacman<'_> {
|
||||||
|
fn is_colliding(&self, other: &dyn Entity) -> bool {
|
||||||
|
let (x, y) = self.position();
|
||||||
|
let (other_x, other_y) = other.position();
|
||||||
|
x == other_x && y == other_y
|
||||||
|
}
|
||||||
|
|
||||||
|
fn position(&self) -> (i32, i32) {
|
||||||
|
self.position
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cell_position(&self) -> (u32, u32) {
|
||||||
|
let (x, y) = self.position();
|
||||||
|
(x as u32 / 24, y as u32 / 24)
|
||||||
|
}
|
||||||
|
}
|
||||||
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