public override void Initialize() { base.Initialize(); // create head var head = new SnakeBlock(Game) {Location = Location, Vector = Vector}; // для головы другую текстурку нужно head.CurrentPathChanged += SnakeHeadCurrentPathChanged; _body.Add(head); for (int i = 1; i < _length; i++) { var last = _body[i - 1]; var next = new SnakeBlock(Game) {Location = last.Location - new Vector2(last.Size.X, 0), GoToPoint = last.Location}; next.CurrentPathChanged += SnakeBodyCurrentPathChanged; _body.Add(next); } _body.ForEach(block => Game.Components.Add(block)); UpdateSnakeBlock(); }
private void SnakeHeadCurrentPathChanged(Vector2 location, Vector2 vector) { var result = _map.UpdateMaskMap(location, vector, Masks.Snake); if (result == TypeMessage.Add) { // если съели яблоко, увеличиваем змею // добавляем в начало голову var newLocation = new Vector2(location.X + Helper.SizeBlock*vector.X, location.Y + Helper.SizeBlock*vector.Y); var head = new SnakeBlock(Game) {Location = newLocation, Vector = vector}; _body[0].CurrentPathChanged -= SnakeHeadCurrentPathChanged; _body[0].CurrentPathChanged += SnakeBodyCurrentPathChanged; head.CurrentPathChanged += SnakeHeadCurrentPathChanged; _body.Insert(0, head); Game.Components.Add(head); UpdateSnakeBlock(); _map.AppleRemove(); _map.AppleAdd(); if (CountBlockChanged != null) CountBlockChanged(_body.Count); } else if (result == TypeMessage.WallBreak) { MessageBox.Show("Столкновение со стеной", "Конец игры", MessageBoxButtons.OK, MessageBoxIcon.Information); Game.Exit(); } }