private void DataReceiveCallback(String data) { Dispatcher.Invoke((Action)(() => { if (data.IndexOf("OK.") > -1) { if (play_page == null) { string a = data.Replace("OK.", ""); PlayerNumber = int.Parse(a); //サーバから送られてきたplayernumberをプレイページに送る。 // Application.Propertiesに設定しておく。 PN number = new PN() { playernumber = PlayerNumber }; Application.Current.Properties["Number"] = number; // 次に表示するページ(プレイ画面)を生成、以後使いまわし play_page = new Play_page(); play_page.clients = clients; } // プレイページへ移動 this.NavigationService.Navigate(play_page); } })); }
private async void Button_Click(object sender, RoutedEventArgs e) { var serverSocket = AsynchronousSocketListener.GetInstance(); // サーバーが実行中の場合 if (serverSocket.IsEnabled) { // スレッドを停止 await serverSocket.StopListeningThread(); } // サーバーの起動 serverSocket.StartListeningThread(); // 自分のPC名を取得 string myPC = Environment.MachineName; clients.DataReceiveCallback = Non; clients.Connect(myPC, 11000); clients.Receive(); clients.Send("CPU<EOF>"); PN number = new PN() { playernumber = PlayerNumber }; Application.Current.Properties["Number"] = number; play_page = new Play_page(); play_page.clients = clients; // プレイページへ移動 this.NavigationService.Navigate(play_page); }
private void Show_Click(object sender, RoutedEventArgs e) { //コントローラーを入れる場所を作る mogurahole targetbutton = Mogura1_1; // 押されたボタンの名前を取得 String buttonName = ((Button)sender).Name; // 押されたボタンによって、対象のイメージコントローラーを切り替える if (buttonName == "Show1") { targetbutton = Mogura1_1; } else if (buttonName == "Show2") { targetbutton = Mogura1_2; } else if (buttonName == "Show3") { targetbutton = Mogura1_3; } else if (buttonName == "Show4") { targetbutton = Mogura2_1; } else { targetbutton = Mogura1_1; } // クリック時のイベントを設定 targetbutton.AttackEvent = () => { Mogura_Click(); count1++; if (PlayerNumber == 1) { this.DataContext = new { Name1 = "プレイヤー1", Name2 = "プレイヤー2", Score1 = count1, Score2 = count2 } } ; else if (PlayerNumber == 2) { this.DataContext = new { Name1 = "プレイヤー2", Name2 = "プレイヤー1", Score1 = count1, Score2 = count2 } } ; // Application.Propertiesに設定しておく。 PN number = new PN() { playernumber = PlayerNumber }; PN C1 = new PN() { c1 = count1 }; PN C2 = new PN() { c2 = count2 }; Application.Current.Properties["Number"] = number; Application.Current.Properties["C1"] = C1; Application.Current.Properties["C2"] = C2; }; // モグラを表示 targetbutton.ShowMogura(); }
// ラグをなくすタイマーが発生した時のイベント public void tienTimerEventHandler(Object sender, EventArgs e) { // ラグをなくすタイマーを停止 tienTimer.IsEnabled = false; tienTimer.Stop(); if (PlayerNumber == 1) { try { clients.receiveDone.Reset(); clients.Send("<p1>" + count1.ToString() + "<score><EOF>"); //clients.receiveDone.WaitOne(); // Receive the clients.response from the remote device. if (clients.response.IndexOf("<score>") > -1) //score送信の部分 { clients.response = (clients.response.Replace("<score>", "")); var score = clients.response.Split(','); count2 = int.Parse(score[1].Replace("<EOF>", "")); } } catch (Exception ea) { Console.WriteLine(ea.ToString()); } } else if (PlayerNumber == 2) { try { clients.receiveDone.Reset(); clients.Send("<p2>" + count1.ToString() + "<score><EOF>"); //clients.receiveDone.WaitOne(); // Receive the clients.response from the remote device. if (clients.response.IndexOf("<score>") > -1) //score送信の部分 { clients.response = (clients.response.Replace("<score>", "")); var score = clients.response.Split(','); count2 = int.Parse(score[0].Replace("<EOF>", "")); } } catch (Exception ea) { Console.WriteLine(ea.ToString()); } // Application.Propertiesに設定しておく。 PN number = new PN() { playernumber = PlayerNumber }; PN C1 = new PN() { c1 = count1 }; PN C2 = new PN() { c2 = count2 }; Application.Current.Properties["Number"] = number; Application.Current.Properties["C1"] = C1; Application.Current.Properties["C2"] = C2; } if (result_page == null) { // 次に表示するページ(結果画面)を生成、以後使いまわし result_page = new Result_page(); } // 制限時間のタイマーを終了 timeupTimer.IsEnabled = false; timeupTimer.Stop(); // 結果ページへ移動 this.NavigationService.Navigate(result_page); }
public Play_page() { InitializeComponent(); //プレイヤー名の設定 PlayerNumber = ((PN)Application.Current.Properties["Number"]).playernumber; //if (PlayerNumber == 1) this.DataContext = new { Name1 = "プレイヤー1", Name2 = "プレイヤー2", Score1 = count1, Score2 = count2 }; //else if (PlayerNumber == 2) this.DataContext = new { Name1 = "プレイヤー2", Name2 = "プレイヤー1", Score1 = count1, Score2 = count2 }; playPageViewModel = new PlayPageViewModel() { Name1 = (PlayerNumber == 1 ? "プレイヤー1" : "プレイヤー2"), Name2 = (PlayerNumber == 1 ? "プレイヤー2" : "プレイヤー1"), Score1 = 0, Score2 = 0 }; this.DataContext = playPageViewModel; //モグラをRandomで出すタイマー RandomTimer = new DispatcherTimer(DispatcherPriority.Normal); RandomTimer.Interval = TimeSpan.FromMilliseconds(RS); RandomTimer.IsEnabled = false; RandomTimer.Tick += RandomTimerEventHandler; //モグラをRandomで出すタイマー2 Random2Timer = new DispatcherTimer(DispatcherPriority.Normal); Random2Timer.Interval = TimeSpan.FromMilliseconds(RS2); Random2Timer.IsEnabled = false; Random2Timer.Tick += Random2TimerEventHandler; //制限時間のタイマー timeup2Timer = new DispatcherTimer(DispatcherPriority.Normal); timeup2Timer.Interval = TimeSpan.FromMilliseconds(stop2); timeup2Timer.IsEnabled = false; timeup2Timer.Tick += timeup2TimerEventHandler; //制限時間前のタイマー timeupTimer = new DispatcherTimer(DispatcherPriority.Normal); timeupTimer.Interval = TimeSpan.FromMilliseconds(stop); timeupTimer.IsEnabled = false; timeupTimer.Tick += timeupTimerEventHandler; //ラグをなくすタイマー tienTimer = new DispatcherTimer(DispatcherPriority.Normal); tienTimer.Interval = TimeSpan.FromMilliseconds(tien); tienTimer.IsEnabled = false; tienTimer.Tick += tienTimerEventHandler; // 制限時間のタイマーを開始 timeupTimer.IsEnabled = true; timeupTimer.Start(); // 制限時間前のタイマーを開始 timeup2Timer.IsEnabled = true; timeup2Timer.Start(); // ランダムタイマーを開始 RandomTimer.IsEnabled = true; RandomTimer.Start(); // リストに各モグラを追加 MoguraList = new List <mogurahole>(); MoguraList.Add(Mogura1_1); MoguraList.Add(Mogura1_2); MoguraList.Add(Mogura1_3); MoguraList.Add(Mogura1_4); MoguraList.Add(Mogura1_5); MoguraList.Add(Mogura1_6); MoguraList.Add(Mogura2_1); MoguraList.Add(Mogura2_2); MoguraList.Add(Mogura2_3); MoguraList.Add(Mogura2_4); MoguraList.Add(Mogura2_5); MoguraList.Add(Mogura2_6); MoguraList.Add(Mogura3_1); MoguraList.Add(Mogura3_2); MoguraList.Add(Mogura3_3); MoguraList.Add(Mogura3_4); MoguraList.Add(Mogura3_5); MoguraList.Add(Mogura3_6); MoguraList.Add(Mogura4_1); MoguraList.Add(Mogura4_2); MoguraList.Add(Mogura4_3); MoguraList.Add(Mogura4_4); MoguraList.Add(Mogura4_5); MoguraList.Add(Mogura4_6); // 全てのボタンにクリックイベントを設定しておく foreach (var targetbutton in MoguraList) { // クリック時のイベントを設定 targetbutton.AttackEvent = () => { Mogura_Click(); count1++; //clients.DataReceiveCallback = Doscore; if (PlayerNumber == 1) { try { clients.receiveDone.Reset(); clients.Send("<p1>" + count1.ToString() + "<score><EOF>"); //clients.receiveDone.WaitOne(); // Receive the clients.response from the remote device. if (clients.response.IndexOf("<score>") > -1) //score送信の部分 { clients.response = (clients.response.Replace("<score>", "")); var score = clients.response.Split(','); count2 = int.Parse(score[1].Replace("<EOF>", "")); } } catch (Exception e) { Console.WriteLine(e.ToString()); } } else if (PlayerNumber == 2) { try { clients.receiveDone.Reset(); clients.Send("<p2>" + count1.ToString() + "<score><EOF>"); //clients.receiveDone.WaitOne(); // Receive the clients.response from the remote device. if (clients.response.IndexOf("<score>") > -1) //score送信の部分 { clients.response = (clients.response.Replace("<score>", "")); var score = clients.response.Split(','); count2 = int.Parse(score[0].Replace("<EOF>", "")); } } catch (Exception e) { Console.WriteLine(e.ToString()); } } // Application.Propertiesに設定しておく。 PN number = new PN() { playernumber = PlayerNumber }; PN C1 = new PN() { c1 = count1 }; PN C2 = new PN() { c2 = count2 }; Application.Current.Properties["Number"] = number; Application.Current.Properties["C1"] = C1; Application.Current.Properties["C2"] = C2; }; } }