Java写的第一个小游戏( 四 )

插入人物-帽子-爆炸物-障碍物图片在com.ytzl.ylm包下创建需要插入的图片包entity7.实现人物的显示7.1在图片包(entity)下创建角色类Buffoon,定义角色属性和方法

  • 定义角色属性
// 人物图片路径属性public static final Image buffoonImage = CommonUtils.getImage("images/body/s-left.png");// 人物-横坐标private int x;// 人物-纵坐标private int y;// 图片-宽度private int width;// 图片-高度private int height;// 人物-速度private int speed;// 人物-方向private String dir;//游戏窗体属性 因为小丑要在窗体中显示private GameClient gameClient;12345678910111213141516
  • 定义无参带参构造以给角色的属性赋值
//无参构造public Buffoon() {}//带参构造public Buffoon(int x, int y, GameClient gameClient) {this.x = x;this.y = y;this.width = 50;this.height = 50;this.speed = 3;this.dir = "STOP";// 人物刚创建时为静止状态this.gameClient = gameClient;}1234567891011121314
  • 定义角色方法
//插入角色图方法public void paint(Graphics g){g.drawImage(buffoonImage,this.x,this.y,this.width,this.height,this.gameClient);}//生成get set 方法public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public int getWidth() {return width;}public void setWidth(int width) {this.width = width;}public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}public int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;}public String getDir() {return dir;}public void setDir(String dir) {this.dir = dir;}public GameClient getGameClient() {return gameClient;}public void setGameClient(GameClient gameClient) {this.gameClient = gameClient;}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758597.2在客户端中实现插入人物图// 创建人物-小丑,在参数中定义位置private Buffoon buffoon = new Buffoon(200,200,this);//重写父类Frame类的paint方法以插入各种图片@Overridepublic void paint(Graphics g){//用角色-小丑调用paint方法以给图片宽 高 速度buffoon.paint(g);}123456788.实现发射物的显示8.1在图片包(entity)下创建发射物类Missile,定义发射物属性和方法
  • 定义发射物-帽子的属性
//发射物图片路径属性public static final Image missileImage = CommonUtils.getImage("images/missile.png");// 帽子-高度private int height;// 帽子-长度private int width;// 帽子-速度private int speed;// 帽子-横坐标private int x;// 帽子-纵坐标private int y;//游戏窗体属性 因为帽子要在窗体中显示private GameClient gameClient;1234567891011121314
  • 定义无参带参构造给帽子的属性赋值
//无参构造public Missile(){}//带参构造public Missile(int x,int y,GameClient gameClient){this.x=x;this.y=y;this.width=20;this.height=20;this.speed=10;this.gameClient=gameClient;}12345678910111213
  • 定义帽子的方法
//插入帽子图片的方法public void paint(Graphics g){ g.drawImage(missileImage,this.x,this.y,this.width,this.height,this.gameClient);}//setget方法public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}public int getWidth() {return width;}public void setWidth(int width) {this.width = width;}public int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public GameClient getGameClient() {return gameClient;}public void setGameClient(GameClient gameClient) {this.gameClient = gameClient;}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051528.2在客户端中实现插入帽子图// 创建发射物-帽子(missile),在参数中定义位置private Missile missile = new Missile(300,300,this);//重写父类Frame类的paint方法以插入各种图片@Overridepublic void paint(Graphics g){//用发射物-帽子调用paint方法以给图片宽 高 速度missile.paint(g);}123456789.实现爆炸物的显示9.1在图片包(entity)下创建爆炸物类(Explode),定义爆炸物属性和方法
  • 定义爆炸物属性
//爆炸物图片路径属性public static final Image explodeImage = CommonUtils.getImage("images/explode.png");//爆炸物-高度private int height;//爆炸物-长度private int width;//爆炸物-横坐标private int x;//爆炸物-纵坐标private int y;//爆炸物窗体属性 因为帽子要在窗体中显示private GameClient gameClient;123456789101112
  • 定义无参带参构造给爆炸物的属性赋值
//无参构造public Explode(){}//带参构造public Explode(int x,int y,GameClient gameClient){this.x=x;this.y=y;this.width=30;this.height=70;this.gameClient=gameClient;}123456789101112