Java 编写捕鱼达人游戏 窗体程序 完整源码

文章介绍了使用Java语言和SWING框架开发的捕鱼达人游戏,提供完整源码,游戏功能包括鼠标操作捕鱼,分数实时更新。开发环境为Eclipse或IDEA,通过读取资源文件实现图像展示,游戏中的图片和音乐来自网络。作者提供了捕获鱼的关键代码段,并邀请读者发现并报告bug以改进游戏。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天为大家分享捕鱼达人游戏的开发与制作,目前是单机版游戏,后续时间空了,会进一步完善。整个系统界面漂亮,有完整得源码,希望大家可以喜欢。喜欢的帮忙点赞和关注。一起编程、一起进步

开发环境

开发语言为Java,开发环境Eclipse或者IDEA都可以。运行主程序,或者执行打开JAR文件即可以运行本程序。

系统框架

利用JDK自带的SWING框架开发,不需要安装第三方JAR包。纯窗体模式,直接运行Main文件即可以。同时带有详细得设计文档。

主要功能

本程序主要用来真实模仿捕鱼的一个游戏,通过玩游戏放松心情,提高工作和学习的效率。

启动方法

对Fishlord.java点右键,run as Application,启动捕鱼达人游戏。

一 . 游戏说明:

1. 游戏操作简单,通过鼠标来移动渔网,对游动的鱼进行捕鱼。当渔网完全覆盖游动的鱼,表示捕鱼成功,相应的分数+1

2. 您的实时分将被显示在最上面

二 . 版权声明:

1. 游戏中的图片均由网络图片经本人整合、修改而成

2. 背景音乐来源网络

3. 如需源代码,评论或者私信

三. 若您在游戏中发现影响游戏的bug请务必联系本人改进

主要功能点

1 通过鼠标来移动渔网,对游动的鱼进行捕鱼。当渔网完全覆盖游动的鱼,表示捕鱼成功,相应的分数+1

2 分数实时更新

3 游戏的要点是动作要迅速

运行效果

关键代码

public class Fishlord {

    public static void main(String[] args) throws Exception {
        JFrame frame = new JFrame("捕鱼达人");
        Pool pool = new Pool();
        frame.add(pool);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 关闭窗口时关闭程序
        frame.setSize(800, 480);
        frame.setLocationRelativeTo(null);// 设置窗口居中,必须放在setSize之后
        frame.setResizable(false);// 不允许用户改变窗口大小
        frame.setVisible(true);
        pool.action();
    }
}

class Pool extends JPanel {
    BufferedImage background = null;
    Fish fish = null;;
    Fish[] fishs = new Fish[9];
    Net net = null;
    int score = 0;
    int fontsize = 20;
    Font font = new Font("楷体", Font.BOLD, fontsize);

    Pool() throws IOException {
        // background = ImageIO.read(new File("bg.jpg")); //读取工程目录图片
        background = ImageIO.read(getClass().getResourceAsStream(
                "/images/bg.jpg"));
        /**1)getClass().getResourceAsStream()方法读取的是src/images包下的图片
         *     2)background = ImageIO.read(new File("images/bg.jpg"));
         * 这个方法读取的是工程CatchFish/images文件夹下的图片
         */
       
        for (int i = 0; i < 9; i++) {
            fish = new Fish("fish0" + (i + 1));
            fishs[i] = fish;
            fish.start();
        }
    }

    public void paint(Graphics g) {  //paint什么时候调用?
        //System.out.println("paint");
        g.drawImage(background, 0, 0, null);
        for (int i = 0; i <fishs.length; i++) {
            Fish tempfish = fishs[i];
            g.drawImage(tempfish.fishimage, tempfish.x, tempfish.y, null);
        }
        if (net.show) {
            g.drawImage(net.netimage, net.x - net.width / 2, net.y - net.height
                    / 2, null);
        }
        g.setFont(font);
        g.setColor(Color.white);
        g.drawString("SCORE:", 10, 20);
        g.setColor(Color.red);
        g.drawString("      " + score, 10, 20);
    }

    public void action() throws Exception {
        
        net = new Net();

        MouseAdapter m = new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                net.show = true;
            }

            public void mouseExited(MouseEvent e) {
                net.show = false;
            }

            // 在鼠标移动时候执行
            public void mouseMoved(MouseEvent e) {
                // MouseEvent 鼠标事件:鼠标事件发生时间地点人物
                long time = e.getWhen();
                int x = e.getX();
                int y = e.getY();

                // Object o=e.getSource();//发生事件的物体pool
                net.x = x;
                net.y = y;
            }

            public void mousePressed(MouseEvent e) {
                catchFish();// catch:抓鱼 在鼠标按下的时候,进行抓鱼操作
            }

        };
        // 在当前方法中代表当前的 这个(this)pool对象
        this.addMouseListener(m); // 处理这个pool对象鼠标动作
        this.addMouseMotionListener(m);
        net.show = true;// 调试代码

        while (true) {
            //System.out.println("repaint");
            repaint();
            try {
                Thread.sleep(80);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    protected void catchFish() {
        // 鱼在不在网的范围内?在的话就让鱼消失
        for (int i = 0; i < fishs.length; i++) {
            fish = fishs[i];
            if (fish.contains(net.x, net.y)) {// 判断在不在网的范围
                fish.getOut();
                score += fish.width / 10;
            }
        }
    }

}

class Fish extends Thread {
    int x, y, index = 0, width, height, step;

    BufferedImage fishimage;
    BufferedImage[] fishimages = new BufferedImage[9];
    Random r;

    Fish(String fishname) throws IOException {
        //System.out.println("Fish()");
        for (int i = 0; i < 9; i++) {
            // BufferedImage tempfishimage = ImageIO.read(new File(fishname +
            // "_0"
            // + (i + 1) + ".png"));
            BufferedImage tempfishimage = ImageIO.read(getClass()
                    .getResourceAsStream(
                            "/images/" + fishname + "_0" + (i + 1) + ".png"));
            fishimages[i] = tempfishimage;
        }
        fishimage = fishimages[index];
        r = new Random();// 不写数字表示的是int范围内的一个数字
        width = fishimage.getWidth();
        height = fishimage.getHeight();
        x = 790;
        y = r.nextInt(470 - height);
        step = r.nextInt(4) + 1;
    }

    public void run() {
        while (true) {
            try {
                Thread.sleep(50);
                index++;
                fishimage = fishimages[index % fishimages.length];
                // 现在要动,所以要改变图片?300
                x = x - step;
                if (x <= 0 || y <= 0 || y >= 480)
                    getOut();
            } catch (Exception e) {
            }
        }
    }

    // 检查(netx,nety)的坐标是否在鱼的范围之内
    public boolean contains(int netx, int nety) {
        int dx = netx - x;
        int dy = nety - y;
        return dx >= 0 && dx <= width && dy >= 0 && dy <= height;
    }

    void getOut() {
        Random r = new Random();
        x = 790;
        y = r.nextInt(470 - height);
        step = r.nextInt(4) + 1;
    }
}

class Net {
    // 网的位置随着鼠标指针的移动而移动
    BufferedImage netimage = null;
    int x = 0, y = 0, width, height;
    boolean show;// 是否显示当前网对象

    Net() throws Exception {
        // netimage = ImageIO.read(new File("net09.png"));

        netimage = ImageIO.read(getClass().getResourceAsStream(
                "/images/net09.png"));
        show = false;
        width = netimage.getWidth();
        height = netimage.getHeight();
    }
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

计算机程序

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值