Bläddra i källkod

Convert to struct

master
Noëlle Anthony 4 år sedan
förälder
incheckning
f33f4fd236
1 ändrade filer med 15 tillägg och 8 borttagningar
  1. 15
    8
      src/main.rs

+ 15
- 8
src/main.rs Visa fil

@@ -6,18 +6,23 @@ use sdl2::image::{self, LoadTexture, InitFlag};
use sdl2::rect::{Point, Rect};
use std::time::Duration;

#[derive(Debug)]
struct Player {
position: Point,
sprite: Rect,
}

fn render(canvas: &mut WindowCanvas,
color: Color,
texture: &Texture,
position: Point,
sprite: Rect) -> Result<(), String> {
player: &Player) -> Result<(), String> {
canvas.set_draw_color(color);
canvas.clear();

let (width, height) = canvas.output_size()?;
let screen_position = position + Point::new(width as i32 / 2, height as i32 / 2);
let screen_rect = Rect::from_center(screen_position, sprite.width(), sprite.height());
canvas.copy(texture, sprite, screen_rect)?;
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)?;

canvas.present();

@@ -40,8 +45,10 @@ fn main() -> Result<(), String> {
let texture_creator = canvas.texture_creator();
let texture = texture_creator.load_texture("assets/bardo.png")?;

let position = Point::new(0,0);
let sprite = Rect::new(0, 0, 26, 26);
let player = Player {
position: Point::new(0,0),
sprite: Rect::new(0, 0, 26, 36),
};

let mut event_pump = sdl_context.event_pump()?;
let mut i = 0;
@@ -58,7 +65,7 @@ fn main() -> Result<(), String> {

i = (i + 1) % 255;

render(&mut canvas, Color::RGB(i, 64, 255-i), &texture, position, sprite)?;
render(&mut canvas, Color::RGB(i, 64, 255-i), &texture, &player)?;

::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
}

Laddar…
Avbryt
Spara