private void Window_KeyDown(object sender, KeyEventArgs e) { Send(e.Key.ToString(), "/l"); if (Common.IsHost) { if (e.Key.ToString().Contains(LetterBox.Text)) { Combo1++; ComboBox1.Text = Combo1.ToString(); Score1 += 2 * Combo1; ScoreBox1.Text = Score1.ToString(); Margin_Letter = 0; InitPicTimer1(); LetterBox.Text = NormArr[GenerateNext()]; EnemyImg.Margin = new Thickness(0, 0, 0, 350); } else { Combo1 = 0; ComboBox1.Text = Combo1.ToString(); } } else { if (e.Key.ToString().Contains(LetterBox.Text)) { Combo2++; ComboBox2.Text = Combo2.ToString(); Score2 += 2 * Combo2; ScoreBox2.Text = Score2.ToString(); InitPicTimer2(); Margin_Letter = 0; LetterBox.Text = NormArr[GenerateNext()]; EnemyImg.Margin = new Thickness(0, 0, 0, 350); } else { Combo2 = 0; ComboBox2.Text = Combo2.ToString(); } } }
private void Receive() { byte[] buffer = new byte[1024]; while (true) { try { using (Socket listener = new Socket(localIp.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) { listener.Bind(localEP); listener.Listen(5); using (Socket socket = listener.Accept()) { int count = 0; count = socket.Receive(buffer); message = Encoding.UTF8.GetString(buffer, 0, count); if (message.Contains("/s") && message.Contains("1")) { Player1Start = true; Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart) delegate() { CheckStart(); }); } if (message.Contains("/s") && message.Contains("2")) { Player2Start = true; Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart) delegate() { CheckStart(); }); } if (message.Contains("/l")) { if (message.Contains(NormArr[next])) { if (Common.IsHost) { Combo2++; Score2 += Combo2 * 2; Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart) delegate() { ScoreBox2.Text = Score2.ToString(); ComboBox2.Text = Combo2.ToString(); Margin_Letter = 0; InitPicTimer2(); LetterBox.Text = NormArr[GenerateNext()]; EnemyImg.Margin = new Thickness(0, 0, 0, 350); }); } else { Combo1++; Score1 += Combo1 * 2; Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart) delegate() { ScoreBox1.Text = Score1.ToString(); ComboBox1.Text = Combo1.ToString(); Margin_Letter = 0; InitPicTimer1(); LetterBox.Text = NormArr[GenerateNext()]; EnemyImg.Margin = new Thickness(0, 0, 0, 350); }); } } else { if (Common.IsHost) { Combo2 = 0; Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart) delegate() { ComboBox2.Text = Combo2.ToString(); }); } else { Combo1 = 0; Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart) delegate() { ComboBox1.Text = Combo1.ToString(); }); } } } if (message.Contains("/p")) { Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart) delegate() { Margin_Letter = 0; timerMove.Stop(); timerPlay.Stop(); TimeBox.Text = ""; LetterBox.Text = ""; EnemyImg.Visibility = Visibility.Hidden; LetterBox.Visibility = Visibility.Hidden; EnemyImg.Margin = new Thickness(0, 0, 0, 350); Player1Start = false; Player2Start = false; }); } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }