java项目大鱼吃小鱼

项目说明

此项目总共代码量为400行左右,在b站有教程(时长1h),适合于刚刚学完了java的同学做第一个项目。

程序运行效果图

图1-1运行效果图

玩法说明

玩家鼠标左键开始游戏,空格键暂停,用WASD控制小鱼的移动,游戏一共有四种难度,并有三种普通鱼和一种boss鱼,吃掉普通鱼会获得相应的积分,无法吃掉boss鱼,积分达到300,游戏胜利。

 图1-2boss鱼

最终打包可运行文件下载

点击下载

以下为项目实现部分------------------------------------------------

文件组织形式

demo2中创建以下几个类,images文件为图片资源,私信获取哦

 

源码部分

Bg.java 

package demo1;

import java.awt.*;

public class Bg
{
    void paintSelf(Graphics g, int fishLevel)
    {
        g.drawImage(GameUtils.bgimg,0,0,null);
        switch (GameWin.state)
        {
            case 0:
                GameUtils.drwaWord(g,"开始",Color.red,80,700,500);
                break;
            case 1:
                GameUtils.drwaWord(g,"积分"+GameUtils.count,Color.orange,50,200,120);
                GameUtils.drwaWord(g,"难度"+GameUtils.level,Color.orange,50,600,120);
                GameUtils.drwaWord(g,"等级"+fishLevel,Color.orange,50,1000,120);
                break;
            case 2:
                GameUtils.drwaWord(g,"积分"+GameUtils.count,Color.orange,50,200,120);
                GameUtils.drwaWord(g,"难度"+GameUtils.level,Color.orange,50,600,120);
                GameUtils.drwaWord(g,"等级"+fishLevel,Color.orange,50,1000,120);
                GameUtils.drwaWord(g,"失败",Color.red,80,700,500);
                break;
            case 3:
                GameUtils.drwaWord(g,"积分"+GameUtils.count,Color.orange,50,200,120);
                GameUtils.drwaWord(g,"难度"+GameUtils.level,Color.orange,50,600,120);
                GameUtils.drwaWord(g,"等级"+fishLevel,Color.orange,50,1000,120);
                GameUtils.drwaWord(g,"胜利",Color.red,80,700,500);
                break;
            case 4:
                break;
            default:
        }
    }
}

Enamy.java

package demo1;

import java.awt.*;

public class Enamy
{
    //定义图片
    Image image;
    //定义物体坐标
    int x;
    int y;
    int width;
    int height;
    //移动速度
    int speed;
    //方向
    int dir = 1;//1为向右 -1为向左
    //类型
    int type;
    //分值
    int count;
    //绘制自身方法
    public void paintSelf(Graphics g)
    {
        g.drawImage(image,x,y,width,height,null);
    }
    //获取自身矩形用于碰撞检测
    public Rectangle getRec()
    {
        return new Rectangle(x,y,width,height);
    }
}

//敌方鱼左类
class Enamy_1_L extends Enamy
{
    Enamy_1_L()
    {
        this.x = -45;
        this.y = (int)(Math.random()*700 + 100);
        this.width = 45;
        this.height = 69;
        this.speed = 10;
        this.count = 1;
        this.type = 1;
        this.image = GameUtils.enamyl_img;
    }
}

//敌方鱼右类
class Enamy_1_R extends Enamy_1_L
{
    Enamy_1_R()
    {
        this.x = 1400;
        dir = -1;
        this.image = GameUtils.enamyr_img;
    }
}

class Enamy_2_L extends Enamy
{
    Enamy_2_L()
    {
        this.x = -100;
        this.y = (int)(Math.random()*700+100);
        this.width = 100;
        this.height = 100;
        this.speed = 5;
        this.count = 2;
        this.type = 2;
        this.image = GameUtils.enamyl_2img;
    }
}

class Enamy_2_R extends Enamy_2_L
{
    Enamy_2_R()
    {
        this.x = 1400;
        dir = -1;
        this.image = GameUtils.enamyr_2img;
    }
}

class Enamy_3_L extends Enamy
{
    Enamy_3_L()
    {
        this.x = -300;
        this.y = (int)(Math.random()*700+100);
        this.width = 300;
        this.height = 150;
        this.speed = 15;
        this.count = 3;
        this.type = 3;
        this.image = GameUtils.enamyl_3img;
    }

    @Override
    public Rectangle getRec()
    {
        return new Rectangle(x + 40, y + 30,width - 80,height +- 60);
    }
}

class Enamy_3_R extends Enamy_3_L
{
    Enamy_3_R()
    {
        this.x = 1400;
        dir = -1;
        this.image = GameUtils.enamyr_3img;
    }
}

class Enamy_Boss extends Enamy
{
    Enamy_Boss()
    {
        this.x = -1000;
        this.y = (int)(Math.random()*700+100);
        this.width = 170;
        this.height = 170;
        this.speed = 40;
        this.count = 0;
        this.type = 10;
        this.image = GameUtils.bossimg;
    }
}

GameUtils.java

package demo1;

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;

    //关卡等级
    static int level = 0;

    //敌方鱼类集合
    public static List<Enamy> EnamyList = new ArrayList<>();

    //背景图
    public static Image bgimg = Toolkit.getDefaultToolkit().createImage("images/sea.jpg");

    //图标
    public static Image icon = Toolkit.getDefaultToolkit().createImage("images/icon.png");
    //敌方鱼类
    public static Image enamyl_img = Toolkit.getDefaultToolkit().createImage("images/enemyFish/fish1_r.gif");
    public static Image enamyr_img = Toolkit.getDefaultToolkit().createImage("images/enemyFish/fish1_l.gif");
    public static Image enamyl_2img = Toolkit.getDefaultToolkit().createImage("images/enemyFish/fish2_r.png");
    public static Image enamyr_2img = Toolkit.getDefaultToolkit().createImage("images/enemyFish/fish2_l.png");
    public static Image enamyl_3img = Toolkit.getDefaultToolkit().createImage("images/enemyFish/fish3_r.png");
    public static Image enamyr_3img = Toolkit.getDefaultToolkit().createImage("images/enemyFish/fish3_l.png");
    public static Image bossimg = Toolkit.getDefaultToolkit().createImage("images/enemyFish/boss.gif");

    //我方鱼类
    public static Image MyFishimg_L = Toolkit.getDefaultToolkit().createImage("images/myFish/myfish_left.gif");
    public static Image MyFishimg_R = Toolkit.getDefaultToolkit().createImage("images/myFish/myfish_right.gif");

    //绘制文字的工具类
    public  static void drwaWord(Graphics g,String str,Color color,int size, int x, int y)
    {
        g.setColor(color);
        g.setFont((new Font("仿宋",Font.BOLD,size)));
        g.drawString(str,x,y);
    }
}

GameWin.java

package demo1;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class GameWin extends JFrame
{
    //游戏的五种状态 0未开始 1游戏中 2通关失败 3通关成功 4暂停

    //游戏默认状态
    static int state = 0;

    Image offScreenImage;

    //宽高
    int width = 1440;
    int height = 900;

    double random;
    //计数器
    int time = 0;
    //背景
    Bg bg = new Bg();

    //敌方鱼类
    Enamy enamy;

    //boss类
    Enamy boss;
    //是否生成boss
    boolean isboss = false;

    //我方鱼类
    MyFish myFish = new MyFish();

    public void lanch()
    {
        this.setVisible(true);
        this.setSize(width,height);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setTitle("大鱼吃小鱼");
        this.setIconImage(GameUtils.icon);
        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)
                {
                    reGame();
                    state = 1;
                }
            }
        });

        //键盘移动
        this.addKeyListener(new KeyAdapter()
        {
            @Override//按压
            public void keyPressed(KeyEvent e)
            {
                super.keyPressed(e);
                //WASD
                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)
                {
                    if(state == 1)
                    {
                        state = 4;
                        GameUtils.drwaWord(getGraphics(),"游戏暂停!!!",Color.red,50,600,400);
                    }
                    else if(state == 4) state = 1;
                }
                if(e.getKeyCode() == 75)//后路,按k 增加50积分
                {
                    GameUtils.count += 50;
                }
            }

            @Override//抬起
            public void keyReleased(KeyEvent e)
            {
                super.keyPressed(e);
                //WASD
                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;
                }
            }
        });

        while(true)
        {
            repaint();
            time ++;
            try
            {
                Thread.sleep(40);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }

    }



    public void paint(Graphics g)
    {
        //懒加载模式初始化对象 双缓存解决闪屏问题
        offScreenImage = createImage(width,height);
        Graphics  gImage = offScreenImage.getGraphics();
        bg.paintSelf(gImage,myFish.level);

        //游戏状态
        switch (state)
        {
            case 0:


                break;
            case 1:

                myFish.paintSelf(gImage);
                logic();
                for(Enamy enamy:GameUtils.EnamyList)
                {
                    enamy.paintSelf(gImage);
                }
                if(isboss)
                {
                    boss.x = boss.x + boss.dir * boss.speed;
                    boss.paintSelf(gImage);
                    if(boss.x < 0)
                    {
                        gImage.setColor(Color.red);
                        gImage.fillRect(boss.x,boss.y,2400,boss.height/30);
                    }
                }
                break;
            case 2:
                for(Enamy enamy:GameUtils.EnamyList)
                {
                    enamy.paintSelf(gImage);
                }
                if(isboss)
                {
                    boss.paintSelf(gImage);
                }
                break;
            case 3:
                myFish.paintSelf(gImage);

                break;
            case 4:
                return;
            default:
        }
        g.drawImage(offScreenImage,0,0,null);
    }



    void logic()
    {
        //关卡难度
        if(GameUtils.count<5)
        {
            GameUtils.level = 0;
            GameUtils.level = 0;
            myFish.level = 1;
        }
        else if(GameUtils.count<=15)
        {
            GameUtils.level = 1;
        }
        else if(GameUtils.count<=50)
        {
            GameUtils.level = 2;
            myFish.level = 2;
        }
        else if(GameUtils.count <= 150)
        {
            GameUtils.level = 3;
            myFish.level = 2;
        }
        else if(GameUtils.count <= 300)
        {
            GameUtils.level = 4;
            myFish.level = 3;
        }
        else if(GameUtils.count > 300)
        {
            state = 3;//游戏胜利
        }

        random = Math.random();

        //敌方鱼生成
        switch (GameUtils.level)
        {
            case 4:
                if(time % 60 == 0)
                {
                        boss = new Enamy_Boss();
                        isboss = true;
                }
            case 3:
            case 2:
                if(time % 30 == 0)
                {
                    if(random > 0.5)
                    {
                        enamy = new Enamy_3_L();
                    }
                    else
                    {
                        enamy = new Enamy_3_R();
                    }
                    GameUtils.EnamyList.add(enamy);
                }
            case 1:
                if(time % 20 == 0)
                {
                    if(random > 0.5)
                    {
                        enamy = new Enamy_2_L();
                    }
                    else
                    {
                        enamy = new Enamy_2_R();
                    }
                    GameUtils.EnamyList.add(enamy);
                }
            case 0:
                if(time % 10 == 0)
                {
                    if(random > 0.5)
                    {
                        enamy = new Enamy_1_L();
                    }
                    else
                    {
                        enamy = new Enamy_1_R();
                    }
                    GameUtils.EnamyList.add(enamy);
                }
                break;
            default:
        }


        //移动方向
        for(Enamy enamy:GameUtils.EnamyList)
        {
            enamy.x += enamy.dir * enamy.speed;

            //和boss鱼类碰撞检测
            if(isboss)
            {
                if(boss.getRec().intersects(enamy.getRec()))
                {
                    enamy.x = -200;
                    enamy.y = -200;
                }
                if (boss.getRec().intersects(myFish.getRec()))
                    state = 2;
            }

            //我方鱼与敌方鱼碰撞检测
            if(myFish.getRec().intersects(enamy.getRec()))
            {
                if(myFish.level >= enamy.type)
                {
                    enamy.x = -200;
                    enamy.y = -200;
                    GameUtils.count = GameUtils.count + enamy.count;
                }
                else //我方鱼被高级鱼吃掉
                {
                    state = 2;
                }
            }
        }
    }


    public static void main(String[] args)
    {
        GameWin gameWin = new GameWin();
        gameWin.lanch();
    }

    //重新开始
    void reGame()
    {
        GameUtils.EnamyList.clear();
        time = 0;
        myFish.level = 1;
        GameUtils.count = 0;
        myFish.x = 700;
        myFish.y = 500;
        myFish.width = 50;
        myFish.height = 50;
        boss = null;
        isboss = false;
    }

}

MyFish.java

package demo1;

import java.awt.*;

public class MyFish
{
    //图片
    Image img = GameUtils.MyFishimg_L;
    //坐标
    int x = 700;
    int y = 500;
    int width = 50;
    int height = 50;
    //移动速度
    int speed  = 20;
    //等级
    int level = 1;

    void logic()
    {
        if(GameUtils.UP)
        {
            y = y - speed;
        }
        if(GameUtils.DOWN)
        {
            y = y + speed;
        }
        if(GameUtils.LEFT)
        {
            x = x - speed;
            img = GameUtils.MyFishimg_L;
        }
        if(GameUtils.RIGHT)
        {
            x = x + speed;
            img = GameUtils.MyFishimg_R;
        }
    }

    //绘制自身的方法
    public void paintSelf(Graphics g)
    {
        logic();
        g.drawImage(img,x,y,width + GameUtils.count,height + GameUtils.count,null);
    }
    //获取自身矩形的方法,用于碰撞检测
    public Rectangle getRec()
    {
        return new Rectangle(x,y,width + GameUtils.count,height + GameUtils.count);
    }
}

  • 7
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/*主框架类*/ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MyJFrame extends JFrame { private static final long serialVersionUID = 1L; public MyJFrame()//构造方法 { init();//自定义方法1 setVisible(true);//可见 } private void init()//自定义方法 { Toolkit tlk = Toolkit.getDefaultToolkit(); Image tittle = tlk.getImage("pho/最小化.png");//添加最小化图标 setIconImage(tittle); setUndecorated(true);//不启用标题栏 MyJPanel jpanel=new MyJPanel(); setContentPane(jpanel);//作用类似于add() setSize(1200,700);//设置窗口大小 setResizable(false);//不可改变大小 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭 setLocationRelativeTo(null);//窗口出现在桌面中间 /*******把鼠标隐藏*****/ Toolkit tool = Toolkit.getDefaultToolkit(); Image mouse=tool.getImage("pho/mouse.png");//给鼠标加一张透明的图片 Cursor cursor = tool.createCustomCursor(mouse,new Point(),null); setCursor(cursor); addKeyListener((KeyListener) new MyKeyAdapter());//设置键盘监听,用来退出游戏 } private class MyKeyAdapter extends KeyAdapter//键盘监听适配器 { public void keyPressed(KeyEvent e) { super.keyPressed(e); int event = e.getKeyCode();//接受键盘事件 if(event == KeyEvent.VK_ESCAPE)//按ESC退出 { int result = JOptionPane.showConfirmDialog(null, "是否退出游戏", "提示", JOptionPane.OK_CANCEL_OPTION); if(result == JOptionPane.OK_OPTION)//点击确定退出 { System.exit(0); } } } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值