Beispiel #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)
        {
            KeyboardState kb = Keyboard.GetState();

            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
                kb.IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }
            ship.Update(gameTime);

            // TODO: Add your update logic here
            if (kb.IsKeyDown(Keys.Space) && !oldKB.IsKeyDown(Keys.Space))
            {
                ship.fire();
            }

            if (ship.IsFiring)
            {
                missiles.Add(new Missile((float)ship.Heading));
            }

            for (int i = 0; i < missiles.Count; i++)
            {
                missiles[i].Update();
                if (missiles[i].isOffScreen)
                {
                    missiles.RemoveAt(i);
                }
            }

            oldKB = kb;
            base.Update(gameTime);
        }
Beispiel #2
0
        protected override void Update(GameTime gameTime)
        {
            KeyboardState kb = Keyboard.GetState();

            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
                kb.IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }
            ship.Update(gameTime);

            // TODO: Add your update logic here
            if (kb.IsKeyDown(Keys.Space) && !oldKB.IsKeyDown(Keys.Space))
            {
                ship.fire();
            }

            //Console.WriteLine(ship.Heading);

            if (ship.IsFiring)
            {
                if (gameTime.TotalGameTime.Seconds % 0 == 0 && shoot)
                {
                    missileList.Add(new Missile(missileTex, ship.Location, ship.Heading, gunstarRect));
                    shoot = false;
                }
                if (gameTime.TotalGameTime.Seconds % 0 != 0)
                {
                    shoot = true;
                }
                Console.WriteLine(gameTime.TotalGameTime.Seconds % 2 == 0);
            }

            foreach (Missile missile in missileList)
            {
                missile.Update(gameTime);
            }

            for (int i = 0; i < missileList.Count; i++)
            {
                if (missileList[i].Location.X > screenRect.Width ||
                    missileList[i].Location.X <0 ||
                                               missileList[i].Location.Y> screenRect.Height ||
                    missileList[i].Location.Y < 0)
                {
                    missileList.RemoveAt(i);
                }
            }

            oldKB = kb;
            base.Update(gameTime);
        }