热门标签 | HotTags
当前位置:  开发笔记 > 运维 > 正文

java实现捕鱼达人游戏

这篇文章主要介绍了java实现捕鱼达人游戏,大部分功能都已实现,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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"); 
 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();
 }
 private void addListener() {
 //添加监听器  
 this.addMouseListener(new MouseAdapter() {
  @Override
  public 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 block
   e.printStackTrace();
  }
  }
  
  @Override
  public void mouseEntered(MouseEvent arg0) {
  //进入,让渔网显示
  net.setShow(true);
  }
  @Override
  public void mouseExited(MouseEvent arg0) {
  //退出,让渔网消失
  net.setShow(false);
  }

 });
 
 //鼠标移动监听
 this.addMouseMotionListener(new MouseAdapter() {
  @Override
  public void mouseMoved(MouseEvent e) {
  mouseX = e.getX() + 20;
  mouseY = e.getY();
  System.out.println("(" + mouseX+ "," +mouseY +")");
  //渔网移动
  net.move(mouseX, mouseY);
  }
 });
 
 }
 
 /**
 * 画界面
 */
 @Override
 public void paint(Graphics arg0) {
 super.paint(arg0);
 arg0.drawImage(backgroud, 0, 0, backgroud.getWidth(), backgroud.getHeight(), null);
 for (int i = 0; i  getBullets() {
 return bullets;
 }
 public void setBullets(LinkedList 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 block
  e.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;
 } 
 
 @Override
 public 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 

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


推荐阅读
  • Linux服务器密码过期策略、登录次数限制、私钥登录等配置方法
    本文介绍了在Linux服务器上进行密码过期策略、登录次数限制、私钥登录等配置的方法。通过修改配置文件中的参数,可以设置密码的有效期、最小间隔时间、最小长度,并在密码过期前进行提示。同时还介绍了如何进行公钥登录和修改默认账户用户名的操作。详细步骤和注意事项可参考本文内容。 ... [详细]
  • Centos7.6安装Gitlab教程及注意事项
    本文介绍了在Centos7.6系统下安装Gitlab的详细教程,并提供了一些注意事项。教程包括查看系统版本、安装必要的软件包、配置防火墙等步骤。同时,还强调了使用阿里云服务器时的特殊配置需求,以及建议至少4GB的可用RAM来运行GitLab。 ... [详细]
  • iOS Swift中如何实现自动登录?
    本文介绍了在iOS Swift中如何实现自动登录的方法,包括使用故事板、SWRevealViewController等技术,以及解决用户注销后重新登录自动跳转到主页的问题。 ... [详细]
  • 本文介绍了使用SSH免密登录的步骤,包括生成公私钥、传递公钥给被登录机、修改文件权限的操作。同时提醒用户注意私钥的传递方式,建议使用U盘等离线方式传递。 ... [详细]
  • Java学习笔记之使用反射+泛型构建通用DAO
    本文介绍了使用反射和泛型构建通用DAO的方法,通过减少代码冗余度来提高开发效率。通过示例说明了如何使用反射和泛型来实现对不同表的相同操作,从而避免重复编写相似的代码。该方法可以在Java学习中起到较大的帮助作用。 ... [详细]
  • 原理:dismiss再弹出,把dialog设为全局对象。if(dialog!null&&dialog.isShowing()&&!(Activity.)isFinishing()) ... [详细]
  • SpringBoot整合SpringSecurity+JWT实现单点登录
    SpringBoot整合SpringSecurity+JWT实现单点登录,Go语言社区,Golang程序员人脉社 ... [详细]
  • LVS实现负载均衡的原理LVS负载均衡负载均衡集群是LoadBalance集群。是一种将网络上的访问流量分布于各个节点,以降低服务器压力,更好的向客户端 ... [详细]
  • 本文详细介绍了在Centos7上部署安装zabbix5.0的步骤和注意事项,包括准备工作、获取所需的yum源、关闭防火墙和SELINUX等。提供了一步一步的操作指南,帮助读者顺利完成安装过程。 ... [详细]
  • GSIOpenSSH PAM_USER 安全绕过漏洞
    漏洞名称:GSI-OpenSSHPAM_USER安全绕过漏洞CNNVD编号:CNNVD-201304-097发布时间:2013-04-09 ... [详细]
  • 本文介绍了在RHEL 7中的系统日志管理和网络管理。系统日志管理包括rsyslog和systemd-journal两种日志服务,分别介绍了它们的特点、配置文件和日志查询方式。网络管理主要介绍了使用nmcli命令查看和配置网络接口的方法,包括查看网卡信息、添加、修改和删除配置文件等操作。 ... [详细]
  • Python脚本编写创建输出数据库并添加模型和场数据的方法
    本文介绍了使用Python脚本编写创建输出数据库并添加模型数据和场数据的方法。首先导入相应模块,然后创建输出数据库并添加材料属性、截面、部件实例、分析步和帧、节点和单元等对象。接着向输出数据库中添加场数据和历程数据,本例中只添加了节点位移。最后保存数据库文件并关闭文件。文章还提供了部分代码和Abaqus操作步骤。另外,作者还建立了关于Abaqus的学习交流群,欢迎加入并提问。 ... [详细]
  •     这里使用自己编译的hadoop-2.7.0版本部署在windows上,记得几年前,部署hadoop需要借助于cygwin,还需要开启ssh服务,最近发现,原来不需要借助cy ... [详细]
  • 大坑|左上角_pycharm连接服务器同步写代码(图文详细过程)
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了pycharm连接服务器同步写代码(图文详细过程)相关的知识,希望对你有一定的参考价值。pycharm连接服务 ... [详细]
  • Hadoop2.6.0 + 云centos +伪分布式只谈部署
    3.0.3玩不好,现将2.6.0tar.gz上传到usr,chmod-Rhadoop:hadophadoop-2.6.0,rm掉3.0.32.在etcp ... [详细]
author-avatar
宛岸
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有