本游戏代码参考【尚学堂】Java开发游戏_大鱼吃小鱼项目实战教程
游戏展示
这是写好的游戏在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<Enemy> EnemyList = new ArrayList<>();
public static List<EnemyBoss> 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<=0){
if(GameUtils.UP){
y = y - speed;
}
if(GameUtils.DOWN){
y = y + speed;
}
if(GameUtils.RIGHT){
x = x + speed;
img = GameUtils.img6;
}
}
else if(y<=0){
if(GameUtils.DOWN){
y = y + speed;
}
if(GameUtils.RIGHT){
x = x + speed;
img = GameUtils.img6;