public static void Main() { //Create collections to hold leaderboard, packet count and server list _leaderboard = new List<LeaderboardPlayer>(); _packetCount = new Dictionary<int, int>(); _serverList = new List<Server>(); _showPopup = true; //Open the game window SwinGame.OpenGraphicsWindow("Petri", 1024, 800); //Load the Arial font Font _gameFont = SwinGame.LoadFont("Arial", 14); Rectangle background = new Rectangle(SwinGame.RGBAColor(0, 0, 0, 155), (SwinGame.ScreenWidth() / 2) - (300 / 2), (SwinGame.ScreenHeight() / 2) - (200 / 2), 300, 150, false); //Create a textbox to get the nick name TextBox nameEntry = new TextBox(_gameFont, (SwinGame.ScreenWidth() / 2) - (190 / 2), background.Y + 30, 190, 20, 16); //Create a combobox to select the server _box = new ComboBox(_gameFont, nameEntry.X, background.Y + 70, 190, 20); _box.ItemSelected += box_ItemSelected; _box.Text = "Loading servers..."; Rectangle connectRect = new Rectangle(Color.DarkGray, _box.X, background.Y + background.Height - 30, 190, 20, false); while (!SwinGame.WindowCloseRequested()) { //Init the server info on load if (!_startup) { _startup = true; LoadData(_box); } //Check for any clicks/key presses SwinGame.ProcessEvents(); //Clear the screen SwinGame.ClearScreen(Color.White); //Draw the world and contents within if (_agarWorld != null) _agarWorld.Draw(); //Draw menu if (_showPopup) { background.Draw(); SwinGame.DrawTextOnScreen("Name:", Color.White, nameEntry.X, nameEntry.Y - 10); SwinGame.DrawTextOnScreen("Server:", Color.White, _box.X, _box.Y - 10); nameEntry.Draw(); if (_connected) { connectRect.Draw(); SwinGame.DrawTextOnScreen("Play", Color.White, connectRect.X + (connectRect.Width / 2) - 16, connectRect.Y + 6); if (SwinGame.MouseClicked(MouseButton.LeftButton) && connectRect.IsAt(SwinGame.MousePosition()) && !_box.DroppedDown) { Play(nameEntry.Text); } } _box.Draw(); } else { if (SwinGame.KeyTyped(KeyCode.vk_SPACE)) { PushMessage(17); } else if (SwinGame.KeyTyped(KeyCode.vk_w)) { PushMessage(21); } else { Movement(); } } //Draw server info SwinGame.DrawTextOnScreen(string.Format("{0} FPS", SwinGame.GetFramerate()), Color.Black, 0, 0); if (_agarWorld != null) SwinGame.DrawTextOnScreen(string.Format("{0},{1}", Math.Round(_agarWorld.CameraX), Math.Round(_agarWorld.CameraY)), Color.Black, 0, 10); SwinGame.DrawTextOnScreen(_packetText, Color.Black, 0, 20); SwinGame.DrawTextOnScreen(_ipAddress, Color.Black, 0, 30); //Draw Leaderboard Info string players = ""; foreach (LeaderboardPlayer p in _leaderboard) { players += string.Format("{0}. {1} ", p.Rank, p.Name); } SwinGame.DrawTextOnScreen(players, Color.Black, _leaderboardMarquee, SwinGame.ScreenHeight() - 10); if (players.Length * 8 > SwinGame.ScreenWidth()) { _leaderboardMarquee += 1; } else { _leaderboardMarquee = 0; } if (_leaderboardMarquee > SwinGame.ScreenWidth()) { _leaderboardMarquee = -(players.Length * 8); } //Refresh the screen at ~60 fps SwinGame.RefreshScreen(60); } }
/// <summary> /// Load the server data into the combo box /// </summary> /// <param name="box">The combo box to load the servers into</param> public async static void LoadData(ComboBox box) { using (var client = new HttpClient()) { var serverJSON = await client.GetStringAsync("http://m.agar.io/info"); JavaScriptSerializer serializer = new JavaScriptSerializer(); var servers = serializer.Deserialize<Dictionary<string, object>>(serverJSON); foreach (var server in (Dictionary<string, object>)servers["regions"]) { Server newServer = new Server(); newServer.Name = server.Key; var serverData = (Dictionary<string, object>)server.Value; newServer.Players = (int)serverData["numPlayers"]; newServer.Realms = (int)serverData["numRealms"]; newServer.Servers = (int)serverData["numServers"]; _serverList.Add(newServer); box.AddItem(newServer.Name); } box.Text = "Select a server"; } }