Esempio n. 1
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }
            // TODO: Add your update logic here
            snake.Movement();

            CheckCollision();

            food.createFood();


            if (foodToEat == snake.SnakeAteCounter)
            {
                var x            = random.Next(1, 99);
                var y            = random.Next(1, 60);
                var addToEat     = random.Next(1, 6);
                var headPosition = snake.segmentsDictionary["Head"].Position;
                var distance     = headPosition - new Vector2(x, y);
                //Console.WriteLine(distance);
                if (Math.Abs(distance.X) < 2 || Math.Abs(distance.Y) < 2)
                {
                    walls.addWalls("Random", Color.Black, x, y);
                }
                else
                {
                    x = x + random.Next(4, 9);
                    y = y + random.Next(4, 9);
                    walls.addWalls("Random", Color.Black, x, y);
                }

                foodToEat = foodToEat + addToEat;
            }

            if (snake.isDead)
            {
                if (Keyboard.GetState().IsKeyDown(Keys.Enter))
                {
                    snake.isDead = false;
                    snake        = new Snake(textureGen, 1, 1);
                    walls.DeleteWalls("Random");
                    foodToEat             = 1;
                    snake.SnakeAteCounter = 0;
                }
            }



            base.Update(gameTime);
        }
Esempio n. 2
0
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            random     = new Random();
            textureGen = new TextureGenerator(GraphicsDevice);
            snake      = new Snake(textureGen, 1, 1);
            walls      = new Walls(textureGen, 1, 1);
            food       = new Food(random, textureGen, 2, 2);
            food.createFood();
            foodToEat = 1;

            walls.addWalls("North", Color.Brown, 0, 0, 100, 0);
            walls.addWalls("South", Color.Green, 0, 0, 0, 100);
            walls.addWalls("West", Color.Pink, 0, 59, 100, 59);
            walls.addWalls("East", Color.Orange, 99, 0, 99, 60);

            base.Initialize();
        }