Ejemplo n.º 1
0
 private void main_timer_Tick(object sender, EventArgs e)
 {
     if (start)
     {
         main_timer.Interval = speed;
         int result = SnackMove();
         SocketClient1.Send(Encoding.Default.GetBytes(GetScreenString()));
         SocketClient2.Send(Encoding.Default.GetBytes(GetScreenString()));
         if (result == 0)
         {
             //p0
             main_timer.Stop();
             SocketClient1.Send(Encoding.Default.GetBytes("`end,lose,"));
             SocketClient2.Send(Encoding.Default.GetBytes("`end,win,"));
         }
         else if (result == 1)
         {
             //p1
             main_timer.Stop();
             SocketClient1.Send(Encoding.Default.GetBytes("`end,win,"));
             SocketClient2.Send(Encoding.Default.GetBytes("`end,lose,"));
         }
         else if (result == 2)
         {
             main_timer.Stop();
             SocketClient1.Send(Encoding.Default.GetBytes("`end,fare,"));
             SocketClient2.Send(Encoding.Default.GetBytes("`end,fare,"));
         }
     }
 }
Ejemplo n.º 2
0
 private bool breakConnection()
 {
     try
     {
         if (null != SocketClient1 && SocketClient1.Connected == true)
         {
             SocketClient1.Shutdown(SocketShutdown.Both);
             SocketClient1.Close();
         }
         if (null != SocketClient2 && SocketClient2.Connected == true)
         {
             SocketClient2.Shutdown(SocketShutdown.Both);
             SocketClient2.Close();
         }
         if (null != SocketServer && SocketServer.Connected == true)
         {
             SocketServer.Shutdown(SocketShutdown.Both);
             SocketServer.Close();
         }
         return(true);
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.ToString());
         return(false);
     }
 }
Ejemplo n.º 3
0
        public void ReceiveData2()
        {
            SocketClient2 = SocketServer.Accept();
            SocketClient2.Send(Encoding.Default.GetBytes("`joinSuccess,1,"));
            log_LB.Items.Add("p1:joinSuccess,1,");
            try
            {
                do
                {
                    if (SocketClient2.Receive(data2) > 0)                       //checking received data != 0
                    {
                        string[] commands = Encoding.Default.GetString(data2).Trim().Split('`');
                        Array.Clear(data2, 0, data2.Length);
                        for (int i = 1; i < commands.Length; i++)
                        {
                            log_LB.Items.Add("p1:" + commands[i]);
                            string[] line = commands[i].Split(',');

                            if (line[0] == "color")
                            {
                                int temp = int.Parse(line[1]);
                                if (p0.SnackColor == temp)
                                {
                                    SocketClient2.Send(Encoding.Default.GetBytes("`color,false,"));
                                    log_LB.Items.Add("p1:color,false");
                                }
                                else
                                {
                                    SocketClient2.Send(Encoding.Default.GetBytes("`color,true,"));
                                    log_LB.Items.Add("p1:color,true");
                                    p1.SnackColor = temp;
                                }
                            }
                            else if (line[0] == "ready")
                            {
                                p1r = true;
                                if (p0r && p1r)
                                {
                                    SocketClient1.Send(Encoding.Default.GetBytes("`colorset," + p1.SnackColor + ","));
                                    log_LB.Items.Add("p0:colorset," + p1.SnackColor);
                                    SocketClient2.Send(Encoding.Default.GetBytes("`colorset," + p0.SnackColor + ","));
                                    log_LB.Items.Add("p1:colorset," + p0.SnackColor);
                                    start = true;
                                }
                            }
                            else if (line[0] == "way")
                            {
                                if (line[1] == "w")
                                {
                                    ChangeWay(p1, Player.Way.Up);
                                }
                                else if (line[1] == "a")
                                {
                                    ChangeWay(p1, Player.Way.Left);
                                }
                                else if (line[1] == "s")
                                {
                                    ChangeWay(p1, Player.Way.Down);
                                }
                                else if (line[1] == "d")
                                {
                                    ChangeWay(p1, Player.Way.Right);
                                }
                            }
                        }
                    }
                    else
                    {
                        SocketClient1.Close();
                        SocketClient2.Close();
                        SocketServer.Close();
                        IsConnected = false;
                        log_LB.Items.Add("close server");
                    }
                } while (true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }