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

            //check for collision
            if (bear0.Active &&
                bear1.Active &&
                bear0.DrawRectangle.Intersects(bear1.DrawRectangle))
            {
                bear0.Active = false;
                bear1.Active = false;


                Rectangle collisionRectangle = Rectangle.Intersect(bear0.DrawRectangle, bear1.DrawRectangle);
                explosion.Play(collisionRectangle.Center.X, collisionRectangle.Center.Y);
            }
            //playing explosion
            explosion.Update(gameTime);

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

            // Updating  the two teddy bears
            teddyBear0.Update();
            teddyBear1.Update();

            // Explode the teddy bears when they collide
            Rectangle teddyBearRectangle0 = teddyBear0.DrawRectangle;
            Rectangle teddyBearRectangle1 = teddyBear1.DrawRectangle;

            if ((teddyBearRectangle0.X / 2 == teddyBearRectangle1.X / 2) &&
                (teddyBearRectangle0.Y / 2 == teddyBearRectangle1.Y / 2))
            {
                // If there is a collision, teddy bears active is false
                teddyBear0.Active = false;
                teddyBear1.Active = false;
                // Play explosion after collision
                explosion.Play(teddyBearRectangle0.Location.X + 5, teddyBearRectangle0.Location.Y + 5);
            }
            explosion.Update(gameTime);
            base.Update(gameTime);
        }
コード例 #3
0
ファイル: Game1.cs プロジェクト: an-doan/csharp-programs
        /// <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
            MouseState mouse = Mouse.GetState();

            //if(bear.DrawRectangle.Contains(mouse.Position) && mouse.LeftButton == ButtonState.Released &&
            //    previousLeftButtonState == ButtonState.Pressed)
            //{
            //    bear.Active = false;
            //    explosion.Play(bear.DrawRectangle.X, bear.DrawRectangle.Y);
            //}

            if (bear.DrawRectangle.Contains(mouse.Position) && mouse.RightButton == ButtonState.Released && previousRightButtonState == ButtonState.Pressed)
            {
                bear.Active = false;
                explosion.Play(bear.DrawRectangle.X, bear.DrawRectangle.Y);
                SpawnBear();
            }

            bear.Update(gameTime);
            explosion.Update(gameTime);

            base.Update(gameTime);
            previousLeftButtonState  = mouse.LeftButton;
            previousRightButtonState = mouse.RightButton;
        }
コード例 #4
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)
            {
                this.Exit();
            }

            // TODO: Add your update logic here
            // update game objects
            teddy0.Update();
            teddy1.Update();
            explode0.Update(gameTime);

            // check for collision
            //if (teddy0.Active && teddy1.Active && teddy0.DrawRectangle.X == teddy1.DrawRectangle.X)
            if (teddy0.Active && teddy1.Active && teddy0.DrawRectangle.Intersects(teddy1.DrawRectangle))
            {
                // deactivate bears
                teddy0.Active = teddy1.Active = false;

                // play explosion at approximately the point of collision
                //explode0.Play(teddy0.DrawRectangle.X, teddy0.DrawRectangle.Y);
                Rectangle collisionRectangle = Rectangle.Intersect(teddy0.DrawRectangle, teddy1.DrawRectangle);
                explode0.Play(collisionRectangle.Center.X, collisionRectangle.Center.Y);
            }
            base.Update(gameTime);
        }
コード例 #5
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)
        {
            // For Mobile devices, this logic will close the Game when the Back button is pressed
            // Exit() is obsolete on iOS
                        #if !__IOS__ && !__TVOS__
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }
                        #endif

            // TODO: Add your update logic here
            teddyBear0.Update(gameTime);
            teddyBear1.Update(gameTime);

            if (teddyBear0.Active &&
                teddyBear0.DrawRectangle.Intersects(teddyBear1.DrawRectangle))
            {
                teddyBear0.Active = false;
                teddyBear1.Active = false;
                explosion.Play(teddyBear0.DrawRectangle.Right,
                               teddyBear0.DrawRectangle.Center.Y);
            }

            explosion.Update(gameTime);

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

            MouseState mouse = Mouse.GetState();

            if (teddyBear.DrawRectangle.Contains(mouse.Position) && mouse.LeftButton == ButtonState.Pressed)
            {
                teddyBear.Active = false;
                Point location = teddyBear.DrawRectangle.Center;
                explosion.Play(location.X, location.Y);

                Vector2 velocity = new Vector2((float)rand.NextDouble(), (float)rand.NextDouble());
                velocity *= 0.2f;
                teddyBear = new TeddyBear(Content, WindowWidth, WindowHeight, "teddybear", rand.Next(WindowWidth), rand.Next(WindowHeight), velocity);
            }

            teddyBear.Update(gameTime);
            explosion.Update(gameTime);

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

            // move the teddybears
            bear0.Update(gameTime);
            bear1.Update(gameTime);

            // playing the explosion
            explosion.Update(gameTime);

            // detect the collision and play the explosion
            if (bear0.Active && bear1.Active && bear0.DrawRectangle.Intersects(bear1.DrawRectangle))
            {
                bear0.Active = false;
                bear1.Active = false;

                // get the collision point of two teddybears
                Rectangle collisionRectangle = Rectangle.Intersect(bear0.DrawRectangle, bear1.DrawRectangle);
                explosion.Play(collisionRectangle.Center.X, collisionRectangle.Center.Y);
            }

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

            // update objects
            teddy.Update(gameTime);
            explosion.Update(gameTime);


            // explode teddy with click
            if (Mouse.GetState().LeftButton == ButtonState.Pressed &&
                teddy.DrawRectangle.Contains(Mouse.GetState().X, Mouse.GetState().Y))
            {
                teddy.Active = false;
                explosion.Play(teddy.DrawRectangle.Center.X, teddy.DrawRectangle.Center.Y);
            }

            // create new teddy with rick click after explode it
            if (teddy.Active == false && Mouse.GetState().RightButton == ButtonState.Pressed)
            {
                Vector2 velocity = new Vector2((float)rand.NextDouble(), (float)rand.NextDouble()) / 5;
                teddy = new TeddyBear(Content, WindowWidth, WindowHeight, "teddybear", WindowWidth / 2, WindowHeight / 2, velocity);
            }

            base.Update(gameTime);
        }
コード例 #9
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)
        {
            // For Mobile devices, this logic will close the Game when the Back button is pressed
            // Exit() is obsolete on iOS
                        #if !__IOS__ && !__TVOS__
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }
                        #endif

            // TODO: Add your update logic here
            teddyBear.Update(gameTime);
            explosion.Update(gameTime);

            MouseState state = Mouse.GetState();

            if (state.X >= teddyBear.DrawRectangle.Left && state.X <= teddyBear.DrawRectangle.Right &&
                state.Y >= teddyBear.DrawRectangle.Top && state.Y <= teddyBear.DrawRectangle.Bottom &&
                state.LeftButton == ButtonState.Pressed)
            {
                teddyBear.Active = false;
                explosion.Play(teddyBear.DrawRectangle.X, teddyBear.DrawRectangle.Y);
            }

            base.Update(gameTime);
        }
コード例 #10
0
ファイル: Game1.cs プロジェクト: HoodPanther/CSharpLearning
        /// <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)
            {
                this.Exit();
            }
            if (GamePad.GetState(PlayerIndex.One).Buttons.B == ButtonState.Pressed)
            {
                // Create a new teddy bear object
                int x_velocity = rand.Next(1, 2);
                int y_velocity = rand.Next(1, 2);
                int x_rand     = rand.Next(0, WINDOW_WIDTH);
                int y_rand     = rand.Next(0, WINDOW_HEIGHT);

                teddyBear.Active = true;

                teddyBear = new TeddyBear(Content, WINDOW_WIDTH, WINDOW_HEIGHT, "teddybear", x_rand, y_rand, new Vector2(x_velocity, y_velocity));
            }

            if (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed)
            {
                explosion.Play((teddyBear.DrawRectangle.X + teddyBear.DrawRectangle.Width / 2), (teddyBear.DrawRectangle.Y + teddyBear.DrawRectangle.Height / 2));
                teddyBear.Active = false;
            }


            teddyBear.Update();
            explosion.Update(gameTime);

            base.Update(gameTime);
        }
コード例 #11
0
ファイル: Game1.cs プロジェクト: an-doan/csharp-programs
        /// <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
            bear0.Update(gameTime);
            bear1.Update(gameTime);

            //if bears collide and bears are active
            if (bear0.Active &&
                bear1.Active &&
                bear0.DrawRectangle.Intersects(bear1.DrawRectangle))
            {
                //make bears inactive
                bear0.Active = false;
                bear1.Active = false;

                //find intersection of the two rectangles of the bears
                Rectangle intersection = Rectangle.Intersect(bear0.DrawRectangle, bear1.DrawRectangle);
                explosion.Play(intersection.X, intersection.Y);
            }
            else
            {
                bear0.Active = true;
                bear1.Active = true;
            }

            explosion.Update(gameTime);

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

            teddyBear.Update();
            // X = x - w/2;
            int teddyBearLocationX = teddyBear.DrawRectangle.X + teddyBear.DrawRectangle.Width;
            int teddyBearLocationY = teddyBear.DrawRectangle.Y + teddyBear.DrawRectangle.Height;



            if ((Mouse.GetState().X > teddyBear.DrawRectangle.X) &&
                (Mouse.GetState().X < teddyBearLocationX) &&
                (Mouse.GetState().Y > teddyBear.DrawRectangle.Y) &&
                (Mouse.GetState().Y < teddyBearLocationY) &&
                (Mouse.GetState().LeftButton == ButtonState.Pressed))
            {
                teddyBear.Active = false;
                explosion.Play(teddyBearLocationX, teddyBearLocationY);
            }

            explosion.Update(gameTime);

            base.Update(gameTime);
        }
コード例 #13
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
            teddy.Update(gameTime);
            explosion.Update(gameTime);
            MouseState mouse = Mouse.GetState();

            int x = mouse.Position.X;
            int y = mouse.Position.Y;

            if (teddy.DrawRectangle.Contains(x, y) && mouse.LeftButton == ButtonState.Released &&
                previousLeftButtonState == ButtonState.Pressed)
            {
                teddy.Active = false;
                explosion.Play(teddy.DrawRectangle.X, teddy.DrawRectangle.Y);
            }

            if (previousMiddleButtonState == ButtonState.Pressed)
            {
                teddy = new TeddyBear(Content, WindowWidth, WindowHeight, @"graphics/teddybear",
                                      300, 200, new Vector2((float)rdn.NextDouble() * (float)0.5, (float)rdn.NextDouble() * (float)0.5));
            }

            if (previousRightButtonState == ButtonState.Pressed)
            {
                explosion.Play(teddy.DrawRectangle.X, teddy.DrawRectangle.Y);
                teddy = new TeddyBear(Content, WindowWidth, WindowHeight, @"graphics/teddybear",
                                      300, 200, new Vector2((float)rdn.NextDouble() * (float)0.5, (float)rdn.NextDouble() * (float)0.5));
            }


            previousLeftButtonState   = mouse.LeftButton;
            previousMiddleButtonState = mouse.MiddleButton;
            previousRightButtonState  = mouse.RightButton;

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

            teddy0.Update(gameTime);
            teddy1.Update(gameTime);

            if (teddy0.Active && teddy1.Active &&
                teddy0.DrawRectangle.Intersects(teddy1.DrawRectangle))
            {
                teddy0.Active = false;
                teddy1.Active = false;
                explosion.Play(teddy0.DrawRectangle.X, teddy0.DrawRectangle.Y);
            }
            explosion.Update(gameTime);
            base.Update(gameTime);
        }
コード例 #15
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();
            }

            MouseState mouseState    = Mouse.GetState();
            Rectangle  bearRectangle = bear.DrawRectangle;

            bool isMouseOverBear = (mouseState.X >= bearRectangle.Left && mouseState.X <= bearRectangle.Right &&
                                    mouseState.Y >= bearRectangle.Top && mouseState.Y <= bearRectangle.Bottom);

            if (isMouseOverBear && mouseState.LeftButton == ButtonState.Pressed && bear.Active)
            {
                bear.Active = false;
                explosion.Play(bearRectangle.Center.X, bearRectangle.Center.Y);
            }

            if (mouseState.MiddleButton == ButtonState.Released && mmbPreviousState == ButtonState.Pressed)
            {
                bear = GetRandomTeddyBear();
            }

            if (mouseState.RightButton == ButtonState.Released && rmbPreviousState == ButtonState.Pressed && bear.Active)
            {
                bear.Active = false;
                explosion.Play(bearRectangle.Center.X, bearRectangle.Center.Y);
                bear = GetRandomTeddyBear();
            }

            rmbPreviousState = mouseState.RightButton;
            mmbPreviousState = mouseState.MiddleButton;

            bear.Update(gameTime);
            explosion.Update(gameTime);

            base.Update(gameTime);
        }
コード例 #16
0
ファイル: Game1.cs プロジェクト: kingwatam/misc_csharp
        /// <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)
            {
                this.Exit();
            }

            // TODO: Add your update logic here
            bear0.Update();
            bear1.Update();
            explosion.Update(gameTime);

            //get mouse state
            MouseState mouse = Mouse.GetState();

            //explode bears if mouse is over it and left mouse button is pressed
            if (bear0.Active &&
                mouse.LeftButton == ButtonState.Pressed &&
                previousMouseLeftPressed == ButtonState.Released &&
                bear0.DrawRectangle.Contains(mouse.X, mouse.Y))
            {
                bear0.Active = false;
                explosion.Play(bear0.DrawRectangle.Center.X, bear0.DrawRectangle.Center.Y);
            }

            else if (bear1.Active &&
                     mouse.LeftButton == ButtonState.Pressed &&
                     previousMouseLeftPressed == ButtonState.Released &&
                     bear1.DrawRectangle.Contains(mouse.X, mouse.Y))
            {
                bear1.Active = false;
                explosion.Play(bear1.DrawRectangle.Center.X, bear1.DrawRectangle.Center.Y);
            }
            //updating current mouse left button state
            previousMouseLeftPressed = mouse.LeftButton;

            //explode bears if they collide with each other
            if (bear0.Active &&
                bear1.Active &&
                bear1.DrawRectangle.Intersects(bear0.DrawRectangle))
            {
                bear0.Active = false;
                bear1.Active = false;

                //play explosion
                Rectangle collisionRectangle = Rectangle.Intersect(bear0.DrawRectangle, bear1.DrawRectangle);

                explosion.Play(collisionRectangle.Center.X, collisionRectangle.Center.Y);
            }

            //respawn bears if inactive and right mouse button is pressed
            if (bear0.Active == false &&
                mouse.RightButton == ButtonState.Pressed &&
                previousMouseRightPressed == ButtonState.Released)
            {
                int     randX         = rand.Next(800);
                int     randY         = rand.Next(600);
                Vector2 velocitySpawn = new Vector2(rand.Next(10), rand.Next(10));
                bear0 = new TeddyBear(Content, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight, "teddybear0", randX, randY, velocitySpawn);
            }
            if (bear1.Active == false &&
                mouse.RightButton == ButtonState.Pressed &&
                previousMouseRightPressed == ButtonState.Released)
            {
                int     randX         = rand.Next(800);
                int     randY         = rand.Next(600);
                Vector2 velocitySpawn = new Vector2(rand.Next(10), rand.Next(10));
                bear1 = new TeddyBear(Content, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight, "teddybear1", randX, randY, velocitySpawn);
            }

            //updating current mouse left button state
            previousMouseRightPressed = mouse.RightButton;

            base.Update(gameTime);
        }
コード例 #17
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 mouse state
            MouseState mouse = Mouse.GetState();

            // click left button on the teddy bear to make it explode
            if (mouse.LeftButton == ButtonState.Pressed &&
                leftButton == ButtonState.Released &&
                teddybear.Active &&
                teddybear.DrawRectangle.Contains(mouse.X, mouse.Y))
            {
                leftButton = ButtonState.Pressed;

                explosion.Play(teddybear.DrawRectangle.Center.X, teddybear.DrawRectangle.Center.Y);

                teddybear.Active = false;
            }

            // track the previous state
            if (mouse.LeftButton == ButtonState.Released)
            {
                leftButton = ButtonState.Released;
            }

            // click right button to replace the current teddy bear
            if (mouse.RightButton == ButtonState.Pressed && rightButton == ButtonState.Released)
            {
                rightButton = ButtonState.Pressed;

                teddybear = new TeddyBear(Content, WindowWidth, WindowHeight, @"graphics\teddybear", random.Next(0, WindowWidth), random.Next(0, WindowHeight), new Vector2((float)random.NextDouble(), (float)random.NextDouble()));
            }

            // track the previous state
            if (mouse.RightButton == ButtonState.Released)
            {
                rightButton = ButtonState.Released;
            }

            // click middle button to explode the curren teddy bear and replace to new one
            if (mouse.MiddleButton == ButtonState.Pressed && middleButton == ButtonState.Released)
            {
                middleButton = ButtonState.Pressed;

                if (teddybear.Active)
                {
                    explosion.Play(teddybear.DrawRectangle.Center.X, teddybear.DrawRectangle.Center.Y);
                }

                teddybear.Active = false;

                teddybear = new TeddyBear(Content, WindowWidth, WindowHeight, @"graphics\teddybear", random.Next(0, WindowWidth), random.Next(0, WindowHeight), new Vector2((float)random.NextDouble(), (float)random.NextDouble()));
            }

            // track the previous state
            if (mouse.MiddleButton == ButtonState.Released)
            {
                middleButton = ButtonState.Released;
            }

            teddybear.Update(gameTime);
            explosion.Update(gameTime);

            base.Update(gameTime);
        }
コード例 #18
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 add mine if necessary
            MouseState mouse = Mouse.GetState();

            if (!mineAdded && mouse.LeftButton == ButtonState.Pressed)
            {
                mines.Add(new Mine(mineSprite, mouse.X, mouse.Y));

                // no more mines until mouse is released
                mineAdded = true;
            }

            // reset flag to allow adding more mines
            if (mineAdded && mouse.LeftButton == ButtonState.Released)
            {
                mineAdded = false;
            }

            // spawn a teddy bear if time is up
            if (millisUntilNewTeddy <= 0)
            {
                // time is up -> spawn new teddy
                teddys.Add(new TeddyBear(teddySprite, generateNewRandomTeddyVelocity(), WindowWidth, WindowHeight));

                // get new millis for teddy bear spawning
                millisUntilNewTeddy = generateNewRandomMillisForTeddy();
            }

            // decrease timer
            millisUntilNewTeddy -= gameTime.ElapsedGameTime.Milliseconds;

            // update teddys
            for (int i = teddys.Count - 1; i >= 0; i--)
            {
                TeddyBear teddy = teddys[i];

                // if teddy is inactive -> remove teddy
                if (!teddy.Active)
                {
                    teddys.Remove(teddy);
                    continue;
                }

                // update the teddy
                teddy.Update(gameTime);

                // check collision
                for (int j = mines.Count - 1; j >= 0; j--)
                {
                    Mine mine = mines[j];

                    // only check active mines
                    if (mine.Active)
                    {
                        if (teddy.CollisionRectangle.Intersects(mine.CollisionRectangle))
                        {
                            // add explosion on mine center
                            Point center = mine.CollisionRectangle.Center;
                            explosions.Add(new Explosion(explosionSprite, center.X, center.Y));

                            // deactivate teddy and mine
                            teddy.Active = false;
                            mine.Active  = false;
                        }
                    }
                    else
                    {
                        // remove inactive mine
                        mines.Remove(mine);
                    }
                }
            }

            // update explosions
            for (int i = explosions.Count - 1; i >= 0; i--)
            {
                Explosion explosion = explosions[i];

                if (explosion.Playing)
                {
                    // update active explosion
                    explosion.Update(gameTime);
                }
                else
                {
                    // update inactive explosion
                    explosions.Remove(explosion);
                }
            }


            base.Update(gameTime);
        }