コード例 #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)
        {
            // Pass keyboard state to Global so we can use it everywhere
            Global.keys = Keyboard.GetState();

            // Update the game objects
            base.Update(gameTime);

            foreach (GameObject GO in gameObjects)
            {
                GO.Update();
            }


            for (int i = 0; i < gameObjects.Count; i++)
            {
                for (int x = 0; x < gameObjects.Count; x++)
                {
                    if (gameObjects[i] is Bullet)
                    {
                        Bullet B = gameObjects[i] as Bullet;
                        if (gameObjects[x] is Player)
                        {
                            Player P = gameObjects[x] as Player;
                            if (Global.keys.IsKeyDown(Keys.Space))
                            {
                                B.Fire(P.position);
                            }
                        }

                        else if (gameObjects[x] is Invader)
                        {
                            Invader I = gameObjects[x] as Invader;
                            if (B.overlaps(B.position.X, B.position.Y, B.texture, I.position.X, I.position.Y, I.texture))
                            {
                                B.Start();
                                I.Start();
                            }
                        }

                        else if (gameObjects[x] is Shield)
                        {
                            Shield S = gameObjects[x] as Shield;
                            if (B.overlaps(B.position.X, B.position.Y, B.texture, S.position.X, S.position.Y, S.texture))
                            {
                                B.Start();
                                gameObjects.RemoveAt(x);
                            }
                        }

                        else if (gameObjects[x] is SpaceShip)
                        {
                            SpaceShip SS = gameObjects[x] as SpaceShip;
                            if (B.overlaps(B.position.X, B.position.Y, B.texture, SS.position.X, SS.position.Y, SS.texture))
                            {
                                B.Start();
                                SS.hits++;
                                if (SS.hits >= 2)
                                {
                                    gameObjects.RemoveAt(x);
                                }
                            }
                        }
                    }
                }
            }
        }