public Player( string name, string id, Point foodInitialLocation, Brush foodBrush, Point snakeInitialLocation, Positioning.Direction snakeInitialDirection, Brush snakeBrush ) { this.Name = name; this.Id = id; this.Food = new(foodInitialLocation, foodBrush); this.Snake = new(snakeInitialLocation, snakeInitialDirection, snakeBrush, this.Id); }
public Snake(Point initialLocation, Positioning.Direction initialDirection, Brush brush, string playerId) { CurrentDirection = initialDirection; NextDirection = initialDirection; this.Brush = brush; this.Segments = new(); Segments.Add(new(initialLocation, this.Brush)); this.FoodEatenCount = 0; this.Speed = this.CalculateSpeed(); this.Alive = true; this.LifecycleTimer = new() { Interval = CalculateLifecycleInterval() }; this.LifecycleTimer.Tick += this.LifecycleTick; this.PlayerId = playerId; }
// Lifecycle private void LifecycleTick(object sender, EventArgs e) { if (this.Alive) { // Motion this.CurrentDirection = this.NextDirection; this.Move(); // Check food collision if (this.DoesCollideWith(this.CurrentPlayer.Food)) { this.CurrentPlayer.Food.Relocate(); this.FoodEatenCount++; SnakeAteFood.Fire(this.CurrentPlayer); } else if (this.DoesCollideWith(this.EnemyPlayer.Food)) { this.EnemyPlayer.Food.Relocate(); this.ShortenSnake(1 + 1 /* <-- move compensation */); } else { this.ShortenSnake(0 + 1 /* <-- move compensation */); } // Check player collision if (DoesCollideWithOneOf(this.Segments.SkipWhile(segment => segment == this.Segments[0]))) { this.ShortenSnake(this.Segments.Count - this.Segments.FindLastIndex(DoesCollideWith)); } else if (DoesCollideWith(this.EnemyPlayer.Snake)) { SnakeDie.Fire(this.CurrentPlayer); } } }