static void OnMouseRelease(object sender, MouseButtonEventArgs e) { RenderWindow window = (RenderWindow)sender; if (pressed.Count == 1) { int index = pressed.First().Value; Sprite button = pressed.First().Key; if (button.GetGlobalBounds().Contains(e.X, e.Y)) { if (screen == 0) { if (index == 1) { screen = 2; } else if (index == 4) { window.Close(); } buttons_menu[index] = button; } else if (screen == 2) { if (index == 1) { screen = 5; } else if (index == 2) { screen = 0; } buttons_multiplayer[index] = button; } else if (screen == 5) { if (Multiplayer.hosting) { if (index == 2) { Multiplayer.server.Stop(); Multiplayer.hosting = false; } } else if (index == 0) { Multiplayer.hosting = true; ThreadStart ts = new ThreadStart(Multiplayer.HostGame); Thread host = new Thread(ts); host.Start(); } else if (index == 1) { ThreadStart ts = new ThreadStart(Multiplayer.JoinGame); Thread host = new Thread(ts); host.Start(); Multiplayer.Run(window); } else if (index == 2) { screen = 2; } buttons_multiplayer_2[index] = button; } } pressed.Remove(button); } }
public static int Run(RenderWindow window) { window.Closed += new EventHandler(OnClose); window.MouseButtonPressed += new EventHandler <MouseButtonEventArgs>(OnMouseClick); window.MouseButtonReleased += new EventHandler <MouseButtonEventArgs>(OnMouseRelease); Texture backgroundTexture = new Texture(@"resources/background.png"); Sprite background = new Sprite(backgroundTexture); Texture button_1_texture = new Texture(@"resources/singleplayer.png"); Sprite button_1 = new Sprite(button_1_texture); button_1.Position = new Vector2f(150, 230); Texture button_2_texture = new Texture(@"resources/multiplayer.png"); Sprite button_2 = new Sprite(button_2_texture); button_2.Position = new Vector2f(150, 280); Texture button_3_texture = new Texture(@"resources/settings.png"); Sprite button_3 = new Sprite(button_3_texture); button_3.Position = new Vector2f(150, 330); Texture button_4_texture = new Texture(@"resources/credits.png"); Sprite button_4 = new Sprite(button_4_texture); button_4.Position = new Vector2f(150, 380); Texture button_5_texture = new Texture(@"resources/exit.png"); Sprite button_5 = new Sprite(button_5_texture); button_5.Position = new Vector2f(150, 430); Texture button_1_pressed_texture = new Texture(@"resources/singleplayer_pressed.png"); Sprite button_1_pressed = new Sprite(button_1_pressed_texture); button_1_pressed.Position = new Vector2f(150, 230); Texture button_2_pressed_texture = new Texture(@"resources/multiplayer_pressed.png"); Sprite button_2_pressed = new Sprite(button_2_pressed_texture); button_2_pressed.Position = new Vector2f(150, 280); Texture button_3_pressed_texture = new Texture(@"resources/settings_pressed.png"); Sprite button_3_pressed = new Sprite(button_3_pressed_texture); button_3_pressed.Position = new Vector2f(150, 330); Texture button_4_pressed_texture = new Texture(@"resources/credits_pressed.png"); Sprite button_4_pressed = new Sprite(button_4_pressed_texture); button_4_pressed.Position = new Vector2f(150, 380); Texture button_5_pressed_texture = new Texture(@"resources/exit_pressed.png"); Sprite button_5_pressed = new Sprite(button_5_pressed_texture); button_5_pressed.Position = new Vector2f(150, 430); buttons_menu.Add(button_1); buttons_menu.Add(button_2); buttons_menu.Add(button_3); buttons_menu.Add(button_4); buttons_menu.Add(button_5); buttons_menu_pressed.Add(button_1_pressed); buttons_menu_pressed.Add(button_2_pressed); buttons_menu_pressed.Add(button_3_pressed); buttons_menu_pressed.Add(button_4_pressed); buttons_menu_pressed.Add(button_5_pressed); Texture button_1_multiplayer_texture = new Texture(@"resources/online.png"); Sprite button_1_multiplayer = new Sprite(button_1_multiplayer_texture); button_1_multiplayer.Position = new Vector2f(150, 230); Texture button_2_multiplayer_texture = new Texture(@"resources/tcpip.png"); Sprite button_2_multiplayer = new Sprite(button_2_multiplayer_texture); button_2_multiplayer.Position = new Vector2f(150, 280); Texture button_3_multiplayer_texture = new Texture(@"resources/back.png"); Sprite button_3_multiplayer = new Sprite(button_3_multiplayer_texture); button_3_multiplayer.Position = new Vector2f(150, 330); Texture button_1_multiplayer_pressed_texture = new Texture(@"resources/online_pressed.png"); Sprite button_1_multiplayer_pressed = new Sprite(button_1_multiplayer_pressed_texture); button_1_multiplayer_pressed.Position = new Vector2f(150, 230); Texture button_2_multiplayer_pressed_texture = new Texture(@"resources/tcpip_pressed.png"); Sprite button_2_multiplayer_pressed = new Sprite(button_2_multiplayer_pressed_texture); button_2_multiplayer_pressed.Position = new Vector2f(150, 280); Texture button_3_multiplayer_pressed_texture = new Texture(@"resources/back_pressed.png"); Sprite button_3_multiplayer_pressed = new Sprite(button_3_multiplayer_pressed_texture); button_3_multiplayer_pressed.Position = new Vector2f(150, 330); buttons_multiplayer.Add(button_1_multiplayer); buttons_multiplayer.Add(button_2_multiplayer); buttons_multiplayer.Add(button_3_multiplayer); buttons_multiplayer_pressed.Add(button_1_multiplayer_pressed); buttons_multiplayer_pressed.Add(button_2_multiplayer_pressed); buttons_multiplayer_pressed.Add(button_3_multiplayer_pressed); Texture button_1_multiplayer_2_texture = new Texture(@"resources/host_game.png"); Sprite button_1_multiplayer_2 = new Sprite(button_1_multiplayer_2_texture); button_1_multiplayer_2.Position = new Vector2f(150, 230); Texture button_2_multiplayer_2_texture = new Texture(@"resources/join_game.png"); Sprite button_2_multiplayer_2 = new Sprite(button_2_multiplayer_2_texture); button_2_multiplayer_2.Position = new Vector2f(150, 280); Texture button_1_multiplayer_2_pressed_texture = new Texture(@"resources/host_game_pressed.png"); Sprite button_1_multiplayer_2_pressed = new Sprite(button_1_multiplayer_2_pressed_texture); button_1_multiplayer_2_pressed.Position = new Vector2f(150, 230); Texture button_2_multiplayer_2_pressed_texture = new Texture(@"resources/join_game_pressed.png"); Sprite button_2_multiplayer_2_pressed = new Sprite(button_2_multiplayer_2_pressed_texture); button_2_multiplayer_2_pressed.Position = new Vector2f(150, 280); buttons_multiplayer_2.Add(button_1_multiplayer_2); buttons_multiplayer_2.Add(button_2_multiplayer_2); buttons_multiplayer_2.Add(button_3_multiplayer); buttons_multiplayer_2_pressed.Add(button_1_multiplayer_2_pressed); buttons_multiplayer_2_pressed.Add(button_2_multiplayer_2_pressed); buttons_multiplayer_2_pressed.Add(button_3_multiplayer_pressed); Font font = new Font(@"resources/newrocker.TTF"); Text host_text = new Text(); host_text.Font = font; host_text.DisplayedString = "Waiting for connection..."; Text connected_text = new Text(); connected_text.Font = font; connected_text.DisplayedString = "Connected!"; while (window.IsOpen) { if (Multiplayer.connected) { Multiplayer.Run(window); } window.DispatchEvents(); window.Clear(); window.Draw(background); //0 - main menu if (screen == 0) { foreach (Sprite s in buttons_menu) { window.Draw(s); } } //2 - multiplayer else if (screen == 2) { foreach (Sprite s in buttons_multiplayer) { window.Draw(s); } } //5 - host/join else if (screen == 5) { if (Multiplayer.hosting) { if (Multiplayer.connected) { window.Draw(connected_text); } else { window.Draw(host_text); } window.Draw(buttons_multiplayer[2]); } else { foreach (Sprite s in buttons_multiplayer_2) { window.Draw(s); } } } window.Display(); } return(-1); }