示例#1
0
        //method that figures out if the snake hit anything.
        //public void makeRewardAppear()
        //{
        //    int x, y;
        //    //choose random x and y cordinate and place a object
        //    //the object will be either a person, mouse, or grasshopper
        //    x = randFood.Next(0, 42) * 10;//creates a random x location
        //    y = randFood.Next(3, 33) * 10;//creates a random y location
        //    //after position is found the we need  to get the object.
        //    //rewardImage.Show();
        //}
        public void collision()
        {
            Score highScores = new Score();
            //this loop is testing to see if the snake hits itself or the boarder

            for (int i = 1; i < snake.SnakeRectangle.Length; i++)
            {
                if (snake.SnakeRectangle[0].IntersectsWith(snake.SnakeRectangle[i]))
                {
                    highScores.HighestScore = score;
                   // highScores.writeToList();
                    restart();// if the snake did eat itself then restart the game.

                }
            }
            if (snake.SnakeRectangle[0].X < 0 || snake.SnakeRectangle[0].X >= 430 ||
                snake.SnakeRectangle[0].Y < 24 || snake.SnakeRectangle[0].Y > 330)
            {
                //if the snake went out of the boarder of the screen for the x axis
                //then restart
                highScores.HighestScore = score;
               // highScores.writeToList();
                restart();
            }
            //if game over is true then set
        }
示例#2
0
 private void highScoresToolStripMenuItem_Click(object sender, EventArgs e)
 {
     //When the highscores button is click in the menu it will open a new
     //window and display them.
     Score highScores = new Score();
     highScores.Show();
 }
示例#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;
        }