⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 planegame.java

📁 飞机大战游戏Java源码.rar 飞机大战游戏Java源码综合运用Java程序设计语言的图形图像处理、事件处理、多线程、socket通讯等技术
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.border.*;
import java.sql.*;
import java.util.*;
import java.net.*;

class EPlaneInfor        //保存敌机运动
{
	Thread planeThread;  //保存敌机运动的线程
	JLabel planeLabel;   //保存敌机显示的Label,以便获取敌机的位置
	boolean section;     //保存敌机是否被击中的状态
	public EPlaneInfor(Thread pT,JLabel pL)   //通过构造函数传递信息给对象
	{
		planeThread=pT;
		planeLabel=pL;
		section=false;
	}
}

class AllMessage         //保存一些零乱信息
{
	int mark;
	int lbllife;
	int level;
	int BitContral;
	int DaoDan;
	int saveline;
	boolean wincontral;
	boolean bool;
	String UserName;
	AllMessage()
	{
		mark=0;          //保存成绩
		lbllife=5;       //设生命为5
		level=0;         //当你的分数达到 100*x(x=1,2,3,4,5,6,7,8) 分时,飞机数量会相应增加(1,2,3,4,5,6,7,8)
		BitContral=0;    //将我方子弹控制在6颗
		DaoDan=7;        //控制安全弹的个数
		saveline=710;    //保存安全线的位置
		wincontral=false;//游戏胜利时会用到
		bool=false;      //用来控制是否播放炮弹声音
		UserName="";     //保存用户名
	}
}

class MyFrame implements ActionListener //图形界面类,其他类都放入其类体中,以便共享数据
{
	JFrame fra;
	
	JPanel pnl;
	JPanel pnlInfo;
	
	MenuBar menubar;
	Menu menufile,menuhelp,menuview;
	MenuItem itemstart,itemclose,itemhelp,itemgood,itemsound;
	
	JLabel picPlane;      //用于显示我方飞机,保存我方飞机的位置信息
	
	JLabel lblhour;       //用于显示时间 时,分,秒
	JLabel lblmini;
	JLabel lblsec;
	
	JLabel mark;          //分数
	
	JLabel lbllife;       //显示生命
	 
	JLabel SaveLine;     //显示安全线
	
	Icon BeiJingIcon1;    //背景图片1
	JLabel BeiJing1;
	Icon BeiJingIcon2;    //背景图片2
	JLabel BeiJing2;
	
	Icon StartIcon;       //开机图片
	JLabel lblstart;
	
	Icon anBullet;       //用于保存安全LINE图片
	Icon picBullet;       //用于保存安全线图片
	Icon genzongEBullet;  //用于保存敌机跟踪弹图片
	Icon []imgBullet;     //用于保存子弹爆炸的一组图片
	
	Vector planeEVector;  //用于保存所有敌机的信息
	Random sleep; 
	int intSleep;         //用于设置敌机的运动速度
	
	AllMessage count;     //保存一些零乱信息
	
	BackSound BS;
	BombSound Bomb;
	
	int KeyLeft=0;
	int KeyLight=0;
	int KeyUp=0;
	int KeyDown=0;
	int KeyBit=0;
	int KeyDaoDan=0;
	MyFrame(String username,AllMessage allmess)
	{
		count=allmess;
		count.UserName=username;
		fra=new JFrame(count.UserName+" :欢迎你进入飞机大战");
		planeEVector=new Vector(1,1);
		sleep=new Random(1234567890L);   //随机获取敌机的运动速度
		
		pnl=new JPanel();               //实例化面板
		pnlInfo=new JPanel();
		pnl.setLayout(null);
		
		menubar=new MenuBar();           //菜单
		menufile=new Menu(" 文 件 ");
		menuview=new Menu(" 查 看 ");
		menuhelp=new Menu(" 帮 助 ");
		itemstart=new MenuItem("开始游戏");
		itemsound=new MenuItem("声音设置");
		itemclose=new MenuItem("退出游戏");
		itemgood=new MenuItem("排名");
		itemhelp=new MenuItem("帮助");
		menubar.add(menufile);
		menubar.add(menuview);
		menubar.add(menuhelp);
		
		menufile.add(itemstart);
		menufile.add(itemsound);
		menufile.add(itemclose);
		menuview.add(itemgood);
		menuhelp.add(itemhelp);
		
		itemstart.addActionListener(this);
		itemsound.addActionListener(this);
		itemclose.addActionListener(this);
		itemgood.addActionListener(this);
		itemhelp.addActionListener(this);
		
		fra.setMenuBar(menubar);
		
		anBullet=new ImageIcon(getClass().getResource("bit\\anquan.gif"));
		picBullet=new ImageIcon(getClass().getResource("bit\\picBit.gif"));
		genzongEBullet=new ImageIcon(getClass().getResource("bit\\genzong.gif"));
		imgBullet=new Icon[16];          //实例十幅图片
		imgBullet[0] = new ImageIcon(getClass().getResource("bit\\e1.gif"));
		imgBullet[1] = new ImageIcon(getClass().getResource("bit\\e2.gif"));
		imgBullet[2] = new ImageIcon(getClass().getResource("bit\\e3.gif"));
		imgBullet[3] = new ImageIcon(getClass().getResource("bit\\e4.gif"));
		imgBullet[4] = new ImageIcon(getClass().getResource("bit\\e5.gif"));
		imgBullet[5] = new ImageIcon(getClass().getResource("bit\\e6.gif"));
		imgBullet[6] = new ImageIcon(getClass().getResource("bit\\e7.gif"));
		imgBullet[7] = new ImageIcon(getClass().getResource("bit\\e8.gif"));
		imgBullet[8] = new ImageIcon(getClass().getResource("bit\\e9.gif"));
		imgBullet[9] = new ImageIcon(getClass().getResource("bit\\e10.gif"));
		imgBullet[10]= new ImageIcon(getClass().getResource("bit\\e11.gif"));
		imgBullet[11]= new ImageIcon(getClass().getResource("bit\\e12.gif"));
		imgBullet[12]= new ImageIcon(getClass().getResource("bit\\e13.gif"));
		imgBullet[13]= new ImageIcon(getClass().getResource("bit\\e14.gif"));
		imgBullet[14]= new ImageIcon(getClass().getResource("bit\\e15.gif"));
		imgBullet[15]= new ImageIcon(getClass().getResource("bit\\e16.gif"));

		Icon imgPlane = new ImageIcon(getClass().getResource("plane\\plane.gif"));//我方飞机的初始位置
		picPlane = new JLabel(imgPlane);
		picPlane.setBounds(250,550,100,100);
		pnl.add(picPlane);
		
		mark=new JLabel("分 数:"+"0");        //分数
		mark.setBounds(0,5,100,20);
		pnlInfo.add(mark);

		lbllife=new JLabel("生命:"+5);        //显示生命
		lbllife.setBounds(100,5,100,20);
		pnlInfo.add(lbllife);
		
		SaveLine=new JLabel("安全线:"+count.DaoDan);        //显示所剩的安全线
		SaveLine.setBounds(200,5,100,20);
		pnlInfo.add(SaveLine);
		
		pnlInfo.setLayout(null);              //显示各种信息的面板
		pnlInfo.setBounds(0,0,600,30);
		pnl.add(pnlInfo);
		
		PlaneControl pm=new PlaneControl();         //飞机游戏的控制的事件监听器
		JTextField jf=new JTextField(10);      
		jf.addKeyListener(pm);
		
		fra.getContentPane().add(jf);
		fra.getContentPane().add(pnl);
		
		StartIcon=new ImageIcon(getClass().getResource("picture\\Start.gif"));
		lblstart=new JLabel(StartIcon);
		lblstart.setBounds(25,100,550,400);
		
		pnl.add(lblstart);
		
	    BeiJingIcon1=new ImageIcon(getClass().getResource("sky\\sky1.jpg"));//实例背景图片
		BeiJing1=new JLabel(BeiJingIcon1);
		BeiJing1.setBounds(0,0,600,700);
		pnl.add(BeiJing1);
		BeiJingIcon2=new ImageIcon(getClass().getResource("sky\\sky2.jpg"));
		BeiJing2=new JLabel(BeiJingIcon1);
		BeiJing2.setBounds(0,-672,600,700);
		pnl.add(BeiJing2);
		
		BS=new BackSound();
		Bomb=new BombSound();
		
		new TimeShow();                     //实例显示时间的类
	    new BeiJingThread();                //实例背景动画类
	    System.out.println("打开了窗体,正等待游戏开始。。");
	    
	    fra.setResizable(false);
		fra.setSize(600,700);
		fra.setLocation(200,20);
		fra.setVisible(true);
	}
	//用来监听MenuItem
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==itemstart)
		{
			lblstart.hide();
			System.out.println("游戏开始...");
			new PlaneThread();            //我方飞机控制类
			System.out.println("用户 "+count.UserName+" 开始了游戏");
		    new ShowEPlane();             //实例自动出现敌机的类
		}
		else if(e.getSource()==itemsound)
		{
			new SetSound();
		}
		else if(e.getSource()==itemclose)
		{
			new GameOver();
			fra.setVisible(false);
			fra=null;
			System.out.println("已上传分数...");
			System.out.println("已经退出游戏...");
		}
		else if(e.getSource()==itemgood)
		{
			new GameOver();
		}
		else if(e.getSource()==itemhelp)
		{
			new Help();
			System.out.println("进入帮助窗口...");
		}
	}
	//如果我方飞机得到一个心,我方飞机生命+1
	class LifeClass extends Thread  //左向下
	{
		JLabel lbllifegif;    //显示一个奖生命的心
		int lifeplace=0;
		public LifeClass(int place)
		{
			lifeplace=place;
		    Icon iconLife;
			iconLife = new ImageIcon(getClass().getResource("picture\\life.gif"));
			lbllifegif = new JLabel(iconLife);
			lbllifegif.setBounds(lifeplace/2,0,50,50); 
			pnl.add(lbllifegif);
			new Thread(this).start();
		}
		public void run()
		{
			while(lbllifegif.getY()<700)
			{
				lbllifegif.setLocation(lifeplace/2,lbllifegif.getY()+20);
		        if(lbllifegif.getX()<-20||lbllifegif.getX()>550||lbllifegif.getY()<-10||lbllifegif.getY()>700)
		        {
		        	lbllifegif.setVisible(false);
		        	lbllifegif.setLocation(-100,-100);
		        	this.stop();
		        }
		        //如果我方飞机接触到心,则我方飞机生命+1
	            if(lbllifegif.getX()>(picPlane.getX()-20) && lbllifegif.getX()<(picPlane.getX()+20) && lbllifegif.getY()>(picPlane.getY()-35) && lbllifegif.getY()<(picPlane.getY()+35))
	            {
            		count.lbllife++;
	        	    System.out.println("生命+1,还有生命"+count.lbllife);
	        	    lbllife.setText("生命:"+count.lbllife);
				    
			    	lbllifegif.setVisible(false);
				    lbllifegif.setLocation(-100,-100);
		        	this.stop();
	            }
		        try
		    	{
			    	this.sleep(100);
			    }
			    catch(InterruptedException e)
		        {
		        	System.out.println("Sleep Error!! :"+e);
		        }
		    }
		    lbllifegif.setVisible(false);
		    lbllifegif.setLocation(-100,-100);
		    this.stop();
		}
	}
	//声音设置对话框
	class SetSound implements ItemListener
	{
		JFrame fra;
		JPanel pnl;
		JLabel BackSound; 
		JLabel BombSound;
		Choice Mchoice1;
		Choice Mchoice2;
		
		SetSound()
		{
			fra=new JFrame();
			pnl=new JPanel();
			pnl.setLayout(null);
			pnl.setBackground(Color.orange);
			
			BackSound=new JLabel("背景音乐");
			BackSound.setBounds(5,50,70,20);
			BombSound=new JLabel("炮弹声音");
			BombSound.setBounds(5,100,70,20);
			
			Mchoice1=new Choice();
			Mchoice1.add("打开背景音乐");
			Mchoice1.add("关闭背景音乐");
			Mchoice1.setBounds(80,50,100,20);
			
			Mchoice2=new Choice();
			Mchoice2.add("打开炮弹声音");
			Mchoice2.add("关闭炮弹声音");
			Mchoice2.setBounds(80,100,100,20);		
			
			Mchoice1.addItemListener(this);
			Mchoice2.addItemListener(this);
			
			pnl.add(Mchoice1);
			pnl.add(Mchoice2);
			pnl.add(BackSound);
			pnl.add(BombSound);
			
			fra.setContentPane(pnl);
			fra.setResizable(false);
			fra.setTitle("声音设置");
			fra.setLocation(300,200);
			fra.setSize(200,200);
			fra.setVisible(true);
		}
		public void itemStateChanged(ItemEvent e) 
		{
			if(e.getItem().equals("打开背景音乐"))
			{
				System.out.println(e.getItem());
				BS.aau.play();
			}
			else if(e.getItem().equals("关闭背景音乐"))
			{
				System.out.println(e.getItem());
				BS.aau.stop();
		    }
		    else if(e.getItem().equals("打开炮弹声音"))
		    {
		    	System.out.println(e.getItem());
		    	count.bool=true;
		    }
		    else if(e.getItem().equals("关闭炮弹声音"))
		    {
		    	System.out.println(e.getItem());
		    	count.bool=false;
		    }
		}
	}
	//显示时间
	class TimeShow extends Thread
	{
		TimeShow()
		{
			lblhour=new JLabel();
			lblmini=new JLabel();
			lblsec=new JLabel();
			lblhour.setBounds(480,5,40,25);
			lblmini.setBounds(520,5,40,25);
			lblsec.setBounds(560,5,40,25);
			new Thread(this).start();
		}
		public void run()
		{
			while(true)
			{
				try
				{
					pane();
					this.sleep(1000);
				}
				catch(InterruptedException e)
				{
					System.out.println("exception"+e);
				}
			}
		}
		public void pane()
		{
			java.util.Date now = new java.util.Date();
			lblhour.setText(Integer.toString(now.getHours())+"时"); //时
			lblmini.setText(Integer.toString(now.getMinutes())+"分");//分 
			lblsec.setText(Integer.toString(now.getSeconds())+"秒"); //秒
			pnlInfo.add(lblhour);
			pnlInfo.add(lblmini);
			pnlInfo.add(lblsec);
		}
	}
	//帮助对话框
    class Help extends JFrame  //帮助对话框
	{
		public Help()
		{
			JPanel pnl=new JPanel();
			JTextArea textarea=new JTextArea(18,24);
			textarea.setLineWrap(true);
			textarea.append("方向盘:"+"\n");
			textarea.append("w 使飞机向上移动"+"\n");
			textarea.append("s 使飞机向上移动"+"\n");
			textarea.append("a 使飞机向上移动"+"\n");
			textarea.append("d 使飞机向上移动"+"\n");
			textarea.append("j 发射我方飞机子弹"+"\n");
			textarea.append("开始游戏:"+"\n");
			textarea.append("点击 |文件| 然后点击 |开始| "+"\n");
			textarea.append("退出游戏:"+"\n");
			textarea.append("点击 |文件| 然后点击 |退出| "+"\n");
			textarea.append("查看成绩排行:"+"\n");
			textarea.append("点击 |查看| 然后点击 |排名| "+"\n");
			textarea.append(" =============游戏说明==============="+"\n");
			textarea.append("开始游戏时,飞机数量设为6"+"\n");
			textarea.append("当你的分数达到 100*x(x=1,2,3,4,5,6,7,8) 分时,"+"\n"+"飞机数量会相应增加(1,2,3,4,5,6,7,8)"+"\n");
			pnl.add(textarea);
			this.setResizable(false);
			this.setContentPane(pnl);
			this.setTitle("游戏使用帮助");
			this.setLocation(300,200);
			this.setSize(300,370);
			this.setVisible(true);
		}
	}
	//背景图片不断更替
	class BeiJingThread extends Thread
	{
		BeiJingThread()
		{
			new Thread(this).start();
		}
		public void run()
		{
			while(true)
			{
				try
			    {
			    	sleep(150);
			    }
			    catch(Exception e)
			    {
			    }
				if(BeiJing1.getY()>672)
				{
					BeiJing1.setLocation(0,-672);
				}
				else
				{
					BeiJing1.setLocation(BeiJing1.getX(),BeiJing1.getY()+2);
				}
				
				if(BeiJing2.getY()>672)
				{
					BeiJing2.setLocation(0,-672);
				}
				else
				{
					BeiJing2.setLocation(BeiJing2.getX(),BeiJing2.getY()+2);
				}
				pnl.add(BeiJing1);
				pnl.add(BeiJing2);
			}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -