예제 #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 ESC is pressed, exit game
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            // TODO: Add your update logic here

            
            // if at start of game, append our asteroids to list
            if (start == true)
            {
                for (int i = 0; i < asteroid_number; i++)
                {
                    asteroid = new Asteroid(Content.Load<Texture2D>("asteroid_sprite1"));
                    System.Console.Write(asteroid.asteroid_x);
                    System.Console.Write("\n");
                    player_ship.asteroid_list.Add(asteroid);
                }
                start = false;
                System.Console.Write("Asteroid Count: ");
                System.Console.Write(player_ship.asteroid_list.Count);
                System.Console.Write("\n");
            }


            // get mouse coordinates and calculate angle between player and mouse
            MouseState mouse_state = Mouse.GetState();
            //MouseState old_mouse_state;
            mouse_x = mouse_state.X;
            mouse_y = mouse_state.Y;
            playerMouse_angle = (float)System.Math.Atan2((mouse_x - player_ship.player_x + (player_ship.playerShip_texture.Width / 2)), -(mouse_y - player_ship.player_y + (player_ship.playerShip_texture.Height / 2))) - (float)System.Math.PI / 2;

            //if left mouse button is pressed, construct a new missile
                      

            if (Mouse.GetState().LeftButton == ButtonState.Pressed && previous_mouse_state.LeftButton == ButtonState.Released)
            {
                missile = new Missile(Content.Load<Texture2D>("missileSprite"), player_ship, playerMouse_angle);
                player_ship.missile_list.Add(missile); //stored in the object player_ship, that  way each ship stores info about its missiles.
                               
                /*
                System.Console.WriteLine(missile_list);
                System.Console.WriteLine(missile_list.Count);
                There is no console; can't really see a use for this
                */
            }

            for (int i = 0; i < player_ship.missile_list.Count; i++)
            {

                player_ship.missile_list[i].Update();

                //if the missile is off the screen, remove it
                if(player_ship.missile_list[i].missile_x>1100|| player_ship.missile_list[i].missile_x < 0|| player_ship.missile_list[i].missile_y > 600|| player_ship.missile_list[i].missile_y < 0)
                {
                    if (player_ship.missile_list[i].bounces_remaining > 0) player_ship.missile_list[i].bounce(); //bounce the missile

                    else player_ship.missile_list.RemoveAt(i); //if no bounces left, remove it
                }
            }

            // save the current mouse state for the next frame
            previous_mouse_state = Mouse.GetState();

            // update player's position
            player_ship.Update(playerMouse_angle);

            // update asteroid's position
            for (int i = 0; i < player_ship.asteroid_list.Count; i++)
            {
                //for (int j = 0; j < player_ship.missile_list.Count; j++)

                    
               // {

                    //if (player_ship.asteroid_list[i].asteroid_x  < player_ship.missile_list[j].missile_x && player_ship.asteroid_list[i].asteroid_x > player_ship.missile_list[j].missile_x)
                    //{
                        //player_ship.asteroid_list.RemoveAt(i);
                        //player_ship.missile_list.RemoveAt(j);
                    //}
                    //if (player_ship.asteroid_list[i].asteroid_y < player_ship.missile_list[j].missile_y && player_ship.asteroid_list[i].asteroid_y > player_ship.missile_list[j].missile_y)
                    //{
                        //player_ship.asteroid_list.RemoveAt(i);
                        //player_ship.missile_list.RemoveAt(j);
                    //}
                player_ship.asteroid_list[i].Update();
                //player_ship.missile_list[j].Update();
                //}
                
            }




            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)
        {
            // if ESC is pressed, exit game
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            // TODO: Add your update logic here


            // if at start of game, append our asteroids to list
            if (start == true)
            {
                for (int i = 0; i < asteroid_number; i++)
                {
                    asteroid = new Asteroid(Content.Load <Texture2D>("asteroid_sprite1"));
                    System.Console.Write(asteroid.asteroid_x);
                    System.Console.Write("\n");
                    player_ship.asteroid_list.Add(asteroid);
                }
                start = false;
                System.Console.Write("Asteroid Count: ");
                System.Console.Write(player_ship.asteroid_list.Count);
                System.Console.Write("\n");
            }


            // get mouse coordinates and calculate angle between player and mouse
            MouseState mouse_state = Mouse.GetState();

            //MouseState old_mouse_state;
            mouse_x           = mouse_state.X;
            mouse_y           = mouse_state.Y;
            playerMouse_angle = (float)System.Math.Atan2((mouse_x - player_ship.player_x + (player_ship.playerShip_texture.Width / 2)), -(mouse_y - player_ship.player_y + (player_ship.playerShip_texture.Height / 2))) - (float)System.Math.PI / 2;

            //if left mouse button is pressed, construct a new missile


            if (Mouse.GetState().LeftButton == ButtonState.Pressed && previous_mouse_state.LeftButton == ButtonState.Released)
            {
                missile = new Missile(Content.Load <Texture2D>("missileSprite"), player_ship, playerMouse_angle);
                player_ship.missile_list.Add(missile); //stored in the object player_ship, that  way each ship stores info about its missiles.

                /*
                 * System.Console.WriteLine(missile_list);
                 * System.Console.WriteLine(missile_list.Count);
                 * There is no console; can't really see a use for this
                 */
            }

            for (int i = 0; i < player_ship.missile_list.Count; i++)
            {
                player_ship.missile_list[i].Update();

                //if the missile is off the screen, remove it
                if (player_ship.missile_list[i].missile_x > 1100 || player_ship.missile_list[i].missile_x < 0 || player_ship.missile_list[i].missile_y > 600 || player_ship.missile_list[i].missile_y < 0)
                {
                    if (player_ship.missile_list[i].bounces_remaining > 0)
                    {
                        player_ship.missile_list[i].bounce();                                                    //bounce the missile
                    }
                    else
                    {
                        player_ship.missile_list.RemoveAt(i);  //if no bounces left, remove it
                    }
                }
            }

            // save the current mouse state for the next frame
            previous_mouse_state = Mouse.GetState();

            // update player's position
            player_ship.Update(playerMouse_angle);

            // update asteroid's position
            for (int i = 0; i < player_ship.asteroid_list.Count; i++)
            {
                //for (int j = 0; j < player_ship.missile_list.Count; j++)


                // {

                //if (player_ship.asteroid_list[i].asteroid_x  < player_ship.missile_list[j].missile_x && player_ship.asteroid_list[i].asteroid_x > player_ship.missile_list[j].missile_x)
                //{
                //player_ship.asteroid_list.RemoveAt(i);
                //player_ship.missile_list.RemoveAt(j);
                //}
                //if (player_ship.asteroid_list[i].asteroid_y < player_ship.missile_list[j].missile_y && player_ship.asteroid_list[i].asteroid_y > player_ship.missile_list[j].missile_y)
                //{
                //player_ship.asteroid_list.RemoveAt(i);
                //player_ship.missile_list.RemoveAt(j);
                //}
                player_ship.asteroid_list[i].Update();
                //player_ship.missile_list[j].Update();
                //}
            }



            base.Update(gameTime);
        }