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

📄 ycgdiamonds.java

📁 一个经典的小游戏
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/*
 * Created on 2003-11-16
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */

/**
 * @author 杨传根
 * Email ycg01@software.nju.edu.cn
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
import java.applet.*;

//定义所有的公用变量
class YcgPublic{
	public static final Color BackColor=new Color(136,245,236);//俄罗斯方块图背景色
	public static final Color BackcolorDiamonds=new Color(116,225,236);//整体背景色
	public static final int PICTURE_NUM=300;//随机生成的图片序列
	public static final int POINT_NUM=137;//随机生成的点列,用于此消彼长
	public static final int SIZEI=24;//定义棋盘大小竖*********************
	public static final int SIZEJ=10;//定义棋盘大小横 *********************
	public static final int SPEED=800;
	public static final Font Font_Caiyue=new Font("华文彩云",Font.BOLD,20);
	public static final Font Font_Title=new Font("华文彩云",Font.BOLD,35);
}


//用来记录每一幅生成图的所有信息
class MyImage{
		public int x1,y1;
		public int x2,y2;
		public int x3,y3;
		public int x4,y4;
		public int color;
		public MyImage(int i1,int j1,int i2,int j2,int i3,int j3,int i4,int j4,int color)
		{
			x1=i1;x2=i2;x3=i3;x4=i4;y1=j1;y2=j2;y3=j3;y4=j4;this.color=color;
		}
		public void SetMyImage(int i1,int j1,int i2,int j2,int i3,int j3,int i4,int j4,int color)
		{
			x1=i1;x2=i2;x3=i3;x4=i4;y1=j1;y2=j2;y3=j3;y4=j4;this.color=color;
		}
		public Object clone()
		{
			return new MyImage(x1,y1,x2,y2,x3,y3,x4,y4,color);
		}
}


//棋子设计,每一个方格相当于一个色块
class ChessButton extends JPanel{
		int BLANK=0,BLUE=1,GREEN=2,RED=3,PINK=4,YELLOW=5,PURPLE=6,BLACK=7;//棋子颜色

		public int i=0,j=0;//位置
		public int color=0;//颜色
		public ChessButton(int i,int j,int color){
			this.i=i;
			this.j=j;
			this.color=color;
		}
  		//绘图
		public void paint(Graphics g) {
			
    		int x1 = 0;
   	 		int y1 = 0;
    		int x2 = getWidth();
    		int y2 = getHeight();

			
    		{
   				g.setColor(YcgPublic.BackcolorDiamonds);
   				g.fillRect(x1,y1,x2,y2);
   			}
    		if(color==BLUE) {
    			g.setColor(new Color(3,45,209));
    			g.drawRect(x1+1, y1+1, x2-2, y2-2);
     			g.fill3DRect(x1+4, y1+4, x2-7, y2-7,true);
   			}
   			else if(color==GREEN){
   				g.setColor(new Color(223,100,26));
   				g.drawRect(x1+1, y1+1, x2-2, y2-2);
     			g.fill3DRect(x1+4, y1+4, x2-7, y2-7,true);
   			}
   			else if(color==RED){
   				g.setColor(new Color(200,46,13));
   				g.drawRect(x1+1, y1+1, x2-2, y2-2);
     			g.fill3DRect(x1+4, y1+4, x2-7, y2-7,true);
   			}
   			else if(color==PINK){
   				g.setColor(new Color(190,23,200));
   				g.drawRect(x1+1, y1+1, x2-2, y2-2);
     			g.fill3DRect(x1+4, y1+4, x2-7, y2-7,true);
   			}
   			else if(color==YELLOW){
   				g.setColor(new Color(224,171,12));
   				g.drawRect(x1+1, y1+1, x2-2, y2-2);
     			g.fill3DRect(x1+4, y1+4, x2-7, y2-7,true);
   			}
   			else if(color==PURPLE){
   				g.setColor(new Color(120,33,180));
   				g.drawRect(x1+1, y1+1, x2-2, y2-2);
     			g.fill3DRect(x1+4, y1+4, x2-7, y2-7,true);
   			}
   			else if(color==BLACK){
   				g.setColor(new Color(69,41,52));
   				g.drawRect(x1+1, y1+1, x2-2, y2-2);
     			g.fill3DRect(x1+4, y1+4, x2-7, y2-7,true);
   			}
   		}//end of paint()
}//end of class ChessButton

//便于以后对次面版扩展
class MyJPanel extends JPanel{
		public void paintComponent(Graphics g) {

    		int x1 = 0;
   	 		int y1 = 0;
    		int x2 = getWidth();
    		int y2 = getHeight();
    		g.setColor(YcgPublic.BackcolorDiamonds);
    		g.fillRect(x1, y1, x2, y2);
    		g.setColor(Color.red);
    		g.drawLine(x2-1,0,x2-1,y2);
    	}
}

//做为网络游戏服务器的主类
class NetDiamonds extends JFrame{
	Player players[];//用户数量
	ServerSocket server;//服务器socket
	JTextArea outputArea;//提示信息显示
	boolean begin=false;//游戏开始标记
	Color BackColor=YcgPublic.BackColor;//background
	int num=0;//人数记录
	JButton button;//开始按键

	final int PICTURE_NUM=YcgPublic.PICTURE_NUM;
	int picture[]=new int[PICTURE_NUM];//记录随机生成的图片序列
	final int POINT_NUM=YcgPublic.POINT_NUM;
	int random_point[]=new int[POINT_NUM];//记录随机生成的点序列

	public NetDiamonds(int number,boolean is_server,int port1,int port2,InetAddress send_address)
	{
		super("YCG Server");
		this.setLocation(5,5);
		if(is_server)
		{
			players=new Player[number];
			if(number!=3)//如果是两人游戏,在开端口port1,是三人的开端口port1+1
			{
				try{
				server=new ServerSocket(port1,number);
				}
				catch(Exception e)
				{
					JOptionPane.showMessageDialog(null,"服务器出错\n请检查网络和端口是否冲突");
					System.exit(0);
				}
			}
			else
			{
				try{
				server=new ServerSocket(port1+1,number);
				}
				catch(Exception e)
				{
					JOptionPane.showMessageDialog(null,"服务器出错\n请检查网络和端口是否冲突");
					System.exit(0);
				}
			}
			
			outputArea=new JTextArea();
			getContentPane().add(outputArea,BorderLayout.CENTER);
			getContentPane().setBackground(BackColor);
			outputArea.setBackground(BackColor);
			outputArea.setText("请开启服务器\n");
			button=new JButton("开启服务器");
			button.setBackground(BackColor);
			getContentPane().add(button,BorderLayout.SOUTH);
			
			button.addActionListener(
				new ActionListener()
				{
					public void actionPerformed(ActionEvent event)
					{
						display("开始连接");
						button.setEnabled(false);
						new MyThread().start();
					}
				}
			);
			
			getContentPane().setBackground(BackColor);
			setSize(200,200);
			setVisible(true);
		}
		else//如果不作为服务器,则弹出客户端窗口
		{
			new NetGame(number,port2,send_address);
		}

	}//end of construct
	
	//用线程做用户连接,可避免一个线程做时的死等情况
	class MyThread extends Thread{
		MyThread()
		{
		}
		public void run()
		{
			while(num<players.length)
			{
				execute();
			}
		}
	}
	public void execute()
	{

		//等待用户连接
		for(int i=0;i<players.length;i++)
		{
			try{
				players[i]=new Player(server.accept(),i);
				display(i+"已经连接一位");
			}
			catch(Exception e){
				JOptionPane.showMessageDialog(null,"服务器工作出错");
				System.exit(0);
			}
			//System.out.println(i);
		}
		begin=true;//游戏开始
		display("连接完毕");
		try{
			DoRun();
		}
		catch(Exception e){
			JOptionPane.showMessageDialog(null,"服务器工作出错");
			System.exit(0);
		};
		//System.out.println(begin);
	}
	
	//提示信息的显示
	public void display(String message)
	{
		outputArea.append(message+"\n");
	}
	
	//玩家的类
	private class Player{
		Socket connection;
		BufferedInputStream input;
		BufferedOutputStream output;
		int playerNumber;

		public Player(Socket socket,int number)
		{
			playerNumber=number;
			connection=socket;
			try{
				input=new BufferedInputStream(connection.getInputStream());
				output=new BufferedOutputStream(connection.getOutputStream());
			}
			catch(Exception e)
			{
				JOptionPane.showMessageDialog(null,"服务器工作出错");
				System.exit(0);
			}
		}

	}


	//随机生成一系列的公用数据,以便发送
	public void Initial()
	{
		for(int i=0;i<PICTURE_NUM;i++)
			picture[i]=(int)(Math.random()*100)%7+1;
		for(int i=0;i<POINT_NUM;i++)
		{
			random_point[i]=(int)(Math.random()*100)%2;
			if((i+3)%10==0)
				random_point[i]=0;
			if(random_point[i]!=0)
				random_point[i]=(int)(Math.random()*100)%7+1;
		}

	}

	public void DoRun()
	{
		Initial();
		for(num=0;num<players.length;num++)
		{
			try{
				for(int i=0;i<PICTURE_NUM;i++)
				{
					players[num].output.write(picture[i]);
					players[num].output.flush();
				}
				for(int i=0;i<POINT_NUM;i++)
				{
					players[num].output.write(random_point[i]);
					players[num].output.flush();
				}
			}
			catch(Exception e)
			{
				JOptionPane.showMessageDialog(null,"服务器出错");
				System.exit(0);
			}
		}

		Game1 gamebegin1=new Game1();
		Game2 gamebegin2=new Game2();
		if(players.length==3)
		{
			three_run=true;
			Game3 gamebegin3=new Game3();
			gamebegin3.start();
		}
		gamebegin1.start();
		gamebegin2.start();

	}
	
	int temp1=0;
	boolean three_run=false;//记录三号是否打开
	private class Game1 extends Thread{
		public void run()
		{
			try{
				sleep(1000);
			}catch(Exception e){};
			while(true)
			{
				try{
				temp1=players[0].input.read();
				}catch(Exception e)
				{
					JOptionPane.showMessageDialog(null,"服务器出错");
					System.exit(0);
				}
				synchronized(this){
				try{
				//System.out.println("jj");
				players[0].output.write(temp1);
				players[1].output.write(temp1);
				if(three_run)
				{
					players[2].output.write(temp1);
					players[2].output.flush();
				}
				players[0].output.flush();
				players[1].output.flush();
				}
				catch(Exception e)
				{
					JOptionPane.showMessageDialog(null,"服务器出错");
					System.exit(0);
				}
				}
			}
		}
	}

	int temp2=0;
	private class Game2 extends Thread{
		public void run()
		{
			try{
				sleep(1000);
			}catch(Exception e){};
			while(true)
			{

				try{
				 temp2=players[1].input.read();
					}catch(Exception e)
				{
					JOptionPane.showMessageDialog(null,"服务器出错");
					System.exit(0);
				}
				synchronized(this){
				try{

				players[0].output.write(temp2+100);
				players[1].output.write(temp2+100);
				if(three_run)
				{
					players[2].output.write(temp2+100);
					players[2].output.flush();
				}
				players[0].output.flush();
				players[1].output.flush();
				}
				catch(Exception e)
				{
					JOptionPane.showMessageDialog(null,"服务器出错");

				}
				}
			}
		}
	}
	int temp3=0;
	private class Game3 extends Thread{
		public void run()
		{
			try{
				sleep(1000);
			}catch(Exception e){};
			while(true)
			{

				try{
				temp3=players[2].input.read();
				}catch(Exception e)
				{
					JOptionPane.showMessageDialog(null,"服务器出错");
					System.exit(0);
				}
				synchronized(this){
				try{
				//System.out.println("jj");

				players[0].output.write(temp3+200);
				players[0].output.flush();
				players[1].output.write(temp3+200);
				players[1].output.flush();
				players[2].output.write(temp3+200);
				players[2].output.flush();

				}
				catch(Exception e)
				{
					JOptionPane.showMessageDialog(null,"服务器出错");
					System.exit(0);
				}
				}
			}
		}
	}
}//end of netDiamonds


class NetGame extends JFrame{

	final int SIZEI=YcgPublic.SIZEI;//定义棋盘大小横 *********************
	final int SIZEJ=YcgPublic.SIZEJ;//定义棋盘大小竖 *********************
	final int SPEED=YcgPublic.SPEED;//速度***************************
	final int BLANK=0,BLUE=1,GREEN=2,RED=3,PINK=4,YELLOW=5,PURPLE=6,BLACK=7;//棋子颜色
	
	InetAddress address;
	int port;
	
	int  list_key=0;//键盘事件缓存
	final int PICTURE_NUM=YcgPublic.PICTURE_NUM;
	int picture[]=new int[PICTURE_NUM];//记录随机生成的图片序列
	int picture_key=0;
	final int POINT_NUM=YcgPublic.POINT_NUM;
	int random_point[]=new int[POINT_NUM];//记录随机生成的点序列
	int random_key=0;
	
	boolean flag1_fail=false;//记录三人是否都已结束,如果1和2都结束了,则游戏重新开始
	boolean flag2_fail=false;
	boolean flag3_fail=false;
	
	Color BackcolorDiamonds=YcgPublic.BackcolorDiamonds;//背景色
	Color Backcolor=YcgPublic.BackColor;

	User user2=new User(10,80,38,32,37,39,40);
	User user1=new User(110,180,138,132,137,139,140);//默认的用户类,参数为按键
	User user3=new User(210,280,238,232,237,239,240);
	
	LinkedList picture1=new LinkedList();//缓存图片
	LinkedList picture2=new LinkedList();//缓存图片

⌨️ 快捷键说明

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