Example #1
0
 // Drawing a custom Listbox Item
 private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
 {
     if (e.Index >= 0)
     {
         MyCustomItem item = (MyCustomItem)scoreBox.Items[e.Index];  // Get the current item and cast it to MyListBoxItem
         e.Graphics.DrawString(item.Message, scoreBox.Font, new SolidBrush(item.ItemColor), 0, e.Index * scoreBox.ItemHeight);
     }
 }
Example #2
0
        // event handler to grab the selected spectate player
        private void spectateComboBox_SelectionChanged(object sender, EventArgs e)
        {
            int id = (int)spectateComboBox.SelectedIndex;

            if (id >= 0)
            {
                MyCustomItem i = (MyCustomItem)spectateComboBox.Items[id];
                if (world.findSnake(i.ID) != null)
                {
                    player = world.findSnake(i.ID);
                }
            }
            block = false;
        }
Example #3
0
        // helper method for processing world data from the server
        private void processWorld(StringBuilder data)
        {
            string[] parts = Regex.Split(data.ToString(), @"(?<=[\n])"); //split the string into parts

            foreach (string s in parts)
            {
                if (s != "")
                {
                    JObject obj       = JObject.Parse(s.Replace("\n", ""));
                    JToken  snakeProp = obj["vertices"];
                    JToken  foodProp  = obj["loc"];

                    if (snakeProp != null) // if the part is a snake
                    {
                        Snake snake = JsonConvert.DeserializeObject <Snake>(s.Replace("\n", ""));
                        if (snake.Id == player.Id) // if the snake being processed is the player we want to do things a little differently
                        {
                            drawingPanel1.playerHead = snake.Head();
                            drawingPanel1.zoom       = snake.Length * 2;

                            world.addSnake(snake, true, playerColor);
                            if (snake.Verts[0].X == -1)
                            {
                                playerDied();
                            }
                        }
                        else
                        {
                            world.addSnake(snake, false, playerColor);
                        }
                    }
                    else if (foodProp != null) // if the part is food
                    {
                        Food food = JsonConvert.DeserializeObject <Food>(s.Replace("\n", ""));
                        world.addFood(food);
                    }
                    data.Remove(0, s.Length); //remove the processed part from the stringbuilder
                }
            }

            //update the scorebox and spectatorbox
            lock (world)
            {
                System.Threading.Thread.Sleep(50);
                this.Invoke(new MethodInvoker(() => scoreBox.Items.Clear()));
                if (!block)
                {
                    this.Invoke(new MethodInvoker(() => spectateComboBox.Items.Clear()));
                }
                foreach (Snake s in world.getSnakes())
                {
                    this.Invoke(new MethodInvoker(() => scoreBox.Items.Add(new MyCustomItem(s.getColor(), s.Name + " " + s.Length))));
                    if (!block)
                    {
                        MyCustomItem i = new MyCustomItem(s.getColor(), s.Name);
                        i.ID = s.Id;
                        this.Invoke(new MethodInvoker(() => spectateComboBox.Items.Add(i)));
                    }
                }
                spectateComboBox.Invalidate();
            }
            UpdateFrame(); //redraw after all parts have been processed
        }