Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SnakeGame.SetupMenuController"/> class.
        /// </summary>
        public SetupMenuController()
        {
            _difficulty = Difficulty.Medium;
            _up         = new BooleanControlsFlag(delegate()
            {
                return(SwinGame.KeyTyped(KeyCode.vk_UP) || SwinGame.KeyTyped(KeyCode.vk_w));
            });
            _up.StateSetTrue += (object sender, EventArgs e) =>
            {
                switch (_difficulty)
                {
                case Difficulty.Easy:
                    _difficulty = Difficulty.Hard;
                    break;

                default:
                    _difficulty = _difficulty.Decrement();
                    break;
                }
            };
            _down = new BooleanControlsFlag(delegate()
            {
                return(SwinGame.KeyTyped(KeyCode.vk_DOWN) || SwinGame.KeyTyped(KeyCode.vk_s));
            });
            _down.StateSetTrue += (object sender, EventArgs e) =>
            {
                switch (_difficulty)
                {
                case Difficulty.Hard:
                    _difficulty = Difficulty.Easy;
                    break;

                default:
                    _difficulty = _difficulty.Increment();
                    break;
                }
            };
            _select = new BooleanControlsFlag(delegate()
            {
                return(SwinGame.KeyTyped(KeyCode.vk_RETURN));
            });
            _select.StateSetTrue += (object sender, EventArgs e) =>
            {
                OnDone(new GameplayController(_difficulty));
            };
            _renderer = delegate(object sender, EventArgs e)
            {
                Color selected = CellDrawing.GetColor("#4ac925");
                SwinGame.DrawText("Choose a difficulty level:", CellDrawing.COLOR, 12, 12);
                IEnumerator <Difficulty> enumerator = _difficulty.GetEnumerator();
                int y         = 24;
                int increment = 12;
                while (enumerator.MoveNext())
                {
                    SwinGame.DrawText(enumerator.Current.ToString(), enumerator.Current == _difficulty ? selected : CellDrawing.COLOR, 12, y);
                    y += increment;
                }
            };
            RenderEvents.RenderTick += _renderer;
        }
Пример #2
0
        public ScoreInputController(Score s)
        {
            _score = s;
            _input = new TextInput();

            _select = new BooleanControlsFlag(delegate()
            {
                return(SwinGame.KeyTyped(KeyCode.vk_RETURN));
            });
            _select.StateSetTrue += (object sender, EventArgs e) =>
            {
                if (_input.Text.Length != 0)
                {
                    _score.PlayerName = _input.Text;
                    ScoreIO io = new ScoreIO();
                    io.AddScore(_score);
                    io.Write();
                }
                //OnDone(new MainMenuController());
            };
            _renderer = delegate(object sender, EventArgs e)
            {
                Color selected = CellDrawing.GetColor("#4ac925");
                SwinGame.DrawText("Please input your name to submit your score:", CellDrawing.COLOR, 16, 128);
                SwinGame.DrawText(_input.Text, selected, 96, 140);
            };
            RenderEvents.RenderTick += _renderer;
        }
Пример #3
0
 void InitDMethods()
 {
     DrawingMethods    = new CellDrawing[qmethods];
     DrawingMethods[0] = new CellDrawing(CurvedCell);
     DrawingMethods[1] = new CellDrawing(RoundedCell);
 }
Пример #4
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;
        }