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

📄 shootplaneserver.java

📁 我的一个java大作业 是一个可以联机的打飞机的游戏 附带聊天功能
💻 JAVA
字号:
/*
 * @(#)Shootplane.java 1.0 06/11/05
 *
 * You can modify the template of this file in the
 * directory ..\JCreator\Templates\Template_1\Project_Name.java
 *
 * You can also create your own project template by making a new
 * folder in the directory ..\JCreator\Template\. Use the other
 * templates as examples.
 *
 */


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
import javax.swing.border.*;
import javax.swing.ImageIcon.*;
import java.net.*;                                              
import java.io.*;
public class Shootplaneserver extends Frame {
	public int flag=1; 
	Socket clt=null;
	Socket chat=null;
	BufferedReader in;
	PrintWriter out;
	PrintWriter outchat;
	BufferedReader inchat;
	ServerSocket svr=null;
	ServerSocket chats=null;
	ServerSocket   chatserver=null;
	Socket   chatSocket=null;
	BufferedReader  inchatSocket;
	PrintWriter  outchatSocket;
	int hurt=0;
	int shoot=4;
	int planes=0;
	int count=0;

	Panel pane1 = new Panel();
	Panel pane2 = new Panel();
	Panel pane3 = new Panel();
	Panel pane4 = new Panel();	
	JPanel pane21 = new JPanel();
	JPanel pane22 = new JPanel();
	JPanel pane23 = new JPanel();
	ImageIcon icon1= new ImageIcon ("pic1.gif");
	ImageIcon icon2= new ImageIcon ("pic2.gif");
	ImageIcon icon3= new ImageIcon ("pic3.gif");
	ImageIcon icon4= new ImageIcon ("pic4.gif");
	JButton icons=new JButton();
	JTextArea textarea = new JTextArea (19,18);
	Button B[]=new Button[5];
	JTextField msg1=new JTextField(20);
	Button D1=new Button("发送");
	 Button D2=new Button("新一局");
	Button D3=new Button("创建连接");
	JTextField ip=new JTextField("               ");
	JPanel pane41=new JPanel();
	JPanel pane42=new JPanel();
	 mypanel[] p=new mypanel[121];
	 mypanel[] q=new mypanel[121]; 
 
	
	public Shootplaneserver() {
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				dispose();
			try{
				in.close();
				out.close();
				clt.close();
				svr.close();
			    inchat.close();
			    outchat.close();
			    chat.close();
			    chats.close();
			}catch(Exception a){
	         		textarea.append("\n关闭出错!!");}
				System.exit(0);
			
			}
		});
	setLayout(new GridLayout(2,2,20,20));
		
	pane1.setLayout(new GridLayout(11,11,0,0));
	pane3.setLayout(new GridLayout(11,11,0,0));
	pane2.setLayout(new GridLayout(1,2,10,10));
	pane4.setLayout(new GridLayout(2,1,1,1));
	
	pane21.setLayout(new GridLayout(2,1,10,10));
	textarea.setEditable(false);
	textarea.setLineWrap(true);
	textarea.setText("设置飞机并创建连接");
	icons.setIcon(icon1);
	pane21.add(icons);
	pane22.add (textarea);
	String s[]={"UP","DOWN","LEFT","RIGHT","RESET"};
	int i ;
    
	for (i=0;i<5;i++)
	 {B[i]=new Button(s[i]);
	 pane23.add (B[i]);
	 }
	 pane21.add(pane23);
	 pane2.add(pane21);
	 pane2.add(pane22);
	 
	 
	 pane41.add(msg1);
	 pane41.add(D1);
	 pane42.add(D2);
	 pane42.add(ip);
	 pane42.add(D3);
	 pane4.add(pane41);
	 pane4.add(pane42);
	 
	 
	 D1.addMouseListener(new MouseAdapter(){
	 	public void mouseClicked(MouseEvent e){
	 		String chatword=msg1.getText();
	 		textarea.append("\n红方:"+chatword);
	 	   try{
	 	   	 outchatSocket.println("红方:"+chatword);
	 	    outchatSocket.flush();
	 	    }catch(Exception a){
	         		textarea.append("\n连接出错!!");}
	 		}
	 	}
      )	;
      
     D2.addMouseListener(new MouseAdapter(){
     	public void mouseClicked(MouseEvent e){
     		if (hurt!=3&&count!=3) return;
     			try{
     			out.println(500);
     			out.flush();
     		}catch(Exception a){
	         		textarea.append("\n连接出错!!");}
     	   
	       for (int i=0;i<121;i++)
	       {
	       	p[i].ip=0;
	       	p[i].setBackground(Color.lightGray);
	       	q[i].setBackground(Color.lightGray);
	       	hurt=0;
         	shoot=4;
	        planes=0;
	        count=0;
	       	}  		
     	}
     	}); 
     	
	 for (i=0;i<121;i++)
	 {
	 mypanel u=new mypanel();
	 p[i]=u; 
	 p[i].ic=i;
	 mypanel v=new mypanel();
	 q[i]=v;
	 q[i].ic=i; 
	 pane1.add(p[i]);
	 pane3.add(q[i]);
	 	}
	 	
	String a;
	for (i=1;i<11;i++)
	{
	 a=Integer.toString(i-1);	
	p[i].add(new Label(a));
	p[i*11].add(new Label(a));
	q[i].add(new Label(a));
	q[i*11].add(new Label(a));}
	
	add(pane1);
	add(pane2);
	add(pane3);
	add(pane4);
	
	D3.addMouseListener(new MouseAdapter(){
	 	public void mouseClicked(MouseEvent e){
	 	if (planes!=3)	{
	 		textarea.append("\n请设置飞机!!");
	 		return;}
	 	ShootClient shot=new ShootClient();
	 	Thread client=new Thread(shot);
	 	client.start();	
	 	chatClient newchat=new chatClient();
		Thread newchatthread=new Thread(newchat);
	  	newchatthread.start();	
	 	}
	 }
	 		);
	 		
	
	B[0].addMouseListener(new MouseAdapter(){
	     public void mouseClicked(MouseEvent e){
	        flag=1;
	       icons.setIcon(icon1); 
	     }
	});
	B[1].addMouseListener(new MouseAdapter(){
	     public void mouseClicked(MouseEvent e){
	        flag=2;
	       icons.setIcon(icon2); 
	     }
	});
	B[2].addMouseListener(new MouseAdapter(){
	     public void mouseClicked(MouseEvent e){
	        flag=3;
	       icons.setIcon(icon3); 
	     }
	});
	B[3].addMouseListener(new MouseAdapter(){
	     public void mouseClicked(MouseEvent e){
	        flag=4;
	       icons.setIcon(icon4); 
	     }
	});
	B[4].addMouseListener(new MouseAdapter(){
	     public void mouseClicked(MouseEvent e){
	     	if (!(svr==null)) {
	     		textarea.append("\n正在游戏中!!!");
	     		return;}
	     	int i;
	        for (i=0;i<121;i++)
	         {p[i].setBackground(Color.lightGray);
	         p[i].ip=0;
	         } 
	         planes=0;
	     }
	});
	
	
	 for (i=0;i<121;i++)
	 {
	 	q[i].addMouseListener(new MouseAdapter(){
	 		public void mouseClicked(MouseEvent e){
	 		if (shoot%2!=0) return ;
	 		mypanel me=new mypanel();
			me=(mypanel)e.getSource();
			int i=me.ic;
			try{
			out.println(Integer.toString(i));
			out.flush();
			int j=Integer.parseInt(in.readLine());
			me.ip=j;
			if (j==2){
				count++;
				me.setBackground(Color.red);
				if (count==3)
				{textarea.append("\n您赢了!!");}
				     }
			else if (j==1){
				me.setBackground(Color.blue);}
			else 
			    {me.setBackground(Color.darkGray);}	
			 shoot--;  
	
	 			}catch(Exception a){
	         		textarea.append("\n连接出错!!");}
	 		}
	 		});
	 	}	
	 	
	 	
	 for (i=0;i<121;i++)
	  {
	  p[i].addMouseListener(new MouseAdapter(){
	      
		public void mouseClicked(MouseEvent e){
			if (planes==3){
				textarea.append("\n只允许放置三架飞机!");
				return;}
			mypanel me=new mypanel();
			me=(mypanel)e.getSource();
			int i=me.ic;
		    int r=i/11+1;
		    int l=i-(r-1)*11+1;
		    switch (flag)
		    { 
		      case 1:
		        {
		          if (r <=9&&r>=3&&l<=9&&l>=4&&p[i].ip==0&&p[i-11].ip==0&&p[i-2].ip==0&&
		          p[i-1].ip==0&&p[i+1].ip==0&&p[i+2].ip==0&&p[i+21].ip==0&&p[i+11].ip==0&&
		          p[i+23].ip==0&&p[i+22].ip==0){
		          	p[i].ip=1;p[i-11].ip=2;
		          	p[i-2].ip=1;p[i-1].ip=1;
		          	p[i+1].ip=1;p[i+2].ip=1;
		          	p[i+22].ip=1;p[i+11].ip=1;
		          	p[i+23].ip=1;p[i+21].ip=1;
		          	p[i].setBackground(Color.blue);
		          	p[i-11].setBackground(Color.blue);
		          	p[i-2].setBackground(Color.blue);
		          	p[i-1].setBackground(Color.blue);
		          	p[i+1].setBackground(Color.blue);
		          	p[i+2].setBackground(Color.blue);
		          	p[i+21].setBackground(Color.blue);
		          	p[i+11].setBackground(Color.blue);
		          	p[i+22].setBackground(Color.blue);
		          	p[i+23].setBackground(Color.blue);
		          	planes++;
		        	}
		        	break;
		    	}
		    	
		    case 2:
		        {
		          if (r <=9&&r>=4&&l<=9&&l>=3&&p[i].ip==0&&p[i-11].ip==0&&p[i-2].ip==0&&
		          p[i-1].ip==0&&p[i+1].ip==0&&p[i+2].ip==0&&p[i-22].ip==0&&p[i+11].ip==0&&
		          p[i-23].ip==0&&p[i-21].ip==0){
		          	p[i].ip=1;p[i-11].ip=1;
		          	p[i-2].ip=1;p[i-1].ip=1;
		          	p[i+1].ip=1;p[i+2].ip=1;
		          	p[i-19].ip=1;p[i+11].ip=2;
		          	p[i-21].ip=1;p[i-20].ip=1;
		          	p[i].setBackground(Color.blue);
		          	p[i-11].setBackground(Color.blue);
		          	p[i-2].setBackground(Color.blue);
		          	p[i-1].setBackground(Color.blue);
		          	p[i+1].setBackground(Color.blue);
		          	p[i+2].setBackground(Color.blue);
		          	p[i-21].setBackground(Color.blue);
		          	p[i+11].setBackground(Color.blue);
		          	p[i-23].setBackground(Color.blue);
		          	p[i-22].setBackground(Color.blue);
		          	planes++;
		        	}
		    	
		    	break;
		    }
		    case 3:
		        {
		          if (r <=9&&r>=4&&l<=9&&l>=3&&p[i].ip==0&&p[i-1].ip==0&&p[i+11].ip==0&&
		          p[i+22].ip==0&&p[i-11].ip==0&&p[i-22].ip==0&&p[i+1].ip==0&&p[i+2].ip==0&&
		          p[i-9].ip==0&&p[i+13].ip==0){
		          	p[i].ip=1;p[i-11].ip=1;
		          	p[i+22].ip=1;p[i-1].ip=2;
		          	p[i+1].ip=1;p[i+2].ip=1;
		          	p[i-9].ip=1;p[i+11].ip=1;
		          	p[i+13].ip=1;p[i-22].ip=1;
		          	p[i].setBackground(Color.blue);
		          	p[i-1].setBackground(Color.blue);
		          	p[i+11].setBackground(Color.blue);
		          	p[i+22].setBackground(Color.blue);
		          	p[i-11].setBackground(Color.blue);
		          	p[i-22].setBackground(Color.blue);
		          	p[i+1].setBackground(Color.blue);
		          	p[i+2].setBackground(Color.blue);
		          	p[i-9].setBackground(Color.blue);
		          	p[i+13].setBackground(Color.blue);
		          	planes++;
		        	}
		    	
		    	break;
		    }
		    case 4:
		        {
		          if (r <=9&&r>=4&&l<=10&&l>=4&&p[i].ip==0&&p[i-11].ip==0&&p[i-2].ip==0&&
		          p[i-22].ip==0&&p[i+1].ip==0&&p[i-1].ip==0&&p[i+9].ip==0&&p[i+11].ip==0&&
		          p[i-13].ip==0&&p[i+22].ip==0){
		          	p[i].ip=1;p[i-11].ip=1;
		          	p[i-2].ip=1;p[i-22].ip=1;
		          	p[i+9].ip=1;p[i+11].ip=1;
		          	p[i-13].ip=1;p[i+22].ip=1;
		          	p[i+1].ip=2;p[i-1].ip=1;
		          	p[i].setBackground(Color.blue);
		          	p[i-11].setBackground(Color.blue);
		          	p[i-2].setBackground(Color.blue);
		          	p[i-22].setBackground(Color.blue);
		          	p[i+1].setBackground(Color.blue);
		          	p[i-1].setBackground(Color.blue);
		          	p[i+9].setBackground(Color.blue);
		          	p[i+11].setBackground(Color.blue);
		          	p[i-13].setBackground(Color.blue);
		          	p[i+22].setBackground(Color.blue);
		          	planes++;
		        	}
		    	
		    	break;	
		    	}		
		}
	  }
	});
	  	}
	}
	
	
	class mypanel extends JButton{
		public int ip;
		public int ic;
		mypanel(){
		  setBorder(new LineBorder(Color.black,1));
		  setBackground(Color.lightGray);
		}
	}
	
	
	
 class ShootClient implements Runnable{
    public ShootClient() {
    	if (!(svr==null)) return;
    	if (!(chats==null)) return;
      try{
      	svr=new ServerSocket(5000);
      	chats=new ServerSocket(6000);
     
      	}catch(Exception a){
      		textarea.append("\n创建连接出错!");}
      }
 
    public void run(){
    	if (!(clt==null)) return;
    	if (!(chat==null)) return;
        try{
	 			
	 			 System.out.println("等待连接......");
	 			 textarea.append("\n等待连接.....");
	 			try{
	 				chat=chats.accept();
	 				clt=svr.accept();
	 				textarea.append("\n连接请求来自:"+clt.getInetAddress());
	 				inchat = new BufferedReader(new InputStreamReader(chat.getInputStream()));
	 				
	 				    outchat =new PrintWriter(chat.getOutputStream());
	 				  
	 				 in = new BufferedReader(new InputStreamReader(clt.getInputStream()));
	 				    out =new PrintWriter(clt.getOutputStream());
	 				    String str=in.readLine();
	 					if (str.equals("ready")){
	 						textarea.append("\n游戏开始!");
	 						out.println("ready");
	 						out.flush();}
	 				
	 				while(true){
	 				
	 					int Bullet=Integer.parseInt(inchat.readLine());
	 					if (Bullet==500) 
	 					{
	 						textarea.append("\n新一局开始请设置飞机!");
	 						 for (int i=0;i<121;i++)
						       {
						       	p[i].ip=0;
						       	p[i].setBackground(Color.lightGray);
						       	q[i].setBackground(Color.lightGray);
						       	hurt=0;
					         	shoot=4;
						        planes=0;
						        count=0;
						       	}
						       	continue;		
	 						}
	 					outchat.println(Integer.toString(p[Bullet].ip));
	 					outchat.flush();
	 					shoot++;
	 					if (p[Bullet].ip==2){hurt++;}
	 					if (hurt>=3){
	 						textarea.append("\n您输了!!");
	 						}
							
	 					      }
	 									      		
	         	}catch(Exception a){
	         		textarea.append("\n连接出错!!");}
	 		}catch(Exception a){
	 			textarea.append("\n无法创建服务器!");}
     }
  }

class chatClient implements Runnable{
    public chatClient() {
    	if (!(chatserver==null)) return;
      try{
      	chatserver=new ServerSocket(5500);
     
      	}catch(Exception a){
      		textarea.append("\n创建连接出错!");}
      }
 
    public void run(){
    	
    	if (!(chatSocket==null)) return;
       
	 			try{
	 				chatSocket=chatserver.accept();
	 				inchatSocket = new BufferedReader(new InputStreamReader(chatSocket.getInputStream()));
	 				
	 				    outchatSocket =new PrintWriter(chatSocket.getOutputStream());	 					 				
	 				while(true){	 				
	 					textarea.append("\n"+inchatSocket.readLine());
	 					}
	 				
	         	}catch(Exception a){
	         		textarea.append("\n连接出错!!");}
	 		
     }
  }


	public static void main(String args[]) {
		System.out.println("Starting Shootplaneserver...");
		Shootplaneserver mainFrame = new Shootplaneserver();
		mainFrame.setSize(900, 700);
		mainFrame.setTitle("Shootplaneserver");
		mainFrame.setVisible(true);
	}
}

⌨️ 快捷键说明

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