標籤:splay rect int ext void extend ase direction 變化
移動圖片的轉換:
定義一個方向,在移動的時候判斷下一次移動是否為當前方向,如果不是則只改變圖片,不做移動。
1 package com.itheima.bean; 2 3 public class Tank extends Element { 4 private int speed = 64; 5 Direction direction = Direction.UP; //初始向上 6 7 public Tank(int x, int y) { 8 this.x = x; 9 this.y = y;10 this.imagePath = "res/img/tank_u.gif";11 12 }13 14 public void move(Direction dir) {15 if (direction != dir) { //如果傳進來的方向不等於當前方向16 direction = dir; 17 switch (dir) { //根據方向轉換圖片18 case UP:19 this.imagePath = "res/img/tank_u.gif";20 break;21 22 case DOWN:23 this.imagePath = "res/img/tank_d.gif";24 break;25 case LEFT:26 this.imagePath = "res/img/tank_l.gif";27 break;28 case RIGHT:29 this.imagePath = "res/img/tank_r.gif";30 break;31 }32 return ; //不繼續向下執行33 }34 switch (dir) { //移動35 case UP:36 y -= speed;37 break;38 39 case DOWN:40 y += speed;41 break;42 case LEFT:43 x -= speed;44 break;45 case RIGHT:46 x += speed;47 break;48 }49 }50 }View Code
隨筆說:
遊戲移動原理就是圖片的座標的變化顯示。
遊戲移動圖片的轉換