用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

简单的飞机游戏

2017-12-22 作者: 庚源码举报

[java]代码库

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class PlaneFrame extends JFrame{
	PlanePanle jp;
	//子弹队列
	static List<Shell> list = new ArrayList<Shell>();
	//boss队列
	static List<Boss> bossList = new ArrayList<Boss>();
	//敌方战机队列
	static List<EnemyPlane> enemyList = new ArrayList<>();
	//存放boss子弹对象
	static ArrayList<Shell> bossBulletList=new ArrayList<Shell>();
	//存放敌机子弹
	static ArrayList<Shell> enemyShell = new ArrayList<Shell>();
	static int enemyPlaneCount=0;//敌方的数目
	static int enemyPlaneNumber=5;   //设置敌人最大飞机数目
	static int bossCount;//boss的数量
    static int bossNumber=1;//boss的最大数量
	public static String path=System.getProperty("user.dir")+"\\Resouce";
	public static HashMap<String,BufferedImage> maps = new HashMap<>();
	public void start() {
		File[] file = new File(path).listFiles();
		for(int i=0;i<file.length;i++) {
			try {
				maps.put(file[i].getName(), ImageIO.read(file[i]));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	public PlaneFrame() {
		//设置窗口大小
		this.setSize(640,700);
		//设置窗口名称
		this.setTitle("信工16级2班黄通作品--飞机游戏");
		//居中
		this.setLocationRelativeTo(null);
		//设置不可改变大小
		this.setResizable(false);
		//关闭程序,释放资源
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//添加一个适配器
		this.addKeyListener(new MyKeyListener());
		//开始加载图片
		start();
		//添加面板
		jp = new PlanePanle();
		add(jp);
		//设置窗口可见
		setVisible(true);	
	}
	//添加一个适配器
	class MyKeyListener extends KeyAdapter{
		public void keyPressed(KeyEvent e) {
			jp.keyPressed(e);
		}
	}
	public static void main(String[] args) {
		new PlaneFrame();
	}
}

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
class PlanePanle extends JPanel{
		int key;
		JLabel bossXue;//boss血量
		JLabel myPlaneXue;//我方飞机血量
		//初始化背景坐标
		Point bgPoint = new Point(0,-2180);
		//初始化我方飞机
		MyPlane myPlane = new MyPlane(280,500);
		//初始化一个boss
		//Boss boss = new Boss();
		//初始化一个敌方飞机的坐标
		Point enemyPlane = new Point(280,10);
		//面板构造方法,启动线程
		public PlanePanle() {
			this.setLayout(null);
			bossXue = new JLabel("boss血量:"+Boss.life);
			bossXue.setForeground(Color.BLACK);
			bossXue.setBounds(300,650,100,30);
			add(bossXue);
			new Thread(new BgThread()).start();
		}
		public void paint(Graphics g) {
			//调用父类paint方法
			super.paint(g);
			//创建一个画板
			BufferedImage image = new BufferedImage(640,700,BufferedImage.TYPE_INT_RGB);
			//创建一个画笔
			Graphics gs = image.createGraphics();
			//画背景两张背景,循环连接播放
			gs.drawImage(PlaneFrame.maps.get("background2a.bmp"),bgPoint.x,bgPoint.y,this);
			gs.drawImage(PlaneFrame.maps.get("background2a.bmp"),bgPoint.x,bgPoint.y-2880,this);
			//画我方飞机
			myPlane.drawMyPlane(gs);
		
				//画敌方飞机
			for(int i = 0;i<PlaneFrame.enemyList.size();i++) {
				PlaneFrame.enemyList.get(i).drawEnemyPlane(gs);
			}
			//画boss
			for(int i = 0;i<PlaneFrame.bossList.size();i++) {
				PlaneFrame.bossList.get(i).drawBoss(gs);
			}
			//子弹
			for(int i = 0;i<PlaneFrame.list.size();i++) {
				PlaneFrame.list.get(i).drawShell(gs);
			}
			//gs.setFont(new Font("宋体",Font.BOLD,20));
			gs.drawString("Boss的生命值:"+Integer.toString(Boss.life),400,20);
			gs.drawString("我方飞机血量:"+Integer.toString(MyPlane.life),400,60);
			gs.drawString("操作方法:方向键控飞机移动,空格键发子弹",200,650);
			//将画板添加到JPanel
			g.drawImage(image,0,0,this);
		}
		class BgThread implements Runnable{
			@Override
			public void run() {
				while(true) {
					//我方飞机死亡,游戏结束,提示窗口
					if(MyPlane.life<=0) {
						JOptionPane.showMessageDialog(null,"飞机死亡,游戏结束");
						System.exit(0);
					}
					if(Boss.life<=0) {
						JOptionPane.showMessageDialog(null,"恭喜,杀死BOSS,游戏胜利!!游戏到此结束");
						System.exit(0);
					}
					//如果敌机小于2 ,boss的生命为整数而且大于5,重新生成小飞机
					if(PlaneFrame.enemyPlaneNumber<=2&&Boss.life%10==0&&Boss.life<100) {
						//boss生命小于30,生成10个飞机
						if(Boss.life<30) {
							PlaneFrame.enemyPlaneNumber=10;
						}
						else
							PlaneFrame.enemyPlaneNumber=5;
						PlaneFrame.enemyPlaneCount=0;
					}
					
					//创建敌机和boss
					creatEnemy();
					creatBoss();
					//如果到到最下面,回到初始位置
					if(bgPoint.y==700) {
						bgPoint.y=-2180;
					}
					bgPoint.y+=1;
					//让子弹飞
					for(int i = 0;i<PlaneFrame.list.size();i++) {
						PlaneFrame.list.get(i).y-=10;
						//子弹越界,删除该子弹对象
						if(PlaneFrame.list.get(i).y<0)
							PlaneFrame.list.remove(i);
					}
					//线程休眠
					try {
						Thread.sleep(30);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					//重新绘图,更新
					repaint();
				}
			}
			
		}
		//键按下去
		public void keyPressed(KeyEvent e) {
			//如果按了空格键
			if(e.getKeyCode()==KeyEvent.VK_SPACE) {
				fire();
			}
			//如果按了上键
			if(e.getKeyCode()==KeyEvent.VK_UP) {
				myPlane.up=true;
				myPlane.down=false;
				myPlane.right=false;
				myPlane.left=false;
			}
			//如果按了下键
			if(e.getKeyCode()==KeyEvent.VK_DOWN) {
				myPlane.down=true;
				myPlane.up=false;
				myPlane.right=false;
				myPlane.left=false;
			}
			//如果按了左键
			if(e.getKeyCode()==KeyEvent.VK_LEFT) {
				myPlane.left=true;
				myPlane.down=false;
				myPlane.up=false;
				myPlane.right=false;
				
			}
			//如果按了右键
			if(e.getKeyCode()==KeyEvent.VK_RIGHT) {
				myPlane.right=true;
				myPlane.left=false;
				myPlane.down=false;
				myPlane.up=false;
			}
			repaint();
		}
		//子弹发射方法
		public void fire() {
			PlaneFrame.list.add(new Shell(myPlane.x+30,myPlane.y-80));
		}
		//创建敌方战机
		public void creatEnemy() {
			if(PlaneFrame.enemyPlaneCount<PlaneFrame.enemyPlaneNumber) {
				PlaneFrame.enemyList.add(new EnemyPlane(myPlane));
				PlaneFrame.enemyPlaneCount++;
			}
		}
		public void creatBoss() {
			if(PlaneFrame.bossCount<PlaneFrame.bossNumber) {
				PlaneFrame.bossList.add(new Boss(myPlane));
				PlaneFrame.bossCount++;
			}
		}
}

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Random;

import org.omg.CORBA.portable.Delegate;

public class Boss {
	Random random = new Random();
	static int life=100;//boss的生命
	int x=random.nextInt(500)-20;  //初始出现boss的x坐标
    int y=random.nextInt(100)+10;   //初始出现boss的y坐标
    int x1,x2;//boss水平移动增量
    int y1,y2;//boss竖直的增量
    int time;//移动一次需要的时间
    static int shootTime;//boss发射子弹时间间隔
    MyPlane myPlane;//传入我方飞机对象
    public Boss(MyPlane myPlane) {
    	setMyPlane(myPlane);
    }
    public void setMyPlane(MyPlane myPlane) {
    	this.myPlane = myPlane;
    }
    ArrayList<Shell> bossBulletList=new ArrayList<Shell>();//存放boss子弹对象
    boolean start = true;//判断游戏开始没,游戏开始产生一个boss
    //画boss
    public void drawBoss(Graphics g) {
    	timeX();
    	move();
    	if(Boss.life>0) {
    			g.drawImage(PlaneFrame.maps.get("boss.png"),x,y,null);
    	}
    	else {
    		PlaneFrame.bossList.remove(this);
    	}
    	bossShot(g);//boss发子弹
    }
    //boss发子弹实现
    public void bossShot(Graphics g) {
    	shootInterval(x,y);//发弹间隔时间
    	ifBossShootMyPlane(g);//判断boss子弹是否射中我方战机 
	}
    //是否击中我方飞机
	public void ifBossShootMyPlane(Graphics g) {
		for(int i=0;i<bossBulletList.size();i++)
		{   
			if(bossBulletList.get(i).y>700)    //如果子弹出界
			{ bossBulletList.remove(bossBulletList.get(i)); }	                 //删除子弹对象
			
			else
			{
				bossBulletList.get(i).drawBossBullet(g);       //画出子弹
				int x1=bossBulletList.get(i).x;
				int y1=bossBulletList.get(i).y;
				if(x1-myPlane.x>-25&&x1-myPlane.x<60&&myPlane.y-y1<60&&y1-myPlane.y<0) {
					System.out.println("子弹:"+"("+x1+","+y1+")");
					System.out.println("飞机:"+"("+myPlane.x+","+myPlane.y+")");
					int ex=myPlane.x;
					int ey=myPlane.y;
					MyPlane.life-=2;
					bossBulletList.remove(bossBulletList.get(i));
					Explode(ex,ey,g);
				}
				
			}
		}
	}
	public void Explode (int x,int y,Graphics g) {
		for(int i=0;i<33;i++) {
			g.drawImage(PlaneFrame.maps.get((i+1)+".png"), x, y,null);
		}
	}
	public void shootInterval(int x, int y) {
		if(shootTime<=0)       //每隔一定时间发射子弹
	    {                                                                                         
	    	shootTime=time;
	    	Shell s=new Shell(x+90,y+145);
	    	bossBulletList.add(s);
	    }
		 shootTime--;
	}
	public void timeX() {
    	if(time<=0) {
    		time=random.nextInt(40)+20; //水平定向移动持续时间
	    	x1=random.nextInt(4);        //产生向右的移动增量
			x2=-random.nextInt(4);
			y1=random.nextInt(4);        //产生向下的移动增量
			y2=-random.nextInt(4);       //产生向上的移动增量
			while(y1+y2==0)              //让正负增量之和不为0 ,为0战机就不动了,不为0就动
				{y2=-random.nextInt(4);} //让正负增量之和不为0 ,为0战机就不动了,不为0就动
    	}
    	time--;
    }
    public void move() {
    	
	    	x=x+x1+x2;
	    	y=y+y1+y2;
	    	//防止敌方飞机出界
	    	if(x<0){
	    		x=0;
	    		time=0;
	    	}  
			if(x>540){
				x=540;
				time=0;
			}
			//保证boss在上方
			if(y>150) {
				y=150;
			}
			if(y<0) {
				y=0;
			}
    	}
    
}

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Random;

public class EnemyPlane {
	Random random = new Random();
	int x=random.nextInt(500)-20;  //初始出现x坐标
    int y=random.nextInt(300)+10;   //初始出现y坐标
    int life=1;//敌方飞机生命
    int x1,x2;//飞机左右移动增量
    int timeX;//水平移动的时间
    int shootTime;
    MyPlane myPlane;
    ArrayList<Shell> enemyShell = new ArrayList<Shell>();
    public EnemyPlane(MyPlane myPlane) {
    	setMyPlane(myPlane);
    }
    public void setMyPlane(MyPlane myPlane) {
    	this.myPlane = myPlane;
    }
    public void drawEnemyPlane(Graphics g) {
    	timeX();
    	move();
    	if(this.life>0) {
    		g.drawImage(PlaneFrame.maps.get("enemy8.png"), x, y+2, null);
    	}else {
    		PlaneFrame.enemyList.remove(this);
    	}
    	enemyShot(g);//敌机发子弹
    }
    public void enemyShot(Graphics g) {
    	shootInterval(x,y,110);//发弹间隔时间
    	ifBossShootMyPlane(g);//判断boss子弹是否射中我方战机 
    }
    private void ifBossShootMyPlane(Graphics g) {
    	for(int i=0;i<enemyShell.size();i++)
		{   
			if(enemyShell.get(i).y>700)    //如果子弹出界
			{ enemyShell.remove(enemyShell.get(i)); }	                 //删除子弹对象
			
			else
			{
				enemyShell.get(i).drawEnemyBullet(g);       //画出子弹
				int x1=enemyShell.get(i).x;
				int y1=enemyShell.get(i).y;
				if(x1-myPlane.x>-10&&x1-myPlane.x<70&&myPlane.y-y1<20&&y1-myPlane.y<0) {
					System.out.println("子弹:"+"("+x1+","+y1+")");
					System.out.println("飞机:"+"("+myPlane.x+","+myPlane.y+")");
					int ex=myPlane.x;
					int ey=myPlane.y;
					MyPlane.life--;
					enemyShell.remove(enemyShell.get(i));
					Explode(ex,ey,g);
				}
				
			}
		}
	}
    public void Explode (int x,int y,Graphics g) {
		for(int i=0;i<33;i++) {
			g.drawImage(PlaneFrame.maps.get((i+1)+".png"), x, y,null);
		}
	}
	private void shootInterval(int x, int y, int i) {
		if(shootTime<=0)       //每隔一定时间发射子弹
	    {                                                                                         
	    	shootTime=i;
	    	Shell s=new Shell(x+30,y+50);
	    	enemyShell.add(s);
	    }
		 shootTime--;
	}
	public void timeX() {
    	if(timeX<=0) {
    		timeX=random.nextInt(40)+20; //水平定向移动持续时间
	    	x1=random.nextInt(4);        //产生向右的移动增量
			x2=-random.nextInt(4);
    	}
    	timeX--;
    }
    public void move() {
    	x=x+x1+x2;
    	//y=y+random.nextInt(2);
    	//防止敌方飞机出界
    	if(x<0){
    		x=0;
    		timeX=0;
    	}  
		if(x>540){
			x=540;
			timeX=0;
		}
    }
}

import java.awt.Graphics;
import java.awt.event.KeyEvent;

import javax.swing.JOptionPane;

public class MyPlane {
	static int life=5;//我方战机的生命
	//我方飞机的坐标
	int x,y;
	//飞机速度
	int speed=4;
	//飞机移动方向
	boolean up;
	boolean down;
	boolean right;
	boolean left;
	
	public MyPlane(int x,int y) {
		this.x=x;
		this.y=y;
	}
	public void drawMyPlane(Graphics g) {
		if(up) {
			y-=speed;
			if(y<=0)y=0;
		}
		if(down) {
			y+=speed;
			if(y>620)y=620;
		}
		if(left) {
			x-=speed;
			if(x<=0)x=0;
		}
		if(right) {
			x+=speed;
			if(x>540)x=540;
		}
		//画我方飞机
		if(life>0) {
			g.drawImage(PlaneFrame.maps.get("plane.png"),x,y,null);
		}
		}
}

[代码运行效果截图]


简单的飞机游戏

[源代码打包下载]




网友评论    (发表评论)

共20 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...