Переглянути джерело

make the move animation smooth

john melesky 6 роки тому
батько
коміт
f890fc4db0
1 змінених файлів з 88 додано та 9 видалено
  1. 88 9
      src/main.rs

+ 88 - 9
src/main.rs

@@ -43,11 +43,16 @@ fn init() -> (Sdl, Window) {
 fn main() {
 
     let mut running: bool = true;
-    let mut blitx: i32 = 7;
-    let mut blity: i32 = 5;
+    let mut blitx: i32 = 7 * 80;
+    let mut blity: i32 = 5 * 80;
+    let mut direction = "";
+    let stepsize = 8;
+    let ticksize = 20;
+    let mut next_tick: u32;
 
     let mut blitrect = Rect::new(blitx * 80, blity * 80, 80, 80);
 
+    // grab the context and window, then set the window up for drawing
     let (context, window) = init();
 
     let mut canvas = match window.into_canvas()
@@ -56,6 +61,7 @@ fn main() {
             Err(err)   => panic!("Could not create canvas from window. Error: {}", err),
         };
 
+    // load the png into a texture
     let tc = canvas.texture_creator();
 
     let texture = match tc.load_texture(&Path::new("assets/blip1.png")) {
@@ -64,8 +70,13 @@ fn main() {
     };
 
 
+    // initialize timer subsystem
+    let mut timer = match context.timer() {
+        Ok(timer) => timer,
+        Err(err)  => panic!("Could not start the timer: {}", err)
+    };
 
-
+    // prime the event pump
     let mut pump = match context.event_pump() {
         Ok(pump) => pump,
         Err(err) => panic!("Could not start pumping: {}", err)
@@ -74,6 +85,9 @@ fn main() {
 
 
     while running {
+        // determine next frame time
+        next_tick = timer.ticks() + ticksize;
+
         // pull all pending events
         for event in pump.poll_iter() {
             match event {
@@ -87,16 +101,24 @@ fn main() {
                         running = false
                     },
                     Some(Keycode::Up) => {
-                        if blity >= 1 { blity -= 1; }
+                        if (blitx % 80) == 0 {
+                            direction = "up"
+                        }
                     },
                     Some(Keycode::Down) => {
-                        if blity < 11 { blity += 1; }
+                        if (blitx % 80) == 0 {
+                            direction = "down"
+                        }
                     },
                     Some(Keycode::Left) => {
-                        if blitx >= 1 { blitx -= 1; }
+                        if (blity % 80) == 0 {
+                            direction = "left"
+                        }
                     },
                     Some(Keycode::Right) => {
-                        if blitx < 15 { blitx += 1; }
+                        if (blity % 80) == 0 {
+                            direction = "right"
+                        }
                     },
                     Some(_) => { },
                     None => {}
@@ -106,8 +128,59 @@ fn main() {
             }
         }
 
-        blitrect.set_x(blitx * 80);
-        blitrect.set_y(blity * 80);
+        match direction {
+            "up" => {
+                if blity <= 0 {
+                    blity = 0;
+                    direction = "";
+                } else {
+                    blity -= stepsize;
+                    if (blity % 80) == 0 {
+                        direction = ""
+                    }
+                }
+            },
+            "down" => {
+                if blity >= (HEIGHT - 80) as i32 {
+                    blity = (HEIGHT - 80) as i32;
+                    direction = "";
+                } else {
+                    blity += stepsize;
+                    if (blity % 80) == 0 {
+                        direction = ""
+                    }
+                }
+            },
+            "left" => {
+                if blitx <= 0 {
+                    blitx = 0;
+                    direction = "";
+                } else {
+                    blitx -= stepsize;
+                    if (blitx % 80) == 0 {
+                        direction = ""
+                    }
+                }
+            },
+            "right" => {
+                if blitx >= (WIDTH - 80) as i32 {
+                    blitx = (WIDTH - 80) as i32;
+                    direction = "";
+                } else {
+                    blitx += stepsize;
+                    if (blitx % 80) == 0 {
+                        direction = ""
+                    }
+                }
+            },
+            _ => { }
+        }
+
+
+
+
+        blitrect.set_x(blitx);
+        blitrect.set_y(blity);
 
         canvas.clear();
         match canvas.copy(&texture, None, blitrect) {
@@ -115,6 +188,12 @@ fn main() {
             Err(err) => panic!("Could not render texture: {}", err),
         };
 
+        // wait til time to flip the frame
+        let now = timer.ticks();
+        if now < next_tick {
+            timer.delay( next_tick - now )
+        }
+
         canvas.present();
 
     }