Bladeren bron

Add directional animations

master
Noëlle Anthony 4 jaren geleden
bovenliggende
commit
98223fd0c0
1 gewijzigde bestanden met toevoegingen van 27 en 2 verwijderingen
  1. 27
    2
      src/main.rs

+ 27
- 2
src/main.rs Bestand weergeven

sprite: Rect, sprite: Rect,
speed: i32, speed: i32,
direction: Direction, direction: Direction,
current_frame: i32,
}

fn direction_spritesheet_row(direction: Direction) -> i32 {
use self::Direction::*;
match direction {
Up => 3,
Down => 0,
Left => 1,
Right => 2,
}
} }


fn render( fn render(
canvas.clear(); canvas.clear();


let (width, height) = canvas.output_size()?; let (width, height) = canvas.output_size()?;

let (frame_width, frame_height) = player.sprite.size();
let current_frame = Rect::new(
player.sprite.x() + frame_width as i32 * player.current_frame,
player.sprite.y() + frame_height as i32 * direction_spritesheet_row(player.direction),
frame_width,
frame_height,
);

let screen_position = player.position + Point::new(width as i32 / 2, height as i32 / 2); let screen_position = player.position + Point::new(width as i32 / 2, height as i32 / 2);
let screen_rect = Rect::from_center(screen_position, player.sprite.width(), player.sprite.height());
canvas.copy(texture, player.sprite, screen_rect)?;
let screen_rect = Rect::from_center(screen_position, frame_width, frame_height);
canvas.copy(texture, current_frame, screen_rect)?;


canvas.present(); canvas.present();


player.position = player.position.offset(0, player.speed); player.position = player.position.offset(0, player.speed);
}, },
} }

if player.speed != 0 {
player.current_frame = (player.current_frame + 1) % 3;
}
} }


fn main() -> Result<(), String> { fn main() -> Result<(), String> {
sprite: Rect::new(0, 0, 26, 36), sprite: Rect::new(0, 0, 26, 36),
speed: 0, speed: 0,
direction: Direction::Right, direction: Direction::Right,
current_frame: 0,
}; };


let mut event_pump = sdl_context.event_pump()?; let mut event_pump = sdl_context.event_pump()?;

Laden…
Annuleren
Opslaan