Monday, July 1, 2013

VII. Improve our tank game - Additional Updates XNA

Let's add additional updates in this project.
Open your project and locate the AI.(gameTime);
We don't want the AI to move when we're already dead , or the AI is dead. So, set a condition before updating the AI

//UPDATE METHOD
if (enemy.alive && player.alive)
{
      AI(gameTime);
}

Next, add a bool to your project called hasstart.
   bool hasstart = false;

Next go to your Game ()method, make the screen larger for more gameplay space.
 public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferHeight = 768;
            graphics.PreferredBackBufferWidth = 1024;
            graphics.ApplyChanges();
        }

//UPDATE METHOD
   if (hasstart) //IF THE GAME HAS STARTED, UPDATE THE GAME OBJECTS, METHODS AND THE GAME
            {
                updateTank();
                if (enemy.alive && player .alive )
                {
                    AI(gameTime);
                }
                if (player.alive)
                {
                    tempposition = player.position;
                    if (currentKeyboardState.IsKeyDown(Keys.Right) && oldKeyboardState.IsKeyDown(Keys.Right))
                    {
                        player.rotation += 0.1f;
                    }
                    if (currentKeyboardState.IsKeyDown(Keys.Left) && oldKeyboardState.IsKeyDown(Keys.Left))
                    {
                        player.rotation -= 0.1f;
                    }
                    if (currentKeyboardState.IsKeyDown(Keys.Up) && oldKeyboardState.IsKeyDown(Keys.Up))
                    {
                        player.position += player.velocity * moveSpeed;
                        player.position.X = MathHelper.Clamp(player.position.X, player.texture.Width / 4, GraphicsDevice.Viewport.Width - player.texture.Width / 4);
                        player.position.Y = MathHelper.Clamp(player.position.Y, player.texture.Height / 4, GraphicsDevice.Viewport.Height - player.texture.Height / 4);
                        soundManager.soundBank.PlayCue("move");
                    }
                    if (currentKeyboardState.IsKeyDown(Keys.Down) && oldKeyboardState.IsKeyDown(Keys.Down))
                    {

                        player.position -= player.velocity * moveSpeed;
                        player.position.X = MathHelper.Clamp(player.position.X, player.texture.Width / 4, GraphicsDevice.Viewport.Width - player.texture.Width / 4);
                        player.position.Y = MathHelper.Clamp(player.position.Y, player.texture.Height / 4, GraphicsDevice.Viewport.Height - player.texture.Height / 4);

                    }
                    if (currentKeyboardState.IsKeyDown(Keys.Space) && oldKeyboardState.IsKeyUp(Keys.Space))
                    {
                        fireBullets();
                    }

                }

                if (enemy.life <= 0)
                {
                    enemy.alive = false;
                }
                if (player.life <= 0)
                {
                    player.alive = false;
                }
                detectCollisionsAgainstObject(player, enemy);
            }
            if (currentKeyboardState.IsKeyDown(Keys.Enter) && oldKeyboardState.IsKeyUp(Keys.Enter) && hasstart == false)
            {
                //IF THE PLAYER PRESSED ENTER KEY AND THE GAME HASNT STARTED , SET THE GAME TO START
                hasstart = true;
            }


NEXT, INFORM THE USER HOW TO START THE GAME
//DRAW METHOD, EDIT YOUR DRAW METHOD

 protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
          
            spriteBatch.Begin();
            spriteBatch.Draw(background , Vector2 .Zero ,
               null, Color.White, 0f,
              Vector2 .Zero ,
               2f, SpriteEffects.None, 0);
            if (enemy.alive)
            {
                spriteBatch.Draw(enemy.texture, enemy.position,
                   null, Color.White, enemy.rotation,
                   new Vector2(enemy.texture.Width / 2, enemy.texture.Height / 2),
                   0.5f, SpriteEffects.None, 0);
            }
            else
            {
                spriteBatch.DrawString(Content.Load("font"), "YOU WIN!: ", new Vector2(GraphicsDevice.Viewport.Width / 2-14, GraphicsDevice.Viewport.Height / 2), Color.Red);
hasstart= false;
            }
            SpriteFont font = Content.Load("font");
            if(!hasstart )
                spriteBatch.DrawString(font, "PRESS ENTER TO START GAME", new Vector2(GraphicsDevice.Viewport.Width / 2 - font.MeasureString("PRESS ENTER TO START GAME").X / 2, GraphicsDevice.Viewport.Height / 2), Color.White);
         
            if (player.alive)
            {
                spriteBatch.Draw(player.texture, player.position,
                null, Color.White, player.rotation,
                new Vector2(player.texture.Width / 2, player.texture.Height / 2), 0.5f, SpriteEffects.None, 0);
            }
            else
            {
                spriteBatch.DrawString(Content.Load("font"), "GAME OVER!: ", new Vector2 (GraphicsDevice .Viewport .Width /2 -14, GraphicsDevice .Viewport .Height /2),Color.Red);
hasstart = false;
           
            }
            updateBullets();
            spriteBatch.DrawString(Content.Load("font"), "BULLETS: " + player.numberOfBullets.ToString() + " " + player .rotation .ToString (), Vector2.Zero, Color.White);
            spriteBatch.DrawString(Content.Load("font"), "ENEMY LIFE: " + enemy.life.ToString(),
             new Vector2(GraphicsDevice .Viewport .Width - 200 ,0), Color.White);
           
            spriteBatch.End();
            foreach (gameObject explode in Explosion)
            {
                drawExplosion(explode);
            }
            base.Draw(gameTime);
        }
         

NOW CREATE A RECTANGLE METHOD THAT WILL DRAW THE LIFE OF THE PLAYER

    public Rectangle life(Vector2 position, int hp, int border )
         {
             
             int w = 1;
             int h = 10;
             return new Rectangle((int)position.X - border , (int)position.Y - border , w * hp +border *2 ,h + border*2 );
         }
         public void drawRectangle(bool alive)
         {
             Color hpColor;
             if (alive)
             {
                 if (player.life < 30)
                 {
                     hpColor = Color.Red;
                 }
                 else
                 {
                     hpColor = Color.ForestGreen;
                 }
                 Texture2D bounds = new Texture2D(GraphicsDevice, 1, 2);
                 Color[] color = new Color[1 * 2];
                 for (int i = 0; i < color.Length; i++)
                     color[i] = Color.Black * 0.5f;
                 bounds.SetData(color);
                 spriteBatch.Draw(bounds, life(new Vector2(player.position.X - player.texture.Width / 4, player.position.Y - player.texture.Height / 3), 100, 2), Color.White);
                 bounds = new Texture2D(GraphicsDevice, 1, 1);
                 bounds.SetData(new Color[] { hpColor });
                 spriteBatch.Draw(bounds, life(new Vector2(player.position.X - player.texture.Width / 4, player.position.Y - player.texture.Height / 3), player.life, 0), Color.White);
             }

         }

THEN ADD IT TO YOUR DRAW METHOD

 drawRectangle(player .alive ); //PLACE IT BEFORE THE SPRITEBATCH.END



RUN THE GAME!!


[AI WONT UPDATE : PLAYER IS DEAD]

[WAIT FOR INPUT DEPENDING ON HAS START BOOL]

[HP BAR USING RECTANGLE METHOD]