Example #1
0
        /// <summary>
        /// Here every gameObject is initialized
        /// for the snake game
        /// creating the snakeObject
        /// creating the map
        /// creating the food
        ///
        /// </summary>
        private void initGameData()
        {
            // Initializze our gameobject list
            gameTime      = new ShowTimeObject(new Rectangle(new Point(300, 0), new Size(10, 10)));
            gameObstacles = new List <IGameObject> ();
            snakeFood     = new List <IGameObject> ();
            // Creates our implemented snake
            snake      = new Snake();
            background = GameUtils.getBlockObject(0, 0, 600, 600);
            background.passData(new GameData(GameState.None));
            snakeFood.Add(GameUtils.getRandomSnakeFoodObject());
            ShowScoreObject gameScore = new ShowScoreObject(new Rectangle(new Point(0, 200), new Size(20, 20)));

            if (realHighScore < highScore)
            {
                realHighScore = highScore;
            }

            gameScore.highScore = realHighScore;
            highScore           = 0;
            score          = 0;
            this.gameScore = gameScore;
            // Create the obstacles in the map
            createPlayingField();
        }
Example #2
0
        private void gameUpdateRunning()
        {
            gameTime.update(gameUpdateSpeed);
            foreach (var item in gameObstacles)
            {
                item.update(gameUpdateSpeed);
                if (GameUtils.isColliding(item, snake))
                {
                    snake.passData(new GameData(GameState.Dead));
                }
            }
            lock (snakeFood) {
                foreach (var item in snakeFood)
                {
                    if (GameUtils.isColliding(item, snake))
                    {
                        player.Play();
                        snake.passData(new GameData(GameState.Grow));
                        snake.passData(new GameData(GameState.SpeedUp));
                        snakeFood.Clear();
                        snakeFood.Add(GameUtils.getRandomSnakeFoodObject());
                        gameScore.passData(new GameData(GameState.Score));
                        score++;
                    }
                }
            }
            if (highScore < score)
            {
                highScore = score;
            }

            snake.update(gameUpdateSpeed);
            gameScore.update(gameUpdateSpeed);

            if (snake.getStates().Contains(GameState.Dead))
            {
                Thread.Sleep(1000);
                modelState = GameState.Menu;
                initGameData();
                initMenu();
            }
        }