private void StartNewGame() { foreach (SnakeElement snakeBodyElement in snakeElements) { if (snakeBodyElement.UiElement != null) { GameBoard.Children.Remove(snakeBodyElement.UiElement); } } snakeElements.Clear(); if (food != null) { GameBoard.Children.Remove(food); } //Reset currentScore = 0; ImagePanel.Source = null; snakeLength = startLength; currentSnakeDirection = CurrentSnakeDirection.Right; snakeElements.Add(new SnakeElement() { Position = new System.Windows.Point(snakeSquareSize * 5, snakeSquareSize * 5) }); gameTimer.Interval = TimeSpan.FromMilliseconds(startSpeed); SpawnSnake(); SpawnFood(); UpdateGame(); gameTimer.IsEnabled = true; }
private void Window_KeyUp(object sender, KeyEventArgs e) { CurrentSnakeDirection originalDirection = currentSnakeDirection; switch (e.Key) { case Key.Up: if (currentSnakeDirection != CurrentSnakeDirection.Down) { currentSnakeDirection = CurrentSnakeDirection.Up; } break; case Key.Down: if (currentSnakeDirection != CurrentSnakeDirection.Up) { currentSnakeDirection = CurrentSnakeDirection.Down; } break; case Key.Left: if (currentSnakeDirection != CurrentSnakeDirection.Right) { currentSnakeDirection = CurrentSnakeDirection.Left; } break; case Key.Right: if (currentSnakeDirection != CurrentSnakeDirection.Left) { currentSnakeDirection = CurrentSnakeDirection.Right; } break; case Key.Space: StartNewGame(); break; } if (currentSnakeDirection != originalDirection) { Movement(); } }