Esempio n. 1
0
        public void TestScoreWhenFruitEaten([Values(2, 3, 4, 5)] int fruitVal)
        {
            Grid  g  = new Grid(16, 16);
            Score sv = new Score(Difficulty.Easy);
            Snake s  = new Snake(g, g[8, 8], 2, Direction.Right);
            Fruit f  = new Fruit(g, fruitVal);

            s.MovementDirection = Direction.Right;
            Cell c = g[s.Head.Cell.X + 1, s.Head.Cell.Y];

            f.OccupiedCell = c;
            FruitEatenHandler h = new FruitEatenHandler(f, s, sv);

            s.Move();
            h.EvaluateState();
            Assert.AreEqual(fruitVal, sv.Value, "Once the snake eats fruit the score should increase by " + fruitVal);
        }
Esempio n. 2
0
        public void TestMovement()
        {
            Grid g = new Grid(16, 16);

            for (int i = 0; i < 1024; i++)
            {
                Score sv = new Score(Difficulty.Easy);
                Snake s  = new Snake(g, g[8, 8], 2, Direction.Right);
                Fruit f  = new Fruit(g, 1);
                s.MovementDirection = Direction.Right;
                Cell c = g[s.Head.Cell.X + 1, s.Head.Cell.Y];
                f.OccupiedCell = c;
                FruitEatenHandler h = new FruitEatenHandler(f, s, sv);
                s.Move();
                h.EvaluateState();
                Assert.AreNotEqual(c, f.OccupiedCell, "After being eaten, a fruit's location must change.");
                foreach (MovementNode mn in s)
                {
                    Assert.AreNotEqual(mn.Cell, f.OccupiedCell, "After being eaten, the fruit's new location must not be inside the snake.");
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SnakeGame.GameplayController"/> class.
        /// </summary>
        /// <param name="difficulty">The difficulty level to play at.</param>
        public GameplayController(Difficulty difficulty)
        {
            _score              = new Score(difficulty);
            _playArea           = new Grid(32, 32);
            _player             = new Snake(_playArea, _playArea[16, 16], 5, Direction.Right);
            _objective          = new Fruit(_playArea, _player.OccupiedCells, 3);
            _handler            = new FruitEatenHandler(_objective, _player, _score);
            _mover              = new SnakeMovementControlHandler(_player, (int)difficulty);
            _mover.OutOfBounds += (object sender, EventArgs e) =>
            {
                string       finalScore   = "Final score: " + _score.Value;
                Color        textColor    = CellDrawing.GetColor("#e00707");
                EventHandler gameOverText = delegate(object sender2, EventArgs e2)
                {
                    SwinGame.DrawText("GAME OVER", textColor, 96, 128);
                    SwinGame.DrawText(finalScore, textColor, 96, 140);
                };
                var gameOverTimeout = new System.Timers.Timer(2048);
                gameOverTimeout.Elapsed += (object sender2, System.Timers.ElapsedEventArgs e2) =>
                {
                    gameOverTimeout.Stop();
                    gameOverTimeout.Dispose();
                    RenderEvents.RenderTick -= gameOverText;
                    OnDone(new ScoreInputController(_score));
                };
                gameOverTimeout.Start();
                RenderEvents.RenderTick += gameOverText;
            };
            _mover.AfterMove += (object sender, EventArgs e) =>
            {
                _handler.EvaluateState();
            };

            _up = new BooleanControlsFlag(delegate()
            {
                return(SwinGame.KeyDown(KeyCode.vk_w) || SwinGame.KeyDown(KeyCode.vk_UP));
            });
            _up.StateSetTrue += (object sender, EventArgs e) =>
            {
                _mover.Enqueue(Direction.Up);
            };
            _left = new BooleanControlsFlag(delegate()
            {
                return(SwinGame.KeyDown(KeyCode.vk_a) || SwinGame.KeyDown(KeyCode.vk_LEFT));
            });
            _left.StateSetTrue += (object sender, EventArgs e) =>
            {
                _mover.Enqueue(Direction.Left);
            };
            _down = new BooleanControlsFlag(delegate()
            {
                return(SwinGame.KeyDown(KeyCode.vk_s) || SwinGame.KeyDown(KeyCode.vk_DOWN));
            });
            _down.StateSetTrue += (object sender, EventArgs e) =>
            {
                _mover.Enqueue(Direction.Down);
            };
            _right = new BooleanControlsFlag(delegate()
            {
                return(SwinGame.KeyDown(KeyCode.vk_d) || SwinGame.KeyDown(KeyCode.vk_RIGHT));
            });
            _right.StateSetTrue += (object sender, EventArgs e) =>
            {
                _mover.Enqueue(Direction.Right);
            };

            Color scoreColor = CellDrawing.GetColor("#008282");

            _renderer = delegate(object sender, EventArgs e)
            {
                int offset = 1;
                int x;
                int y;
                for (y = -1, x = -1; x <= _playArea.Width; x++)
                {
                    CellDrawing.Draw(offset, offset, new Cell(_playArea, x, y));
                    CellDrawing.Draw(offset, offset, new Cell(_playArea, x, _playArea.Height));
                }
                for (y = 0, x = -1; y < _playArea.Height; y++)
                {
                    CellDrawing.Draw(offset, offset, new Cell(_playArea, x, y));
                    CellDrawing.Draw(offset, offset, new Cell(_playArea, _playArea.Width, y));
                }
                foreach (MovementNode node in _player)
                {
                    CellDrawing.Draw(offset, offset, node.Cell);
                }
                CellDrawing.Draw(offset, offset, _objective.OccupiedCell);
                SwinGame.DrawText("Score: " + _score.Value, scoreColor, 12, 2);
            };
            RenderEvents.RenderTick += _renderer;
        }