本文实例为大家分享了java实现捕鱼达人游戏的具体代码,供大家参考,具体内容如下
效果图如下:
源代码分享:
测试类:
package game;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
/**
* 测试类
* @author Lenovo
*
*/
public class Client {
public static void main(String[] args) throws IOException {
//创建窗口
JFrame gameFrame = new JFrame("捕鱼达人");
//将池塘放入到界面中去
Pool pool = new Pool();
gameFrame.setContentPane(pool);
//创建窗口图标,绝对路径
BufferedImage icon = ImageIO.read(new File("E:/New_life/fish_game/resource/images/fish07_03.png"));
gameFrame.setIconImage(icon);
//设置窗口的尺寸
gameFrame.setSize(800, 500);
//窗口的位置
gameFrame.setLocation(10, 10);
//设置窗口不可拖拽
gameFrame.setResizable(false);
//设置窗口可以关闭
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//让窗口显示
gameFrame.setVisible(true);
//调用方法
pool.action();
}
}
大炮的设置:
package game;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Cannon {
//大炮的图片
private BufferedImage image;
//坐标值
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;
}
}
与鱼塘的设置:
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;
/**
* 背景图片
* 海王
* 鱼
* 大炮
* 状态栏
*/
//池塘
private BufferedImage backgroud;
//单条鱼
// private Fish fish;
//多条与
private Fish[] fishes;
//状态栏
private BufferedImage statusImage;
//大炮
private Cannon cannon;
//鼠标x轴
private int mouseX;
//鼠标Y轴
private int mouseY;
//渔网
private Net net;
//子弹发射的角度
private double theta;
//子弹
private LinkedList bullets;
//反射原点
public Pool() throws IOException {
this.backgroud = ImageIO.read(new File("resource/images/bg.jpg"));
// this.fish = new Fish("fish08");
//设置10条鱼
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");
thi