//插入角色图方法public void paint(Graphics g){g.drawImage(explodeImage,x,y,this.width,this.height,this.gameClient);}//生成get set方法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 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;}1234567891011121314151617181920212223242526272829303132333435363738394041424344
9.2在客户端中实现插入爆炸物图
// 创建发射物-爆炸物(explode),在参数中定义位置private Explode explode = new Explode(300,100,this);//重写父类Frame类的paint方法以插入各种图片@Overridepublic void paint(Graphics g){//用爆炸物调用paint方法以给图片宽 高explode.paint(g);}12345678
10.实现墙体的显示10.1在图片包(entity)下创建障碍物类(Wall),定义障碍物属性和方法
//墙体图片属性public static final Image wallImageZ = CommonUtils.getImage("images/wall-v.png");//墙体-高度private int height;//墙体-长度private int width;//墙体-横坐标private int x;//墙体-纵坐标private int y;//游戏窗体属性 因为墙要在窗体中显示private GameClient gameClient;123456789101112
//无参构造public Wall(){}//带参构造public Wall(int x,int y,GameClient gameClient){this.x=x;this.y=y;this.width=30;this.height=70;this.gameClient=gameClient;}123456789101112
//插入墙体图片的方法public void paint(Graphics g){g.drawImage(wallImageZ,this.x,this.y,this.width,this.height,this.gameClient);}//生成get set 方法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 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;}1234567891011121314151617181920212223242526272829303132333435363738394041424344
10.2在客户端中实现插入障碍物图
// 创建墙体(wall),在参数中定义位置private Wall wall=new Wall(500,200,this);12
第一天(完成)效果图如下:第二天(人物以及帽子移动方向)优化代码(数据维护):
- 基于之前发表的文章详情可见点击查看前文
- 回顾之前的代码我们可以看到很多的固定常量值或是属性我们都会大量的重复使用,这并不符合我们代码简洁易懂的特点
- 所以我们可以把重复出现的量或是属性或是方法使用面向对象的思想 , 减少代码的冗余使代码简单明了
1.数据维护1.1在工具包(util)下创建数据维护类(DataPropertiesUtils)1.2在数据维护类中定义我们重复用到的常量属性值:
//窗体宽度public static final int CLIENT_WIDTH = 700;//窗体高度public static final int CLIENT_HEIGHT = 500;1234
1.3将所有涉及到代码冗余的方法、属性、数值、都在数据维护类中定义(此处省略)2.使人物移动2.1在人物类中加入