コード例 #1
0
        /// <summary>
        /// Spawns a new teddy bear at a random location
        /// </summary>
        private void SpawnBear()
        {
            // generate random location
            int randomLocationX = GetRandomLocation(GameConstants.SpawnBorderSize, (GameConstants.WindowWidth - GameConstants.SpawnBorderSize));
            int randomLocationY = GetRandomLocation(GameConstants.SpawnBorderSize, (GameConstants.WindowHeight - GameConstants.SpawnBorderSize));

            // generate random velocity
            float   randomSpeed    = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange);
            float   randomAngle    = RandomNumberGenerator.NextFloat(2 * (float)Math.PI);
            Vector2 randomVelocity = new Vector2((float)(randomSpeed * Math.Cos(randomAngle)), (float)(randomSpeed * Math.Sin(randomAngle)));

            // create new bear
            TeddyBear newBear = new TeddyBear(Content, @"BuildGraphics\teddybear", randomLocationX, randomLocationY, randomVelocity, teddyBounce, teddyShot);

            // make sure we don't spawn into a collision
            List <Rectangle> otherRectangles = GetCollisionRectangles();

            while (CollisionUtils.IsCollisionFree(newBear.DrawRectangle, otherRectangles) == false)
            {
                newBear.X = GetRandomLocation(GameConstants.SpawnBorderSize, (GameConstants.WindowWidth - GameConstants.SpawnBorderSize));
                newBear.Y = GetRandomLocation(GameConstants.SpawnBorderSize, (GameConstants.WindowHeight - GameConstants.SpawnBorderSize));
            }

            // add new bear to list
            bears.Add(newBear);
        }
コード例 #2
0
        /// <summary>
        /// Spawns a new teddy bear at a random location
        /// </summary>
        private void SpawnBear()
        {
            // generate random location
            int xBear = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize * 2);
            int yBear = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize * 2);

            // generate random velocity

            float randVel = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange - GameConstants.MinBearSpeed);

            float randAng = RandomNumberGenerator.NextFloat((float)Math.PI / 2);

            Vector2 velBear = new Vector2(randVel * (float)Math.Cos(randAng), randVel * (float)Math.Sin(randAng));

            // create new bear

            TeddyBear newBear = new TeddyBear(Content, "graphics/teddybear", xBear, yBear, velBear, teddyBounce, teddyShot);

            // make sure we don't spawn into a collision
            List <Rectangle> otherCollisionRectangles = GetCollisionRectangles();

            while (!CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, otherCollisionRectangles))
            {
                newBear.X = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize * 2);
                newBear.Y = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize * 2);
            }

            // add new bear to list
            bears.Add(newBear);
        }
コード例 #3
0
ファイル: Game1.cs プロジェクト: patex1987/TeddyBearGame
        /// <summary>
        /// Spawns a new teddy bear at a random location
        /// </summary>
        private void SpawnBear()
        {
            // generate random location
            int bearRndLocX  = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize);
            int bearRandLoxY = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize);

            // generate random velocity, angle and Vector for the bear velocity
            float bearRndSpeed = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange);
            float bearRndAngle = RandomNumberGenerator.NextFloat((float)Math.PI);
            //float bearRndAngle = (float)Math.PI/3;
            Vector2 bearVelocityVector = new Vector2((float)(bearRndSpeed * Math.Cos(Convert.ToDouble(bearRndAngle))), (float)(bearRndSpeed * Math.Sin(Convert.ToDouble(bearRndAngle))));

            // create new bear
            TeddyBear newBear = new TeddyBear(Content, @"graphics\teddybear", bearRndLocX, bearRandLoxY, bearVelocityVector, teddyBounce, teddyShot);


            // make sure we don't spawn into a collision
            List <Rectangle> collisionRectangles = GetCollisionRectangles();

            while (!CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, collisionRectangles))
            {
                newBear.X = GetRandomLocation(0, GameConstants.WindowWidth);
                newBear.Y = GetRandomLocation(0, GameConstants.WindowHeight);
            }

            // add new bear to list
            bears.Add(newBear);
        }
コード例 #4
0
ファイル: Game1.cs プロジェクト: Yukilynnren/ShootingGame
        /// <summary>
        /// Spawns a new teddy bear at a random location
        /// </summary>
        private void SpawnBear()
        {
            // generate random location
            int x = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE,
                                      GameConstants.WINDOW_WIDTH - 2 * GameConstants.SPAWN_BORDER_SIZE);
            int y = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE,
                                      GameConstants.WINDOW_HEIGHT - 2 * GameConstants.SPAWN_BORDER_SIZE);
            // generate random velocity
            float Rand_Speed = RandomNumberGenerator.NextFloat(GameConstants.BEAR_SPEED_RANGE);

            if (Rand_Speed < GameConstants.MIN_BEAR_SPEED)
            {
                Rand_Speed += GameConstants.MIN_BEAR_SPEED;
            }
            float   Rand_Angle    = RandomNumberGenerator.NextFloat((float)Math.PI * 2);
            Vector2 Rand_Velocity = new Vector2(Rand_Speed * (float)Math.Cos(Rand_Angle),
                                                Rand_Speed * (float)Math.Sin(Rand_Angle));

            // create new bear
            TeddyBear newBear = new TeddyBear(Content, "teddybear", x, y, Rand_Velocity, teddyBounce, teddyShot);

            // make sure we don't spawn into a collision
            while (CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, GetCollisionRectangles()) == false)
            {
                newBear.X = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE,
                                              GameConstants.WINDOW_WIDTH - 2 * GameConstants.SPAWN_BORDER_SIZE);
                newBear.Y = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE,
                                              GameConstants.WINDOW_WIDTH - 2 * GameConstants.SPAWN_BORDER_SIZE);
            }
            // add new bear to list
            bears.Add(newBear);
        }
コード例 #5
0
ファイル: Game1.cs プロジェクト: drazen41/GameProject
        /// <summary>
        /// Spawns a new teddy bear at a random location
        /// </summary>
        private void SpawnBear()
        {
            // generate random location
            int x = GetRandomLocation(GameConstants.SpawnBorderSize,
                                      graphics.PreferredBackBufferWidth - 2 * GameConstants.SpawnBorderSize);
            int y = GetRandomLocation(GameConstants.SpawnBorderSize,
                                      graphics.PreferredBackBufferHeight - 2 * GameConstants.SpawnBorderSize);
            // generate random velocity
            float   speed    = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange);
            float   angle    = RandomNumberGenerator.NextFloat(2 * (float)Math.PI);
            Vector2 velocity = new Vector2(
                (float)(speed * Math.Cos(angle)), (float)(speed * Math.Sin(angle)));
            // create new bear
            TeddyBear newBear = new TeddyBear(Content, @"graphics\teddybear", x, y, velocity, null, null);
            // make sure we don't spawn into a collision
            List <Rectangle> rects = GetCollisionRectangles();

            while (!CollisionUtils.IsCollisionFree(newBear.DrawRectangle, rects))
            {
                x         = GetRandomLocation(GameConstants.SpawnBorderSize, graphics.PreferredBackBufferWidth - 2 * GameConstants.SpawnBorderSize);
                y         = GetRandomLocation(GameConstants.SpawnBorderSize, graphics.PreferredBackBufferHeight - 2 * GameConstants.SpawnBorderSize);
                newBear.X = x;
                newBear.Y = y;
            }
            // add new bear to list
            bears.Add(newBear);
        }
コード例 #6
0
ファイル: Game1.cs プロジェクト: Sumit2105/2D_Arcade
        /// <summary>
        /// Spawns a new teddy bear at a random location
        /// </summary>
        private void SpawnBear()
        {
            // generate random location
            int start_x = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_WIDTH - 2 * GameConstants.SPAWN_BORDER_SIZE);
            int start_y = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_HEIGHT - 2 * GameConstants.SPAWN_BORDER_SIZE);

            // generate random velocity
            //float v_random=GameConstants.MIN_BEAR_SPEED+RandomNumberGenerator.NextFloat(RandomNumberGenerator.NextFloat((GameConstants.BEAR_SPEED_RANGE)));
            float v_random = GameConstants.MIN_BEAR_SPEED + RandomNumberGenerator.NextFloat(GameConstants.BEAR_SPEED_RANGE);

            //float angle_random = (float)Math.PI * RandomNumberGenerator.NextFloat(2);
            float angle_random = RandomNumberGenerator.NextFloat((float)Math.PI * 2);

            Vector2 velocity = new Vector2(v_random * (float)Math.Cos(angle_random), v_random * (float)Math.Sin(angle_random));

            // create new bear
            TeddyBear NewBear = new TeddyBear(Content, "teddybear", start_x, start_y, velocity, teddyBounce, teddyShot);

            // make sure we don't spawn into a collision
            List <Rectangle> collisionRectangles = GetCollisionRectangles();

            while (!CollisionUtils.IsCollisionFree(NewBear.CollisionRectangle, collisionRectangles))
            {
                NewBear.X = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE,
                                              graphics.PreferredBackBufferWidth - 2 * GameConstants.SPAWN_BORDER_SIZE);
                NewBear.Y = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE,
                                              graphics.PreferredBackBufferHeight - 2 * GameConstants.SPAWN_BORDER_SIZE);
            }
            // add new bear to list
            bears.Add(NewBear);
        }
コード例 #7
0
ファイル: Game1.cs プロジェクト: muadib25/TeddyBurgers
        /// <summary>
        /// Spawns a new teddy bear at a random location
        /// </summary>
        private void SpawnBear()
        {
            // generate random location
            //Vector2 bearLocation = new Vector2(GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize),
            //                                   GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize));
            int bearLocationX = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize);
            int bearLocationY = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize);


            // generate random velocity
            float bearSpeed = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange);
            float bearAngle = RandomNumberGenerator.NextFloat(2) * (float)Math.PI; // Radians

            // Using Math.Sin to calculate the 'y' component and Math.Cos for the 'x' component
            Vector2 bearVelocity = new Vector2((float)Math.Cos(bearAngle) * bearSpeed, (float)Math.Sin(bearAngle) * bearSpeed);

            // create new bear
            TeddyBear newBear = new TeddyBear(Content, @"graphics\teddybear", bearLocationX, bearLocationY, bearVelocity, teddyBounce, teddyShot);

            // make sure we don't spawn into a collision
            List <Rectangle> collisionRectangles = GetCollisionRectangles();

            while (!CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, collisionRectangles))
            {
                newBear.X = GetRandomLocation(0, GameConstants.WindowWidth);
                newBear.Y = GetRandomLocation(0, GameConstants.WindowHeight);
            }

            // add new bear to list
            bears.Add(newBear);
        }
コード例 #8
0
        /// <summary>
        /// Spawns a new teddy bear at a random location
        /// </summary>
        private void SpawnBear()
        {
            // generate random location
            int locationX = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, graphics.PreferredBackBufferWidth - 2 * GameConstants.SPAWN_BORDER_SIZE);
            int locationY = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, graphics.PreferredBackBufferHeight - 2 * GameConstants.SPAWN_BORDER_SIZE);

            // generate random velocity
            float   speed    = GameConstants.MIN_BEAR_SPEED + RandomNumberGenerator.NextFloat(GameConstants.BEAR_SPEED_RANGE);
            float   angle    = RandomNumberGenerator.NextFloat((float)(Math.PI * 2));
            Vector2 velocity = new Vector2(speed * (float)Math.Cos((double)angle), speed * (float)Math.Sin((double)angle));

            // create new bear
            TeddyBear newBear = new TeddyBear(Content, "teddybear", locationX, locationY, velocity, teddyBounce, teddyShot);

            // make sure we don't spawn into a collision
            List <Rectangle> collisionRectangles = GetCollisionRectangles();

            while (!CollisionUtils.IsCollisionFree(newBear.DrawRectangle, collisionRectangles))
            {
                newBear.X = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, graphics.PreferredBackBufferWidth - 2 * GameConstants.SPAWN_BORDER_SIZE);
                newBear.Y = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, graphics.PreferredBackBufferHeight - 2 * GameConstants.SPAWN_BORDER_SIZE);
            }

            // add new bear to list
            bears.Add(newBear);
        }
コード例 #9
0
        /// <summary>
        /// Spawns a new teddy bear at a random location
        /// </summary>
        private void SpawnBear()
        {
            // generate random location
            int X = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - (2 * GameConstants.SpawnBorderSize));
            int Y = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - (2 * GameConstants.SpawnBorderSize));

            // generate random velocity
            float   angle      = RandomNumberGenerator.NextFloat((float)Math.PI * 2);
            float   velocity   = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange);
            Vector2 bearVector = new Vector2(velocity * (float)Math.Cos(angle), velocity * (float)Math.Sin(angle));

            // create new bear
            TeddyBear newBear = new TeddyBear(Content, @"graphics\teddybear", X, Y, bearVector, null, null);

            // make sure we don't spawn into a collision
            List <Rectangle> collisionRectangles = GetCollisionRectangles();

            while (CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, collisionRectangles) == false)
            {
                newBear.X = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth -
                                              (2 * GameConstants.SpawnBorderSize));
                newBear.Y = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth -
                                              (2 * GameConstants.SpawnBorderSize));
            }

            // add new bear to list
            bears.Add(newBear);
        }
コード例 #10
0
        /// <summary>
        /// Spawns a new teddy bear at a random location
        /// </summary>
        private void SpawnBear()
        {
            // generate random location
            int bearX = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize);
            int bearY = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize);

            // generate random velocity
            float velMagnitude = RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange);
            if(velMagnitude < GameConstants.MinBearSpeed)
            {
                velMagnitude = GameConstants.MinBearSpeed;
            }
            float velAngle = RandomNumberGenerator.NextFloat(2 * (float)Math.PI);
            Vector2 bearVelocity = new Vector2((float)(velMagnitude * Math.Cos((double)velAngle)), (float)(velMagnitude * Math.Sin((double)velAngle)));

            // create new bear
            TeddyBear newBear = new TeddyBear(Content, @"graphics\teddyBear", bearX, bearY, bearVelocity, teddyBounce, teddyShot);

            // make sure we don't spawn into a collision
            List<Rectangle> collisionRectangles = GetCollisionRectangles();
            while(CollisionUtils.IsCollisionFree(newBear.DrawRectangle, collisionRectangles))
            {
                bearX = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize);
                bearY = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize);

                newBear.X = bearX;
                newBear.Y = bearY;
            }

            // add new bear to list
            bears.Add(newBear);
        }
コード例 #11
0
ファイル: Game1.cs プロジェクト: awasthi/C-Sharp-Examples
        /// <summary>
        /// Spawns a new teddy bear at a random location
        /// </summary>
        private void SpawnBear()
        {
            // generate random location
            int x = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE,
                                      graphics.PreferredBackBufferWidth - 2 * GameConstants.SPAWN_BORDER_SIZE);
            int y = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE,
                                      graphics.PreferredBackBufferHeight - 2 * GameConstants.SPAWN_BORDER_SIZE);

            // generate random velocity
            float speed = GameConstants.MIN_BEAR_SPEED +
                          RandomNumberGenerator.NextFloat(GameConstants.BEAR_SPEED_RANGE);
            float   angle    = RandomNumberGenerator.NextFloat(2 * (float)Math.PI);
            Vector2 velocity = new Vector2(
                (float)(speed * Math.Cos(angle)), (float)(speed * Math.Sin(angle)));

            // create new bear
            TeddyBear newBear = new TeddyBear(Content, "teddybear", x, y, velocity,
                                              null, null);

            // make sure we don't spawn into a collision
            List <Rectangle> CollisionRectangles = new List <Rectangle>(GetCollisionRectangles());

            while (CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, CollisionRectangles) == false)
            {
                newBear.X = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_WIDTH - (2 * GameConstants.SPAWN_BORDER_SIZE));
                newBear.Y = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_HEIGHT - (2 * GameConstants.SPAWN_BORDER_SIZE));
            }
            // add new bear to list
            bears.Add(newBear);
        }
コード例 #12
0
        /// <summary>
        /// Spawns a new teddy bear at a random location
        /// </summary>
        private void SpawnBear()
        {
            // generate random location
            int xLoc = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_WIDTH - 2 * GameConstants.SPAWN_BORDER_SIZE);
            int yLoc = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_HEIGHT - 2 * GameConstants.SPAWN_BORDER_SIZE);

            // generate random velocity
            float   speed    = GameConstants.MIN_BEAR_SPEED + RandomNumberGenerator.NextFloat(GameConstants.BEAR_SPEED_RANGE);
            float   angle    = RandomNumberGenerator.NextFloat(2 * (float)Math.PI);
            Vector2 velocity = new Vector2(speed * (float)Math.Cos(angle), speed * (float)Math.Sin(angle));

            // create new bear
            TeddyBear newBear = new TeddyBear(Content, "teddybear", xLoc, yLoc, velocity, teddyBounce, teddyShot);

            // make sure we don't spawn into a collision
            List <Rectangle> rectangles = GetCollisionRectangles();

            while (!CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, rectangles))
            {
                newBear.X = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_WIDTH - 2 * GameConstants.SPAWN_BORDER_SIZE);
                newBear.Y = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_HEIGHT - 2 * GameConstants.SPAWN_BORDER_SIZE);
            }


            // add new bear to list
            bears.Add(newBear);
        }
コード例 #13
0
        /// <summary>
        /// Spawns a new teddy bear at a random location
        /// </summary>
        private void SpawnBear()
        {
            // generate random location
            int bx = GetRandomLocation(GameConstants.SpawnBorderSize, graphics.PreferredBackBufferWidth - 2 * GameConstants.SpawnBorderSize);
            int by = GetRandomLocation(GameConstants.SpawnBorderSize, graphics.PreferredBackBufferWidth - 2 * GameConstants.SpawnBorderSize);

            // generate random velocity
            float   vel = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange);
            float   ang = RandomNumberGenerator.NextFloat((float)Math.PI);
            Vector2 vec = new Vector2((float)Math.Cos(ang) * vel, -1 * (float)Math.Sin(ang) * vel);

            // create new bear
            TeddyBear newbear = new TeddyBear(Content, @"graphics/teddybear", bx, by, vec, teddyBounce, teddyShot);

            // make sure we don't spawn into a collision
            List <Rectangle> collisionRectangules = GetCollisionRectangles();

            while (!CollisionUtils.IsCollisionFree(newbear.DrawRectangle, collisionRectangules))
            {
                newbear.X = GetRandomLocation(GameConstants.SpawnBorderSize, graphics.PreferredBackBufferWidth - 2 * GameConstants.SpawnBorderSize);
                newbear.Y = GetRandomLocation(GameConstants.SpawnBorderSize, graphics.PreferredBackBufferWidth - 2 * GameConstants.SpawnBorderSize);
            }

            // add new bear to list
            bears.Add(newbear);
        }
コード例 #14
0
        /// <summary>
        /// Spawns a new teddy bear at a random location
        /// </summary>
        private void SpawnBear()
        {
            // generate random location

            int x = GetRandomLocation(GameConstants.SpawnBorderSize,
                                      GameConstants.WindowWidth - 2 * GameConstants.SpawnBorderSize);
            int y = GetRandomLocation(GameConstants.SpawnBorderSize,
                                      GameConstants.WindowHeight - 2 * GameConstants.SpawnBorderSize);

            // generate random velocity

            float   speed    = RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange) + GameConstants.MinBearSpeed;
            float   angle    = RandomNumberGenerator.NextFloat(2.0f * (float)Math.PI);
            Vector2 velocity = new Vector2(speed * (float)Math.Cos(angle), speed * (float)Math.Sin(angle));

            // create new bear
            TeddyBear newBear = new TeddyBear(Content, @"graphics\teddybear", x, y, velocity, teddyBounce, teddyShot);

            // make sure we don't spawn into a collision
            List <Rectangle> collisionRectangles = GetCollisionRectangles();

            while (!CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, collisionRectangles))
            {
                newBear.X = GetRandomLocation(GameConstants.SpawnBorderSize,
                                              GameConstants.WindowWidth - 2 * GameConstants.SpawnBorderSize);
                newBear.Y = GetRandomLocation(GameConstants.SpawnBorderSize,
                                              GameConstants.WindowHeight - 2 * GameConstants.SpawnBorderSize);
            }

            // add new bear to list
            bears.Add(newBear);
        }
コード例 #15
0
        /// <summary>
        /// Spawns a new teddy bear at a random location
        /// </summary>
        private void SpawnBear()
        {
            // generate random location
            int locationX = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth);
            int locationY = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight);

            // generate random velocity
            float speed1 = RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange);

            float speed2 = RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange);

            /*     -------  Vidi za angle generator --------------
             * RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange);
             * // generate random angle            float angle = (float)Math.PI * RandomNumberGenerator.NextFloat(360.0f) / 180.0f;
             * // generate vector velocity            Vector2 velocity = new Vector2((float)Math.Cos(angle) * speed, -(float)Math.Sin(angle) * speed);
             */

            Vector2 vector = new Vector2(speed1, speed2);
            // create new bear
            TeddyBear newBear = new TeddyBear(Content, @"graphics/teddybear", locationX, locationY, vector, teddyBounce, teddyShot);

            // make sure we don't spawn into a collision
            List <Rectangle> collisionsList = GetCollisionRectangles();

            while (!CollisionUtils.IsCollisionFree(newBear.DrawRectangle, collisionsList))
            {
                newBear.X = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth);
                newBear.Y = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight);
            }

            // add new bear to list
            bears.Add(newBear);
        }
コード例 #16
0
        /// <summary>
        /// Spawns a new teddy bear at a random location
        /// </summary>
        private void SpawnBear()
        {
            // generate random location
            int y = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize * 2);
            int x = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize * 2);

            // generate random velocity
            float   speed    = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange);
            float   angle    = RandomNumberGenerator.NextFloat((float)Math.PI * 2);
            Vector2 velocity = new Vector2((float)(speed * Math.Cos(angle)), (float)(speed * Math.Sin(angle)));

            // create new bear
            TeddyBear bear = new TeddyBear(Content, @"graphics/teddybear", x, y, velocity, null, null);

            // make sure we don't spawn into a collision

            List <Rectangle> rectangles = GetCollisionRectangles();

            while (!CollisionUtils.IsCollisionFree(bear.CollisionRectangle, rectangles))
            {
                y = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize * 2);
                x = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize * 2);

                bear.X = x;
                bear.Y = y;
            }

            // add new bear to list
            bears.Add(bear);
        }
コード例 #17
0
        /// <summary>
        /// Spawns a new teddy bear at a random location
        /// </summary>
        private void SpawnBear()
        {
            // generate random location (bear sprite size = 46 x 64)
            int randomLocationX = GetRandomLocation(GameConstants.SpawnBorderSize,
                                                    GameConstants.WindowWidth - 2 * GameConstants.SpawnBorderSize);
            int randomLocationY = GetRandomLocation(GameConstants.SpawnBorderSize,
                                                    GameConstants.WindowHeight - 2 * GameConstants.SpawnBorderSize);

            // generate random velocity
            float bearRandomSpeed = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange);
            float randomAngle     = RandomNumberGenerator.NextFloat((float)Math.PI * 2);
            float speedX          = bearRandomSpeed * (float)Math.Cos(randomAngle);
            float speedY          = bearRandomSpeed * (float)Math.Sin(randomAngle);

            Vector2 bearVelocity = new Vector2(speedX, speedY);

            // create new bear
            TeddyBear newBear;

            newBear = new TeddyBear(Content, @"graphics\teddybear", randomLocationX, randomLocationY, bearVelocity, teddyBounce, teddyShot);

            // make sure we don't spawn into a collision

            while (!CollisionUtils.IsCollisionFree(newBear.DrawRectangle, GetCollisionRectangles()))
            {
                newBear.X = GetRandomLocation(GameConstants.SpawnBorderSize,
                                              GameConstants.WindowWidth - 2 * GameConstants.SpawnBorderSize);
                newBear.Y = GetRandomLocation(GameConstants.SpawnBorderSize,
                                              GameConstants.WindowHeight - 2 * GameConstants.SpawnBorderSize);
            }

            // add new bear to list
            bears.Add(newBear);
        }
コード例 #18
0
ファイル: Game1.cs プロジェクト: a2pr/TeddyBearProject
        /// <summary>
        /// Spawns a new teddy bear at a random location
        /// </summary>
        private void SpawnBear()
        {
            // generate random location
            int x = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize);
            int y = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize);
            // generate random velocity
            float BearSpeed = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange);
            //random angle
            float BearAngle = RandomNumberGenerator.NextFloat((float)Math.PI);
            //new vector2 object
            Vector2 Velocity = new Vector2((float)Math.Cos(BearAngle) * BearSpeed, ((float)Math.Sin(BearAngle) * BearSpeed));
            // create new bear
            TeddyBear newBear = new TeddyBear(Content, @"graphics\teddybear", x,
                                              y, Velocity, teddyBounce, teddyShot);
            // make sure we don't spawn into a collision
            List <Rectangle> collisionList = GetCollisionRectangles();

            while (!CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, collisionList))
            {
                newBear.X = GetRandomLocation(0, GameConstants.WindowWidth);
                newBear.Y = GetRandomLocation(0, GameConstants.WindowHeight);
            }

            // add new bear to list
            bears.Add(newBear);
        }
コード例 #19
0
        /// <summary>
        /// Spawns a new teddy bear at a random location
        /// </summary>
        private void SpawnBear()
        {
            // generate random location
            int X = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize);
            int Y = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize);

            // generate random velocity
            float XVelocity = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange);
            float YVelocity = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange);

            float   angle        = (float)((RandomNumberGenerator.NextFloat(360)) / (2 * Math.PI));
            Vector2 bearVelocity = new Vector2((float)(XVelocity * Math.Sin(angle)), (float)(YVelocity * Math.Cos(angle)));

            // create new bear
            TeddyBear newBear = new TeddyBear(Content, "Graphics//teddyBear", X, Y, bearVelocity, teddyBounce, teddyShot);
            // make sure we don't spawn into a collision
            List <Rectangle> collisionRectangles = GetCollisionRectangles();

            while (!CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, collisionRectangles))
            {
                newBear.X = GetRandomLocation(0, GameConstants.WindowWidth);
                newBear.Y = GetRandomLocation(0, GameConstants.WindowHeight);
            }
            bears.Add(newBear);


            // add new bear to list
        }
コード例 #20
0
        /// <summary>
        /// Spawns a new teddy bear at a random location
        /// </summary>
        private void SpawnBear()
        {
            // generate random location : SpawnBorderSize is subtracted twice, one for left/top side and one for right/bottom side

            int randomXLocation = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - (GameConstants.SpawnBorderSize * 2));
            int randomYLocation = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - (GameConstants.SpawnBorderSize * 2));

            // generate random velocity

            //create random speeds
            float randomSpeed = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange);

            //create random angle
            float randomAngle = 2 * (float)Math.PI * RandomNumberGenerator.NextFloat(1);

            //create Vector2
            Vector2 randomVelocity = new Vector2((float)Math.Cos(randomAngle) * randomSpeed, (float)Math.Sin(randomAngle) * randomSpeed);


            // create new bear
            TeddyBear newBear = new TeddyBear(Content, @"graphics\teddybear", randomXLocation, randomYLocation, randomVelocity, teddyBounce, teddyShot);

            // make sure we don't spawn into a collision
            while (CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, GetCollisionRectangles()) == false)
            {
                //change x and y values
                newBear.X = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - (GameConstants.SpawnBorderSize * 2));
                newBear.Y = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - (GameConstants.SpawnBorderSize * 2));
            }


            // add new bear to list
            bears.Add(newBear);
        }
コード例 #21
0
        /// <summary>
        /// Spawns a new teddy bear at a random location
        /// </summary>
        private void SpawnBear()
        {
            // generate random location
            int x = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_WIDTH - 2 * GameConstants.SPAWN_BORDER_SIZE);
            int y = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.WINDOW_HEIGHT - 2 * GameConstants.SPAWN_BORDER_SIZE);

            // generate random velocity
            float   bearSpeedValue = GameConstants.MIN_BEAR_SPEED + RandomNumberGenerator.NextFloat(GameConstants.BEAR_SPEED_RANGE);
            float   angle          = RandomNumberGenerator.NextFloat((float)Math.PI * 2);
            Vector2 bearVelocity   = new Vector2(bearSpeedValue * (float)Math.Cos(angle), (float)bearSpeedValue * (float)Math.Sin(angle));

            // create new bear
            TeddyBear newBear = new TeddyBear(Content, "teddybear", x, y, bearVelocity, teddyBounce, teddyShot);

            // make sure we don't spawn into a collision

            //Get list of collision rectangles
            List <Rectangle> collisionRectangles = GetCollisionRectangles();

            //Reset spawn location until collision free location is chosen
            while (!CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, collisionRectangles))
            {
                newBear.X = GetRandomLocation(0, GameConstants.WINDOW_WIDTH);
                newBear.Y = GetRandomLocation(0, GameConstants.WINDOW_HEIGHT);
            }


            // add new bear to list
            bears.Add(newBear);
        }
コード例 #22
0
ファイル: Game1.cs プロジェクト: DC-Shi/GameProgramming
        /// <summary>
        /// Spawns a new teddy bear at a random location
        /// </summary>
        private void SpawnBear()
        {
            // generate random location, note the location might be in border since we use it's center as (x,y)
            int x = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize);
            int y = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize);

            // generate random velocity
            float   v        = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange - GameConstants.MinBearSpeed);
            float   angle    = RandomNumberGenerator.NextFloat(2 * (float)Math.PI);
            Vector2 velocity = new Vector2((float)(v * Math.Cos(angle)), (float)(v * Math.Sin(angle)));

            // create new bear
            TeddyBear newBear = new TeddyBear(Content, @"graphics\teddybear", x, y, velocity, teddyBounce, teddyShot);

            // make sure we don't spawn into a collision
            while (!CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, GetCollisionRectangles()))
            {
                x       = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize);
                y       = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize);
                newBear = new TeddyBear(Content, @"graphics\teddybear", x, y, velocity, teddyBounce, teddyShot);
            }

            // add new bear to list
            bears.Add(newBear);
        }
コード例 #23
0
ファイル: Game1.cs プロジェクト: vnikitchyn/GameProject
        /// <summary>
        /// Spawns a new teddy bear at a random location
        /// </summary>
        private void SpawnBear()
        {
            // generate random location
            int xBearRandom = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize * 2 - Content.Load <Texture2D>(@"graphics\teddybear").Width);
            int yBearRandom = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize * 2 - Content.Load <Texture2D>(@"graphics\teddybear").Height);


            //GameConstants.SpawnBorderSize + RandomNumberGenerator.Next(GameConstants.WindowWidth - GameConstants.SpawnBorderSize * 2 + 1);

            // generate random velocity
            float speedBear = RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange);

            if (speedBear < GameConstants.MinBearSpeed)
            {
                speedBear = GameConstants.MinBearSpeed;
            }

            // a random angle using Math.PI and the RandomNumberGenerator NextFloat method
            float angleInRadians = RandomNumberGenerator.NextFloat(2 * (float)Math.PI); //360 = 2*Pi
            float angleInDegree  = angleInRadians * (180 / (float)Math.PI);

            //random speed and angles you generated and the appropriate trigonometry: speed as the length of hypotenuse of a right triangle
            float   xVelocity    = speedBear * (float)Math.Cos(angleInRadians);
            float   yVelocity    = speedBear * (float)Math.Sin(angleInRadians);
            Vector2 velocityBear = new Vector2(xVelocity, yVelocity);

            // create new bear  be speed * cos(angle) and the Y component would be speed * sin(angle)
            TeddyBear newBear = new TeddyBear(Content, @"graphics\teddybear", xBearRandom, yBearRandom, velocityBear, teddyBounce, teddyShot);

            // make sure we don't spawn into a collision
            List <Rectangle> collisions = GetCollisionRectangles();

            while (!CollisionUtils.IsCollisionFree(newBear.DrawRectangle, collisions))
            {
                int newXBearRandom = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowWidth - GameConstants.SpawnBorderSize * 2 - Content.Load <Texture2D>(@"graphics\teddybear").Width);
                int newYBearRandom = GetRandomLocation(GameConstants.SpawnBorderSize, GameConstants.WindowHeight - GameConstants.SpawnBorderSize * 2 - Content.Load <Texture2D>(@"graphics\teddybear").Height);
                newBear.X = newXBearRandom;
                newBear.Y = newYBearRandom;

                //alternate way ()instead of 2 above statements:
                //newBear = new TeddyBear(Content, @"graphics\teddybear", newXBearRandom, newYBearRandom, velocityBear, null, null);
            }


            // add new bear to list
            bears.Add(newBear);
        }
コード例 #24
0
ファイル: Game1.cs プロジェクト: papakosh/TeddyBearsAttack
        /// <summary>
        /// Spawns a new teddy bear at a random location
        /// </summary>
        private void SpawnBear()
        {
            // generate random location
            int xLocation = GetRandomLocation(0 + GameConstants.SpawnBorderSize,
                                              GameConstants.WindowWidth - GameConstants.SpawnBorderSize);
            int yLocation = GetRandomLocation(0 + GameConstants.SpawnBorderSize,
                                              GameConstants.WindowHeight - GameConstants.SpawnBorderSize);

            // generate random speed
            float speed = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange);

            // generate random angle
            float angle = RandomNumberGenerator.NextFloat((float)Math.PI);

            // generate random velocity
            // sine angle * speed = y velocity
            // cos angle * speed = x velocity
            float   xVelocity = (float)Math.Sin(angle) * speed;
            float   yVelocity = (float)Math.Cos(angle) * speed;
            Vector2 velocity  = new Vector2(xVelocity, yVelocity);

            // create new bear
            // make sure we don't spawn into a collision
            TeddyBear newBear = new TeddyBear(Content, @"graphics\teddybear", xLocation, yLocation, velocity, teddyBounce, teddyShot);

            // ensure teddy bears don't spawn into collisions
            List <Rectangle> allCollisions = GetCollisionRectangles();

            while (!CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, allCollisions))
            {
                int newXLocation = GetRandomLocation(0 + GameConstants.SpawnBorderSize, GameConstants.WindowWidth -
                                                     GameConstants.SpawnBorderSize);
                int newYLocation = GetRandomLocation(0 + GameConstants.SpawnBorderSize, GameConstants.WindowHeight -
                                                     GameConstants.SpawnBorderSize);
                newBear.X = newXLocation;
                newBear.Y = newYLocation;
            }

            // add new bear to list
            bears.Add(newBear);
        }
コード例 #25
0
ファイル: Game1.cs プロジェクト: CormacJ10/TeddyBearShootout
        /// <summary>
        /// Spawns a new teddy bear at a random location
        /// </summary>
        private void SpawnBear()
        {
            // generate random location
            int randX = RandomNumberGenerator.Next(GameConstants.SpawnBorderSize);
            int randY = RandomNumberGenerator.Next(GameConstants.SpawnBorderSize);
            // generate random velocity
            float   randSpeed    = GameConstants.MinBearSpeed + RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange);
            float   randAngle    = RandomNumberGenerator.NextFloat(2 * (float)Math.PI);
            Vector2 randVelocity = new Vector2(randSpeed * (float)Math.Cos(randAngle), randSpeed * (float)Math.Sin(randAngle));
            // create new bear
            TeddyBear newBear = new TeddyBear(Content, @"graphics\teddybear", randX, randY, randVelocity, teddyBounce, teddyShot);
            // make sure we don't spawn into a collision
            List <Rectangle> CollisionRectangles = GetCollisionRectangles();

            while (CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, CollisionRectangles) != true)
            {
                newBear.X = GetRandomLocation(GameConstants.SpawnBorderSize, graphics.PreferredBackBufferWidth - 2 * GameConstants.SpawnBorderSize);
                newBear.Y = GetRandomLocation(GameConstants.SpawnBorderSize, graphics.PreferredBackBufferHeight - 2 * GameConstants.SpawnBorderSize);
            }
            // add new bear to list
            bears.Add(newBear);
        }
コード例 #26
0
ファイル: Game1.cs プロジェクト: drazen41/GameProject
        /// <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();
            }

            // get current mouse state and update burger
            MouseState mouse = Mouse.GetState();

            burger.Update(gameTime, mouse);
            // update other game objects
            foreach (TeddyBear bear in bears)
            {
                bear.Update(gameTime);
            }
            foreach (Projectile projectile in projectiles)
            {
                projectile.Update(gameTime);
            }
            foreach (Explosion explosion in explosions)
            {
                explosion.Update(gameTime);
            }


            foreach (var bear in bears)
            {
                foreach (var projectile in projectiles)
                {
                    if (bear.Active && projectile.Active && bear.CollisionRectangle.Intersects(projectile.CollisionRectangle))
                    {
                        // check and resolve collisions between teddy bears and projectiles
                        if (projectile.Type == ProjectileType.FrenchFries)
                        {
                            bear.Active       = false;
                            projectile.Active = false;
                            explosions.Add(new Explosion(explosionSpriteStrip, bear.Location.X, bear.Location.Y));
                        }
                    }
                }
            }
            // check and resolve collisions between teddy bears
            for (int i = 0; i < bears.Count; i++)
            {
                for (int j = i + 1; j < bears.Count; j++)
                {
                    if (bears[i].Active && bears[j].Active)
                    {
                        CollisionResolutionInfo resolutionInfo = CollisionUtils.CheckCollision(gameTime.ElapsedGameTime.Milliseconds, GameConstants.WindowWidth,
                                                                                               GameConstants.WindowHeight, bears[i].Velocity, bears[i].DrawRectangle, bears[j].Velocity, bears[j].DrawRectangle);
                        if (resolutionInfo != null)
                        {
                            if (resolutionInfo.FirstOutOfBounds)
                            {
                                bears[i].Active = false;
                            }
                            else
                            {
                                bears[i].DrawRectangle = resolutionInfo.FirstDrawRectangle;
                                bears[i].Velocity      = resolutionInfo.FirstVelocity;
                            }
                            if (resolutionInfo.SecondOutOfBounds)
                            {
                                bears[j].Active = false;
                            }
                            else
                            {
                                bears[j].DrawRectangle = resolutionInfo.SecondDrawRectangle;
                                bears[j].Velocity      = resolutionInfo.SecondVelocity;
                            }
                        }
                    }
                }
            }

            // check and resolve collisions between burger and teddy bears
            foreach (var bear in bears)
            {
                if (bear.Active && bear.CollisionRectangle.Intersects(burger.CollisionRectangle))
                {
                    burger.Health -= GameConstants.BearDamage;
                    bear.Active    = false;
                    Explosion explosion = new Explosion(explosionSpriteStrip, bear.DrawRectangle.X, bear.DrawRectangle.Y);
                    explosions.Add(explosion);
                }
            }
            // check and resolve collisions between burger and projectiles
            foreach (var projectile in projectiles)
            {
                if (projectile.Type == ProjectileType.TeddyBear && projectile.Active && projectile.CollisionRectangle.Intersects(burger.CollisionRectangle))
                {
                    burger.Health    -= GameConstants.TeddyBearProjectileDamage;
                    projectile.Active = false;
                }
            }


            // clean out inactive teddy bears and add new ones as necessary
            for (int i = bears.Count - 1; i >= 0; i--)
            {
                if (!bears[i].Active)
                {
                    bears.RemoveAt(i);
                }
            }
            while (bears.Count != GameConstants.MaxBears)
            {
                SpawnBear();
            }
            // clean out inactive projectiles
            for (int i = projectiles.Count - 1; i >= 0; i--)
            {
                if (!projectiles[i].Active)
                {
                    projectiles.RemoveAt(i);
                }
            }
            // clean out finished explosions
            for (int i = explosions.Count - 1; i >= 0; i--)
            {
                if (explosions[i].Finished)
                {
                    explosions.RemoveAt(i);
                }
            }
            base.Update(gameTime);
        }
コード例 #27
0
ファイル: Game1.cs プロジェクト: awasthi/C-Sharp-Examples
        /// <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)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
                Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }

            // get current mouse state and update burger
            MouseState mouse = Mouse.GetState();

            burger.Update(gameTime, mouse);

            // update other game objects
            foreach (TeddyBear bear in bears)
            {
                bear.Update(gameTime);
            }
            foreach (Projectile projectile in projectiles)
            {
                projectile.Update(gameTime);
            }
            foreach (Explosion explosion in explosions)
            {
                explosion.Update(gameTime);
            }

            // check and resolve collisions between teddy bears
            for (int i = 0; i < bears.Count; i++)
            {
                for (int j = i + 1; j < bears.Count - 1; j++)
                {
                    if (bears[i].Active && bears[j].Active)
                    {
                        CollisionResolutionInfo teddyCollision = CollisionUtils.CheckCollision(gameTime.ElapsedGameTime.Milliseconds, GameConstants.WINDOW_WIDTH, GameConstants.WINDOW_HEIGHT, bears[i].Velocity, bears[i].CollisionRectangle, bears[j].Velocity, bears[j].CollisionRectangle);

                        if (teddyCollision != null)
                        {
                            if (teddyCollision.FirstOutOfBounds)
                            {
                                bears[i].Active = false;
                            }
                            else if (!teddyCollision.FirstOutOfBounds)
                            {
                                bears[i].Velocity      = teddyCollision.FirstVelocity;
                                bears[i].DrawRectangle = teddyCollision.FirstDrawRectangle;
                            }
                            if (teddyCollision.SecondOutOfBounds)
                            {
                                bears[j].Active = false;
                            }
                            else if (!teddyCollision.SecondOutOfBounds)
                            {
                                bears[j].Velocity      = teddyCollision.SecondVelocity;
                                bears[j].DrawRectangle = teddyCollision.SecondDrawRectangle;
                            }
                        }
                    }
                }
            }

            // check and resolve collisions between burger and teddy bears

            // check and resolve collisions between burger and projectiles

            // check and resolve collisions between teddy bears and projectiles
            foreach (TeddyBear bear in bears)
            {
                foreach (Projectile projectile in projectiles)
                {
                    if (projectile.Type == ProjectileType.FrenchFries && bear.Active && projectile.Active &&
                        bear.CollisionRectangle.Intersects(projectile.CollisionRectangle))
                    {
                        bear.Active       = false;
                        projectile.Active = false;
                        explosions.Add(new Explosion(explosionSpriteStrip, bear.CollisionRectangle.Center.X, bear.CollisionRectangle.Center.Y));
                    }
                }
            }


            // clean out inactive teddy bears and add new ones as necessary
            for (int i = bears.Count - 1; i >= 0; i--)
            {
                if (bears[i].Active == false)
                {
                    bears.RemoveAt(i);
                }
                if (bears.Count < GameConstants.MAX_BEARS)
                {
                    SpawnBear();
                }
            }

            // clean out inactive projectiles
            for (int i = projectiles.Count - 1; i >= 0; i--)
            {
                if (projectiles[i].Active == false)
                {
                    projectiles.RemoveAt(i);
                }
            }

            // clean out finished explosions

            base.Update(gameTime);
        }
コード例 #28
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();
            }

            // get current mouse state and update burger
            KeyboardState keyboard = Keyboard.GetState();

            burger.Update(gameTime, keyboard);

            // update other game objects
            foreach (TeddyBear bear in bears)
            {
                bear.Update(gameTime);

                foreach (Projectile projectile in projectiles)
                {
                    //Deactive teddies hit by a projectile
                    if (projectile.Type == ProjectileType.FrenchFries)
                    {
                        if (bear.Active && projectile.Active &&
                            bear.CollisionRectangle.Intersects(projectile.CollisionRectangle))
                        {
                            bear.Active       = false;
                            projectile.Active = false;
                            score            += GameConstants.BearPoints;
                            setScoreString();
                            explosions.Add(new Explosion(explosionSpriteStrip, bear.Location.X, bear.Location.Y, explosion));
                        }
                    }
                }

                // check and resolve collisions between burger and teddy bears
                if (bear.Active && bear.CollisionRectangle.Intersects(burger.CollisionRectangle))
                {
                    burger.Health -= GameConstants.BearDamage;
                    setBurgerHealthString();
                    burgerDamage.Play();
                    bear.Active = false;
                    explosions.Add(new Explosion(explosionSpriteStrip, burger.CollisionRectangle.X, burger.CollisionRectangle.Y, explosion));
                    CheckBurgerKill();
                }
            }

            foreach (Explosion explosion in explosions)
            {
                explosion.Update(gameTime);
            }

            // check and resolve collisions between teddy bears
            for (int i = 0; i < bears.Count; i++)
            {
                for (int j = i + 1; j < bears.Count; j++)
                {
                    if (bears[i].Active && bears[j].Active)
                    {
                        CollisionResolutionInfo collisionInfo;

                        collisionInfo = CollisionUtils.CheckCollision(gameTime.ElapsedGameTime.Milliseconds,
                                                                      GameConstants.WindowWidth, GameConstants.WindowHeight, bears[i].Velocity,
                                                                      bears[i].DrawRectangle, bears[j].Velocity, bears[j].DrawRectangle);

                        if (collisionInfo != null)
                        {
                            //First collisioned teddy
                            if (collisionInfo.FirstOutOfBounds)
                            {
                                bears[i].Active = false;
                            }
                            else
                            {
                                bears[i].Velocity      = collisionInfo.FirstVelocity;
                                bears[i].DrawRectangle = collisionInfo.FirstDrawRectangle;
                            }

                            //Second collisioned teddy
                            if (collisionInfo.SecondOutOfBounds)
                            {
                                bears[j].Active = false;
                            }
                            else
                            {
                                bears[j].Velocity      = collisionInfo.SecondVelocity;
                                bears[j].DrawRectangle = collisionInfo.SecondDrawRectangle;
                            }

                            // Collision sound
                            teddyBounce.Play();
                        }
                    }
                }
            }


            // check and resolve collisions between burger and projectiles
            foreach (Projectile p in projectiles)
            {
                p.Update(gameTime);

                if (p.Active && p.Type == ProjectileType.TeddyBear && p.CollisionRectangle.Intersects(burger.CollisionRectangle))
                {
                    p.Active       = false;
                    burger.Health -= GameConstants.FrenchFriesProjectileDamage;
                    setBurgerHealthString();
                    burgerDamage.Play();
                    CheckBurgerKill();
                }
            }

            // clean out inactive teddy bears and add new ones as necessary
            for (int i = bears.Count - 1; i >= 0; i--)
            {
                if
                (!bears[i].Active)
                {
                    bears.RemoveAt(i);
                }
            }

            // Spawn bears until we reach MaxBears constant
            while (bears.Count < GameConstants.MaxBears)
            {
                this.SpawnBear();
            }

            // clean out inactive projectiles
            for (int i = projectiles.Count - 1; i >= 0; i--)
            {
                if
                (!projectiles[i].Active)
                {
                    projectiles.RemoveAt(i);
                }
            }

            // clean out finished explosions
            for (int i = explosions.Count - 1; i >= 0; i--)
            {
                if
                (explosions[i].Finished)
                {
                    explosions.RemoveAt(i);
                }
            }



            base.Update(gameTime);
        }
コード例 #29
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)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
                Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }

            // get current mouse state and update burger
            MouseState    mouse    = Mouse.GetState();
            KeyboardState keyboard = Keyboard.GetState();

            //burger.Update(gameTime, mouse);
            burger.Update(gameTime, keyboard);

            // update other game objects
            foreach (TeddyBear bear in bears)
            {
                bear.Update(gameTime);
            }
            foreach (Projectile projectile in projectiles)
            {
                projectile.Update(gameTime);
            }
            foreach (Explosion explosion in explosions)
            {
                explosion.Update(gameTime);
            }

            // check and resolve collisions between teddy bears
            for (int i = 0; i < bears.Count; i++)
            {
                for (int j = i + 1; j < bears.Count; j++)
                {
                    //two bears are active and they collide
                    if (bears[i].CollisionRectangle.Intersects(bears[j].CollisionRectangle) && bears[i].Active && bears[j].Active)
                    {
                        //use the collision utils to get the info
                        CollisionResolutionInfo collisionInfoItem = CollisionUtils.CheckCollision(gameTime.ElapsedGameTime.Milliseconds, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight, bears[i].Velocity, bears[i].DrawRectangle, bears[j].Velocity, bears[j].DrawRectangle);

                        //there was a collision to resolve
                        if (collisionInfoItem != null)
                        {
                            //collision put a bear outside the window, deactivate it
                            if (collisionInfoItem.FirstOutOfBounds)
                            {
                                bears[i].Active = false;
                            }
                            else
                            {
                                //change the draw rectangle and direction of the bear according to the util function
                                bears[i].Velocity      = collisionInfoItem.FirstVelocity;
                                bears[i].DrawRectangle = collisionInfoItem.FirstDrawRectangle;
                                teddyBounce.Play();
                            }

                            //collision put a bear outside the window, deactivate it
                            if (collisionInfoItem.SecondOutOfBounds)
                            {
                                bears[j].Active = false;
                            }
                            else
                            {
                                //change the draw rectangle and direction of the bear according to the util function
                                bears[j].Velocity      = collisionInfoItem.SecondVelocity;
                                bears[j].DrawRectangle = collisionInfoItem.SecondDrawRectangle;
                                teddyBounce.Play();
                            }
                        }
                    }
                }
            }

            // check and resolve collisions between burger and teddy bears
            foreach (TeddyBear bear in bears)
            {
                if (bear.CollisionRectangle.Intersects(burger.CollisionRectangle) && bear.Active)
                {
                    burger.Health -= GameConstants.BEAR_DAMAGE;
                    CheckBurgerKill();
                    bear.Active = false;
                    explosions.Add(new Explosion(explosionSpriteStrip, bear.Location.X, bear.Location.Y, explosion));
                    healthString = GameConstants.HEALTH_PREFIX + burger.Health.ToString();
                    burgerDamage.Play();
                }
            }

            // check and resolve collisions between burger and projectiles
            foreach (Projectile projectile in projectiles)
            {
                if (projectile.Type == ProjectileType.TeddyBear && projectile.Active && projectile.CollisionRectangle.Intersects(burger.CollisionRectangle))
                {
                    burger.Health -= GameConstants.TEDDY_BEAR_PROJECTILE_DAMAGE;
                    CheckBurgerKill();
                    projectile.Active = false;
                    healthString      = GameConstants.HEALTH_PREFIX + burger.Health.ToString();
                    burgerDamage.Play();
                }
            }

            // check and resolve collisions between teddy bears and projectiles
            foreach (TeddyBear bear in bears)
            {
                foreach (Projectile projectile in projectiles)
                {
                    //active french fry projectile hits an active bear actor
                    if (projectile.Type == ProjectileType.FrenchFries && bear.CollisionRectangle.Intersects(projectile.CollisionRectangle) && projectile.Active && bear.Active)
                    {
                        bear.Active       = false;
                        projectile.Active = false;
                        //play the explosion at the bear's location
                        explosions.Add(new Explosion(explosionSpriteStrip, bear.Location.X, bear.Location.Y, explosion));

                        score      += GameConstants.BEAR_POINTS;
                        scoreString = GameConstants.SCORE_PREFIX + score;
                    }
                }
            }

            // clean out inactive teddy bears and add new ones as necessary
            for (int i = bears.Count - 1; i >= 0; i--)
            {
                if (!bears[i].Active)
                {
                    bears.RemoveAt(i);
                }
            }
            //later, we'll add bears up to the max again
            while (bears.Count < GameConstants.MAX_BEARS)
            {
                SpawnBear();
            }

            // clean out inactive projectiles
            for (int i = projectiles.Count - 1; i >= 0; i--)
            {
                if (!projectiles[i].Active)
                {
                    projectiles.RemoveAt(i);
                }
            }

            // clean out finished explosions
            for (int i = explosions.Count - 1; i >= 0; i--)
            {
                if (explosions[i].Finished)
                {
                    explosions.RemoveAt(i);
                }
            }

            base.Update(gameTime);
        }
コード例 #30
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)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
                Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }

            // get current keyboard state and update burger
            KeyboardState keyboard = Keyboard.GetState();

            burger.Update(gameTime, keyboard);

            // update other game objects
            foreach (TeddyBear bear in bears)
            {
                bear.Update(gameTime);
            }
            foreach (Projectile projectile in projectiles)
            {
                projectile.Update(gameTime);
            }
            foreach (Explosion explosion in explosions)
            {
                explosion.Update(gameTime);
            }

            // check and resolve collisions between teddy bears
            for (int i = 0; i < bears.Count; i++)
            {
                for (int j = i + 1; j < bears.Count; j++)
                {
                    if (bears[i].Active && bears[j].Active)
                    {
                        //Getting collision info
                        CollisionResolutionInfo CRI = CollisionUtils.CheckCollision(gameTime.ElapsedGameTime.Milliseconds,
                                                                                    GameConstants.WINDOW_WIDTH, GameConstants.WINDOW_HEIGHT,
                                                                                    bears[i].Velocity, bears[i].CollisionRectangle,
                                                                                    bears[j].Velocity, bears[j].CollisionRectangle);

                        //checking for collision
                        if (CRI != null)
                        {
                            //play bounce sound
                            teddyBounce.Play();

                            //dealing with first teddy (i)
                            if (CRI.FirstOutOfBounds)
                            {
                                bears[i].Active = false;
                            }
                            else
                            {
                                bears[i].Velocity      = CRI.FirstVelocity;
                                bears[i].DrawRectangle = CRI.FirstDrawRectangle;
                            }

                            //dealing with second teddy (j)
                            if (CRI.SecondOutOfBounds)
                            {
                                bears[j].Active = false;
                            }
                            else
                            {
                                bears[j].Velocity      = CRI.SecondVelocity;
                                bears[j].DrawRectangle = CRI.SecondDrawRectangle;
                            }
                        }
                    }
                }
            }


            // check and resolve collisions between burger and teddy bears
            foreach (TeddyBear teddy in bears)
            {
                //if burger and teddy collide
                if (teddy.Active && burger.CollisionRectangle.Intersects(teddy.CollisionRectangle) && burger.Health > 0)
                {
                    //play sound
                    burgerDamage.Play();

                    //decrease burger health and change health score
                    burger.Health -= GameConstants.BEAR_DAMAGE;
                    healthString   = GameConstants.HEALTH_PREFIX + burger.Health;
                    if (!burgerDead)
                    {
                        CheckBurgerKill();
                    }

                    //destroy teddy and explode him
                    Explosion explosion = new Explosion(explosionSpriteStrip, teddy.Location.X, teddy.Location.Y, explosionSound);
                    explosions.Add(explosion);
                    teddy.Active = false;
                }
            }

            // check and resolve collisions between burger and projectiles
            foreach (Projectile projectile in projectiles)
            {
                //if projectile is an Active teddy bear and collides with burger
                if (projectile.Active && projectile.Type == ProjectileType.TeddyBear &&
                    projectile.CollisionRectangle.Intersects(burger.CollisionRectangle) && burger.Health > 0)
                {
                    //destroy projectile
                    projectile.Active = false;

                    //decrease burger health and change health score
                    burger.Health -= GameConstants.TEDDY_BEAR_PROJECTILE_DAMAGE;
                    healthString   = GameConstants.HEALTH_PREFIX + burger.Health;
                    if (!burgerDead)
                    {
                        CheckBurgerKill();
                    }
                }
            }


            // check and resolve collisions between teddy bears and projectiles
            foreach (TeddyBear bear in bears)
            {
                foreach (Projectile projectile in projectiles)
                {
                    if (projectile.Type == ProjectileType.FrenchFries && projectile.Active && bear.Active && bear.CollisionRectangle.Intersects(projectile.CollisionRectangle))
                    {
                        //deactivate colliding bear and projectile
                        bear.Active       = false;
                        projectile.Active = false;

                        //increase score
                        score      += GameConstants.BEAR_POINTS;
                        scoreString = GameConstants.SCORE_PREFIX + score;

                        //create new exploson object and add to the list
                        Explosion explosion = new Explosion(explosionSpriteStrip,
                                                            bear.Location.X, bear.Location.Y,
                                                            explosionSound);
                        explosions.Add(explosion);
                    }
                }
            }


            // clean out inactive teddy bears and add new ones as necessary
            for (int i = bears.Count - 1; i >= 0; i--)
            {
                if (!bears[i].Active)
                {
                    bears.RemoveAt(i);
                }
            }

            while (bears.Count != GameConstants.MAX_BEARS)
            {
                SpawnBear();
            }

            // clean out inactive projectiles
            for (int i = projectiles.Count - 1; i >= 0; i--)
            {
                if (!projectiles[i].Active)
                {
                    projectiles.RemoveAt(i);
                }
            }

            // clean out finished explosions
            for (int i = explosions.Count - 1; i >= 0; i--)
            {
                if (explosions[i].Finished)
                {
                    explosions.RemoveAt(i);
                }
            }
            base.Update(gameTime);
        }