📄 ycgdiamonds.java
字号:
}
if(user3.flag_random)
{
MyImage tt3=(MyImage)picture3.removeFirst();
user3.flag_random=false;
user3.tempMyImage.SetMyImage(tt3.x1,tt3.y1,tt3.x2,tt3.y2,tt3.x3,tt3.y3,tt3.x4,tt3.y4,tt3.color);
if(user3.diamonds_panel[2][5]!=0)
{
user3.diamonds_panel[2][6]=1;
result.setForeground(new Color(43,43,255));
result.setText("3已结束");
}
}
MyImage t_temp1=(MyImage)picture1.getFirst();
MyImage t_temp2=(MyImage)picture2.getFirst();
MyImage t_temp3=(MyImage)picture3.getFirst();
for(int i=1;i<=4;i++)
for(int j=1;j<=4;j++)
{
pp_cb1[i][j].color=BLANK;
pp_cb2[i][j].color=BLANK;
pp_cb3[i][j].color=BLANK;
}
pp_cb1[t_temp1.x1][t_temp1.y1-2].color=t_temp1.color;
pp_cb1[t_temp1.x2][t_temp1.y2-2].color=t_temp1.color;
pp_cb1[t_temp1.x3][t_temp1.y3-2].color=t_temp1.color;
pp_cb1[t_temp1.x4][t_temp1.y4-2].color=t_temp1.color;
pp_cb2[t_temp2.x1][t_temp2.y1-2].color=t_temp2.color;
pp_cb2[t_temp2.x2][t_temp2.y2-2].color=t_temp2.color;
pp_cb2[t_temp2.x3][t_temp2.y3-2].color=t_temp2.color;
pp_cb2[t_temp2.x4][t_temp2.y4-2].color=t_temp2.color;
pp_cb3[t_temp3.x1][t_temp3.y1-2].color=t_temp3.color;
pp_cb3[t_temp3.x2][t_temp3.y2-2].color=t_temp3.color;
pp_cb3[t_temp3.x3][t_temp3.y3-2].color=t_temp3.color;
pp_cb3[t_temp3.x4][t_temp3.y4-2].color=t_temp3.color;
pp1.repaint();
pp2.repaint();
pp3.repaint();
}
private Socket connection;
private BufferedInputStream input;
private BufferedOutputStream output;
//用线程类开启游戏
class GameStart extends Thread{
public void run(){
while(!successful)
{
Gamestart();//当连接成功时successful会改变为true
{
user1.CreatDiamondsPanel();
user1.RandomMyImage();
user1.ReDraw();
}
{
user3.CreatDiamondsPanel();
user3.RandomMyImage();
user3.ReDraw();
}
net_state.setText("开始游戏");
user2.diamonds_thread=user2.new Diamonds();
user2.diamonds_thread.start();
}
};
}
boolean successful=false;
public void Gamestart()
{
while(true)//连接成功,break退出,不成功,抛出继续连接
{
try{
connection=new Socket(address,port);
successful=true;
input=new BufferedInputStream(connection.getInputStream());
output=new BufferedOutputStream(connection.getOutputStream());
break;
}
catch(IOException ioException){
ioException.printStackTrace();
continue;
}
}
net_state.setText("已经连接,等待其他玩家");
Game();//建立连接完成时,开始游戏
//System.out.println("jj");
}
public void Game(){
{
picture_key=0;
random_key=0;
try{
for(int i=0;i<PICTURE_NUM;i++)//先从服务端将游戏所需信息全部预先下载
{
picture[i]=input.read();
}
for(int i=0;i<POINT_NUM;i++)
{
random_point[i]=input.read();
}
}
catch(IOException ioException){
ioException.printStackTrace();
}
PictureCreat();
transmit=new Transmit();
transmit.start();
}
}
//该线程专门用于从网络接受数据,一有数据就马上处理
Transmit transmit;
class Transmit extends Thread{
public void run()
{
while(true)
{
try{
synchronized(this){
list_key=input.read();
DealWithKey();
}
//System.out.println("jj");
}catch(Exception e){};
}
}
}
}
//NetDiamondsWelcome window
class NetDiamondsWelcome extends JFrame{
Color BackColor=YcgPublic.BackColor;//background
YcgDiamonds ycg_diamonds;//the welcome window
NetDiamondsWelcome game_window;//the net_game window
JTextField server_address;
JTextField client_address;
JTextField server_port;
JTextField client_port;
JButton server_two;
JButton client_two;
JButton server_three;
JButton client_three;
String send_port="";
String send_address_string="";
//judge if have noclash ,return true if there is no clash
//the three to sent to the game
InetAddress send_address;
int port1;
int port2;
boolean NoClash()
{
// System.out.println("**");
try{
send_address=InetAddress.getByName(client_address.getText());
port1=Integer.parseInt(server_port.getText());
port2=Integer.parseInt(client_port.getText());
if(port1<1||port1>60000||port2<0||port2>60000)
throw new Exception();
}
catch(Exception e)
{
//System.out.println("youwu");
return false;
}
//System.out.println("wu wu");
return true;
}
//construct
public NetDiamondsWelcome(YcgDiamonds ycg)
{
super("网络版俄罗斯方块");
ycg_diamonds=ycg;
this.setSize(600,300);
this.setVisible(true);
this.setLocation(200,200);
Container ctn=this.getContentPane();
ctn.setBackground(BackColor);
ctn.setLayout(new GridLayout(5,2));
JPanel jp[]=new JPanel[10];
for(int i=0;i<10;i++)
{
jp[i]=new JPanel();
jp[i].setBackground(BackColor);
ctn.add(jp[i]);
}
jp[0].add(new JLabel("本机地址"));
jp[1].add(server_address=new JTextField(10));
server_address.setEditable(false);
jp[1].add(new JLabel("端口"));
jp[1].add(server_port=new JTextField(4));
server_address.setBackground(BackColor);
server_port.setBackground(BackColor);
server_port.setText("825");
server_address.setText("");
try{
InetAddress address=InetAddress.getByName(null);
server_address.setText(address.getHostAddress());
}catch(Exception e){};
jp[2].add(new JLabel("服务器地址"));
jp[3].add(client_address=new JTextField(10));
jp[3].add(new JLabel("端口"));
jp[3].add(client_port=new JTextField(4));
client_address.setBackground(BackColor);
client_port.setBackground(BackColor);
client_port.setText("825");
jp[4].add(new JLabel("两人对战"));
jp[5].add(new JLabel("三人对战"));
jp[6].add(server_two=new JButton("作为服务端"));
jp[7].add(server_three=new JButton("作为服务端"));
jp[8].add(client_two=new JButton("作为客户端"));
jp[9].add(client_three=new JButton("作为客户端"));
server_two.setBackground(BackColor);
server_three.setBackground(BackColor);
client_two.setBackground(BackColor);
client_three.setBackground(BackColor);
this.addWindowListener(
new WindowAdapter(){
//close window event
public void windowClosing(WindowEvent event)
{
ycg_diamonds.setVisible(true);
setVisible(false);
}//end of window event
}
);
server_two.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event)
{
if(NoClash())
{
server_two.setEnabled(false);
server_three.setEnabled(false);
client_three.setEnabled(false);
NetDiamonds game_window=new NetDiamonds(2,true,port1,port2,send_address);
}
else
{
JOptionPane.showMessageDialog(
null,"设置有误,请重新设置");
}
}
}
);
client_two.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event)
{
if(NoClash())
{
NetDiamonds game_window=new NetDiamonds(2,false,port1,port2,send_address);
}
else
{
JOptionPane.showMessageDialog(
null,"设置有误,请重新设置");
}
}
}
);
server_three.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event)
{
if(NoClash())
{
server_two.setEnabled(false);
server_three.setEnabled(false);
client_two.setEnabled(false);
NetDiamonds game_window=new NetDiamonds(3,true,port1,port2,send_address);
}
else
{
JOptionPane.showMessageDialog(
null,"设置有误,请重新设置");
}
}
}
);
client_three.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event)
{
if(NoClash())
{
NetDiamonds game_window=new NetDiamonds(3,false,port1,port2,send_address);
}
else
{
JOptionPane.showMessageDialog(
null,"设置有误,请重新设置");
}
}
}
);
}//end of construct
}//end of class NetDiamondsWelcome
class SingleDiamonds extends JFrame{
Color Backcolor=YcgPublic.BackColor;
int PICTURE_NUM=YcgPublic.PICTURE_NUM;
int POINT_NUM=YcgPublic.POINT_NUM;
int SIZEI=YcgPublic.SIZEI;
int SIZEJ=YcgPublic.SIZEJ;
int state=0;//游戏的模式
int picture_key=0;//记录图片指针
int random_key=0;//点列指针
int BLANK=0,BLUE=1,GREEN=2,RED=3,PINK=4,YELLOW=5,PURPLE=6,BLACK=7;//棋子颜色
int SPEED=YcgPublic.SPEED;
int picture[]=new int[PICTURE_NUM];//记录随机生成的图片序列
int random_point[]=new int[POINT_NUM];//记录随机生成的点序列
LinkedList list_key=new LinkedList();//键盘事件缓存
User user1;//默认的用户类,参数为按键
User user2;
LinkedList picture1=new LinkedList();//缓存图片//目的是使得两者出现的图象序列是相同的,以示公平
LinkedList picture2=new LinkedList();//缓存图片
JLabel begin,end;
JLabel num1=new JLabel("");
JLabel num2=new JLabel("");
JLabel result=new JLabel("");
JLabel kind=new JLabel("");
JPanel pp1=new JPanel();//显示下一个图片的预览窗口
JPanel pp2=new JPanel();
ChessButton pp_cb1[][]=new ChessButton[5][5];
ChessButton pp_cb2[][]=new ChessButton[5][5];
//construct
public SingleDiamonds(int user1_up,int user1_down,int user1_left,int user1_right,int user1_bottom,
int user2_up,int user2_down,int user2_left,int user2_right,int user2_bottom){
setSize(800,560);
this.setLocation(40,40);
user1=new User(10,80,user1_up,user1_down,user1_left,user1_right,user1_bottom);//默认的用户类,参数为按键
user2=new User(10,80,user2_up,user2_down,user2_left,user2_right,user2_bottom);
Container cph=getContentPane();
cph.setBackground(Backcolor);
Container cp=new JPanel();
//this.setResizable(false);
cp.setLayout(new GridLayout(1,5));
//最左边部分
JPanel jp2=new JPanel();
jp2.setLayout(new GridLayout());
jp2.add(user1.chessView);
cp.add(jp2);
//中间部分
JPanel jpp=new JPanel();
jpp.setLayout(new GridLayout(8,1));
pp1.setLayout(new GridLayout(4,4));//小窗口
pp2.setLayout(new GridLayout(4,4));
for(int i=1;i<=4;i++)
for(int j=1;j<=4;j++)
{
pp1.add(pp_cb1[i][j]=new ChessButton(i,j,BLANK));
}
for(int i=1;i<=4;i++)
for(int j=1;j<=4;j++)
{
pp2.add(pp_cb2[i][j]=new ChessButton(i,j,BLANK));
}
JPanel pp=new JPanel();
pp.setLayout(new GridLayout());
pp.add(pp1);
pp.add(new JLabel(""));
pp.add(pp2);
JPanel pppp=new JPanel();
pppp.add(pp);
jpp.add(pppp);
JPanel p00=new JPanel();
JPanel p0=new JPanel();
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
JPanel p5=new JPanel();
p00.setBackground(Backcolor);
p0.setBackground(Backcolor);
p1.setBackground(Backcolor);
p2.setBackground(Backcolor);
p3.setBackground(Backcolor);
p4.setBackground(Backcolor);
p5.setBackground(Backcolor);
pp.setBackground(Backcolor);
pppp.setBackground(Backcolor);
JLabel label_t1=new JLabel("更换模式按R");
p0.add(label_t1);
p00.add(kind=new JLabel("单人版"));
p1.add(begin=new JLabel("开始按回车"));
p2.add(end=new JLabel("暂停按P"));
p3.add(num1=new JLabel("1消去"+user1.win_number+"分数:"+user1.mark));
p4.add(num2=new JLabel("2消去"+user2.win_number+"分数:"+user2.mark));
p5.add(result);
jpp.add(p0);
jpp.add(p00);
jpp.add(p1);
jpp.add(p2);
jpp.add(p3);
jpp.add(p4);
jpp.add(p5);
cp.add(jpp);
jpp.setBackground(Backcolor);
//最右边部分
JPanel jp6=new JPanel();
jp6.setLayout(new GridLayout());
jp6.add(user2.chessView);
cp.add(jp6);
//周边
cph.add(new JLabel(" "),BorderLayout.WEST);
cph.add(new JLabel(" "),BorderLayout.EAST);
cph.add(new JLabel(" "),BorderLayout.NORTH);
cph.add(new JLabel(" "),BorderLayout.SOUTH);
cph.add(cp,BorderLayout.CENTER);
OpenKey();//添加键盘事件
}//end of SingleDiamonds()
//用户类
class User{
int begin_key,pause_key,up_key,down_key,left_key,right_key,bottom_key;
ChessButton cb[][]=new ChessButton[SIZEI+1][SIZEJ+1];//棋子
JPanel chessView=new MyJPanel();//棋盘面版
boolean flag=true;//记录有无暂停
int speed=YcgPublic.SPEED;
int win_number=0;
int mark=0;
int diamonds_panel[][]=new int[SIZEI+2][SIZEJ+2];
Diamonds diamonds_thread;
int win_num;//当前成的行数
boolean flag_random=false;//是否请求缓存
MyImage tempMyImage=new MyImage(0,0,0,0,0,0,0,0,0);//记录正在被控制的图象的信息
//构造函数含有七个参数,分别记录按键事件
public User(int begin,int pause,int up,int down,int left,int right,int bottom)
{
begin_key=begin;
pause_key=pause;
up_key=up;
down_key=down;
left_key=left;
right_key=right;
bottom_key=bottom;
chessView.setLayout(new GridLayout(SIZEI-3,SIZEJ));
for(int i=1;i<=SIZEI;i++)
for(int j=1;j<=SIZEJ;j++)
{
cb[i][j]=new ChessButton(i,j,BLANK);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -