首页 > 代码库 > 捕鱼达人

捕鱼达人

界面

技术分享
package fishgame;

import javax.swing.JFrame;

public class FishGame extends JFrame {
    public static final int HEIGHT=480;
    public static final int WIDTH=800;
    public static void main(String args[]){
        JFrame frame = new JFrame("捕鱼达人");
        frame.setSize(WIDTH, HEIGHT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        FishPanel panel = new FishPanel(); //创建鱼池
        frame.add(panel);//加入FishPanel
        frame.addMouseMotionListener(panel);//注册鼠标移动监听
        frame.addMouseListener(panel);//注册鼠标单击监听
        frame.setUndecorated(true);//设置窗口无边缘
        frame.setVisible(true);
        panel.action();//启动鱼池的方法
    }
}
View Code

面板

技术分享
package fishgame;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.util.Random;

import javax.swing.JPanel;

public class FishPanel extends JPanel implements MouseListener,MouseMotionListener {
    BufferedImage bg;//背景图
    Fish fishs[]=new Fish[11];//有11中鱼
    int score;//分数
    Net net=new Net();//
    FishPanel(){
        bg=ImageUtil.getImage("img/bg.jpg");
        for(int i=0;i<11;i++){//插入11种鱼
            if(i<9){
                fishs[i]=new Fish("img/fish0"+(i+1));
            }
            else{
                fishs[i]=new Fish("img/fish"+(i+1));
            }
        }
    }
    public void paint(Graphics g){
        super.paint(g);
        g.drawImage(bg, 0, 0, null);//静态显示背景图片
        for(Fish fish : fishs){
            fish.draw(g);//动态显示鱼的图片
        }
          net.draw(g);//动态显示渔网
          g.setColor(Color.white);
          g.drawString("积分:"+score, 10, 20);
        }

    public void action(){
         for(Fish fish : fishs){
                fish.start();//每条鱼是一个线程,启动线程
         }
         while(true){
             repaint();  //最终调用paint(Graphics g)
         }

    }
    @Override
    public void mouseDragged(MouseEvent e) {
        // TODO 自动生成的方法存根
        
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        // TODO 自动生成的方法存根
        net.x=e.getX();
        net.y=e.getY();
        repaint();
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        Random rd = new Random();
        for(Fish fish : fishs){//全部扫描,看哪条鱼
            if(net.contain(fish)){//渔网能网住鱼
                int n = rd.nextInt(100); //有几率杀鱼
                   if(n>50){
                         score+=fish.width/10;
                         fish.out();//杀死鱼了
                   }
             }
        }
    }


    @Override
    public void mousePressed(MouseEvent e) {
        // TODO 自动生成的方法存根
        
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO 自动生成的方法存根
        
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO 自动生成的方法存根
        
    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO 自动生成的方法存根
        
    }

}
View Code

技术分享
package fishgame;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

public class Net {
    int x ;
    int y ;
    int width ;
    int height ;
    BufferedImage img ;
    
    public Net(){
        img = ImageUtil.getImage("img/net.png");
        width = img.getWidth();
        height = img.getHeight();
        x = 50;
        y = 50;
    }
    public void draw(Graphics g){//调整网,由于坐标y轴是反转的,所以是减的
        g.drawImage(img, x-width/2, y-height/2-6, null);
    }

    boolean contain(Fish fish){//捕鱼逻辑
        return (x - fish.x > 0 && x - fish.x<fish.width &&
                y - fish.y > 0 && y - fish.y < fish.height);

    }
}
View Code

技术分享
package fishgame;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;

public class Fish extends Thread {
    public int x;
    public int width;
    public int y;
    public int height;
    BufferedImage image[]=new BufferedImage[10];//鱼游泳图
    BufferedImage fishCatch[]=new BufferedImage[2];//捉鱼图片
    BufferedImage img;//要展示的图
    Random rd=new Random();
    int step;
    boolean isOut=false;

    Fish(String s){
        for(int i=0;i<10;i++){
            if(i<9){//把一种鱼游泳图放进去
                image[i]=ImageUtil.getImage(s+"_0"+(i+1)+".png");
                if(i<2)
                    fishCatch[i]=ImageUtil.getImage(s+"_catch_0"+(i+1)+".png");
            }
            else 
                image[i]=ImageUtil.getImage(s+"_"+(i+1)+".png");
        }
        img = image[0];//同一种鱼的尺寸是一样的
        width = img.getWidth();//一开始的位置
        height = img.getHeight();
        x = rd.nextInt(FishGame.WIDTH-width);
        y = rd.nextInt(FishGame.HEIGHT-height);
        step = rd.nextInt(5)+5;//步长,游速
        
    }
    public void draw(Graphics g){//画鱼
        g.drawImage(img, x, y, null);
    }
    public void move(){//鱼游动
        x -= step;
        if(x<-width){//如果鱼已经游出界面,重新定位鱼游入界面的x,y值
            x = 800;
            y = rd.nextInt(FishGame.HEIGHT-height);
            step = rd.nextInt(5)+5;
        }
    }
    public void run(){
        int index=0;
        for(;;){
            if(step==0){//捉到鱼
                for(int i=0;i<10;i++){
                    img=fishCatch[i%2];
                    try {
                        Thread.sleep(100);//线程0.1秒休眠一下
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                x = 800;//重新给他速度和时间
                y = rd.nextInt(FishGame.HEIGHT-height);
                step = rd.nextInt(5)+5;

            }
            else{
                move();//一直移动
                img=image[index%10];
                index++;
                try {//总的来说 step就是一步多远,线程就是时间 ,速度=一步多远+时间间隔
                    Thread.sleep(100);//线程0.1秒休眠一下
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }
    public void out(){
//        x = 800;
//        y = rd.nextInt(FishGame.HEIGHT-height);
        step=0;
        
    }

}
View Code

图片捕捉

技术分享
package fishgame;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;

import javax.imageio.ImageIO;

public class ImageUtil {//辅助提起图片
//    public static BufferedImage getImage(String fileName){
//        BufferedImage image=null;
//        try{
//            image=ImageIO.read(new FileInputStream(fileName));
//        }catch(Exception e){
//            System.out.println(e.getMessage());
//        }
//        return image;
//    }
    public static BufferedImage getImage(String fileName){
        BufferedImage image=null;
        try{
            image=ImageIO.read(new FileInputStream(fileName) );
            
        }catch(Exception e){
            System.out.println("111");
            System.out.println(e.getMessage());
        }
        return image;
    }
}
View Code

技术分享

 

图片资源可以加我qq 1063215202 要

捕鱼达人