示例#1
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            // Load fonts
            fontAgency   = Content.Load <SpriteFont>("AgencyFB");
            fontAgency16 = Content.Load <SpriteFont>("AgencyFB16");
            fontArial    = Content.Load <SpriteFont>("Arial");
            // Textures
            playerTex       = Content.Load <Texture2D>("tank");
            player.Texture  = playerTex;
            player.Position = new Vector2(ResolutionX / 2 - playerTex.Width / 2,
                                          ResolutionY - playerTex.Height - 40);
            enemyTex = Content.Load <Texture2D>("alien2");

            for (int i = 1; i <= enemies.Count; i++)
            {
                Enemy enemy = enemies[i - 1];
                enemy.Texture  = enemyTex;
                enemy.Position = new Vector2((ResolutionX / (enemies.Count + 1)) * i - enemyTex.Width / 2,
                                             200 - enemyTex.Height / 2);
                enemy.MovePattern = new Vector2[] {
                    new Vector2(enemy.Position.X - ((ResolutionX / (enemies.Count + 1))
                                                    - enemyTex.Width / 2),
                                enemy.Position.Y),
                    new Vector2(enemy.Position.X + ((ResolutionX / (enemies.Count + 1)) - enemyTex.Width / 2),
                                enemy.Position.Y)
                };
                enemy.MoveSpeed = 1;
            }

            bunkerTex = Content.Load <Texture2D>("bunker");
            for (int i = 1; i <= bunkers.Count; i++)
            {
                Bunker b = bunkers[i - 1];
                b.Texture  = bunkerTex;
                b.Position = new Vector2((ResolutionX / (bunkers.Count + 1)) * i - bunkerTex.Width / 2,
                                         ResolutionY - bunkerTex.Height - 200);
                b.BoundingBox = new BoundingBox(new Vector3(b.Position.X, b.Position.Y + 17, 0),
                                                new Vector3(b.Position.X + b.Texture.Width, b.Position.Y + 31, 0));
            }
            bunker_hit    = new Texture2D[3];
            bunker_hit[0] = Content.Load <Texture2D>("bunker_broken03");
            bunker_hit[1] = Content.Load <Texture2D>("bunker_broken02");
            bunker_hit[2] = Content.Load <Texture2D>("bunker_broken01");

            bombTex   = Content.Load <Texture2D>("alienbomb");
            bulletTex = Content.Load <Texture2D>("playerbullet");
        }
示例#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)
        {
            // PLAYER CONTROLS
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                if (player.Position.X > 4)
                {
                    player.Position.X -= 4;
                    player.UpdateBoundingBox();
                }
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                if (player.Position.X + playerTex.Width < ResolutionX)
                {
                    player.Position.X += 4;
                    player.UpdateBoundingBox();
                }
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Space))
            {
                if ((gameTime.TotalGameTime.TotalMilliseconds - playerFireCooldown) > lastFired)
                {
                    FireBullet(player.Position, gameTime);
                    lastFired = (int)gameTime.TotalGameTime.TotalMilliseconds;
                }
            }
            //----------------------------------------


            // ENEMY PROCEDURES
            foreach (Enemy enemy in enemies)
            {
                // Move enemy
                if (enemy.Position.X - enemy.MovePattern[enemy.CurrentGoal].X < 0)
                {
                    enemy.Position.X += enemy.MoveSpeed;
                    enemy.UpdateBoundingBox();
                }
                else if (enemy.Position.X - enemy.MovePattern[enemy.CurrentGoal].X > 0)
                {
                    enemy.Position.X -= enemy.MoveSpeed;
                    enemy.UpdateBoundingBox();
                }
                else //current move goal reached, change move goal
                {
                    if (enemy.CurrentGoal + 1 == enemy.MovePattern.Length)
                    {
                        enemy.CurrentGoal = 0;
                    }
                    else
                    {
                        enemy.CurrentGoal++;
                    }
                }
                // Detertime bomb dropping
                if (gameTime.TotalGameTime.TotalMilliseconds - enemy.LastDropped > bombDropCooldown)
                {
                    if (random.Next(1, bombDropChance + 1) == bombDropChance)
                    {
                        DropBomb(enemy.Position, gameTime);
                    }
                    enemy.LastDropped = (int)gameTime.TotalGameTime.TotalMilliseconds;
                }
            }

            //----------------------------------------



            // ADVANCE PROJECTILES AND CHECK COLLISIONS


            for (int i = 0; i < bullets.Count; i++)
            {
                Projectile p = bullets[i];
                p.Position.Y -= 10;
                p.UpdateBoundingBox();
                if (p.Position.Y < -(p.Texture.Height))
                {
                    bullets.Remove(p);
                    continue;
                }
                // Collision detection
                for (int j = 0; j < bunkers.Count; j++)
                {
                    Bunker b = bunkers[j];
                    if (p.BoundingBox.Intersects(b.BoundingBox))
                    {
                        bullets.Remove(p);
                        b.Health--;
                        if (b.Health < 0)
                        {
                            bunkers.Remove(b);
                        }
                        else
                        {
                            b.Texture = bunker_hit[b.Health];
                        }
                        break;
                    }
                }
                for (int k = 0; k < enemies.Count; k++)
                {
                    Enemy enemy = enemies[k];
                    if (p.BoundingBox.Intersects(enemy.BoundingBox))
                    {
                        enemies.Remove(enemy);
                        bullets.Remove(p);
                        break;
                    }
                }
            }


            for (int i = 0; i < bombs.Count; i++)
            {
                Bomb b = bombs[i];
                b.Position.Y += 10;
                b.UpdateBoundingBox();

                // Collision detection
                for (int j = 0; j < bunkers.Count; j++)
                {
                    Bunker bunker = bunkers[j];
                    if (b.BoundingBox.Intersects(bunker.BoundingBox))
                    {
                        bombs.Remove(b);
                        bunker.Health--;
                        if (bunker.Health < 0)
                        {
                            bunkers.Remove(bunker);
                        }
                        else
                        {
                            bunker.Texture = bunker_hit[bunker.Health];
                        }
                        break;
                    }
                }

                if (b.BoundingBox.Intersects(ground.BoundingBox) ||
                    b.Position.Y > ResolutionY)
                {
                    bombs.Remove(b);
                }
            }


            //----------------------------------------



            base.Update(gameTime);
        }