用面向对象写个简化版的捕鱼达人,因为没有捕鱼功能,就两种鱼类,就叫个“深海大作战1.0版”吧!哈哈
文章源自亦枫博客-https://yflad.cn/2018.html
案例具体要求:
- 有图片
- x轴,y轴坐标
- 图片的高度,宽度
- 有多种“鱼”类,会动,且按一定比例随机出现
文章源自亦枫博客-https://yflad.cn/2018.html
代码如下:
package cn.yflad.buyudaren; /** * 所有鱼的父类 * @author yflad-F * */ import java.awt.image.BufferedImage; public abstract class Fish { public BufferedImage image; //有图片 public int x;//x坐标 public int y; public int height;//图片的高度 public int width; public abstract void step(); }
文章源自亦枫博客-https://yflad.cn/2018.html
package cn.yflad.buyudaren; import java.awt.image.BufferedImage; /** * 章鱼类 * @author yflad-F */ public class Zy extends Fish{ int xspeed=2; //x轴移动的速度 //图片数组 BufferedImage[] images; //通过无参构造函数对成员进行初始化操作 public Zy() { image=FishGame.zy1; //章鱼的图片 images=new BufferedImage[] {FishGame.zy1,FishGame.zy2,FishGame.zy3,FishGame.zy4,FishGame.zy5,FishGame.zy6,FishGame.zy7,FishGame.zy8,FishGame.zy9,FishGame.zy10,FishGame.zy11,FishGame.zy12,FishGame.zy13,FishGame.zy14,FishGame.zy15,FishGame.zy16}; width=image.getWidth(); // height=image.getHeight(); x=FishGame.WIDTH; //章鱼在窗口的最右端出来 y=(int) (Math.random()*(FishGame.HEIGHT-height)); } //章鱼的移动方式 int index=0; //控制图片 int stepIndex=0; @Override public void step() { /* * 定时器10毫秒执行一次 step()方法,相当于50毫秒切换一次图片 */ if (index++%5==0) { image=images[stepIndex++%images.length]; } x-=xspeed; } }
文章源自亦枫博客-https://yflad.cn/2018.html
package cn.yflad.buyudaren; import java.awt.image.BufferedImage; /** * 飞鱼类 * @author yflad-F */ public class Fy extends Fish{ int xspeed=2; //x轴移动的速度 //图片数组 BufferedImage[] images; //通过无参构造函数对成员进行初始化操作 public Fy() { image=FishGame.fy1; //飞鱼的图片 images=new BufferedImage[] {FishGame.fy1,FishGame.fy2,FishGame.fy3,FishGame.fy4,FishGame.fy5,FishGame.fy6,FishGame.fy7,FishGame.fy8,FishGame.fy9,FishGame.fy10,FishGame.fy11,FishGame.fy12,FishGame.fy13,FishGame.fy14,FishGame.fy15,FishGame.fy16,FishGame.fy17}; width=image.getWidth(); // height=image.getHeight(); x=FishGame.WIDTH; //飞鱼在窗口的最右端出来 y=(int) (Math.random()*(FishGame.HEIGHT-height)); } //飞鱼的移动方式 int index=0; //控制图片 int stepIndex=0; @Override public void step() { /* * 定时器10毫秒执行一次 step()方法,相当于50毫秒切换一次图片 */ if (index++%5==0) { image=images[stepIndex++%images.length]; } x-=xspeed; } }
文章源自亦枫博客-https://yflad.cn/2018.html
实现代码:
package cn.yflad.buyudaren; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.security.PublicKey; import java.util.Arrays; import java.util.Timer; import java.util.TimerTask; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; public class FishGame extends JPanel { // 定义窗口的宽高 public static final int WIDTH = 730; public static final int HEIGHT = 445; // 定义要加载的图片 public static BufferedImage bj; public static BufferedImage zy1; public static BufferedImage zy2; public static BufferedImage zy3; public static BufferedImage zy4; public static BufferedImage zy5; public static BufferedImage zy6; public static BufferedImage zy7; public static BufferedImage zy8; public static BufferedImage zy9; public static BufferedImage zy10; public static BufferedImage zy11; public static BufferedImage zy12; public static BufferedImage zy13; public static BufferedImage zy14; public static BufferedImage zy15; public static BufferedImage zy16; public static BufferedImage fy1; public static BufferedImage fy2; public static BufferedImage fy3; public static BufferedImage fy4; public static BufferedImage fy5; public static BufferedImage fy6; public static BufferedImage fy7; public static BufferedImage fy8; public static BufferedImage fy9; public static BufferedImage fy10; public static BufferedImage fy11; public static BufferedImage fy12; public static BufferedImage fy13; public static BufferedImage fy14; public static BufferedImage fy15; public static BufferedImage fy16; public static BufferedImage fy17; // 创建一个存储鱼类的数组 Fish[] fish = {}; static { try { bj = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\bg.jpg")); zy1 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\zy\\zy1.png")); zy2 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\zy\\zy2.png")); zy3 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\zy\\zy3.png")); zy4 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\zy\\zy4.png")); zy5 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\zy\\zy5.png")); zy6 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\zy\\zy6.png")); zy7 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\zy\\zy7.png")); zy8 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\zy\\zy8.png")); zy9 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\zy\\zy9.png")); zy10 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\zy\\zy10.png")); zy11 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\zy\\zy11.png")); zy12 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\zy\\zy12.png")); zy13 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\zy\\zy13.png")); zy14 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\zy\\zy14.png")); zy15 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\zy\\zy15.png")); zy16 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\zy\\zy16.png")); fy1 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\fy\\fy1.png")); fy2 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\fy\\fy2.png")); fy3 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\fy\\fy3.png")); fy4 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\fy\\fy4.png")); fy5 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\fy\\fy5.png")); fy6 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\fy\\fy6.png")); fy7 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\fy\\fy7.png")); fy8 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\fy\\fy8.png")); fy9 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\fy\\fy9.png")); fy10 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\fy\\fy10.png")); fy11 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\fy\\fy11.png")); fy12 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\fy\\fy12.png")); fy13 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\fy\\fy13.png")); fy14 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\fy\\fy14.png")); fy15 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\fy\\fy15.png")); fy16 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\fy\\fy16.png")); fy17 = ImageIO.read(FishGame.class.getResource("..\\images\\fish\\fy\\fy17.png")); } catch (Exception e) { System.out.println("图片加载失败!"); } } //重写JPanel中的paint()方法 @Override public void paint(Graphics g) { // 画背景 g.drawImage(bj, 0, 0, null); // 画鱼 for (int i = 0; i < fish.length; i++) { // 遍历数组对象 Fish f = fish[i]; // 获取每一个章鱼对象 if (f instanceof Zy) { // 判断 Zy zy = (Zy) f; // 获取每一个章鱼对象 g.drawImage(zy.image, zy.x, zy.y, null); } if (f instanceof Fy) { Fy fy = (Fy) f; // 获取每一个飞鱼对象 g.drawImage(fy.image, fy.x, fy.y, null); } } } // 创建章鱼,飞鱼对象。比例9:1 public Fish nextOne() { if ((int) (Math.random() * 10) == 0) { return new Fy(); // Fy fy=new fy(); } return new Zy(); // Zy zy=new Zy(); } // 创建对象存入数组的方法 int index = 0; public void addAction() { // 对数组进行扩容 if (index++ % 40 == 0) {// 相当于400毫秒创建一个对象 // 对数组进行扩容 fish = Arrays.copyOf(fish, fish.length + 1); fish[fish.length - 1] = nextOne(); } } // 所有鱼类移动的方法 public void stepAction() { // 遍历数组中的每一个鱼类对象 for (int i = 0; i < fish.length; i++) { Fish f = fish[i]; // 获取每一个对象 if (f instanceof Zy) { Zy z = (Zy) f; z.step(); // 让每一个章鱼对象动起来 } if (f instanceof Fy) { Fy fy = (Fy) f; fy.step(); // 让每一个飞鱼对象动起来 } } } // 用定时器让鱼类移动 Timer timer = new Timer(); public void action() { timer.schedule(new TimerTask() { @Override public void run() { addAction();// 调用 将鱼类对象放入敌机数组 的方法 stepAction();// 调用 移动 方法 repaint(); // 刷新界面 } }, 0, 10); } public static void main(String[] args) { JFrame frame = new JFrame("深海大游行"); FishGame game = new FishGame(); frame.add(game); // 相当于在画板中添加画笔 frame.setSize(WIDTH, HEIGHT);// 设置窗口的大小 frame.setVisible(true);// 窗口显示 frame.setLocationRelativeTo(null);// 窗口居中 frame.setAlwaysOnTop(true);// 窗口置顶 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置窗口关闭按钮 game.action(); } }
文章源自亦枫博客-https://yflad.cn/2018.html
文章源自亦枫博客-https://yflad.cn/2018.html文章源自亦枫博客-https://yflad.cn/2018.html继续阅读
扫扫关注公众号
我的微信
扫扫体验小程序
我的公众号