实现思路:
1、获取tank的宽高
2、根据tank的方向更换子弹的图片
3、获取每个方向子弹图片的宽高
4、计算出子弹的坐标
5、为子弹加速
代码实现:
1、定义Element类,属性有坐标、宽高和图片路径,方法有draw()方法和getSize()方法
1 package Itheima.bean; 2 3 import java.io.IOException; 4 5 import org.itheima.game.utils.DrawUtils; 6 7 public class Element { 8 int x; //x坐标 9 int y; //y坐标 10 int width; //宽度 11 int height; //高度 12 String imagePath; //图片路径 13 public void draw() { 14 try { 15 DrawUtils.draw(imagePath, x, y); 16 } catch (IOException e) { 17 // TODO 自动生成的 catch 块 18 e.printStackTrace(); 19 } 20 } 21 public void getSize(){ //获取图片宽高 22 try { 23 int[] size = DrawUtils.getSize(imagePath); 24 this.width=size[0]; 25 this.height=size[1]; 26 } catch (IOException e) { 27 // TODO 自动生成的 catch 块 28 e.printStackTrace(); 29 } 30 } 31 }
Element
2、让Tank类和Bullet类继承Element类
1 package Itheima; 2 3 import java.util.List; 4 import java.util.concurrent.CopyOnWriteArrayList; 5 6 import org.itheima.game.Window; 7 import org.lwjgl.input.Keyboard; 8 9 import Itheima.bean.Bullet; 10 import Itheima.bean.Direction; 11 import Itheima.bean.Element; 12 import Itheima.bean.Tank; 13 14 public class TankWorld extends Window { 15 Tank tank; 16 List<Element> list; 17 18 public TankWorld(String title, int width, int height, int fps) { 19 super(title, width, height, fps); 20 // TODO 自动生成的构造函数存根 21 } 22 23 @Override 24 protected void onCreate() { 25 list = new CopyOnWriteArrayList<>(); 26 tank = new Tank(0, 0); 27 list.add(tank); 28 } 29 30 @Override 31 protected void onMouseEvent(int key, int x, int y) { 32 // TODO 自动生成的方法存根 33 34 } 35 36 @Override 37 protected void onKeyEvent(int key) { 38 switch (key) { 39 case Keyboard.KEY_UP: 40 tank.move(Direction.UP); 41 break; 42 43 case Keyboard.KEY_DOWN: 44 tank.move(Direction.DOWN); 45 break; 46 case Keyboard.KEY_LEFT: 47 tank.move(Direction.LEFT); 48 break; 49 case Keyboard.KEY_RIGHT: 50 tank.move(Direction.RIGHT); 51 break; 52 case Keyboard.KEY_SPACE: 53 Bullet bullet = tank.fire(); 54 list.add(bullet); 55 break; 56 } 57 } 58 59 @Override 60 protected void onDisplayUpdate() { 61 for (Element e : list) { 62 e.draw(); 63 } 64 } 65 66 }
设置类
1 package Itheima.bean; 2 3 import Itheima.Constants; 4 5 public class Tank extends Element { 6 private int speed = 32; //移动速度 7 Direction dir = Direction.UP; //方向默认向上 8 public Tank(int x, int y) { //构造方法 9 this.x = x; 10 this.y = y; 11 this.imagePath = "res/img/tank_u.gif"; 12 getSize(); //获取tank宽高 13 } 14 15 public void move(Direction dir) { 16 if(dir!=this.dir){ //传入的方向不等于当前方向 17 this.dir=dir; //当前方向等于传入的方向 18 switch (dir) { //只更换方向不做移动 19 case UP: 20 this.imagePath = "res/img/tank_u.gif"; 21 break; 22 23 case DOWN: 24 this.imagePath = "res/img/tank_d.gif"; 25 break; 26 case LEFT: 27 this.imagePath = "res/img/tank_l.gif"; 28 break; 29 case RIGHT: 30 this.imagePath = "res/img/tank_r.gif"; 31 break; 32 } 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 if(x<0){ 51 x=0; 52 } 53 if(x>Constants.WIDTH-this.width){ 54 x=Constants.WIDTH-this.width; 55 } 56 if(y<0){ 57 y=0; 58 } 59 if(y>Constants.HEIGHT-this.height){ 60 y=Constants.HEIGHT-this.height; 61 } 62 } 63 64 public Bullet fire() { 65 Bullet b = new Bullet(this); //根据tank坐标生成子弹 66 return b; 67 } 68 }
Tank
1 package Itheima.bean; 2 3 import java.io.IOException; 4 5 import org.itheima.game.utils.DrawUtils; 6 7 public class Bullet extends Element { 8 Direction dir; 9 private int speed = 32; //子弹速度 10 public Bullet(Tank tank) { 11 int x1 = tank.x; 12 int y1 = tank.y; 13 int w1 = tank.width; 14 int h1 = tank.height; 15 this.dir = tank.dir; 16 switch (dir) { //根据tank的方向计算子弹坐标 17 case UP: 18 this.imagePath = "res/img/bullet_u.gif"; 19 getSize(); 20 this.x = x1 + (w1 - this.width) / 2; 21 this.y = y1 - this.height; 22 break; 23 24 case DOWN: 25 this.imagePath = "res/img/bullet_d.gif"; 26 getSize(); 27 this.x = x1 + (w1 - this.width) / 2; 28 this.y = y1 + w1; 29 break; 30 case LEFT: 31 this.imagePath = "res/img/bullet_l.gif"; 32 getSize(); 33 this.x = x1 - this.width; 34 this.y = y1 + (h1 - this.height) / 2; 35 break; 36 case RIGHT: 37 this.imagePath = "res/img/bullet_r.gif"; 38 getSize(); 39 this.x = x1 + w1; 40 this.y = y1 + (h1 - this.height) / 2; 41 break; 42 } 43 } 44 45 public void draw() { //重写draw方法 46 try { 47 DrawUtils.draw(imagePath, x, y); 48 } catch (IOException e) { 49 // TODO 自动生成的 catch 块 50 e.printStackTrace(); 51 } 52 switch (dir) { 53 case UP: 54 y -= speed; 55 break; 56 57 case DOWN: 58 y += speed; 59 break; 60 case LEFT: 61 x -= speed; 62 break; 63 case RIGHT: 64 x += speed; 65 break; 66 } 67 } 68 }
Bullet
随笔说:
本节实现了子弹对象的创建、坐标的计算、移动效果。体会到继承的好处,
以及游戏实现原理。
时间: 2024-08-05 11:13:02