/// <summary> /// Add a new piece at the end of the body of the snake after eat a food /// </summary> private void AddNewSnakePieceToBodyAfterEat() { SnakePiece lastSnakePiece = _snake.Body.Count > 0 ? _snake.Body.LastOrDefault() : _snake.Head; SnakePiece newBodyPiece = null; switch (_settings.Direction) { case Directions.Up: newBodyPiece = new SnakePiece(lastSnakePiece.X, lastSnakePiece.Y + _settings.PieceSize); break; case Directions.Down: newBodyPiece = new SnakePiece(lastSnakePiece.X, lastSnakePiece.Y - _settings.PieceSize); break; case Directions.Left: newBodyPiece = new SnakePiece(lastSnakePiece.X + _settings.PieceSize, lastSnakePiece.Y); break; case Directions.Right: newBodyPiece = new SnakePiece(lastSnakePiece.X - _settings.PieceSize, lastSnakePiece.Y); break; } if (newBodyPiece != null) { _snake.Body.Add(newBodyPiece); } }
public void addPiece() { //MessageBox.Show(""+ (SnakePiece.snakePieces.Count - 1)); int previousX = SnakePiece.snakePieces[SnakePiece.snakePieces.Count - 1].Left - 30; int previousY = SnakePiece.snakePieces[SnakePiece.snakePieces.Count - 1].Top; var additionalPiece = new SnakePiece(previousX, previousY, false); Controls.Add(SnakePiece.snakePieces[SnakePiece.snakePieces.Count - 1]); //SnakePiece.snakePieces.Add(newPiece); //Controls.Add(SnakePiece.snakePieces[SnakePiece.snakePieces.Count - 1]); }
public Snake(int _horSp, int _verSp, int _piecesCount, int _headX, int _headY) { HorSpeed = _horSp; VerSpeed = _verSp; PiecesCount = _piecesCount; HeadX = _headX; HeadY = _headY; // building the initial pieces SnakePiece head = new SnakePiece(HeadX, HeadY, true); int tempX = HeadX; for (int i = 1; i < PiecesCount; i++) { tempX -= HorSpeed; new SnakePiece(tempX, HeadY, false); } }
public bool Move() { if (_shouldGrow) { _shouldGrow = false; } else { RemoveTail(); } int newHeadX = Head.X; int newHeadY = Head.Y; if (Direction == Direction.Right) { newHeadX += 1; } if (Direction == Direction.Left) { newHeadX -= 1; } if (Direction == Direction.Up) { newHeadY -= 1; } if (Direction == Direction.Down) { newHeadY += 1; } var newHead = new SnakePiece(newHeadX, newHeadY); Body.Add(newHead); return(true); // todo colisions }