Java fishing game

Time:2021-1-11

This example for you to share the Java fishing Daren game specific code, for your reference, the specific content is as follows

The effect picture is as follows:

Source code sharing:

Test category:

package game;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

/**
 *Test class
 * @author Lenovo
 *
 */
public class Client {
 public static void main(String[] args) throws IOException {
 //Create window
 JFrame gameframe = new JFrame ("fishing talent");
 
 //Put the pond in the interface
 Pool pool = new Pool();
 gameFrame.setContentPane(pool);
 
 //Create window图标,绝对路径
 BufferedImage icon = ImageIO.read(new File("E:/New_life/fish_game/resource/images/fish07_03.png"));
 gameFrame.setIconImage(icon);
 //Set the size of the window
 gameFrame.setSize(800, 500);
 //The position of the window
 gameFrame.setLocation(10, 10);
 //The setting window cannot be dragged
 gameFrame.setResizable(false);
 //The settings window can be closed
 gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 //Let the window show
 gameFrame.setVisible(true);
 
 //Call method
 pool.action();
 }
}

Cannon setting:

package game;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Cannon {
 //Pictures of cannons
 private BufferedImage image;
 //Coordinate value
 private int x;
 
 private int y;
 
 public Cannon() throws IOException {
 this.image = ImageIO.read(new File("resource/images/barrel.png"));
 this.x = 420;
 this.y = 400; 
 
 }

 public BufferedImage getImage() {
 return image;
 }

 public void setImage(BufferedImage image) {
 this.image = image;
 }

 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;
 }
 
}

And fish pond setting:

package game;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class Pool extends JPanel{

 private static final long serialVersionUID = 1L;
 /**
 *Background picture
 *Sea King
 *Fish
 *Cannon
 *Status bar
 */
 //Pond
 private BufferedImage backgroud; 
 //Single fish
// private Fish fish;
 //Multiple and
 private Fish[] fishes;
 //Status bar
 private BufferedImage statusImage;
 //Cannon
 private Cannon cannon;
 //Mouse X axis
 private int mouseX;
 //Mouse Y axis
 private int mouseY;
 //Fishing net
 private Net net;
 //The angle at which the bullet was fired
 private double theta;
 //Bullet
 private LinkedList<Bullet> bullets;
 //Reflection origin
 
 
 public Pool() throws IOException {
 this.backgroud = ImageIO.read(new File("resource/images/bg.jpg"));
// this.fish = new Fish("fish08");
 //Set up 10 fish
 this.fishes = new Fish[11];
 for (int i = 0; i < 9; i++) {
  String fishName = "fish0" + (i+1);
  Fish fish = new Fish(fishName);
  this.fishes[i] = fish;
 }
 this.fishes[9] = new Fish("fish13"); 
 this.fishes[10] = new Fish("fish14"); 
 //Initialization status bar
 this.statusImage = ImageIO.read(new File("resource/images/bottom-bar.png"));
 //Initialize cannon
 this.cannon = new Cannon();
 //Call listener
 this.addListener();
 //Create a network
 this.net = new Net();
 //Array definition
 this.bullets = new LinkedList<Bullet>();
 }
 private void addListener() {
 //Add listener  
 this.addMouseListener(new MouseAdapter() {
  @Override
  public void mouseClicked(MouseEvent arg0) {
  System.out.println "Shoot! "";
  try {
   //Create bullets
   Bullet bullet = new Bullet(cannon.getX(), cannon.getY(), theta, Pool.this);
   //Start thread
   bullet.start();
   //Add objects to the collection
   bullets.add(bullet);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  }
  
  @Override
  public void mouseEntered(MouseEvent arg0) {
  //Enter and let the net show
  net.setShow(true);
  }
  @Override
  public void mouseExited(MouseEvent arg0) {
  //Quit, let the net disappear
  net.setShow(false);
  }

 });
 
 //Mouse movement monitoring
 this.addMouseMotionListener(new MouseAdapter() {
  @Override
  public void mouseMoved(MouseEvent e) {
  mouseX = e.getX() + 20;
  mouseY = e.getY();
  System.out.println("(" + mouseX+ "," +mouseY +")");
  //Fishing net移动
  net.move(mouseX, mouseY);
  }
 });
 
 }
 
 /**
 *Drawing interface
 */
 @Override
 public void paint(Graphics arg0) {
 super.paint(arg0);
 arg0.drawImage(backgroud, 0, 0, backgroud.getWidth(), backgroud.getHeight(), null);
 for (int i = 0; i < fishes.length; i++) {
  Fish fish = this.fishes[i];
  arg0.drawImage(fish.getImage(), 
   fish.getX(), 
   fish.getY(), 
   fish.getWidth(), 
   fish.getHeight(), null);
 }
// arg0.drawImage(this.fish.getImage(), this.fish.getX(), this.fish.getY(), this.fish.getWidth(), this.fish.getHeight(), null);
 //Draw status bar
 arg0.drawImage(statusImage,15, 400, statusImage.getWidth(), statusImage.getHeight(), null);
 
 //Painting cannon
 //Graphics: you can't draw a rotated picture. You need graphics2d to draw a rotated picture. Create a brush
 Graphics2D graphics2d = (Graphics2D) arg0.create();
 //Sets the angle of the brush

 //Calculate the center of rotation of the cannon
 double centerX = this.cannon.getX() + this.cannon.getImage().getWidth()/2;
 double centerY = this.cannon.getY() + this.cannon.getImage().getHeight()/4*3;
 double xx = this.mouseX - centerX;
 double yy = this.mouseY - centerY;
 //Finding the angle of inverse tangent
 this.theta =-Math.atan(xx/yy);
 graphics2d.rotate(theta, centerX ,centerY);
 
 graphics2d.drawImage(this.cannon.getImage(),
    this.cannon.getX(), 
    this.cannon.getY(), 
    this.cannon.getImage().getWidth(),
    this.cannon.getImage().getHeight(), null); 
 //Painting cannon结束
 
 //Draw fishing net, DrawImage is the parameter, is int type, so cast
 if (this.net.isShow()) {
  arg0.drawImage(this.net.getImage(),
    (int)this.net.getX(),
    (int)this.net.getY(),
    (int)this.net.getImage().getWidth(),
    (int)this.net.getImage().getHeight(),null);
 } 
 //Draw bullets
 //Bullet没有发射子弹之前
 for (Bullet bullet : bullets) {
  Graphics2D graphics2d2 = (Graphics2D)arg0.create();
  graphics2d2.rotate(bullet.getThread(), centerX, centerY);
  graphics2d2.drawImage(bullet.getImage(), 
    bullet.getX(), 
    bullet.getY(), 
    bullet.getWidth(), 
    bullet.getHeight(), null);  
 } 
 
 }
 
 
 /**
 *Please start your performance
 */
 public void action() {
 //Let the fish move
// this.fish.start();
 for (Fish fish : this.fishes) {
  fish.start();
 }
 //Redraw interface, anonymous inner class
 new Thread() {
  public void run() {
  while (true) {
   repaint();
  }
  };
 }.start();
 }
 public LinkedList<Bullet> getBullets() {
 return bullets;
 }
 public void setBullets(LinkedList<Bullet> bullets) {
 this.bullets = bullets;
 }
 public Fish[] getFishes() {
 return fishes;
 }
 public void setFishes(Fish[] fishes) {
 this.fishes = fishes;
 }
 
}

Fish setting:

package game;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;

public class Fish extends Thread{
 
 //Width
 @SuppressWarnings("unused")
 private int width;
 @SuppressWarnings("unused")
 private int height;
 //Location
 //X coordinate
 @SuppressWarnings("unused")
 private int x;
 //Y coordinate
 @SuppressWarnings("unused")
 private int y;
 //Picture
 @SuppressWarnings("unused")
 private BufferedImage image;
 //Speed
 @SuppressWarnings("unused")
 private int step;
 //Are you arrested
 @SuppressWarnings("unused")
 private boolean isCatch; 
 //Image array of fish swimming
 @SuppressWarnings("unused")
 private BufferedImage[] images;
 //Pictures of captured fish
 private BufferedImage[] catchImages;
 //Picture的下标
 @SuppressWarnings("unused")
 private int imagesIndex;
 
 /**
 *The structure of fish
 *@ param name the picture name of the fish
 * @throws IOException 
 */
 public Fish(String imageName) throws IOException {
 //Initialization of fish swimming
 this.images = new BufferedImage[10]; 
 for (int i = 0; i < 10; i++) {
  String fishName = imageName + "_0" + i + ".png";
  BufferedImage tempImage = ImageIO.read(new File("resource/images/" + fishName));
  images[i] = tempImage;
 }
 
 //Initialize image subscript
 this.imagesIndex = 0;
 this.image = this.images[this.imagesIndex];
 
 //Initialize the width and height of the fish
 this.width = this.image.getWidth();
 this.height = this.image.getHeight();
 
 //Initialize the coordinates of X and y
 this.x = 800;
 Random random = new Random();
 int nextInt = random.nextInt(400);
 this.y = nextInt;
 
 //Initialization speed
 this.step = random.nextInt(5);
 
 //Is initialization caught
 this.isCatch = false;
 this.catchImages = new BufferedImage[2];
 this.catchImages[0] = ImageIO.read(new File("resource/images/" + imageName + "_catch_01.png"));
 
// this.width = image.getWidth();
 }
 
 /**
 *The swimming of fish
 */
 public void move() {
 //Coordinates minus the speed of the swim
 this.x = this.x - this.step;
 
 //Switch pictures of fish
 this.image = this.images[this.imagesIndex ++ % this.images.length];
 
 //Swim again, less than fish and abscissa will return
 if (this.x < -this.width) {
  //Reset X coordinate
  this.x = 800;
  //Reset y coordinate
  Random random = new Random();
  this.y = random.nextInt(375);
  //Reset fish speed
  this.step = random.nextInt(5) + 1;
 }
 //Dormancy
 try {
  sleep(50);
 } catch (InterruptedException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 }
 /**
 *Tumble when captured
 */
 public void turnOver() {
 //Toggles the image of the fish when it is captured
 for (int i = 0; i < 6; i++) {
  this.image = this.catchImages[i % this.catchImages.length];
  try {
  sleep(50);
  } catch (InterruptedException e) {
  e.printStackTrace();
  }
 }
 //Reset the fish's attributes, coordinates, speed and whether it is caught or not
 this.x = 800;
 Random random = new Random();
 this.y = random.nextInt(375);
 this.step = random.nextInt(5) + 1;
 this.isCatch = false;
 } 
 
 @Override
 public void run() {
 while (true) {
  if (this.isCatch) {
  turnOver();
  }else {
  move(); 
  }
 } 
 }

 /**
 *The set and get methods of fish properties are generated
 * @return
 */
 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 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 BufferedImage getImage() {
 return image;
 }

 public void setImage(BufferedImage image) {
 this.image = image;
 }

 public boolean isCatch() {
 return isCatch;
 }

 public void setCatch(boolean isCatch) {
 this.isCatch = isCatch;
 }

}

Setting of fishing net (here the fishing net is static and defective)

package game;
/**
 *Fishing nets
 * @author Lenovo
 *
 */

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Net {
 //Picture
 private BufferedImage image;
 //X coordinate
 private double x;
 //Y coordinate
 private double y;
 //Width
 private double width;
 //Height
 private double height;
 //Display or not
 private boolean isShow;
 /**
 *Construction method of fishing net
 * @throws IOException 
 */
 public Net() throws IOException {
 //Initialize picture
 this.image = ImageIO.read(new File("resource/images/net09.png"));
 this.x = 100;
 this.y = 100;
 this.width = this.image.getWidth();
 this.height = this.image.getHeight();
 this.isShow = true; 
 
 }
 /**
 *Movement of fishing nets
 * @param mouseX
 * @param mouseY
 */
 public void move(double mouseX, double mouseY) {
 //Finding the center point of fishing net
 double centerX = this.x + this.width/2;
 double centerY = this.y + this.height/2;
 
 //Center point and x position from mouse
 double xx = mouseX - centerX;
 //Center point and y position from mouse
 double yy = mouseY - centerY;
 
 //Top left corner translation
 this.x = this.x + xx;
 this.y = this.y + yy; 
 
 }
 public BufferedImage getImage() {
 return image;
 }
 public void setImage(BufferedImage image) {
 this.image = image;
 }
 public double getX() {
 return x;
 }
 public void setX(double x) {
 this.x = x;
 }
 public double getY() {
 return y;
 }
 public void setY(double y) {
 this.y = y;
 }
 public double getWidth() {
 return width;
 }
 public void setWidth(double width) {
 this.width = width;
 }
 public double getHeight() {
 return height;
 }
 public void setHeight(double height) {
 this.height = height;
 }
 public boolean isShow() {
 return isShow;
 }
 public void setShow(boolean isShow) {
 this.isShow = isShow;
 }
}

Bullets fired

package game;

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 *Bullets fired
 * @author zouzhuo
 *
 */
public class Bullet extends Thread{
 //Picture
 private BufferedImage image;
 //Coordinate value
 private int x;
 private int y;
 //Size
 private int width;
 private int height;
 //Is it alive
 private boolean isAlive;
 //Speed
 private int step;
 //Angle
 private double thread;
 //The origin of the bullet
 private Point point;
 //Pond
 private Pool pool;
 
 public Bullet(int x, int y, Double thread, Pool pool) throws IOException {
 this.image = ImageIO.read(new File("resource/images/bullet1.png"));
 this.width = this.image.getWidth();
 this.height = this.image.getHeight();
 this.isAlive = true;
 this.step = 10;
 this.x = x;
 this.y = y;
 this.thread = thread;
 this.point = new Point(x, y);
// this.point.x = x;
// this.point.y =y;
 this.pool = pool;
 }
 /**
 *The speed at which the bullet moves
 */
 public void move() {
 this.y = this.y - this.step;
 
 //Judge out of bounds
 int distance = this.point.y - this.y;
 //For XX, further coercion is needed
 int xx = (int) (distance * Math.sin(this.thread));
 int xxx = this.point.x + xx;
 //Finding YY coordinates
 int yy = (int) (distance * Math.cos(this.thread));
 int yyy = this.point.y - yy;
 //Judge whether it is out of bounds
 if (xxx < 0 || xxx > 800 || yyy < 0) {
  //Put the bullet to death
  this.isAlive = false;
  //Delete bullet in array
  this.pool.getBullets().remove(this);
 }
 
 //Judge whether to hit the fish
 Fish[] fishs = pool.getFishes();
 for (Fish fish : fishs) {
  //X-coordinate range of fish
  int maxX = fish.getX() + fish.getWidth();
  //Y coordinate range of fish
  int mayY = fish.getY() + fish.getHeight();
  if (xxx > fish.getX() && xxx < maxX && fish.getY() < yyy && yyy < mayY) {
  //Set the fish to be caught
  fish.setCatch(true);
  //Set the bullet to disappear
  this.isAlive = false;
  //Delete bullet in array
  this.pool.getBullets().remove(this);
  }
 }
 try {
  sleep(50);
 } catch (InterruptedException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } 
 }
 @Override
 public void run() {
 super.run();
 while (true) {
  //Let the bullet fly
  if (isAlive) {
  move();
  }else {
  //End thread directly
  return;
  }
  
 }
 }
 public BufferedImage getImage() {
 return image;
 }
 public void setImage(BufferedImage image) {
 this.image = image;
 }
 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 double getThread() {
 return thread;
 }
 public void setThread(double thread) {
 this.thread = thread;
 }
 
}

There is also a scoreboard that has not been written down. There is no beginning and ending interface. The fishing net is static. These functions have not been realized and will be updated in the future.

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.