行业知识
Java《大鱼吃小鱼》游戏思路及实现(含源码)
2026-12-04 18:57:34
来源:互联网转载
本游戏代码参考【尚学堂】Java开发游戏_大鱼吃小鱼项目实战教程
目录
- 游戏展示
- 游戏脑图
- 详细分析
- GameUtils
- Enemy
- Myfish
- GameWin
- 键盘监听
- 鼠标监听
- 重绘界面
- 游戏状态
- 产生敌方鱼
- 碰撞检测
- 重新游戏
- 添加背景音乐
- 全部源代码
游戏展示
这是写好的游戏在idea里的运行结果
大鱼吃小鱼游戏视频
游戏脑图
写游戏时的思想框架
详细分析
GameUtils
用Image来绘制各种背景,各种鱼类的图片
用ArrayList集合来存储在游戏过程中会出现的各种鱼类,以便在主方法中获取里面的鱼,和在重新游戏的功能里清空游戏界面上的鱼类
定义静态的变量,以便在其他类中的使用
package EatGame;import java.awt.*;import java.util.ArrayList;import java.util.List;public class GameUtils {// //方向 //添加方向判定,对我方鱼的移动的控制 static boolean UP = false; static boolean DOWN = false; static boolean LEFT = false; static boolean RIGHT = false; //分数 static int count = 0; //ArrayList集合来存储在游戏过程中会出现的各种鱼类 //创建敌方鱼类集合 批量生产鱼类 public static List EnemyList = new ArrayList(); public static List EnemyBossList = new ArrayList(); //背景图片 public static Image img1 = Toolkit.getDefaultToolkit().createImage("D:\IDEACode\untitled\src\EatGame\picture\1.jpg"); //开始游戏图片 public static Image img2 = Toolkit.getDefaultToolkit().createImage("D:\IDEACode\untitled\src\EatGame\picture\2.jpg"); //敌方鱼类A右 public static Image img3 = Toolkit.getDefaultToolkit().createImage("D:\IDEACode\untitled\src\EatGame\picture\3.gif"); //敌方鱼类A左 public static Image img4 = Toolkit.getDefaultToolkit().createImage("D:\IDEACode\untitled\src\EatGame\picture\4.gif"); //敌方鱼类B左 public static Image img9 = Toolkit.getDefaultToolkit().createImage("D:\IDEACode\untitled\src\EatGame\picture\fish2_r.png");//敌方鱼类B左 //敌方鱼类B右 public static Image img12 = Toolkit.getDefaultToolkit().createImage("D:\IDEACode\untitled\src\EatGame\picture\fish2_l.png");//敌方鱼类B左 //敌方鱼类C左 public static Image img10 = Toolkit.getDefaultToolkit().createImage("D:\IDEACode\untitled\src\EatGame\picture\fish3_r.png"); //敌方鱼类C右 public static Image img13 = Toolkit.getDefaultToolkit().createImage("D:\IDEACode\untitled\src\EatGame\picture\fish3_l.png"); //Boss鱼 public static Image img11 = Toolkit.getDefaultToolkit().createImage("D:\IDEACode\untitled\src\EatGame\picture\boss.gif"); //我方鱼类 public static Image img5 = Toolkit.getDefaultToolkit().createImage("D:\IDEACode\untitled\src\EatGame\picture\myfish_left.gif"); public static Image img6 = Toolkit.getDefaultToolkit().createImage("D:\IDEACode\untitled\src\EatGame\picture\myfish_right.gif"); //胜利 public static Image img7 = Toolkit.getDefaultToolkit().createImage("D:\IDEACode\untitled\src\EatGame\picture\win.jpg"); //失败 public static Image img8 = Toolkit.getDefaultToolkit().createImage("D:\IDEACode\untitled\src\EatGame\picture\fail.jpg"); //结束 public static Image img14 = Toolkit.getDefaultToolkit().createImage("D:\IDEACode\untitled\src\EatGame\picture\5.jpg");}Enemy
Enemy类是所有敌方鱼类的父类,里面有对敌方鱼的描述和获得自身矩形以便碰撞检测的方法
x,y分别为鱼在画布上的坐标
width,height分别为绘制的鱼类图片的大小
import java.awt.*;public class Enemy { //定义图片 Image img; //定义物体坐标 int x; int y; int width; int height; //移动速度 int speed; //方向 向右为1;向左为-1 int dir ; //类型 int type; //吃了获得的分值 int count; //等级 int size; //绘制自身方法 public void paintSelf(Graphics g){ g.drawImage(img,x,y,width,height,null); } //获取自身矩形以用于检测碰撞 public Rectangle getRec(){ return new Rectangle(x,y,width,height); }}//敌方鱼类A左class EnemyAZ extends Enemy{ EnemyAZ(){ this.x = -45; this.y = (int)(Math.random()*700+100); this.width = 30; this.height = 50; this.speed = 8; this.dir = 1; this.count = 1; this.type = 1; this.size = 0; this.img = GameUtils.img4; }}//敌方鱼类A右class EnemyAY extends Enemy{ EnemyAY(){ this.x = 1440; this.y = (int)(Math.random()*700+100); this.width = 30; this.height = 50; this.speed = 8; this.dir = -1; this.count = 1; this.type = 1; this.size = 0; this.img = GameUtils.img3; }}//敌方鱼类B左class EnemyBZ extends Enemy{ EnemyBZ(){ this.x = -45; this.y = (int)(Math.random()*700+100); this.width = 50; this.height = 80; this.speed = 5; this.dir = 1; this.count = 3; this.type = 2; this.size = 15; this.img = GameUtils.img9; }}//敌方鱼类B右class EnemyBY extends Enemy{ EnemyBY(){ this.x = 1440; this.y = (int)(Math.random()*700+100); this.width = 50; this.height = 80; this.speed = 5; this.dir = -1; this.count = 3; this.type = 2; this.size = 15; this.img = GameUtils.img12; }}//敌方鱼类C左class EnemyCZ extends Enemy { EnemyCZ() { this.x = -400; this.y = (int) (Math.random() * 700 + 100); this.width = 300; this.height = 120; this.speed = 7; this.dir = 1; this.count = 10; this.type = 2; this.size = 100; this.img = GameUtils.img10; }}//敌方鱼类C右class EnemyCY extends Enemy { EnemyCY() { this.x = 1600; this.y = (int) (Math.random() * 700 + 100); this.width = 300; this.height = 120; this.speed = 7; this.dir = -1; this.count = 10; this.type = 2; this.size = 100; this.img = GameUtils.img13; }}//Boss鱼class EnemyBoss extends Enemy { EnemyBoss() { this.x = -1000; this.y = (int) (Math.random() * 700 + 100); this.width = 200; this.height = 220; this.speed = 60; this.dir = 1; this.count = 5; this.type = 2; this.size = 1000; this.img = GameUtils.img11; }}Myfish
Myfish类是定义的自己的鱼的类
getRec方法可以获取自身矩形,用于碰撞检测
logic方法用if语句判断自己的鱼是不是超出定义的画布的范围,使自己的鱼可以一直在画布的范围内行动
import java.awt.*;public class MyFish { //图片 Image img = GameUtils.img5; //坐标 int x = 700; int y = 500; int width = 50; int height = 50; //移动速度 int speed = 20; //等级 int level = 1; //大小 int size = 0; //生命 int life = 3; void logic(){ if(x if(GameUtils.UP){ y = y - speed; } if(GameUtils.DOWN){ y = y + speed; } if(GameUtils.RIGHT){ x = x + speed; img = GameUtils.img6; } } else if(y if(GameUtils.DOWN){ y = y + speed; } if(GameUtils.RIGHT){ x = x + speed; img = GameUtils.img6; } if(GameUtils.LEFT){ x = x - speed; img = GameUtils.img5; } } else if(x=1400){ if(GameUtils.UP){ y = y - speed; } if(GameUtils.DOWN){ y = y + speed; } if(GameUtils.LEFT){ x = x - speed; img = GameUtils.img5; } } else if(y=850){ if(GameUtils.UP){ y = y - speed; } if(GameUtils.RIGHT){ x = x + speed; img = GameUtils.img6; } if(GameUtils.LEFT){ x = x - speed; img = GameUtils.img5; } } else{ if(GameUtils.UP){ y = y - speed; } if(GameUtils.DOWN){ y = y + speed; } if(GameUtils.RIGHT){ x = x + speed; img = GameUtils.img6; } if(GameUtils.LEFT){ x = x - speed; img = GameUtils.img5; } } } //绘制自身方法 public void paintSelf(Graphics g){ logic(); g.drawImage(img,x,y,width+GameUtils.count/3,height+GameUtils.count/3,null); } //获取自身矩形,用于碰撞检测 public Rectangle getRec(){ return new Rectangle(x,y,width+GameUtils.count/3,height+GameUtils.count/3); }}GameWin
GameWin是含有main方法的Java类,是小游戏的核心程序所在
键盘监听
使用键盘监听,重写keyPressed方法,使用if语句根据游戏所处状态以及键盘的抬起松开达到键盘控制游戏内容的能力
用WASD来控制我方鱼的移动
用空格键来暂停游戏
this.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { //super.keyPressed(e); //WASD控制移动 W 86 S 83 A 65 D if (e.getKeyCode() == 87) { GameUtils.UP = true; } if (e.getKeyCode() == 83) { GameUtils.DOWN = true; } if (e.getKeyCode() == 65) { GameUtils.LEFT = true; } if (e.getKeyCode() == 68) { GameUtils.RIGHT = true; } //游戏过程中暂停游戏 if(e.getKeyCode() == 32){ switch (state){ case 1: state = 4; break; case 4: state = 1; break; } } } @Override// 抬起 public void keyReleased(KeyEvent e) { //super.keyReleased(e); if (e.getKeyCode() == 87) { GameUtils.UP = false; } if (e.getKeyCode() == 83) { GameUtils.DOWN = false; } if (e.getKeyCode() == 65) { GameUtils.LEFT = false; } if (e.getKeyCode() == 68) { GameUtils.RIGHT = false; } } });鼠标监听
使用键盘监听,重写mouseClicked方法,使用if语句根据游戏所处状态以及鼠标的左右键来实现游戏过程中的重新游戏,及胜利失败后是否要重新玩一局游戏
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); this.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { super.mouseClicked(e); if (e.getButton() == 1 && state == 0) { state = 1; repaint(); } if(e.getButton() == 1 && (state == 2||state == 3)){ state = 5; System.out.println("*****"); } //胜利或者结束后的界面右键重新开始游戏 if(e.getButton() == 3 && state==5){ System.out.println("====="); reGame(); state = 1; } //游戏过程中右键重新开始 if(e.getButton() == 3 && state==1){ System.out.println("====="); reGame(); state = 1; } } });重绘界面
设置while(true)***循环
每30毫秒重绘一次游戏界面,以达到动态的效果
while (true) { repaint(); time++; try { Thread.sleep(30); } catch (InterruptedException e) { throw new RuntimeException(e); } }游戏状态
用是switch判断游戏的状态
游戏状态: 0 未开始 1 游戏中 2 通关失败 3 通关成功 4 暂停 5 游戏结束
每个状态中绘制GameUtils中相应的图片
switch (state) { case 0: gImage.drawImage(GameUtils.img2, 0, 0, null); break; case 1: gImage.drawImage(GameUtils.img1, 0, 0, null); //显示分数,生命 bg.paintSelf(gImage); gImage.setColor(Color.MAGENTA); gImage.setFont(new Font("仿宋", Font.BOLD, 50)); gImage.drawString("分数" + GameUtils.count, 200, 120); gImage.drawString("生命" + myFish.life, 1000, 120); myFish.paintSelf(gImage); logic(); //循环遍历敌方鱼的生成 for (Enemy enemy : GameUtils.EnemyList) { enemy.paintSelf(gImage); } for (EnemyBoss enemyBoss : GameUtils.EnemyBossList) { enemyBoss.paintSelf(gImage); if(enemyBoss.x gImage.setColor(Color.red); gImage.fillRect(enemyBoss.x,enemyBoss.y+110,1440,2); } } break; case 2: gImage.drawImage(GameUtils.img8, 0, 0, null); break; case 3: gImage.drawImage(GameUtils.img7, 0, 0, null); break; case 4: return; case 5: gImage.drawImage(GameUtils.img14, 0, 0, null); break; } g.drawImage(offScreenImage, 0, 0, null); } if(time%10 ==0){ EnemyAZ enemy = new EnemyAZ(); GameUtils.EnemyList.add(enemy); } else{ EnemyAY enemy = new EnemyAY(); GameUtils.EnemyList.add(enemy); } } System.out.println("碰撞了"); if (myFish.size = enemy.size) { //吃掉敌方鱼 enemy.x = -200; enemy.y = -200; GameUtils.count = GameUtils.count + enemy.count; myFish.size += enemy.count; } else { //我方鱼等级小于敌方 enemy.x = -200; enemy.y = -200; if(myFish.life
下一篇:如何装饰黑色手机?