Compare commits

...

2 Commits

Author SHA1 Message Date
c90f221c73 feat: speed property to PacMan 2025-06-17 11:50:44 -05:00
841943e121 fix: frame flashing in sprite tick 2025-06-17 11:50:37 -05:00
2 changed files with 27 additions and 15 deletions

View File

@@ -18,7 +18,7 @@ pub struct AnimatedTexture<'a> {
impl<'a> AnimatedTexture<'a> { impl<'a> AnimatedTexture<'a> {
pub fn new( pub fn new(
texture:Texture<'a>, texture: Texture<'a>,
ticks_per_frame: u32, ticks_per_frame: u32,
frame_count: u32, frame_count: u32,
frame_width: u32, frame_width: u32,
@@ -49,7 +49,7 @@ impl<'a> AnimatedTexture<'a> {
} else { } else {
self.ticker += 1; self.ticker += 1;
if self.ticker > self.ticks_per_frame * self.frame_count { if self.ticker + 1 == self.ticks_per_frame * self.frame_count {
self.reversed = !self.reversed; self.reversed = !self.reversed;
} }
} }
@@ -64,7 +64,12 @@ impl<'a> AnimatedTexture<'a> {
) )
} }
pub fn render(&mut self, canvas: &mut Canvas<Window>, position: (i32, i32), direction: Direction) { pub fn render(
&mut self,
canvas: &mut Canvas<Window>,
position: (i32, i32),
direction: Direction,
) {
let frame_rect = self.get_frame_rect(); let frame_rect = self.get_frame_rect();
let position_rect = Rect::new(position.0, position.1, self.frame_width, self.frame_height); let position_rect = Rect::new(position.0, position.1, self.frame_width, self.frame_height);
@@ -76,9 +81,10 @@ impl<'a> AnimatedTexture<'a> {
direction.angle(), direction.angle(),
None, None,
false, false,
false false,
).expect("Could not render texture on canvas"); )
.expect("Could not render texture on canvas");
self.next_frame(); self.next_frame();
} }
} }

View File

@@ -1,11 +1,15 @@
use sdl2::{render::{Canvas, Texture}, video::Window}; use sdl2::{
render::{Canvas, Texture},
video::Window,
};
use crate::{direction::Direction, entity::Entity, animation::AnimatedTexture}; use crate::{animation::AnimatedTexture, direction::Direction, entity::Entity};
pub struct Pacman<'a> { pub struct Pacman<'a> {
// Absolute position on the board (precise) // Absolute position on the board (precise)
pub position: (i32, i32), pub position: (i32, i32),
pub direction: Direction, pub direction: Direction,
speed: u32,
sprite: AnimatedTexture<'a>, sprite: AnimatedTexture<'a>,
} }
@@ -14,11 +18,12 @@ impl Pacman<'_> {
Pacman { Pacman {
position: starting_position.unwrap_or((0i32, 0i32)), position: starting_position.unwrap_or((0i32, 0i32)),
direction: Direction::Right, direction: Direction::Right,
sprite: AnimatedTexture::new(atlas, 4, 3, 32,32), speed: 2,
sprite: AnimatedTexture::new(atlas, 4, 3, 32, 32),
} }
} }
pub fn render(&mut self, canvas: &mut Canvas<Window>) { pub fn render(&mut self, canvas: &mut Canvas<Window>) {
self.sprite.render(canvas, self.position, self.direction); self.sprite.render(canvas, self.position, self.direction);
} }
} }
@@ -40,19 +45,20 @@ impl Entity for Pacman<'_> {
} }
fn tick(&mut self) { fn tick(&mut self) {
let speed = self.speed as i32;
match self.direction { match self.direction {
Direction::Right => { Direction::Right => {
self.position.0 += 1; self.position.0 += speed;
} }
Direction::Left => { Direction::Left => {
self.position.0 -= 1; self.position.0 -= speed;
} }
Direction::Up => { Direction::Up => {
self.position.1 -= 1; self.position.1 -= speed;
} }
Direction::Down => { Direction::Down => {
self.position.1 += 1; self.position.1 += speed;
} }
} }
} }
} }