效果图如下:
图片资源:
链接:https://pan.baidu.com/s/197orGk1P2UG9wi9ef4MDlw
提取码:a77d
源代码分享:
测试类:

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<Bullet> 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"); this.fishes[10] = new Fish("fish14"); //初始化状态栏this.statusImage = ImageIO.read(new File("resource/images/bottom-bar.png"));//初始化大炮this.cannon = new Cannon();//调用监听器this.addListener();//创建网this.net = new Net();//数组定义this.bullets = new LinkedList<Bullet>();}private void addListener() {//添加监听器        this.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent arg0) {System.out.println("发射子弹!");try {//创建子弹Bullet bullet = new Bullet(cannon.getX(), cannon.getY(), theta, Pool.this);//启动线程bullet.start();//将对象添加到集合中去bullets.add(bullet);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Overridepublic void mouseEntered(MouseEvent arg0) {//进入,让渔网显示net.setShow(true);}@Overridepublic void mouseExited(MouseEvent arg0) {//退出,让渔网消失net.setShow(false);}});//鼠标移动监听this.addMouseMotionListener(new MouseAdapter() {@Overridepublic void mouseMoved(MouseEvent e) {mouseX = e.getX() + 20;mouseY = e.getY();System.out.println("(" + mouseX+ "," +mouseY +")");//渔网移动net.move(mouseX, mouseY);}});}/*** 画界面*/@Overridepublic 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);//画状态栏arg0.drawImage(statusImage,15, 400, statusImage.getWidth(), statusImage.getHeight(), null);//画大炮//Graphics:不能画旋转的图片,画旋转的图片需要Graphics2D,创建画笔Graphics2D graphics2d = (Graphics2D) arg0.create();//设置画笔的角度//计算大炮的旋转中心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;//求反切角度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);   //画大炮结束//画渔网,drawImage是参数是int类型,所以进行强制转换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);}    //画子弹//子弹没有发射子弹之前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);           }       }/*** 请开始你的表演*/public void action() {//让鱼动起来
//      this.fish.start();for (Fish fish : this.fishes) {fish.start();}//重新画界面,匿名内部类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;}}

鱼类的设置:

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{//宽度@SuppressWarnings("unused")private int width;@SuppressWarnings("unused")private int height;//位置//x坐标@SuppressWarnings("unused")private int x;//y坐标@SuppressWarnings("unused")private int y;//图片@SuppressWarnings("unused")private BufferedImage image;//速度@SuppressWarnings("unused")private int step;//是否被抓@SuppressWarnings("unused")private boolean isCatch;      //鱼游动的图片数组@SuppressWarnings("unused")private BufferedImage[] images;//抓获鱼的图片private BufferedImage[] catchImages;//图片的下标@SuppressWarnings("unused")private int imagesIndex;/***鱼的构造方法* @param name 鱼的图片名称* @throws IOException */public Fish(String imageName) throws IOException {//鱼游动的初始化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;}//初始化图片下标this.imagesIndex = 0;this.image = this.images[this.imagesIndex];//初始化鱼的宽度和高度this.width = this.image.getWidth();this.height = this.image.getHeight();//初始化x和y的坐标this.x = 800;Random random = new Random();int nextInt = random.nextInt(400);this.y = nextInt;//初始化速度this.step = random.nextInt(5);//初始化是否被抓住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();}/*** 鱼的游动*/public void move() {//坐标减去游动的速度this.x = this.x - this.step;//切换鱼的图片this.image = this.images[this.imagesIndex ++ % this.images.length];//重新游一遍,小于鱼与横坐标则返回if (this.x < -this.width) {//重置x坐标this.x = 800;//重置y坐标Random random = new Random();this.y = random.nextInt(375);//重置鱼游的速度this.step = random.nextInt(5) + 1;}//休眠try {sleep(50);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/*** 被捕获时翻滚*/public void turnOver() {//切换鱼被捕获时鱼的图片for (int i = 0; i < 6; i++) {this.image = this.catchImages[i % this.catchImages.length];try {sleep(50);} catch (InterruptedException e) {e.printStackTrace();}}//重置鱼的属性,坐标,速度,是否被抓this.x = 800;Random random = new Random();this.y = random.nextInt(375);this.step = random.nextInt(5) + 1;this.isCatch = false;}  @Overridepublic void run() {while (true) {if (this.isCatch) {turnOver();}else {move(); }}      }/*** 生成了鱼的属性set和get方法* @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;}}

鱼网的设置(这里渔网是静态的,有缺陷):

package game;
/*** 捕鱼网* @author Lenovo**/import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;import javax.imageio.ImageIO;public class Net {//图片private BufferedImage image;//X坐标private double x;//Y坐标private double y;//宽度private double width;//高度private  double height;//是否展示private boolean isShow;/*** 渔网构造方法* @throws IOException */public Net() throws IOException {//初始化图片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;       }/*** 渔网的移动* @param mouseX* @param mouseY*/public void move(double mouseX, double mouseY) {//求渔网的中心点double centerX = this.x + this.width/2;double centerY = this.y + this.height/2;//中心点与离鼠标的x位置double xx = mouseX - centerX;//中心点与离鼠标的y位置double  yy = mouseY - centerY;//左上角点平移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;}
}

发射的子弹

package game;import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;import javax.imageio.ImageIO;/*** 发射的子弹* @author zouzhuo**/
public class Bullet extends Thread{//图片private BufferedImage image;//坐标值private int x;private int y;//大小private int width;private int height;//是否活着private boolean isAlive;//速度private int step;//角度private double thread;//子弹发射的原点private Point point;//池塘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;}/*** 子弹移动的速度*/public void move() {this.y = this.y - this.step;//判断出界int distance = this.point.y - this.y;//求xx,需要进一步进行强制转换int xx = (int) (distance * Math.sin(this.thread));int xxx = this.point.x + xx;//求yy坐标int yy = (int) (distance * Math.cos(this.thread));int yyy = this.point.y - yy;//判断是否出界if (xxx < 0 || xxx > 800 || yyy < 0) {//将子弹置为死亡this.isAlive = false;//在数组中删除子弹this.pool.getBullets().remove(this);}//判断是否击中鱼Fish[] fishs = pool.getFishes();for (Fish fish : fishs) {//鱼的x坐标范围int maxX = fish.getX() + fish.getWidth();//鱼的y坐标范围int mayY = fish.getY() + fish.getHeight();if (xxx > fish.getX() && xxx < maxX && fish.getY() < yyy && yyy < mayY) {//设置鱼被抓到fish.setCatch(true);//设置让子弹消失this.isAlive = false;//在数组中删除子弹this.pool.getBullets().remove(this);}}try {sleep(50);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}       }@Overridepublic void run() {super.run();while (true) {//让子弹飞一会if (isAlive) {move();}else {//直接结束线程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;}}

还有一个计分板没有写上,没有开始结束的界面,渔网是静态的,这些功能都还没有实现,日后更新

java捕鱼达人游戏(大部分功能都已实现)相关推荐

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

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

  2. 基于Java+Swing实现捕鱼达人游戏(含课程报告)

    基于Java+Swing实现捕鱼达人游戏(含课程报告) 一.系统介绍 1.开发背景 2.基本内容.实现方法及主要技术实现目标 3实现目标 二.功能展示 三.其他系统 四.获取源码 一.系统介绍 1.开 ...

  3. java捕鱼达人_java实现捕鱼达人游戏

    本文实例为大家分享了java实现捕鱼达人游戏的具体代码,供大家参考,具体内容如下 效果图如下: 源代码分享: 测试类: package game; import java.awt.image.Buff ...

  4. 通过捕鱼达人游戏学习Java

    项目视频: Java项目捕鱼达人游戏 项目结构: 功能分析: 一.背景功能     1.Pool类:新建Pool类,池塘对象     2.特征,池塘环境,BufferedImageIO bg;    ...

  5. html5 canvas简易版捕鱼达人游戏源码

    插件描述:html5利用canvas写的一个js版本的捕鱼,有积分统计,鱼可以全方位移动,炮会跟着鼠标移动,第一次打开需要鼠标移出背景图,再移入的时候就可以控制炮的转动,因为是用的mouseover触 ...

  6. html5游戏开发 网页版-捕鱼达人游戏源码下载

    html5游戏开发 网页版-捕鱼达人游戏源码下载 来玩一把! 转载于:https://www.cnblogs.com/jsfoot/p/3215371.html

  7. Android开源项目:捕鱼达人游戏源代码

     Android开源项目:捕鱼达人游戏源代码 这是一个Android上的开源项目:捕鱼达人游戏源代码,github上的地址链接是:https://github.com/zhangphil/Andr ...

  8. 精仿高仿捕鱼达人游戏Android源码

    给大家分享一款精仿高仿捕鱼达人游戏源码,Android版,喜欢开发游戏的朋友可以下载学习,研究一下. 下载地址: http://code.662p.com/view/buyudaren.html

  9. 用cocos2dx做一个简单的单机捕鱼达人游戏(1)

    用cocos2dx做一个简单的单机捕鱼达人游戏(1) 我使用了cocos2dx 3.9版本和vs2017来开发 今天先做游戏开始界面 开始界面很简单,一个背景图,一个logo,3个button(三种登 ...

最新文章

  1. JavaScript中正则表达式学习(一)
  2. 俄罗斯“木船”机器人系统将于2020年部署部队
  3. 创建和触发Notification
  4. 显著增加bash交互舒适度的.inputrc
  5. 【完结】有三AI阿里云的深度学习基础课程暂时完结,欢迎扩散学习
  6. C++ 字符串编程训练1
  7. 索引访问方法及索引优化
  8. 基于Qt的光盘刻录开发
  9. Asp.Net Core中利用Seq组件展示结构化日志功能
  10. 嵌入式Linux系统编程学习之九基于文件描述符的文件操作
  11. 使用Promise链式调用解决多个异步回调的问题
  12. UE4 视差毛发材质
  13. C/C++ 大小端转换
  14. sub求阶乘c语言,用VB编写程序求S=A!+B!+C!,要求阶乘的计算使用Sub过程来实现,参数A、B、C的值从键盘输入的程序代码?...
  15. NET 常见网络命令
  16. 计算机网卡和交换机网卡以及交换机数据转发
  17. 贪吃蛇小游戏(C语言实现简易版)
  18. 货币战争悲壮的英雄:帕潘德里欧
  19. 从新手到Flutter架构师,一篇就够!这原因我服了
  20. MCE公司:免疫治疗新课题——好心情,要保持!

热门文章

  1. 工程管理系统源码企业工程管理系统简介
  2. 支付宝支付 开放平台设置以及公钥私钥生成配置
  3. socket 半双工
  4. JAVA之多sheet页表格生成工具类
  5. 防止windows 10系统崩溃的几种操作
  6. 如何使用git将文件夹中的代码导入到码云中
  7. Android 点击图标使APP由后台切换至前台重新启动欢迎页的问题
  8. 蚂蚁3期JAVA互联网高级架构师视频教程
  9. 选择万德L2接口需要遵循什么原则?
  10. 着色器语言(GLSL)基础学习三