示例#1
0
        private void UpdatePlayers(GameStateView gameStateView)
        {
            List <string> updatedPlayers = new List <string>();

            foreach (Player player in gameStateView.Players)
            {
                if (!playerPictureBoxes.TryGetValue(player.PlayerId, out PictureBox pictureBox))
                {
                    pictureBox = CreatePlayerPictureBox();

                    InitializeDrawing(pictureBox);

                    playerPictureBoxes.Add(player.PlayerId, pictureBox);
                }

                // Update labels for playing player
                if (player.PlayerId == this.Pid)
                {
                    ScoreLabel.SetPropertyThreadSafe(() => ScoreLabel.Text, String.Format("Player {0} Score: {1}", Pid, player.Score.ToString()));

                    if (!player.Alive)
                    {
                        GameStatusLabel.SetPropertyThreadSafe(() => GameStatusLabel.Text, "You are dead");
                    }
                }

                if (player.Alive)
                {
                    if (pictureBox.Tag == null || ((Direction)pictureBox.Tag) != player.Direction)
                    {
                        Direction direction     = pictureBox.Tag == null ? Direction.RIGHT : player.Direction;
                        Bitmap    updatedBitmap = DirectionToResource(direction);

                        if (updatedBitmap != null)
                        {
                            pictureBox.Image = updatedBitmap;
                            pictureBox.Tag   = direction;
                        }
                    }

                    pictureBox.Location = new Point(player.X, player.Y);
                }
                else
                {
                    pictureBox.Visible = false;
                }

                updatedPlayers.Add(player.PlayerId);
            }

            // Remove removed players
            foreach (string playerId in playerPictureBoxes.Keys.Except(updatedPlayers).ToList())
            {
                if (playerPictureBoxes.TryGetValue(playerId, out PictureBox pictureBox))
                {
                    this.Controls.Remove(pictureBox);
                    playerPictureBoxes.Remove(playerId);
                }
            }
        }
示例#2
0
        internal void UpdateScore(GameStateView gameStateView)
        {
            foreach (Player p in gameStateView.Players)
            {
                if (p.PlayerId == this.Pid)
                {
                    this.score = p.Score;

                    ScoreLabel.SetPropertyThreadSafe(() => ScoreLabel.Text, "Score: " + score.ToString());
                    //ScoreLabel.Text = "Player " + this.Pid + " Score: " + score.ToString();
                }
            }
        }