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;}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
7.2在客户端中实现插入人物图// 创建人物-小丑,在参数中定义位置private Buffoon buffoon = new Buffoon(200,200,this);//重写父类Frame类的paint方法以插入各种图片@Overridepublic void paint(Graphics g){//用角色-小丑调用paint方法以给图片宽 高 速度buffoon.paint(g);}12345678
8.实现发射物的显示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;}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
8.2在客户端中实现插入帽子图// 创建发射物-帽子(missile),在参数中定义位置private Missile missile = new Missile(300,300,this);//重写父类Frame类的paint方法以插入各种图片@Overridepublic void paint(Graphics g){//用发射物-帽子调用paint方法以给图片宽 高 速度missile.paint(g);}12345678
9.实现爆炸物的显示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
- 现状|程序员现状揭秘:平均年薪20.36万,Java人才需求量最大
- 文案|「热点传递」为什么别人卖点写的“勾人”?
- 程序员学英语第1天——JavaScript 程序测试的介绍1
- 三年Java开发,刚从美团、京东、阿里面试归来,分享个人面经
- 《深入理解Java虚拟机》:对象创建、布局和访问全过程
- java面试题整理
- Kotlin集合vs Kotlin序列与Java流
- Java安全之Javassist动态编程
- 推荐Java工程师必看,12个Hadoop领域的上手项目
- 震惊!京东T4大佬面试整整三个月,才写了两份java面试笔记