Exemple #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)
        {
            // TODO: Add your update logic here
            newKeyboardState = Keyboard.GetState();

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            if (newKeyboardState.IsKeyDown(Keys.Escape))
                Exit();

            // update the time
            timer += (float)gameTime.ElapsedGameTime.TotalSeconds;

            // update the shp 
            ship.Update(gameTime, rocks);

            // update rocks
            foreach (Rock rock in rocks)
            {
                rock.Update(gameTime);
                if (Collisions.CollidesWith(rock.Bounds, ship.Bounds))
                {
                    ship.HasCrashed = true; 
                } 
            }

            if(ship.HasCrashed == true)
            {
                ship.Crash(); 
                System.Threading.Thread.Sleep(5000);
                this.Exit(); 
            }

            // check the timer and add a new rock if it is less than .4 seconds
            if (timer > 0.20f)
            {
                timer = 0;
                rocks.Add(new Rock(this, Content));
            }

            // remove rocks from the list that are past the screen
            for (int i = 0; i < rocks.Count; i++)
            {
                var rock = rocks[i];
                if (rock.IsRemoved)
                {
                    rocks.RemoveAt(i);
                    i--;
                    rock.Miss();
                }
            }

            oldKeyboardState = newKeyboardState;

            base.Update(gameTime);
        }
Exemple #2
0
        /// <summary>
        /// Updates the rock
        /// </summary>
        /// <param name="gameTime">Games gamtime to use</param>
        public void Update(GameTime gameTime, Ship ship)
        {
            var viewport = game.GraphicsDevice.Viewport;

            Bounds.Bottom += 0.5f * (float)gameTime.ElapsedGameTime.TotalSeconds * Velocity.Y;

            if (Collisions.CollidesWith(this.Bounds, ship.Bounds))
            {
                game.Exit();
            }
            // if the rock is at the bottom
            if (Bounds.Bottom >= viewport.Height)
            {
                IsRemoved = true;
            }

            // if we havent hit the bottom
            if (Bounds.Bottom < viewport.Height)
            {
                //float delta = viewport.Height - Bounds.Bottom - Bounds.Y;
                //Bounds.Y += 2 * delta;
                Bounds.Y += Velocity.Y * Bounds.Height;
            }
        }
Exemple #3
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)
        {
            newKeyboardState = Keyboard.GetState();

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }
            if (newKeyboardState.IsKeyDown(Keys.Enter))
            {
                Restart();
            }

            if (newKeyboardState.IsKeyDown(Keys.Escape))
            {
                Exit();
            }
            if (lost == false)
            {
                //paddle.Update(gameTime);
                ufo.Update(gameTime);
                foreach (Asteroid a in asteroids)
                {
                    a.Update(gameTime);
                }
                shield.Update(gameTime, ufo);

                time = gameTime.TotalGameTime.TotalSeconds;
                if ((int)time != 0 && timeFlag == false)
                {
                    if ((int)time % 5 == 0)
                    {
                        Array.Resize(ref asteroids, asteroids.Length + 1);
                        Asteroid temp = new Asteroid(this, random);
                        temp.LoadContent(Content);
                        asteroids[numberOfAsteroids] = temp;
                        numberOfAsteroids++;
                        timeFlag = true;
                    }
                }
                if ((int)time != 0 && timeFlagScore == false)
                {
                    if ((int)time % 2 == 0)
                    {
                        score        += 100;
                        timeFlagScore = true;
                    }
                }
                if ((int)time != 0)
                {
                    if ((int)time % 5 != 0)
                    {
                        timeFlag = false;
                    }
                    if ((int)time % 2 != 0)
                    {
                        timeFlagScore = false;
                    }
                }



                //oldKeyboardState = newKeyboardState;
                foreach (Asteroid a in asteroids)
                {
                    if (Collisions.CollidesWith(ufo.bounds, a.bounds))
                    {
                        if (shield.used == true)
                        {
                            if (!a.destroyed)
                            {
                                asteroidExplode.Play();
                            }
                            a.Destroy();
                        }
                        else if (!a.destroyed)
                        {
                            lost = true;
                            playerHit.Play();
                        }
                        else
                        {
                        }
                    }
                }
            }

            base.Update(gameTime);
        }