private Vector GenerateRandomFreePosition() { Vector generatedPosition; do { generatedPosition = new Vector(random.Next(gameWidth), random.Next(gameHeight)); }while(_snake == generatedPosition || _foodPositions.Any(food => food == generatedPosition)); return(generatedPosition); }
private void InitGame() { _snake = new Vector(gameWidth / 2, gameHeight / 2); for (int i = 0; i < foodCount; i++) { _foodPositions.Add( GenerateRandomFreePosition() ); } RenderState(); }
private bool CalculateState() { _snake = _snake + _direction; if (_snake.X < 0 || _snake.X >= gameWidth || _snake.Y < 0 || _snake.Y >= gameHeight) { return(false); } else { return(true); } }
private void DrawPoint(Vector position, Brush brush) { var shape = new Rectangle { Fill = brush }; var unitX = gameCanvas.Width / gameWidth; var unitY = gameCanvas.Height / gameHeight; shape.Width = unitX; shape.Height = unitY; Canvas.SetLeft(shape, position.X * unitX); Canvas.SetTop(shape, position.Y * unitY); gameCanvas.Children.Add(shape); }
private void ApplyInputKey(Key PressedKey) { switch (PressedKey) { case Key.Up: _direction = Vector.Up; break; case Key.Down: _direction = Vector.Down; break; case Key.Left: _direction = Vector.Left; break; case Key.Right: _direction = Vector.Right; break; } }