100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > Java 编写捕鱼达人游戏 窗体程序 完整源码

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

时间:2018-08-07 11:21:13

相关推荐

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

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

开发环境

开发语言为Java,开发环境Eclipse或者IDEA都可以。运行主程序,或者执行打开JAR文件即可以运行本程序。

系统框架

利用JDK自带的SWING框架开发,不需要安装第三方JAR包。纯窗体模式,直接运行Main文件即可以。同时带有详细得设计文档。

主要功能

本程序主要用来真实模仿捕鱼的一个游戏,通过玩游戏放松心情,提高工作和学习的效率。

启动方法

对Fishlord.java点右键,run as Application,启动捕鱼达人游戏。

一 . 游戏说明:

1. 游戏操作简单,通过鼠标来移动渔网,对游动的鱼进行捕鱼。当渔网完全覆盖游动的鱼,表示捕鱼成功,相应的分数+1

2. 您的实时分将被显示在最上面

二 . 版权声明:

1. 游戏中的图片均由网络图片经本人整合、修改而成

2. 背景音乐来源网络

3. 如需源代码,评论或者私信

三. 若您在游戏中发现影响游戏的bug请务必联系本人改进

主要功能点

1 通过鼠标来移动渔网,对游动的鱼进行捕鱼。当渔网完全覆盖游动的鱼,表示捕鱼成功,相应的分数+1

2 分数实时更新

3 游戏的要点是动作要迅速

运行效果

关键代码

public class Fishlord {public static void main(String[] args) throws Exception {JFrame frame = new JFrame("捕鱼达人");Pool pool = new Pool();frame.add(pool);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 关闭窗口时关闭程序frame.setSize(800, 480);frame.setLocationRelativeTo(null);// 设置窗口居中,必须放在setSize之后frame.setResizable(false);// 不允许用户改变窗口大小frame.setVisible(true);pool.action();}}class Pool extends JPanel {BufferedImage background = null;Fish fish = null;;Fish[] fishs = new Fish[9];Net net = null;int score = 0;int fontsize = 20;Font font = new Font("楷体", Font.BOLD, fontsize);Pool() throws IOException {// background = ImageIO.read(new File("bg.jpg")); //读取工程目录图片background = ImageIO.read(getClass().getResourceAsStream("/images/bg.jpg"));/**1)getClass().getResourceAsStream()方法读取的是src/images包下的图片*2)background = ImageIO.read(new File("images/bg.jpg"));* 这个方法读取的是工程CatchFish/images文件夹下的图片*/for (int i = 0; i < 9; i++) {fish = new Fish("fish0" + (i + 1));fishs[i] = fish;fish.start();}}public void paint(Graphics g) { //paint什么时候调用?//System.out.println("paint");g.drawImage(background, 0, 0, null);for (int i = 0; i <fishs.length; i++) {Fish tempfish = fishs[i];g.drawImage(tempfish.fishimage, tempfish.x, tempfish.y, null);}if (net.show) {g.drawImage(image, net.x - net.width / 2, net.y - net.height/ 2, null);}g.setFont(font);g.setColor(Color.white);g.drawString("SCORE:", 10, 20);g.setColor(Color.red);g.drawString("" + score, 10, 20);}public void action() throws Exception {net = new Net();MouseAdapter m = new MouseAdapter() {public void mouseEntered(MouseEvent e) {net.show = true;}public void mouseExited(MouseEvent e) {net.show = false;}// 在鼠标移动时候执行public void mouseMoved(MouseEvent e) {// MouseEvent 鼠标事件:鼠标事件发生时间地点人物long time = e.getWhen();int x = e.getX();int y = e.getY();// Object o=e.getSource();//发生事件的物体poolnet.x = x;net.y = y;}public void mousePressed(MouseEvent e) {catchFish();// catch:抓鱼 在鼠标按下的时候,进行抓鱼操作}};// 在当前方法中代表当前的 这个(this)pool对象this.addMouseListener(m); // 处理这个pool对象鼠标动作this.addMouseMotionListener(m);net.show = true;// 调试代码while (true) {//System.out.println("repaint");repaint();try {Thread.sleep(80);} catch (Exception e) {e.printStackTrace();}}}protected void catchFish() {// 鱼在不在网的范围内?在的话就让鱼消失for (int i = 0; i < fishs.length; i++) {fish = fishs[i];if (fish.contains(net.x, net.y)) {// 判断在不在网的范围fish.getOut();score += fish.width / 10;}}}}class Fish extends Thread {int x, y, index = 0, width, height, step;BufferedImage fishimage;BufferedImage[] fishimages = new BufferedImage[9];Random r;Fish(String fishname) throws IOException {//System.out.println("Fish()");for (int i = 0; i < 9; i++) {// BufferedImage tempfishimage = ImageIO.read(new File(fishname +// "_0"// + (i + 1) + ".png"));BufferedImage tempfishimage = ImageIO.read(getClass().getResourceAsStream("/images/" + fishname + "_0" + (i + 1) + ".png"));fishimages[i] = tempfishimage;}fishimage = fishimages[index];r = new Random();// 不写数字表示的是int范围内的一个数字width = fishimage.getWidth();height = fishimage.getHeight();x = 790;y = r.nextInt(470 - height);step = r.nextInt(4) + 1;}public void run() {while (true) {try {Thread.sleep(50);index++;fishimage = fishimages[index % fishimages.length];// 现在要动,所以要改变图片?300x = x - step;if (x <= 0 || y <= 0 || y >= 480)getOut();} catch (Exception e) {}}}// 检查(netx,nety)的坐标是否在鱼的范围之内public boolean contains(int netx, int nety) {int dx = netx - x;int dy = nety - y;return dx >= 0 && dx <= width && dy >= 0 && dy <= height;}void getOut() {Random r = new Random();x = 790;y = r.nextInt(470 - height);step = r.nextInt(4) + 1;}}class Net {// 网的位置随着鼠标指针的移动而移动BufferedImage netimage = null;int x = 0, y = 0, width, height;boolean show;// 是否显示当前网对象Net() throws Exception {// netimage = ImageIO.read(new File("net09.png"));netimage = ImageIO.read(getClass().getResourceAsStream("/images/net09.png"));show = false;width = netimage.getWidth();height = netimage.getHeight();}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。