void RandomizeFruitPosition(FlxObject object1 = null, FlxObject object2 = null) { _fruit.X = FlxG.Random.Next(0, (int)Math.Floor(FlxG.Width / 8f) - 1) * 8f; _fruit.Y = FlxG.Random.Next(0, (int)Math.Floor(FlxG.Height / 8f) - 1) * 8f; FlxG.Overlap(_fruit, _snakeBody, RandomizeFruitPosition); }
public override void Update(GameTime gameTime) { base.Update(gameTime); if (_scoreText.Alpha < 1) { _scoreText.Alpha += 0.1f; } if (!_snakeHead.Alive) { if (FlxG.Keyboard.AnyJustReleased(new Keys[] { Keys.Space, Keys.R })) { FlxG.ResetState(); } return; } FlxG.Overlap(_snakeHead, _fruit, CollectFruit); FlxG.Overlap(_snakeHead, _snakeBody, GameOver); if (FlxG.Keyboard.AnyPressed(new Keys[] { Keys.Up, Keys.W }) && _currentDirection != FlxObjectDirection.DOWN) { _nextDirection = FlxObjectDirection.UP; } else if (FlxG.Keyboard.AnyPressed(new Keys[] { Keys.Down, Keys.S }) && _currentDirection != FlxObjectDirection.UP) { _nextDirection = FlxObjectDirection.DOWN; } else if (FlxG.Keyboard.AnyPressed(new Keys[] { Keys.Left, Keys.A }) && _currentDirection != FlxObjectDirection.RIGHT) { _nextDirection = FlxObjectDirection.LEFT; } else if (FlxG.Keyboard.AnyPressed(new Keys[] { Keys.Right, Keys.D }) && _currentDirection != FlxObjectDirection.LEFT) { _nextDirection = FlxObjectDirection.RIGHT; } }