java飞机大战:传统与维新の对决(屎山)

目录

嘛,众所周知中国的烟草税收是很高的,而在这种高税收的影响下,经过工业革命的影响,电子烟登上了历史的舞台,并且以顶针为首开启了扬雾运动......而传统派的芙蓉王*源对于这种新兴势力的敌对态度,必然会掀起一场战争.......(误)

                          游戏运行的大致样子

 1.开始界面

2.运行时的截图

                  大致的问题

1.导致删除元素时候自己云里雾里~~~(优化产生了问题)

2.对之前建立的类已然忘记去如何使用~~~

3画面啥的基本上忘记了~~~

 4.角色没有换皮啊啊~~~

现在把程序设计的大致思维图奉上~

1.游戏设计玩法的大致思路

2.游戏代码的结构

                                别的不多说把代码奉上~~~

在软件包utils下的类

1.程序核心GameWin

2.各种元素集合以及引用图片元素的类GameUtils

在软件包obj下的各个类

1.很必要的,影响其他类的类:dzGameObj

2.游戏背景的BgObj

3.与玩家相关的设定

  1.玩家:PlaneObj

  2.子弹:ShellObj

4.敌人相关的类

                                           小卡拉米的类

1.Enemy1Obj

2.Enemy2Obj

3.Enemy3Obj

                                         boss相关的类

1.boss的设定:BossObj

2.boss的子弹:BossBullet

                               boss出现时候的警告WaringObj

5.游戏中各种元素的类

                                                    补给的设定GiftObj

                                               游戏爆炸的设定ExplodeObj

游戏各种图片素材的素材库.......


嘛,众所周知中国的烟草税收是很高的,而在这种高税收的影响下,经过工业革命的影响,电子烟登上了历史的舞台,并且以顶针为首开启了扬雾运动......而传统派的芙蓉王*源对于这种新兴势力的敌对态度,必然会掀起一场战争.......(误)

 哈哈,玩笑就开到这里,其实这个java作业有很多的问题.....比如优化之类的,画面bug之类的,由于这个java作业我分了两次写,第一次建立的很多类,之后时隔了一个多月再次写,已经快把之前写的忘记的差不多啦QwQ。

                          游戏运行的大致样子

       吐槽:啊啊啊,抠图电子烟,芙蓉王,各种小飞机好麻烦啊啊啊啊啊

 1.开始界面

2.运行时的截图

                  大致的问题

1.导致删除元素时候自己云里雾里~~~(优化产生了问题)

但是大致是这边的元素删除有问题,个人感觉已经删了,然并卵~

2.对之前建立的类已然忘记去如何使用~~~

       有些类没用过嘞,但是自己又懒的删了~

3画面啥的基本上忘记了~~~

会导致黑屏的问题了,我估计是前面的问题,但是因为前面的问题是老早写的,已然忘记又懒得去找了~~~

 4.角色没有换皮啊啊~~~

判断语句换皮会黑屏啊,后面打算建立两个角色的类,但是自己又懒的整了,好麻烦~~~

现在把程序设计的大致思维图奉上~
1.游戏设计玩法的大致思路

2.游戏代码的结构

                                别的不多说把代码奉上~~~

在软件包utils下的类

1.程序核心GameWin
package utils;

import obj.*;

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;

import static java.lang.Thread.sleep;

public class GameWin extends JFrame
{
    //角色选择的状态
   public static int character=0;
    //游戏的暂停与否
    //static boolean start=false;
    public static int state=0;//0为暂停,1为开始,3为失败,4为通关

    BgObj bgObj=new BgObj(GameUtils.bdImg,0,0,2);
    Image offScreenImage=null;
    //飞机设定
    PlaneObj dzplaneObj=new PlaneObj(GameUtils.dzplImg,88,94,500,500,0,this);

    PlaneObj wyplaneObj=new PlaneObj(GameUtils.wyplImg,89,104,500,500,0,this);
    //ShellObj dzshellObj=new ShellObj(GameUtils.yt1Img,7,36, dzplaneObj.getX(), dzplaneObj.getY(), 5,this);
//记录次数
    int count=1;
    //获取敌方的boss对象
     BossObj bossObj=new BossObj(GameUtils.wybosslImg,88,94,180,-100,3,this);
    WaringObj waringObj=new WaringObj(GameUtils.waringImg,236,234,500,0,0,this);
    //记录游戏得分
    public static int score=0;
    public void launch()//窗口
    {
        this.setVisible(true);
        this.setSize(1280,720);
        this.setLocationRelativeTo(null);
        this.setTitle("传统与维新の太空对决");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //所有需要绘制的游戏物体全部放入所有元素集合中进行绘制
        //GameUtils.gameobjList.add();
        //这里有大问题
//        GameUtils.dzgameobjList.add(bgObj);
//        if(character==1)
//        {
//            GameUtils.wygameobjList.add(bgObj);
//            GameUtils.wygameobjList.add(wyplaneObj);
//        }
//        if(character==2)
        {
            GameUtils.dzgameobjList.add(bgObj);
            GameUtils.dzgameobjList.add(dzplaneObj);
        }
            //GameUtils.dzgameobjList.add(bgObj);
            //GameUtils.dzgameobjList.add(dzplaneObj);
        //暂停的设定
        //this.addKeyListener();
        //开始的设定,角色的选择
        if(state==0)
        {
            this.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (e.getButton() == 1 && state == 0)
                    {
                        character = 1;
                        state = 1;
                        repaint();
                    }
                    //可行
                    else
                    {
                        character = 2;
                        state = 1;
                        repaint();
                    }
                }
            });
            /*this.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (e.getButton() == 3 && state == 0)
                    {
                        character = 2;
                        state = 1;
                        repaint();
                    }
                }
            });*/
        }
        //键盘暂停的设定
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode()==32)
                {
                    if(state==1)
                    {
                        state=2;
                    } else if (state==2)
                    {
                        state=1;
                    }
                }
            }
        }) ;
         while(true)
            {
                createObj();
                repaint();
                try
                {
                    Thread.sleep(25);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
    }
    @Override
    public void paint(Graphics g)
    {//初始界面以及初始化缓存
        if(offScreenImage==null)
        {
            offScreenImage=createImage(1280,720);
        }
        Graphics gImage=offScreenImage.getGraphics();
        gImage.fillRect(0,0,1280,720);
        if(state==0)
        {
        gImage.drawImage(GameUtils.bdImg, 0, 0, null);
        gImage.drawImage(GameUtils.wyImg, 30, 200, null);
        gImage.drawImage(GameUtils.dzImg, 750, 200, null);
        gImage.setColor(Color.white);
        gImage.setFont(new Font("仿宋", Font.BOLD,60));
        gImage.drawString("请鼠标左键点击或者右键点击来选择你的阵营",20,150);
        gImage.drawString("左键:芙蓉王*源",30,620);
        gImage.drawString("右键:尼古丁*真",750,620);
        }
        if(state==1)
        {//游戏时间的记录
            count++;
            //将爆炸集合添加到所有元素集合中
            GameUtils.gameobjList.addAll(GameUtils.explodeObjList);

            //不再单独绘制每个元素,将每个元素打包成集合
            if(character==1)//有问题
            {
                for(int i=0;i<GameUtils.dzgameobjList.size()-1;i++)
                {
                    GameUtils.dzgameobjList.get(i).paintSelf(gImage);
                }
            }
            if(character==2)
            {
                for(int i=0;i<GameUtils.dzgameobjList.size()-1;i++)
                {
                GameUtils.dzgameobjList.get(i).paintSelf(gImage);
                }
            }
            for(int i=0;i<GameUtils.gameobjList.size()-1;i++)
            {
                GameUtils.gameobjList.get(i).paintSelf(gImage);
            }
            //敲~~大的毛病~~,缓存图片不起作用~
            if(state==2)
            {
              gImage.drawImage(GameUtils.bdImg,0,0,null);
              //g.drawImage(bgObj.getImg(), 0, 0, null);
              GameUtils.drawWord(gImage,"游戏暂停",Color.YELLOW,50,220,300);
            }
            if(state==3)
            {
                {
                    gImage.drawImage(GameUtils.bdImg, 0, 0, null);
                    //g.drawImage(offScreenImage, 0, 0, null);
                    //g.drawImage(bgObj.getImg(), 0, 0, null);
                    GameUtils.drawWord(gImage, "游戏失败", Color.RED, 50, 220, 300);
                }
            }
            if(state==4)
            {
                {
                gImage.drawImage(GameUtils.bdImg,0,0,null);
                //g.drawImage(bgObj.getImg(), 0, 0, null);
                GameUtils.drawWord(gImage,"游戏通关",Color.GREEN,50,220,300);}
            }
            //绘制积分面板
            GameUtils.drawWord(gImage,score+"分",Color.green,40,30,100);
            //将要移除元素丛所有元素集合中删除
            GameUtils.gameobjList.removeAll(GameUtils.removeList);
        }
        //双缓冲图片绘制在窗口,放这里会导致黑屏问题,好煞笔
        g.drawImage(offScreenImage, 0, 0, null);

        System.out.println(GameUtils.dzgameobjList.size());
    }
    //批量创建物体
    void createObj() {//子弹产生速度,图像有问题
        if (character == 1) {
            if (count % 15 == 0) {
                if (PlaneObj.times == 0) {
                    GameUtils.dzshellObjList.add(new ShellObj(GameUtils.wyminImg, 17, 27, wyplaneObj.getX() + 43, wyplaneObj.getY() - 30, 5, this));
                    GameUtils.dzgameobjList.add(GameUtils.dzshellObjList.get(GameUtils.dzshellObjList.size() - 1));
                }
                if (PlaneObj.times == 1) {
                    GameUtils.dzshellObjList.add(new ShellObj(GameUtils.wymidImg, 20, 35, wyplaneObj.getX() + 43, wyplaneObj.getY() - 30, 5, this));
                    GameUtils.dzgameobjList.add(GameUtils.dzshellObjList.get(GameUtils.dzshellObjList.size() - 1));
                }
                //这个三级强度太低,不行嘞
//                if (PlaneObj.times >= 2) {
//                    GameUtils.dzshellObjList.add(new ShellObj(GameUtils.wymaxImg, 25, 40, wyplaneObj.getX() + 43, wyplaneObj.getY() - 30, 8, this));
//                    GameUtils.dzgameobjList.add(GameUtils.dzshellObjList.get(GameUtils.dzshellObjList.size() - 1));
//                }
            }
            if(count%7==0&&PlaneObj.times>=2)
            {
                 {
                    GameUtils.dzshellObjList.add(new ShellObj(GameUtils.wymaxImg, 25, 40, wyplaneObj.getX() + 43, wyplaneObj.getY() - 30, 8, this));
                    GameUtils.dzgameobjList.add(GameUtils.dzshellObjList.get(GameUtils.dzshellObjList.size() - 1));
                }
            }
            //这边两个角色都有的应该可以优化在下面来减少一半的代码量
            //敌人生成
            if (count % 25 == 0) {
                GameUtils.enemy1ObjList.add(new Enemy1Obj(GameUtils.e1Img, 82, 62, (int) (Math.random() * 13) * 90, 0, 7, this));
                GameUtils.gameobjList.add(GameUtils.enemy1ObjList.get(GameUtils.enemy1ObjList.size() - 1));
            }
            if (count % 50 == 0) {
                GameUtils.enemy2ObjList.add(new Enemy2Obj(GameUtils.e2Img, 100, 76, (int) (Math.random() * 13) * 90, 0, 3, this));
                GameUtils.gameobjList.add(GameUtils.enemy2ObjList.get(GameUtils.enemy2ObjList.size() - 1));
            }
            if (count % 40 == 0) {
                GameUtils.enemy3ObjList.add(new Enemy3Obj(GameUtils.e3Img, 104, 74, (int) (Math.random() * 13) * 90, 0, 5, this));
                GameUtils.gameobjList.add(GameUtils.enemy3ObjList.get(GameUtils.enemy3ObjList.size() - 1));

            }
            //补给的生成
            if (count % 150 == 0) {
                GameUtils.giftObjList.add(new GiftObj(GameUtils.giftImg, 111, 78, (int) (Math.random() * 13) * 90, 0, 3, this));
                GameUtils.gameobjList.add(GameUtils.giftObjList.get(GameUtils.giftObjList.size() - 1));
            }
            //boss的生成
            if (count == 500 && (!GameUtils.gameobjList.contains(bossObj))) {
                GameUtils.gameobjList.add(bossObj);
            }
            //boss子弹
            if(count%40==0)
            {
                if (GameUtils.gameobjList.contains(bossObj)) {
                    GameUtils.bossBulletList.add(new BossBullet(GameUtils.yt1Img, 20, 35, bossObj.getX() + 43, bossObj.getY() - 30, 5, -10, this));
                    GameUtils.gameobjList.add(GameUtils.bossBulletList.get(GameUtils.bossBulletList.size() - 1));
                    if (count % 40 == 0) {
                        GameUtils.bossBulletList.add(new BossBullet(GameUtils.yt5Img, 56, 330, (int) (Math.random() * 13) * 90, -500, 15, this));
                        GameUtils.gameobjList.add(GameUtils.bossBulletList.get(GameUtils.bossBulletList.size() - 1));
                    }
                }
            }
        }
        if (character == 2) {
            if (count % 12 == 0) {
                if (PlaneObj.times == 0) {
                    GameUtils.dzshellObjList.add(new ShellObj(GameUtils.yt1Img, 17, 27, dzplaneObj.getX() + 43, dzplaneObj.getY() - 30, 5, this));
                    GameUtils.dzgameobjList.add(GameUtils.dzshellObjList.get(GameUtils.dzshellObjList.size() - 1));
                }
                if (PlaneObj.times == 1) {
                    GameUtils.dzshellObjList.add(new ShellObj(GameUtils.yt1Img, 17, 27, dzplaneObj.getX() + 43, dzplaneObj.getY() - 30, 5, this));
                    GameUtils.dzgameobjList.add(GameUtils.dzshellObjList.get(GameUtils.dzshellObjList.size() - 1));
                    GameUtils.dzshellObjList.add(new ShellObj(GameUtils.yt2Img, 17, 27, dzplaneObj.getX() + 20, dzplaneObj.getY() - 30, 5, this));
                    GameUtils.dzgameobjList.add(GameUtils.dzshellObjList.get(GameUtils.dzshellObjList.size() - 1));
                }
                if (PlaneObj.times == 2) {
                    GameUtils.dzshellObjList.add(new ShellObj(GameUtils.yt1Img, 17, 27, dzplaneObj.getX() + 43, dzplaneObj.getY() - 30, 5, this));
                    GameUtils.dzgameobjList.add(GameUtils.dzshellObjList.get(GameUtils.dzshellObjList.size() - 1));
                }
                if (PlaneObj.times >= 3) {
                    GameUtils.dzshellObjList.add(new ShellObj(GameUtils.yt1Img, 17, 27, dzplaneObj.getX() + 43, dzplaneObj.getY() - 30, 7, this));
                    GameUtils.dzgameobjList.add(GameUtils.dzshellObjList.get(GameUtils.dzshellObjList.size() - 1));
                    GameUtils.dzshellObjList.add(new ShellObj(GameUtils.yt2Img, 17, 27, dzplaneObj.getX() + 25, dzplaneObj.getY() - 30, 5, 4, this));
                    GameUtils.dzgameobjList.add(GameUtils.dzshellObjList.get(GameUtils.dzshellObjList.size() - 1));
                    GameUtils.dzshellObjList.add(new ShellObj(GameUtils.yt3Img, 17, 27, dzplaneObj.getX() + 50, dzplaneObj.getY() - 30, -5, 4, this));
                    GameUtils.dzgameobjList.add(GameUtils.dzshellObjList.get(GameUtils.dzshellObjList.size() - 1));}
        }
           //敌人生成
           if(count%25==0)
            {
                GameUtils.enemy1ObjList.add(new Enemy1Obj(GameUtils.e1Img,82,62,(int)(Math.random()*13)*90,0,7,this));
                GameUtils.gameobjList.add(GameUtils.enemy1ObjList.get(GameUtils.enemy1ObjList.size()-1));
            }
            if(count%50==0)
            {
                GameUtils.enemy2ObjList.add(new Enemy2Obj(GameUtils.e2Img,100,76,(int)(Math.random()*13)*90,0,3,this));
                GameUtils.gameobjList.add(GameUtils.enemy2ObjList.get(GameUtils.enemy2ObjList.size()-1));
            }
            if(count%40==0)
            {
                GameUtils.enemy3ObjList.add(new Enemy3Obj(GameUtils.e3Img,104,74,(int)(Math.random()*13)*90,0,5,this));
                GameUtils.gameobjList.add(GameUtils.enemy3ObjList.get(GameUtils.enemy3ObjList.size()-1));

            }
            //补给的生成
            if(count%150==0)
            {
                GameUtils.giftObjList.add(new GiftObj(GameUtils.giftImg,111,78,(int)(Math.random()*13)*90,0,3,this));
                GameUtils.gameobjList.add(GameUtils.giftObjList.get(GameUtils.giftObjList.size()-1));
            }
            //boss的生成
            if(count==500&&(!GameUtils.gameobjList.contains(bossObj)))
            {
                GameUtils.gameobjList.add(bossObj);
            }
            //boss子弹生成
            if(count%55==0)
            {
//                if (count == 130 && (!GameUtils.gameobjList.contains(bossObj))) {
//                    GameUtils.bossObjList.add(new BossObj(GameUtils.wybosslImg, 88, 94, (int) (Math.random() * 13) * 90, -40, 3, this));
//                    GameUtils.gameobjList.add(GameUtils.bossObjList.get(GameUtils.bossObjList.size() - 1));
//                }
                if (GameUtils.gameobjList.contains(bossObj))
                {
//                      GameUtils.dzshellObjList.add(new ShellObj(GameUtils.wymidImg, 20, 35, bossObj.getX() + 43, bossObj.getY() - 100, -5, this));
//                      GameUtils.gameobjList.add(GameUtils.dzshellObjList.get(GameUtils.dzshellObjList.size() - 1));
                    GameUtils.bossBulletList.add(new BossBullet(GameUtils.wymidImg, 20, 35, bossObj.getX() + 43, bossObj.getY() - 30, 5, this));
                    GameUtils.gameobjList.add(GameUtils.bossBulletList.get(GameUtils.bossBulletList.size() - 1));
                    if(count%20==0)
                    {
                        GameUtils.bossBulletList.add(new BossBullet(GameUtils.wybossbuttleImg, 59, 565, (int)(Math.random()*13)*90, -500, 10, this));
                        GameUtils.gameobjList.add(GameUtils.bossBulletList.get(GameUtils.bossBulletList.size() - 1));
                    }
                }
            }
        }
        //waring出现消失的时间
        if((count==400)&&(!GameUtils.gameobjList.contains(waringObj)))
        {
            GameUtils.gameobjList.add(waringObj);
        }
        if(count==500)
        {
            GameUtils.removeList.add(waringObj);
        }
    }
    public static void main(String[] args)
    {
        GameWin gameWin=new GameWin();
        gameWin.launch();
    }
}
2.各种元素集合以及引用图片元素的类GameUtils
package utils;

import obj.*;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class GameUtils {
    public static Image bdImg = Toolkit.getDefaultToolkit().getImage("imgs/bg.jpg");
    public static Image dzImg = Toolkit.getDefaultToolkit().getImage("imgs/dz.jpg");

    public static Image wyImg = Toolkit.getDefaultToolkit().getImage("imgs/fr.jpg");
    public static Image dzplImg = Toolkit.getDefaultToolkit().getImage("imgs/dzp.png");
    public static Image wyplImg = Toolkit.getDefaultToolkit().getImage("imgs/wyp.png");
    public static Image dzbosslImg = Toolkit.getDefaultToolkit().getImage("imgs/dzboss.png");
    public static Image wybosslImg = Toolkit.getDefaultToolkit().getImage("imgs/wyboss.png");
    public static Image e1Img = Toolkit.getDefaultToolkit().getImage("imgs/e1.png");
    public static Image e2Img = Toolkit.getDefaultToolkit().getImage("imgs/e2.png");
    public static Image e3Img = Toolkit.getDefaultToolkit().getImage("imgs/e3.png");
    public static Image yt1Img = Toolkit.getDefaultToolkit().getImage("imgs/dzbuttle/1yt.png");
    public static Image yt2Img = Toolkit.getDefaultToolkit().getImage("imgs/dzbuttle/2yt.png");
    public static Image yt3Img = Toolkit.getDefaultToolkit().getImage("imgs/dzbuttle/3yt.png");
    public static Image yt4Img = Toolkit.getDefaultToolkit().getImage("imgs/dzbuttle/4yt.png");
    public static Image giftImg = Toolkit.getDefaultToolkit().getImage("imgs/gift.png");
    /*public static Image boom1Img =Toolkit.getDefaultToolkit().getImage("imgs/boom/boom1.gif");
    public static Image boom2Img =Toolkit.getDefaultToolkit().getImage("imgs/boom/boom2.gif");
    public static Image boom3Img =Toolkit.getDefaultToolkit().getImage("imgs/boom/boom3.gif");
    public static Image boom4Img =Toolkit.getDefaultToolkit().getImage("imgs/boom/boom4.gif");
    public static Image boom5Img =Toolkit.getDefaultToolkit().getImage("imgs/boom/boom5.gif");
    public static Image boom6Img =Toolkit.getDefaultToolkit().getImage("imgs/boom/boom6.gif");*/
//警告标志
    public static Image waringImg = Toolkit.getDefaultToolkit().getImage("imgs/waring.png");


    //目前打算作为丁真boss的攻击方式
    public static Image yt5Img = Toolkit.getDefaultToolkit().getImage("imgs/dzbuttle/yt5.png");
    public static Image wymaxImg = Toolkit.getDefaultToolkit().getImage("imgs/wybuttle/wymax.png");
    public static Image wymidImg = Toolkit.getDefaultToolkit().getImage("imgs/wybuttle/wymid.png");
    public static Image wyminImg = Toolkit.getDefaultToolkit().getImage("imgs/wybuttle/wymin.png");
    public static Image wybossbuttleImg = Toolkit.getDefaultToolkit().getImage("imgs/wybuttle/wybossb.png");

    //飞机子弹集合
    public static List<ShellObj> dzshellObjList = new ArrayList<>();
    public static List<ShellObj> wyshellObjList = new ArrayList<>();
    public static List<EnemyBulletObj> enemyBulletObjList = new ArrayList<>();
    public static List<BossBullet> bossBulletList = new ArrayList<>();
    //二级飞机子弹集合
    //public static List<DoubleShellObj> dzdoubleShellObjList = new ArrayList<>();
    //public static List<DoubleShellObj> wydoubleShellObjList=new ArrayList<>();
    //三级飞机子弹集合
    //public static List<TripleShellObj> dztripleShellObjList = new ArrayList<>();
    //所有元素的集合
    public static List<dzGameObj> gameobjList = new ArrayList<>();
    public static List<dzGameObj> dzgameobjList = new ArrayList<>();
    public static List<dzGameObj> wygameobjList = new ArrayList<>();
    //敌人的集合
    public static List<Enemy1Obj> enemy1ObjList = new ArrayList<>();
    public static List<Enemy2Obj> enemy2ObjList = new ArrayList<>();
    public static List<Enemy3Obj> enemy3ObjList = new ArrayList<>();
    public static List<BossObj> bossObjList = new ArrayList<>();
    //删除游戏窗口的集合
    public static List<dzGameObj> removeList = new ArrayList<>();
    //爆炸集合
    public static List<ExplodeObj> explodeObjList = new ArrayList<>();
    public static List<GiftObj> giftObjList = new ArrayList<>();

    //用来绘制文字的
    public static void drawWord(Graphics gImage, String str, Color color, int size, int x, int y)
    {
        gImage.setColor(color);
        gImage.setFont(new Font("仿宋",Font.BOLD,size));
        gImage.drawString(str,x,y);
    }
}

在软件包obj下的各个类

1.很必要的,影响其他类的类:dzGameObj
package obj;


import utils.GameWin;

import java.awt.*;

//父类
public class dzGameObj
{
    //元素图片
    Image img;
    //元素大小
    int width;
    int height;
    int x;
    int y;
    double speed;
    double speedx;
    double speedy;
    GameWin frame;
    //set and get
    public Image getImg() {
        return img;
    }

    public void setImg(Image img) {
        this.img = img;
    }

    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 double getSpeed() {
        return speed;
    }

    public void setSpeedx(double speedx) {
        this.speed = speedx;
    }
    public double getSpeedx() {
        return speed;
    }

    public void setSpeedy(double speedy) {
        this.speed = speedy;
    }
    public double getSpeedy() {
        return speed;
    }

    public void setSpeed(double speed) {
        this.speed = speed;
    }

    public GameWin getFrame() {
        return frame;
    }

    public void setFrame(GameWin frame) {
        this.frame = frame;
    }
    //构造方法
    public dzGameObj() {
    }

    public dzGameObj(Image img, int x, int y, double speed) {
        this.img = img;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }

    public dzGameObj(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public dzGameObj(Image img, int width, int height, int x, int y, double speed, GameWin frame) {
        this.img = img;
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speed = speed;
        this.frame = frame;
    }
    public dzGameObj(Image img, int width, int height, int x, int y, double speedx,double speedy, GameWin frame) {
        this.img = img;
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speedx = speedx;
        this.speedy=speedy;
        this.frame = frame;
    }
    //绘制元素自身的方法
    public  void paintSelf(Graphics g)
    {
        g.drawImage(img,x,y,null);
    }
    public  Rectangle getRec()
    {
        return new Rectangle(x,y,width,height);
    }
}
2.游戏背景的BgObj
package obj;

import utils.GameUtils;
import utils.GameWin;

import java.awt.*;

public class BgObj extends dzGameObj
{
    public BgObj() {
        super();
    }

    public BgObj(Image img, int width, int height, int x, int y, double speed, GameWin frame) {
        super(img, width, height, x, y, speed, frame);
    }

   /* @Override
    public Image getImg() {
        return super.getImg();
    }*/

    public BgObj(Image img, int x, int y, double speed) {
        super(img, x, y, speed);
    }

    @Override
    public void paintSelf(Graphics g) {
        super.paintSelf(g);
        y+=speed;
        if(y>=0)
        {
            y = 0;
            GameUtils.removeList.add(this);
        }
    }

    @Override
    public Rectangle getRec() {
        return super.getRec();
    }
}
3.与玩家相关的设定
  1.玩家:PlaneObj
package obj;

import utils.GameUtils;
import utils.GameWin;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class PlaneObj extends dzGameObj
{
    public static int HP=6;
    //记录当前获得补给次数
    public static int times=0;
    public static int shellhp=1;
    @Override
    public Image getImg() {
        return super.getImg();
    }

    public PlaneObj() {
        super();
    }

    public PlaneObj(Image img, int x, int y, double speed) {
        super(img, x, y, speed);
    }

    public PlaneObj(Image img, int width, int height, int x, int y, double speed, GameWin frame) {
        super(img, width, height, x, y, speed, frame);
        //移动设定
        this.frame.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                PlaneObj.super.x=e.getX()-44;
                PlaneObj.super.y=e.getY()-50;
            }
        });
    }

    @Override
    public void paintSelf(Graphics g) {
        super.paintSelf(g);
        //飞机消失的判定
        if(HP<=0)
        {
            this.x=-200;
            this.y=-200;
            GameUtils.removeList.add(this);
            GameWin.state=3;
        }
        //碰撞设定
        //敌方飞机与我方飞机的碰撞
        for (Enemy1Obj enemy1Obj:GameUtils.enemy1ObjList)
        {
            if(this.getRec().intersects(enemy1Obj.getRec()))
            {
                //碰撞爆炸动画
                ExplodeObj explodeObj=new ExplodeObj(x,y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeList.add(explodeObj);

                enemy1Obj.setX(-100);
                enemy1Obj.setX(-100);
                this.x=-200;
                this.y=-200;
                GameUtils.removeList.add(enemy1Obj);
                //这有bug,得注释
                GameUtils.removeList.add(this);
                HP--;
            }
        }
        for (Enemy2Obj enemy2Obj:GameUtils.enemy2ObjList)
        {
            if(this.getRec().intersects(enemy2Obj.getRec()))
            {
                //碰撞爆炸动画
                ExplodeObj explodeObj=new ExplodeObj(x,y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeList.add(explodeObj);

                enemy2Obj.setX(-100);
                enemy2Obj.setX(-100);
                this.x=-200;
                this.y=-200;
                GameUtils.removeList.add(enemy2Obj);
                GameUtils.removeList.add(this);
                HP--;
            }
        }
        for (Enemy3Obj enemy3Obj:GameUtils.enemy3ObjList)
        {
            if(this.getRec().intersects(enemy3Obj.getRec()))
            {
                //碰撞爆炸动画
                ExplodeObj explodeObj=new ExplodeObj(x,y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeList.add(explodeObj);

                enemy3Obj.setX(-100);
                enemy3Obj.setX(-100);
                this.x=-200;
                this.y=-200;
                GameUtils.removeList.add(enemy3Obj);
                GameUtils.removeList.add(this);
                HP--;
            }
        }
        //我方飞机与补给的碰撞
        for(GiftObj giftObj:    GameUtils.giftObjList)
        {
            if(this.getRec().intersects(giftObj.getRec()))
            {
                giftObj.setX(-100);
                giftObj.setY(-100);
                GameUtils.removeList.add(giftObj);
                times++;
                if (GameWin.character == 1) {
                    if (shellhp <= 3)
                        shellhp++;
                }
            }
        }
        //飞机碰撞boss子弹后消失
        for(BossBullet bossBullet:GameUtils.bossBulletList)
        {
            if(this.getRec().intersects(bossBullet.getRec()))
            {
                //碰撞爆炸动画
                ExplodeObj explodeObj=new ExplodeObj(x,y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeList.add(explodeObj);

                bossBullet.setY(-100);
                bossBullet.setX(-100);
                GameUtils.removeList.add(bossBullet);
                HP--;
            }
        }
        //白色矩形
        g.setColor(Color.WHITE);
        g.fillRect(300,700,500,10);
        //红色矩形
        g.setColor(Color.RED);
        g.fillRect(300,700,HP*500/6,10);
    }

    @Override
    public Rectangle getRec() {
        return super.getRec();
    }
}
  2.子弹:ShellObj
package obj;

import utils.GameUtils;
import utils.GameWin;

import java.awt.*;

public class ShellObj extends dzGameObj
{
    public ShellObj() {
        super();
    }

    public ShellObj(Image img, int x, int y, double speed) {
        super(img, x, y, speed);
    }

    public ShellObj(Image img, int width, int height, int x, int y, double speed, GameWin frame) {
        super(img, width, height, x, y, speed, frame);
    }

    public ShellObj(Image img, int width, int height, int x, int y, double speedx, double speedy, GameWin frame) {
        super(img, width, height, x, y, speedx, speedy, frame);
    }

    @Override
    public void paintSelf(Graphics g) {
        super.paintSelf(g);
        //子弹的移动实现
        y -= speed;
        //dz子弹升级后的移动实现
        if (GameWin.character == 2&& PlaneObj.times>=2)
        {
            y-=speedy;
            x-=speedx;
            //x+=speedx;
        }
        //wy子弹消失的判定
        if (GameWin.character == 1) {
            int zdhp = PlaneObj.shellhp;
            if (zdhp == 0) {
                this.setX(-100);
                this.setY(-100);
                GameUtils.removeList.add(this);
            }
            for (Enemy1Obj enemy1Obj : GameUtils.enemy1ObjList) {
                if (this.getRec().intersects(enemy1Obj.getRec())) {
                    zdhp--;
                }
            }
            for (Enemy2Obj enemy2Obj : GameUtils.enemy2ObjList) {
                if (this.getRec().intersects(enemy2Obj.getRec())) {
                    zdhp--;
                }
            }
            for (Enemy3Obj enemy3Obj : GameUtils.enemy3ObjList) {
                if (this.getRec().intersects(enemy3Obj.getRec())) {
                    zdhp--;
                }
            }
        }
        if (GameWin.character == 2)
        {
            int zdhp=1;
            if (zdhp == 0) {
                this.setX(-100);
                this.setY(-100);
                GameUtils.removeList.add(this);
            }
            for (Enemy1Obj enemy1Obj : GameUtils.enemy1ObjList) {
                if (this.getRec().intersects(enemy1Obj.getRec())) {
                    zdhp--;
                }
            }
            for (Enemy2Obj enemy2Obj : GameUtils.enemy2ObjList) {
                if (this.getRec().intersects(enemy2Obj.getRec())) {
                    zdhp--;
                }
            }
            for (Enemy3Obj enemy3Obj : GameUtils.enemy3ObjList) {
                if (this.getRec().intersects(enemy3Obj.getRec())) {
                    zdhp--;
                }
            }
        }
        if(y<0)
        {
            GameUtils.removeList.add(this);
        }
    }

    @Override
    public Rectangle getRec() {
        return super.getRec();
    }
}
4.敌人相关的类
                                           小卡拉米的类
1.Enemy1Obj
package obj;

import utils.GameUtils;
import utils.GameWin;

import java.awt.*;

public class Enemy1Obj extends dzGameObj
{
    public Enemy1Obj() {
        super();
    }

    public Enemy1Obj(Image img, int width, int height, int x, int y, double speed, GameWin frame) {
        super(img, width, height, x, y, speed, frame);
    }

    @Override
    public void paintSelf(Graphics g) {
        super.paintSelf(g);
        y+=speed;
        for(ShellObj shellObj: GameUtils.dzshellObjList)
        {
            if(this.getRec().intersects(shellObj.getRec()))
            {
                //碰撞爆炸动画
                ExplodeObj explodeObj=new ExplodeObj(x,y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeList.add(explodeObj);

//                shellObj.setX(-100);
//                shellObj.setY(-100);
//                GameUtils.removeList.add(shellObj);
                this.x=-200;
                this.y=-200;
                GameUtils.removeList.add(this);
                GameWin.score++;
            }
        }
        if(this.y>720)
        {
            GameUtils.removeList.add(this);
        }
    }

    @Override
    public Rectangle getRec() {
        return super.getRec();
    }

    public Enemy1Obj(Image img, int x, int y, double speed) {
        super(img, x, y, speed);
    }
}
2.Enemy2Obj
package obj;

import utils.GameUtils;
import utils.GameWin;

import java.awt.*;

public class Enemy2Obj extends dzGameObj
{
    //设定血量
    int health=9;
    public Enemy2Obj() {
        super();
    }

    public Enemy2Obj(Image img, int x, int y, double speed) {
        super(img, x, y, speed);
    }

    public Enemy2Obj(Image img, int width, int height, int x, int y, double speed, GameWin frame) {
        super(img, width, height, x, y, speed, frame);
    }

    @Override
    public void paintSelf(Graphics g) {
        super.paintSelf(g);
        y+=speed;
        //我方飞机子弹击中碰撞判定
        for(ShellObj shellObj: GameUtils.dzshellObjList)
        {
            if(this.getRec().intersects(shellObj.getRec())&&health>0)
            {
                shellObj.setX(-100);
                shellObj.setY(-100);
                GameUtils.removeList.add(shellObj);
                health-=PlaneObj.shellhp;
            }
            else if(this.getRec().intersects(shellObj.getRec())&&health<=0)
            {
                //碰撞爆炸动画
                ExplodeObj explodeObj=new ExplodeObj(x,y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeList.add(explodeObj);

//                shellObj.setX(-100);
//                shellObj.setY(-100);
//                GameUtils.removeList.add(shellObj);
                this.x=-200;
                this.y=-200;
                GameUtils.removeList.add(this);
                GameWin.score+=3;
            }
        }
        if(this.y>720)
        {
            GameUtils.removeList.add(this);
        }
    }

    @Override
    public Rectangle getRec() {
        return super.getRec();
    }
}
3.Enemy3Obj
package obj;

import utils.GameUtils;
import utils.GameWin;

import java.awt.*;

public class Enemy3Obj extends dzGameObj
{ int health=2;
    public Enemy3Obj() {
        super();
    }

    public Enemy3Obj(Image img, int x, int y, double speed) {
        super(img, x, y, speed);
    }

    public Enemy3Obj(Image img, int width, int height, int x, int y, double speed, GameWin frame) {
        super(img, width, height, x, y, speed, frame);
    }

    @Override
    public void paintSelf(Graphics g) {
        super.paintSelf(g);
        y+=speed;
        for(ShellObj shellObj: GameUtils.dzshellObjList)
        {
            if(this.getRec().intersects(shellObj.getRec())&&health>0)
            {
                shellObj.setX(-100);
                shellObj.setY(-100);
                GameUtils.removeList.add(shellObj);
                health-=PlaneObj.shellhp;
            }
            else if(this.getRec().intersects(shellObj.getRec())&&health<=0)
            {
                //碰撞爆炸动画
                ExplodeObj explodeObj=new ExplodeObj(x,y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeList.add(explodeObj);

//                shellObj.setX(-100);
//                shellObj.setY(-100);
//                GameUtils.removeList.add(shellObj);
                this.x=-200;
                this.y=-200;
                GameUtils.removeList.add(this);
                GameWin.score+=2;
            }
        }
        if(this.y>800)
        {
            GameUtils.removeList.add(this);
        }
    }

    @Override
    public Rectangle getRec() {
        return super.getRec();
    }
}
                                         boss相关的类
1.boss的设定:BossObj
package obj;

import utils.GameUtils;
import utils.GameWin;

import java.awt.*;

public class BossObj extends dzGameObj
{
    int health=30;
    public BossObj() {
        super();
    }

    public BossObj(Image img, int x, int y, double speed) {
        super(img, x, y, speed);
    }

    public BossObj(int x, int y) {
        super(x, y);
    }

    public BossObj(Image img, int width, int height, int x, int y, double speed, GameWin frame) {
        super(img, width, height, x, y, speed, frame);
    }

    @Override
    public void paintSelf(Graphics g) {
        super.paintSelf(g);
        if(y<150)
        {
            y+=speed;
        }
        else
        {
         x+=speed;
         if(x<0||x>1280)
         {
          speed=-speed;
         }
        }
        //我方子弹命中boss
        for(ShellObj shellObj: GameUtils.dzshellObjList)
        {
            if (this.getRec().intersects(shellObj.getRec()))
            {
                //碰撞爆炸动画
                ExplodeObj explodeObj = new ExplodeObj(x, y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeList.add(explodeObj);

                shellObj.setX(-100);
                shellObj.setY(-100);
                GameUtils.removeList.add(shellObj);
                health--;
                if(health<=0)
                {
                    this.setX(-100);
                    this.setY(-100);
                    GameUtils.removeList.add(this);
                    GameWin.state=4;
                }
            }
        }
        //白色矩形
        g.setColor(Color.WHITE);
        g.fillRect(300,40,500,10);
        //红色矩形
        g.setColor(Color.RED);
        g.fillRect(300,40,health*500/30,10);
    }

    @Override
    public Rectangle getRec() {
        return super.getRec();
    }
}
2.boss的子弹:BossBullet
package obj;

import utils.GameUtils;
import utils.GameWin;

import java.awt.*;
import java.util.Random;

public class BossBullet extends dzGameObj
{
    public BossBullet() {
        super();
    }

    public BossBullet(Image img, int x, int y, double speed) {
        super(img, x, y, speed);
    }

    public BossBullet(int x, int y) {
        super(x, y);
    }

    public BossBullet(Image img, int width, int height, int x, int y, double speed, GameWin frame) {
        super(img, width, height, x, y, speed, frame);
    }

    public BossBullet(Image img, int width, int height, int x, int y, double speedx, double speedy, GameWin frame) {
        super(img, width, height, x, y, speedx, speedy, frame);
    }

    @Override
    public void paintSelf(Graphics g) {
        super.paintSelf(g);
        y+=speed;
        //int rand=(int)(Math.random()*5) + 10;
        if (GameWin.character ==1)
        {
            int rand=(int)(Math.random()*5) + 10;
            y-=speedy;
            if(rand>=5) x-=speedx;
            if(rand<10) x+=speedx;
//            if(GameWin.bossObj.getX()>=640)x-=speedx;
//            if(GameWin.bossObj.getX()<640)x+=speedx;
            //x+=speedx;
        }
        if(this.y>800)
        {
            GameUtils.removeList.add(this);
        }
    }

    @Override
    public Rectangle getRec() {
        return super.getRec();
    }
}
                               boss出现时候的警告WaringObj
package obj;

import utils.GameWin;

import java.awt.*;

public class WaringObj extends dzGameObj
{
    public WaringObj() {
        super();
    }

    public WaringObj(Image img, int x, int y, double speed) {
        super(img, x, y, speed);
    }

    public WaringObj(int x, int y) {
        super(x, y);
    }

    public WaringObj(Image img, int width, int height, int x, int y, double speed, GameWin frame) {
        super(img, width, height, x, y, speed, frame);
    }

    public WaringObj(Image img, int width, int height, int x, int y, double speedx, double speedy, GameWin frame) {
        super(img, width, height, x, y, speedx, speedy, frame);
    }

    @Override
    public void paintSelf(Graphics g) {
        super.paintSelf(g);
    }
}

5.游戏中各种元素的类
                                                    补给的设定GiftObj
package obj;

import utils.GameUtils;
import utils.GameWin;

import java.awt.*;

public class GiftObj extends dzGameObj
{
    public GiftObj() {
        super();
    }

    public GiftObj(Image img, int x, int y, double speed) {
        super(img, x, y, speed);
    }

    public GiftObj(int x, int y) {
        super(x, y);
    }

    public GiftObj(Image img, int width, int height, int x, int y, double speed, GameWin frame) {
        super(img, width, height, x, y, speed, frame);
    }

    @Override
    public void paintSelf(Graphics g) {
        super.img=GameUtils.giftImg;
        super.width=111;
        super.height=78;
        super.paintSelf(g);
        y+=speed;
        if(this.y>800)
        {
            GameUtils.removeList.add(this);
        }
    }

    @Override
    public Rectangle getRec() {
        return super.getRec();
    }
}
                                               游戏爆炸的设定ExplodeObj
package obj;

import java.awt.*;

public class ExplodeObj  extends dzGameObj
{
    //定义一个image类型的静态数组来存放爆炸图
    static Image[] explodePic=new Image[6];
    //定义变量来存放爆炸图次数
    int explodeCount=0;
    //定义一个静态代码块来将爆炸图片放到数组当中
    static
    {
        for(int i=0;i< explodePic.length;i++)
        {
            explodePic[i]=Toolkit.getDefaultToolkit().getImage("imgs/boom/boom"+(i+1)+".gif");
        }
    }

    @Override
    public void paintSelf(Graphics g) {
        if(explodeCount<6)
        {
            super.img=explodePic[explodeCount];
            super.paintSelf(g);
            explodeCount++;
        }
        //super.paintSelf(g);
    }

    public ExplodeObj(int x, int y) {
        super(x, y);
    }
}

游戏各种图片素材的素材库.......

留言:害,java其实自己根本没怎么学过,平时自己基本上只研究研究c++之类的,所以这个java代码写的很烂,加上之后也不打算打开修改这个不完善的代码了,就干脆发出来,看各位是否需要之类的,或者有大佬对我这个屎山进行修改是最好的,如果大佬发现这个代码怎么修完美也希望能告诉鄙人哇~

后记:为什么顶针能射这么多烟弹,问就是顶针电子烟换弹容易,为什么王⚪能最后射这么快,问就是抽了芙蓉王赛过活神仙

   还有电子烟boss本人一开始是打算让boss随机射左射右的,但是加了随机数设定和其他各种设定也没搞出想要的结果就干脆让boss只斜射一边了qwq

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值