void gameTimer_OnTick() { if (IsValidNextMove() == true && gameLost == false && gameWon == false) { snake.MoveSnake(); SnakePart sp = snake.GetSnakeHead(); NPC currentNPC = level.GetNPC(sp.PositionOnX, sp.PositionOnY); Notify(this); } else { Lost(); } }
public bool CheckTail() { SnakePart snakeHead = SnakeHead; if (snakeHead != null) { foreach (var snakeEl in SnakeElements.Where(x => !x.Head)) { if (snakeEl.X == snakeHead.X && snakeEl.Y == snakeHead.Y) { return(true); } } } return(false); }
private bool IsValidNextMove() { SnakeDirection currentDirection = snake.Direction; SnakePart currentSnakeHead = snake.GetSnakeHead(); MapCell currentMapCell = level.LevelMap.GetMapCell(currentSnakeHead.PositionOnX, currentSnakeHead.PositionOnY); MapCell nextMapCell = null; switch (currentDirection) { case SnakeDirection.Up: nextMapCell = new MapCell(currentMapCell.PositionOnX, currentMapCell.PositionOnY - 1); break; case SnakeDirection.Down: nextMapCell = new MapCell(currentMapCell.PositionOnX, currentMapCell.PositionOnY + 1); break; case SnakeDirection.Left: nextMapCell = new MapCell(currentMapCell.PositionOnX - 1, currentMapCell.PositionOnY); break; case SnakeDirection.Right: nextMapCell = new MapCell(currentMapCell.PositionOnX + 1, currentMapCell.PositionOnY); break; default: return(false); } if (nextMapCell.PositionOnX < 0 || nextMapCell.PositionOnY >= level.LevelMap.MapRows || nextMapCell.PositionOnY < 0 || nextMapCell.PositionOnX >= level.LevelMap.MapColumns) { return(false); } if (snake.Contain(new SnakePart(nextMapCell.PositionOnX, nextMapCell.PositionOnY)) == true) { return(false); } nextMapCell = level.LevelMap.GetMapCell(nextMapCell.PositionOnX, nextMapCell.PositionOnY); if (nextMapCell.CellType == MapCellType.Block) { return(false); } return(true); }
internal void MoveSnake() { SnakePart head = SnakeElements[0]; SnakePart tail = SnakeElements[SnakeElements.Count - 1]; SnakeTail = new SnakePart(elemenetSize) { X = tail.X, Y = tail.Y }; head.Head = false; tail.Head = true; tail.X = head.X; tail.Y = head.Y; switch (Direction) { case Direction.Up: tail.Y -= elemenetSize; break; case Direction.Down: tail.Y += elemenetSize; break; case Direction.Left: tail.X -= elemenetSize; break; case Direction.Right: tail.X += elemenetSize; break; } SnakeElements.RemoveAt(SnakeElements.Count - 1); SnakeElements.Insert(0, tail); }
public void MoveSnake() { SnakePart currentHeadPosition = bodyParts.ElementAt(HEAD_INDEX); SnakePart newHeadPosition; switch (direction) { case SnakeDirection.Up: newHeadPosition = new SnakePart(currentHeadPosition.PositionOnX, currentHeadPosition.PositionOnY - 1); break; case SnakeDirection.Down: newHeadPosition = new SnakePart(currentHeadPosition.PositionOnX, currentHeadPosition.PositionOnY + 1); break; case SnakeDirection.Left: newHeadPosition = new SnakePart(currentHeadPosition.PositionOnX - 1, currentHeadPosition.PositionOnY); break; case SnakeDirection.Right: newHeadPosition = new SnakePart(currentHeadPosition.PositionOnX + 1, currentHeadPosition.PositionOnY); break; default: newHeadPosition = currentHeadPosition; break; } bodyParts.Insert(HEAD_INDEX, newHeadPosition); bodyParts.RemoveAt(bodyParts.Count - 1); if (waitingParts.Count > 0) { if (bodyParts.Contains(waitingParts.Peek()) == false) { bodyParts.Add(waitingParts.Dequeue()); } } }
public void AddSnakePart(int x, int y) { SnakePart newSnakePart = new SnakePart(x, y); waitingParts.Enqueue(newSnakePart); }
internal bool Contain(SnakePart snakePart) { return(bodyParts.Contains(snakePart)); }
public void AddSnakePart(int x, int y) { SnakePart newSnakePart = new SnakePart(x, y); bodyParts.Add(newSnakePart); }