Пример #1
0
        private void Collision(GameTime gameTime, GameArea gameArea, Apple apple, ScreenManager screenManager)
        {
            if(snakeSectionArray[0].Rectangle.X < gameArea.LeftBound
                || snakeSectionArray[0].Rectangle.Y < 0
                || snakeSectionArray[0].Rectangle.X + snakeSectionArray[0].Rectangle.Width> gameArea.RightBound
                || snakeSectionArray[0].Rectangle.Y + snakeSectionArray[0].Rectangle.Height > Global.GraphicsManager.PreferredBackBufferHeight)
            {
                GameOver(screenManager);
            }

            for (int i = 1; i < snakeSectionArray.Length; i++)
            {
                if(snakeSectionArray[0].Rectangle.Intersects(snakeSectionArray[i].Rectangle))
                {
                    GameOver(screenManager);
                }
            }
            if(snakeSectionArray[0].Rectangle.Intersects(apple.Rectangle))
            {
                score.IncreaseScore();
                apple.IsAlive = false;
                SnakeSection[] buffer = snakeSectionArray;
                snakeSize++;
                snakeSectionArray = new SnakeSection[snakeSize];

                for (int i = 0; i < buffer.Length; i++)
                {
                    snakeSectionArray[i] = buffer[i];
                }
                if(snakeSectionArray[snakeSectionArray.Length - 2].Rectangle.X - snakeSectionArray[snakeSectionArray.Length - 3].Rectangle.X == 0)
                {
                    snakeSectionArray[snakeSectionArray.Length - 1] = new SnakeSection(pathToTexture, new Rectangle(snakeSectionArray[snakeSectionArray.Length - 2].Rectangle.X,
                        snakeSectionArray[snakeSectionArray.Length - 2].Rectangle.Y + Texture.Height, Texture.Width, Texture.Height));
                }
                else if (snakeSectionArray[snakeSectionArray.Length - 2].Rectangle.Y - snakeSectionArray[snakeSectionArray.Length - 3].Rectangle.Y == 0)
                {
                    snakeSectionArray[snakeSectionArray.Length - 1] = new SnakeSection(pathToTexture, new Rectangle(snakeSectionArray[snakeSectionArray.Length - 2].Rectangle.X,
                        snakeSectionArray[snakeSectionArray.Length - 2].Rectangle.Y + Texture.Height, Texture.Width, Texture.Height));
                }
            }
        }
Пример #2
0
 public void Update(GameTime gameTime, ScreenPad screenPad, GameArea gameArea, Apple apple, ScreenManager screenManager)
 {
     Movement(gameTime, screenPad);
     Collision(gameTime, gameArea, apple, screenManager);
 }
Пример #3
0
        private void LoadContent()
        {
            gameArea = new GameArea();

            if (Global.TextureWidth == 20)
            {
                snake = new Snake("Snake/SmallSize/middle");
                apple = new Apple("Snake/SmallSize/apple");
            }
            else
            {
                snake = new Snake("Snake/MediumSize/middle");
                apple = new Apple("Snake/MediumSize/apple");
            }
            screenPad = new ScreenPad(Global.Content.Load<Texture2D>("Controls/upButton"),
                Global.Content.Load<Texture2D>("Controls/downButton"),
                Global.Content.Load<Texture2D>("Controls/leftButton"),
                Global.Content.Load<Texture2D>("Controls/rightButton"), false);
        }