遊戲移動的實現

來源:互聯網
上載者:User

標籤:util   rri   start   font   方向   for   style   儲存   介面   

遊戲移動的實現原理:

            畫出坦克後,在protected void onKeyEvent(int key) 方法中定義一個swich語句接收鍵盤按鍵

        根據按鍵的方向來調用Tank的移動方法,當然要先編寫Tank的移動方法。根據傳入的方向加減Tank

        圖片的座標實現移動。

 

 

 

實現步驟:

1、建立一個父類,命名為Element

 1 public class Element {    //所有對象的父類 2     int x;     3     int y;     4     int width; //圖片的寬 5     int height;    //圖片的高 6     String imagePath;    //儲存圖片路徑 7      8     public  void draw(){    //Draw方法 畫出對象 9         try {10             DrawUtils.draw(imagePath, x, y);11         } catch (IOException e) {12             // TODO 自動產生的 catch 塊13             e.printStackTrace();14         }15     }16 }
Element

2、建立一個枚舉類,命名為Direction 

1 public enum Direction {    //定義一個枚舉類2     UP,DOWN,LEFT,RIGHT    3 }
Direction

3、讓Tank類繼承Element類並添加移動方法

 1 public class Tank extends Element { 2     private int speed = 64;    //移動的速度 3     public Tank(int x, int y) {    //定義一個Tank的構造方法 4         this.x = x;    //座標的設定 5         this.y = y; 6         this.imagePath = "res/img/tank_u.gif";    //Tank的圖片路徑 7     } 8  9     public void move(Direction dir) {    //Tank移動的方法10         switch (dir) {    //根據接收到的方向來移動11         case UP:12             y -= speed;13             break;14 15         case DOWN:16             y += speed;17             break;18         case LEFT:19             x -= speed;20             break;21         case RIGHT:22             x += speed;23             break;24         }25     }26 }
Tank

4、在設定類中的onKeyEvent方法中設定移動方法的調用

 1 public class TankWorld extends Window { 2     Tank tank;    //定義一個Tank對象 3     List<Element> list;    //定義一個集合儲存物件 4  5     public TankWorld(String title, int width, int height, int fps) { 6         super(title, width, height, fps); 7         // TODO 自動產生的建構函式存根 8     } 9 10     @Override11     protected void onCreate() {    12         try {13             SoundUtils.play("res/snd/start.wav");    //遊戲開始音樂 14         } catch (IOException e) {15             // TODO 自動產生的 catch 塊16             e.printStackTrace();17         }18         list = new CopyOnWriteArrayList<>();    //可遍曆增刪集合19         tank = new Tank(0, 0);    //產生的Tank對象座標20         list.add(tank);    //將TankObject Storage Service到集合21     }22 23     @Override24     protected void onMouseEvent(int key, int x, int y) {25         // TODO 自動產生的方法存根26 27     }28 29     @Override30     protected void onKeyEvent(int key) {  //鍵盤監聽方法31         switch (key) {        //根據接收的按鍵執行32         case Keyboard.KEY_UP:    //上方向鍵33             tank.move(Direction.UP); //Tank移動的方法34             break;35 36         case Keyboard.KEY_DOWN:    //下方向鍵37             tank.move(Direction.DOWN);38             break;39 40         case Keyboard.KEY_LEFT:    //左方向鍵41             tank.move(Direction.LEFT);42             break;43 44         case Keyboard.KEY_RIGHT:    //右方向鍵45             tank.move(Direction.RIGHT);46             break;47 48         }49 50     }51 52     @Override53     protected void onDisplayUpdate() {54         for (Element e : list) {    //迴圈遍曆集合中的對象55             e.draw();    //在介面畫出對象56         }57     }58 59 }
設定類

 

 

隨筆說:

        目前移動實現的效果還不完善,只能橫著移動。素材可以自己在網上找一些比較酷炫的代替。

    需要對繼承、介面足夠瞭解,才能事半功倍。不用重複的定義一個對象的屬性方法。

遊戲移動的實現

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.