• Java课程设计


    1.团队博客链接

    https://www.cnblogs.com/2b-or-not-2b-/p/14342149.html

    2.个人负责模块

    飞机大战基础功能实现
    1.基础界面
    2.角色控制的飞机
    3.敌机
    4.子弹
    5.攻击

    3.git提交记录

    4.我的代码

    以下代码的基础功能由我实现,涉及其它模块及进阶内容与我无关。

    1.BFrame.java

    package com.view;
    
    import java.awt.Dimension;
    import java.awt.Image;
    import java.awt.Toolkit;
    import javax.swing.JFrame;
    
    import com.listener.FrameMouseListener;
    import com.listener.MenuListener;
    import com.model.*;
    
    public class BFrame extends JFrame{
    	
    	
    	public static int pageHeigh = 800;
    	public static int pageWidth = 495;
    	public Image ima = Toolkit.getDefaultToolkit().getImage("Image/IconImage.png");
    	
    	public BPanel nPanel;
    	public FrameMouseListener fmListener;
    	public MenuListener prListener;*/
    	
    	public void setListener()
    	{
    		this.fmListener = new FrameMouseListener();
    		this.fmListener.bFrame = this;
    		this.addMouseMotionListener(this.fmListener);
    		
    		this.prListener = new MenuListener();
    		this.prListener.bFrame = this;
    		this.addMouseMotionListener(this.prListener);
    		
    		this.prListener = new MenuListener();
    		this.prListener.bFrame = this;
    		this.addMouseListener(this.prListener);
    	}
    	public void addType(Class c)
    	{
    		this.nPanel.ensType.add(c);
    	}
    	public BFrame()
    	{
    		//获取屏幕分辨率
    		Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
    		//设置大小
    		setBounds((int)(screensize.getWidth()-pageWidth)/2, 0, pageWidth, pageHeigh);
    		//设置标题
    		setTitle("PlaneWar"); 
    		//设置居中
    		setLocationRelativeTo(null);
    		
            this.nPanel = new BPanel();
            this.nPanel.setBounds(0,0,pageWidth,pageHeigh);
            this.add(this.nPanel);
            //监听器
            setListener();
            addType(Enemy01.class);
            addType(Enemy02.class);
            addType(Enemy03.class);
            addType(Enemy04.class);
            addType(Enemy05.class);
            addType(Enemy06.class);
            //显示窗口
    		setVisible(true);
    		setResizable(false);
    		setIconImage(ima);
    		//默认关闭
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 		
    	}
    		
    }
    

    2.Main.java

    package com.view;
    
    import com.thread.MusicThread;
    
    public class Main {
    
    	public static void main(String[] args) {
    		new BFrame();
    		//new MusicThread("C:/Users/Administrator/Desktop/飞机大战/飞机大战/Hero/5481.wav").start();
    	
    	}
    
    }
    

    3.BPanel.java

    package com.view;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.lang.reflect.InvocationTargetException;
    import java.util.ArrayList;
    
    import javax.swing.JPanel;
    
    import com.model.Background;
    import com.model.Bullet;
    import com.model.DeadDialog;
    import com.model.Enemy;
    import com.model.Enemy01;
    import com.model.Item;
    import com.model.MenuImage;
    import com.model.Plane;
    import com.thread.DrawableTread;
    import com.thread.MusicThread;
    
    public class BPanel extends JPanel{
    	
    	public Image Bg;
    	public Background back;
    	public int i = 0;
    	public int j = 0;
    	public int time = 0;
    	public int top = 0;
    	public int flag = 1;
    	public DrawableTread dwtread;
    	
    	public MenuImage menu;
    	public DeadDialog dg;
    	public Plane plane;
    	
    	public ArrayList<Bullet> bts = new ArrayList<Bullet>();
    	public ArrayList<Enemy> ens = new ArrayList<Enemy>();
    	public ArrayList<Item> its = new ArrayList<Item>();
    	//所有敌机类型
    	public ArrayList<Class> ensType = new ArrayList<Class>();
    
    	public Boolean isStart = false;
    	public Boolean isDead = false;
    	
    	public Boolean hasBullet = false;
    	public Boolean hasEnemy = false;
    
    	public BPanel()
    	{	
    		this.dwtread = new DrawableTread(this);
    		this.menu = new MenuImage(this);
    		this.dg = new DeadDialog(this);
    		this.plane = new Plane(this);
    		this.back = new Background(this);
    		this.dwtread.start();
    	}
    	
    	@Override
    	public void paintComponent(Graphics g)
    	{
    		super.paintComponent(g);
    		
    		//界面时间
    		time++;
    		if(time == 10000) 
    		{
    			time = 0;
    		}
    		//背景绘制
    		back.ChooseType();
    		this.back.drawBack(g);
    		
    		//界面动画
    		if(isStart==false)
    		{
    			this.menu.drawImage(g);
    		}
    		else if(isStart==true)
    		{
    			
    			this.menu.drawImageBack(g);;
    		}
    		
    		//绘制飞机
    		this.plane.ChooseType();
    		this.plane.drawplane(g);
    		
    		
    		//子弹绘制
    		if(hasBullet == true)
    		{
    			if(time%100==0)
    			{
    				new MusicThread("Music/shoot.wav").start();
    				if(this.plane.mode == 1)
    				{
    					Bullet bt = new Bullet(this);
    					bt.width = 15;
    					bt.height = 65;
    					bt.x = this.plane.x+this.plane.width/2-bt.width/2;
    					bt.y = this.plane.y;
    					this.bts.add(bt);
    				}
    				else if(this.plane.mode == 2)
    				{
    					Bullet bt = new Bullet(this);
    					bt.width = 15;
    					bt.height = 65;
    					bt.x = this.plane.x+this.plane.width/4-bt.width/2+5;
    					bt.y = this.plane.y;
    					this.bts.add(bt);
    					
    					Bullet bt1 = new Bullet(this);
    					bt1.width = 15;
    					bt1.height = 65;
    					bt1.x = this.plane.x+this.plane.width*3/4-bt.width/2-5;
    					bt1.y = this.plane.y;	
    					this.bts.add(bt1);
    				}
    				else if(this.plane.mode == 3)
    				{
    					Bullet bt = new Bullet(this);
    					bt.width = 15;
    					bt.height = 65;
    					bt.x = this.plane.x+this.plane.width/2-bt.width/2;
    					bt.y = this.plane.y;
    					this.bts.add(bt);
    					
    					Bullet bt1 = new Bullet(this);
    					bt1.x = this.plane.x+this.plane.width/4-bt.width/2+2;
    					Bullet bt2 = new Bullet(this);
    					bt2.x = this.plane.x+this.plane.width*3/4-bt.width/2-2;
    					bt1.y =bt2.y = bt.y+20;
    					bt1.width =bt2.width= 15;
    					bt1.height = bt2.height = 65;
    					this.bts.add(bt1);
    					this.bts.add(bt2);
    				}
    				else if(this.plane.mode == 4)
    				{
    					
    					Bullet bt = new Bullet(this);
    					bt.choose = 2;
    					
    					bt.width = 10;
    					bt.height = 20;
    					
    					bt.x = this.plane.x+this.plane.width/2-bt.width/2;
    					bt.y = this.plane.y;
    					this.bts.add(bt);
    				}
    				else if(this.plane.mode == 5)
    				{
    					Bullet bt = new Bullet(this);
    					bt.choose = 2;
    					
    					bt.width = 10;
    					bt.height = 20;
    					
    					bt.x = this.plane.x+this.plane.width/2-bt.width/2;
    					bt.y = this.plane.y;
    					this.bts.add(bt);
    					
    					Bullet bt1 = new Bullet(this);
    					bt1.x = bt.x-10;
    					Bullet bt2 = new Bullet(this);
    					bt1.choose = bt2.choose = 2;
    					
    					bt2.x = bt.x+10;
    					
    					bt1.y =bt2.y = bt.y+10;
    					bt1.width =bt2.width= 10;
    					bt1.height = bt2.height = 20;
    					this.bts.add(bt1);
    					this.bts.add(bt2);
    				}
    				else if(this.plane.mode == 6)
    				{
    					Bullet bt = new Bullet(this);
    					bt.choose = 2;
    					
    					bt.width = 10;
    					bt.height = 20;
    					
    					bt.x = this.plane.x+this.plane.width/2-bt.width/2;
    					bt.y = this.plane.y;
    					this.bts.add(bt);
    					
    					Bullet bt1 = new Bullet(this);
    					bt1.x = bt.x-10;
    					Bullet bt2 = new Bullet(this);
    					bt1.choose = bt2.choose = 2;
    					
    					bt2.x = bt.x+10;
    					
    					bt1.y =bt2.y = bt.y+10;
    					bt1.width =bt2.width= 10;
    					bt1.height = bt2.height = 20;
    					this.bts.add(bt1);
    					this.bts.add(bt2);
    					
    					Bullet bt3 = new Bullet(this);
    					bt3.x = bt.x-20;
    					Bullet bt4 = new Bullet(this);
    					bt3.choose = bt4.choose = 2;
    					bt4.x = bt.x+20;
    					
    					bt3.y =bt4.y = bt.y+20;
    					bt3.width =bt4.width= 10;
    					bt3.height = bt4.height = 20;
    					this.bts.add(bt3);
    					this.bts.add(bt4);
    				}
    				else if(this.plane.mode == 7)
    				{
    					
    					Bullet bt = new Bullet(this);
    					bt.choose = 3;
    					
    					bt.width = 10;
    					bt.height = 45;
    					
    					bt.x = this.plane.x+this.plane.width/2-bt.width/2;
    					bt.y = this.plane.y;
    					this.bts.add(bt);
    				}
    				else if(this.plane.mode == 8)
    				{
    					Bullet bt = new Bullet(this);
    					bt.choose = 3;
    					
    					bt.width = 11;
    					bt.height = 65;
    					
    					bt.x = this.plane.x+this.plane.width/2-bt.width/2;
    					bt.y = this.plane.y;
    					this.bts.add(bt);
    					
    				}
    				else if(this.plane.mode == 9)
    				{
    					Bullet bt = new Bullet(this);
    					bt.choose = 3;
    					
    					bt.width = 11;
    					bt.height = 100;
    					
    					bt.x = this.plane.x+this.plane.width/4;
    					bt.y = this.plane.y;
    					this.bts.add(bt);
    					
    					Bullet bt1 = new Bullet(this);
    					bt1.choose = 3;
    					
    					bt1.width = 11;
    					bt1.height = 100;
    					
    					bt1.x = this.plane.x+this.plane.width*3/4-bt1.width;
    					bt1.y = this.plane.y;
    					this.bts.add(bt1);
    				}
    				else if(this.plane.mode == 10)
    				{
    					
    					Bullet bt = new Bullet(this);
    					bt.choose = 4;
    					
    					bt.width = 17;
    					bt.height = 22;
    					
    					bt.x = this.plane.x+this.plane.width/2-bt.width/2;
    					bt.y = this.plane.y;
    					this.bts.add(bt);
    				}
    				else if(this.plane.mode == 11)
    				{
    					
    					Bullet bt = new Bullet(this);
    					bt.choose = 4;
    					
    					bt.width = 25;
    					bt.height = 32;
    					
    					bt.x = this.plane.x+this.plane.width/2-bt.width/2;
    					bt.y = this.plane.y;
    					this.bts.add(bt);
    				}
    				else if(this.plane.mode == 12)
    				{
    					
    					Bullet bt = new Bullet(this);
    					bt.choose = 4;
    					
    					bt.width = 32;
    					bt.height = 40;
    					
    					bt.x = this.plane.x+this.plane.width/2-bt.width/2;
    					bt.y = this.plane.y;
    					this.bts.add(bt);
    					
    					Bullet bt1 = new Bullet(this);
    					bt1.choose = 4;
    					
    					bt1.width = 17;
    					bt1.height = 22;
    					
    					bt1.x = this.plane.x+this.plane.width/2-bt1.width/2;
    					bt1.y = this.plane.y+20;
    					this.bts.add(bt1);
    				}
    				else if(this.plane.mode == 13)
    				{
    					
    					Bullet bt = new Bullet(this);
    					bt.choose = 5;
    					
    					bt.width = 16;
    					bt.height = 99;
    					
    					bt.x = this.plane.x+this.plane.width/2-bt.width/2;
    					bt.y = this.plane.y;
    					this.bts.add(bt);
    				}
    				else if(this.plane.mode == 14)
    				{
    					
    					Bullet bt = new Bullet(this);
    					bt.choose = 6;
    					
    					bt.width = 20;
    					bt.height = 99;
    					
    					bt.x = this.plane.x+this.plane.width/2-bt.width/2;
    					bt.y = this.plane.y;
    					this.bts.add(bt);
    				}
    				else if(this.plane.mode == 15)
    				{
    					
    					Bullet bt = new Bullet(this);
    					bt.choose = 6;
    					
    					bt.width = 20;
    					bt.height = 99;
    					
    					bt.x = this.plane.x+this.plane.width/2-bt.width/2;
    					bt.y = this.plane.y;
    					this.bts.add(bt);
    					
    					Bullet bt1 = new Bullet(this);
    					bt1.choose = 5;
    					
    					bt1.width = 10;
    					bt1.height = 99;
    					
    					bt1.x = this.plane.x+this.plane.width/2-bt.width/2-10;
    					bt1.y = this.plane.y+10;
    					this.bts.add(bt1);
    					
    					Bullet bt2 = new Bullet(this);
    					bt2.choose = 5;
    					
    					bt2.width = 10;
    					bt2.height = 99;
    					
    					bt2.x = this.plane.x+this.plane.width/2-bt.width/2+20;
    					bt2.y = this.plane.y+10;
    					this.bts.add(bt2);
    					
    				}
    				else if(this.plane.mode == 16)
    				{
    					
    					Bullet bt = new Bullet(this);
    					bt.choose = 7;
    					
    					bt.width = 50;
    					bt.height = 50;
    					
    					bt.x = this.plane.x+this.plane.width/2-bt.width/2;
    					bt.y = this.plane.y;
    					this.bts.add(bt);
    				}
    				else if(this.plane.mode == 17)
    				{
    					
    					Bullet bt = new Bullet(this);
    					bt.choose = 7;
    					
    					bt.width = 50;
    					bt.height = 50;
    					
    					bt.x = this.plane.x+this.plane.width/2-bt.width/2;
    					bt.y = this.plane.y;
    					this.bts.add(bt);
    					
    					Bullet bt1 = new Bullet(this);
    					bt1.choose = 7;
    					
    					bt1.width = 30;
    					bt1.height = 30;
    					
    					bt1.x = this.plane.x-30;
    					bt1.y = this.plane.y+20;
    					this.bts.add(bt1);
    					
    					Bullet bt2 = new Bullet(this);
    					bt2.choose = 7;
    					
    					bt2.width = 30;
    					bt2.height = 30;
    					
    					bt2.x = this.plane.x+this.plane.width;
    					bt2.y = this.plane.y+20;
    					this.bts.add(bt2);
    					
    					Bullet bt3 = new Bullet(this);
    					Bullet bt4 = new Bullet(this);
    					bt3.choose = bt4.choose = 2;
    					bt3.x = this.plane.x;
    					bt4.x = this.plane.x+this.plane.width-10;
    					
    					bt3.y =bt4.y = bt.y+10;
    					bt3.width =bt4.width= 10;
    					bt3.height = bt4.height = 20;
    					this.bts.add(bt3);
    					this.bts.add(bt4);
    				}
    				else if(this.plane.mode == 18)
    				{
    					Bullet bt = new Bullet(this);
    					bt.choose = 1;
    					bt.width = 15;
    					bt.height = 65;
    					bt.x = this.plane.x+this.plane.width/2-bt.width/2;
    					bt.y = this.plane.y;
    					this.bts.add(bt);
    					
    					Bullet bt1 = new Bullet(this);
    					bt1.x = this.plane.x+this.plane.width/4-bt.width/2+2;
    					Bullet bt2 = new Bullet(this);
    					bt2.x = this.plane.x+this.plane.width*3/4-bt.width/2-2;
    					bt2.choose = bt1.choose = 1;
    					bt1.y =bt2.y = bt.y+20;
    					bt1.width =bt2.width= 15;
    					bt1.height = bt2.height = 65;
    					this.bts.add(bt1);
    					this.bts.add(bt2);
    					
    					Bullet bt3 = new Bullet(this);
    					bt3.x = this.plane.x+this.plane.width/4-bt.width/2-10;
    					Bullet bt4 = new Bullet(this);
    					bt4.x = this.plane.x+this.plane.width*3/4-bt.width/2+10;
    					bt3.choose = bt4.choose = 1;
    					bt3.y =bt4.y = bt.y+40;
    					bt3.width =bt4.width= 15;
    					bt3.height = bt4.height = 65;
    					this.bts.add(bt3);
    					this.bts.add(bt4);
    				}
    			}
    			for(j = 0;j<this.bts.size();j++)
    			{
    				this.bts.get(j).drawBullet(g);
    			}
    		}
    		
    		//创建敌机
    		if(hasEnemy == true)
    		{
    			if(time%500==0)
    			{
    				if(this.ensType.size()>0)
    				{
    					int i = (int)(Math.random()*this.ensType.size());
    					Enemy enemy = null;
    						try {
    							//反射创建
    							enemy = (Enemy)this.ensType.get(i).getConstructors()[0].newInstance(new Object[] {this});
    						} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
    								| InvocationTargetException | SecurityException e) {
    							// TODO Auto-generated catch block
    							e.printStackTrace();
    						}
    					this.ens.add(enemy);
    				}
    			}
    			for(i = 0;i<this.ens.size();i++)
    			{
    				this.ens.get(i).drawEnemy(g);
    			}
    		}
    		
    		//绘制道具
    		if(isStart == true)
    		{
    			for(i = 0;i<this.its.size();i++)
    			{
    				this.its.get(i).drawItem(g);
    			}
    		}
    		if(isStart == true)
    		{
    			g.setColor(Color.WHITE);
    			g.setFont(new Font(" ",Font.BOLD, 20));
    			g.drawString("SCORE "+this.plane.count+" ",BFrame.pageWidth-150,30);
    			g.drawString("HP "+this.plane.hp+" ",50,30);
    		}
    		
    		if(isDead == true)
    		{
    			this.dg.drawDialog(g);
    		}
    	}
    
    }
    

    4.DrawableTread.java

    package com.thread;
    
    import com.view.BPanel;
    
    public class DrawableTread extends Thread{
    		
    	public BPanel bPanel;
    	public DrawableTread(BPanel bPanel) 
    	{
    		this.bPanel = bPanel;
    	}
    	
    	public void run()
    	{
    		while(true)
    		{
    			this.bPanel.repaint();
    			try {
    				this.currentThread().sleep(1);
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    	}
    
    }
    

    5.FrameMouseListener.java

    package com.listener;
    
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.util.ArrayList;
    
    import com.view.BFrame;
    
    public class FrameMouseListener implements MouseMotionListener{
    	
    	public BFrame bFrame;
    	@Override
    	public void mouseDragged(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void mouseMoved(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    		//开始后鼠标操控
    		if(this.bFrame.nPanel.isStart == true)
    		{
    			this.bFrame.nPanel.plane.x=e.getX()-this.bFrame.nPanel.plane.width/2-5;
    			this.bFrame.nPanel.plane.y=e.getY()-this.bFrame.nPanel.plane.height/2-15;
    		}
    		
    		//死亡后按钮按压反馈
    		if(this.bFrame.nPanel.isDead == true)
    		{
    			if((e.getX()>67&&e.getX()<405&&e.getY()>500&&e.getY()<606))
    			{
    				this.bFrame.nPanel.dg.ispress = true;
    			}
    			else
    			{
    				this.bFrame.nPanel.dg.ispress = false;
    			}
    			
    			
    		}
    
    		//System.out.println(e.getX()+" "+e.getY());
    
    	}
    
    
    }
    

    6.Bullet.java

    package com.model;
    
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.util.Arrays;
    
    import com.thread.MusicThread;
    import com.view.BPanel;
    
    public class Bullet {
    	public BPanel bPanel;
    	public int width = 50;
    	public int height = 50;
    	
    	public int x;
    	public int y;
    	public int choose = 1;
    	public Bullet(BPanel bPanel) 
    	{
    		this.bPanel = bPanel;
    	}
    	
    
    	public Image[] bt_1 = new Image[] {
    			Toolkit.getDefaultToolkit().getImage("Image/bullet20.png"),
    			Toolkit.getDefaultToolkit().getImage("Image/bullet21.png"),
    	};
    	public Image[] bt_2 = new Image[] {
    			Toolkit.getDefaultToolkit().getImage("Image/bullet60.png"),
    			Toolkit.getDefaultToolkit().getImage("Image/bullet61.png"),
    	};
    	public Image[] bt_3 = new Image[] {
    			Toolkit.getDefaultToolkit().getImage("Image/bullet10.png"),
    			Toolkit.getDefaultToolkit().getImage("Image/bullet11.png"),
    	};
    	public Image[] bt_4 = new Image[] {
    			Toolkit.getDefaultToolkit().getImage("Image/bullet1.png"),
    			Toolkit.getDefaultToolkit().getImage("Image/bullet2.png"),
    			Toolkit.getDefaultToolkit().getImage("Image/bullet3.png")
    	};
    	public Image[] bt_5 = new Image[] {
    			Toolkit.getDefaultToolkit().getImage("Image/bullet40.png"),
    			Toolkit.getDefaultToolkit().getImage("Image/bullet41.png"),
    			Toolkit.getDefaultToolkit().getImage("Image/bullet42.png")
    	};
    	public Image[] bt_6 = new Image[] {
    			Toolkit.getDefaultToolkit().getImage("Image/bullet30.png"),
    			Toolkit.getDefaultToolkit().getImage("Image/bullet31.png"),
    			Toolkit.getDefaultToolkit().getImage("Image/bullet32.png")
    	};
    	public Image[] bt_7 = new Image[] {
    			Toolkit.getDefaultToolkit().getImage("Image/bullet50.png"),
    			Toolkit.getDefaultToolkit().getImage("Image/bullet51.png"),
    	};
    	
    	public Image[] bt = bt_1;
    	public int index = 0;
    	public void drawBullet(Graphics g)
    	{	
    		if(choose == 1)
    		{
    			this.bt = Arrays.copyOf(bt_1, 2);
    		}
    		else if(choose == 2)
    		{
    			this.bt = Arrays.copyOf(bt_2, 2);
    		}
    		else if(choose == 3)
    		{
    			this.bt = Arrays.copyOf(bt_3, 2);
    		}
    		else if(choose == 4)
    		{
    			this.bt = Arrays.copyOf(bt_4, 3);
    		}
    		else if(choose == 5)
    		{
    			this.bt = Arrays.copyOf(bt_5, 3);
    		}
    		else if(choose == 6)
    		{
    			this.bt = Arrays.copyOf(bt_6, 3);
    		}
    		else if(choose == 7)
    		{
    			this.bt = Arrays.copyOf(bt_7, 2);
    		}
    		g.drawImage(bt[index], x, y, width,height, null);
    		if(this.bPanel.time%1==0)
    		{
    			index++;
    			if(index==bt.length)
    			{
    				index = 0;
    			}
    			
    			y--;
    			if(y+this.height<=0)
    			{
    				this.bPanel.bts.remove(this);
    			}
    		}
    		
    		for(int i = 0;i<this.bPanel.ens.size();i++)
    		{
    			Enemy e = this.bPanel.ens.get(i);
    			if((this.x>=e.x-this.width&&this.x<=e.x+e.width+this.width)&&(this.y>=e.y+this.height&&this.y<=e.y+this.height))
    			{
    				this.bPanel.bts.remove(this);
    				e.BeAttack();
    			}
    			
    		}
    	}
    	
    }
    

    7.Enemy.java

    package com.model;
    
    import java.awt.Graphics;
    import java.awt.Image;
    
    import com.thread.MusicThread;
    import com.view.BFrame;
    import com.view.BPanel;
    
    public class Enemy {
    	
    	public BPanel bPanel;
    	public int x;
    	public int y;
    	public int bx;
    	public int by;
    	public int width;
    	public int height;
    	
    	public int HP;
    	public int speed;
    	public int count;
    	public Item[] items;
    	public Image[] plane;
    	public Image[] boom;
    	public int index = 0;
    	public Enemy(BPanel bPanel)
    	{
    		this.bPanel = bPanel;
    	}
    	
    	public void drawEnemy(Graphics g)
    	{
    		if(HP>0)
    		{
    			g.drawImage(plane[index], x, y, width, height, null);
    			if(this.bPanel.time%speed==0)
    			{
    				if(this.bPanel.time%2==0)
    				{
    					index++;
    					if(index==plane.length)
    					{
    						index = 0;
    					}
    					y++;
    					if(y>BFrame.pageHeigh)
    					{
    						this.bPanel.ens.remove(this);
    					}
    				}
    			}
    		}
    		else
    		{
    			g.drawImage(boom[index], x, y, bx, by, null);
    			if(this.bPanel.time%20==0)
    			{
    				index++;
    				if(index == boom.length)
    				{
    					destory();
    				}
    			}
    		}
    	}
    	
    	public void BeAttack() {
    		if(HP>0)
    		{
    			HP--;
    		}
    	}
    	
    	public void destory()
    	{
    		if(items!=null&&items.length>0)
    		{
    			for(int i = 0;i<items.length;i++)
    			{
    				Item item = items[i];
    				item.y=this.y;
    				item.x=this.x+this.width/2;
    				this.bPanel.its.add(item);
    			}
    		}
    		this.bPanel.plane.count+=this.count;
    		new MusicThread("Music/boom.wav").start();
    		this.bPanel.ens.remove(this);
    		
    	}
    
    	
    
    }
    

    8.Plane.java

    package com.model;
    
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.util.Arrays;
    
    import com.thread.MusicThread;
    import com.view.BFrame;
    import com.view.BPanel;
    
    public class Plane {
    	
    	BPanel bPanel;
    	public int width = 60;
    	public int height = 90;
    	public int bw = 170;
    	public int bh = 170;
    	public int x,y;
    	public int dx = 480/2;
    	public int dy = 800;
    	public int hp = 100;
    	public int mode = 1;
    	public int choose = 1;
    	public Image[] plane1 = new Image[]
    			{
    					Toolkit.getDefaultToolkit().getImage("Image/W1.png"),
    					Toolkit.getDefaultToolkit().getImage("Image/W2.png"),
    			};
    	public Image[] plane2 = new Image[]
    			{
    					Toolkit.getDefaultToolkit().getImage("Image/Y1.png"),
    					Toolkit.getDefaultToolkit().getImage("Image/Y2.png"),
    			};
    	public Image[] plane3 = new Image[]
    			{
    					Toolkit.getDefaultToolkit().getImage("Image/T1.png"),
    					Toolkit.getDefaultToolkit().getImage("Image/T2.png"),
    			};
    	public Image[] plane4 = new Image[]
    			{
    					Toolkit.getDefaultToolkit().getImage("Image/M1.png"),
    					Toolkit.getDefaultToolkit().getImage("Image/M2.png"),
    			};
    	public Image[] boom = new Image[]
    			{
    					Toolkit.getDefaultToolkit().getImage("Image/boom600.png"),
    					Toolkit.getDefaultToolkit().getImage("Image/boom601.png"),
    					Toolkit.getDefaultToolkit().getImage("Image/boom602.png"),
    					Toolkit.getDefaultToolkit().getImage("Image/boom603.png"),
    			};
    	public Image[] images;
    	
    	public int index=0;
    	public int i=0;
    	public int count = 0;
    	
    	public boolean hasPlane = true;
    	
    	public Plane(BPanel bPanel)
    	{
    		this.bPanel = bPanel;
    //		this.x = (BFrame.pageWidth-this.width)/2;
    //		this.y = (BFrame.pageHeigh-this.height*2);
    	}
    	
    	public void ChooseType()
    	{
    		if(choose == 1)
    		{
    			this.images = Arrays.copyOf(plane1, 2);
    		}
    		else if(choose == 2)
    		{
    			this.images = Arrays.copyOf(plane2, 2);
    		}
    		else if(choose == 3)
    		{
    			this.images = Arrays.copyOf(plane3, 2);
    		}
    		else if(choose == 4)
    		{
    			this.images = Arrays.copyOf(plane4, 2);
    		}
    	}
    
    	public void drawplane(Graphics g)
    	{
    		if(this.bPanel.plane.hasPlane == true)
    		{
    			g.drawImage(this.images[index],x, y, width, height, null);
    			
    			//未开始时飞机动画
    			if(this.bPanel.isStart == false)
    			{
    				g.drawImage(this.images[index],x, y, width, height, null);
    				x = dx-width/2;
    				y = dy;
    				if(this.bPanel.time%1==0)
    				{
    					if(dy>620)
    					{
    						if(this.bPanel.time%10==0)
    						{
    							dy--;
    						}
    					}
    				}
    			}
    			
    			
    			if(this.bPanel.time%20==0)
    			{
    				index++;
    				if(this.index == this.images.length)
    				{
    					this.index = 0;
    				}
    			}
    			
    			
    			//吃东西
    			for(int i=0;i<this.bPanel.its.size();i++)
    			{
    				Item item = this.bPanel.its.get(i);
    				if(this.x>=item.x-this.width&&this.x<=item.x+item.width&&this.y>=item.y-this.height&&this.y<=item.y+this.height)
    				{
    					item.eated();
    				}
    			}
    			
    			//机体撞击
    			for(int i=0;i<this.bPanel.ens.size();i++)
    			{
    				Enemy e = this.bPanel.ens.get(i);
    				if((this.x>=e.x-this.width&&this.x<=e.x+e.width&&this.y>=e.y-this.height&&this.y<=e.y+this.height))
    				{
    					if(hp>0)
    					{
    						hp--;
    					}
    					if(hp<=0)
    					{
    						destory(g);
    						this.bPanel.isDead = true;
    						//new MusicThread("Music/boom.wav").start();
    					}
    				}
    			}
    		}
    	}
    	public void destory(Graphics g)
    	{
    		
    		g.drawImage(boom[i], x, y,bw, bh, null);
    		if(this.bPanel.time%20==0)
    		{
    			i++;
    			if(i == boom.length)
    			{
    				this.bPanel.plane.hasPlane = false;
    				this.bPanel.hasBullet = false;
    				this.bPanel.hasEnemy = false;
    				new MusicThread("Music/boom.wav").start();
    			}
    		}
    	}
    	
    }
    

    5.反思

    代码基础不够扎实,仅实现了同微信小程序中的飞机大战的一些功能,对于碰撞检测方面没有较好的解决方案。

  • 相关阅读:
    CSRF攻击与防御
    nmap报错: Failed to open device ethxxx
    谷歌黑客语法(google hacking)让你的搜索更精准有效
    查看CPU相关信息
    使用fiddler抓取jmeter发送的请求
    20191129字符串对齐方法
    20191126拼接字符串
    robotframework 模拟滚动鼠标到底部
    SQL中的左连接与右连接,内连接有什么区别
    robotframework 获取坐标
  • 原文地址:https://www.cnblogs.com/2b-or-not-2b-/p/14342350.html
Copyright © 2020-2023  润新知