Jump to content

  • Log In with Google      Sign In   
  • Create Account


scole127

Member Since 26 May 2013
Offline Last Active May 27 2013 03:08 AM
-----

Topics I've Started

Help deleting an object in visual C# XNA

26 May 2013 - 03:22 AM

Hi, new to this site
 
I'm a beginner at programming and have a school project that involves remaking the Atari parachute game in Visual C# 2010 Express.
 
My question is when i have released the parachute how do i delete the object when it collides with the birds.
 
Any help would be great. As mentioned before I'm new to this so please give easy to follow steps.
Thanks.

 

 


 (There is some code referring to a class called Plane but i ended up finding a different way to do the plane but i didn't want to delete it yet in case i need).

 

 


Game 1 class:

 


[using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
 
namespace WindowsGame1
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
      //  Plane PlaneObject;
        Truck TruckObject;
        //Parachute ParachuteObject;
        Bullet BulletObject;
 
        Texture2D planeTexture;
        Vector2 planePosition;
 
        bool personHit = false;
 
 
 
 
        Birds[] ballObjects;
        int Birdcount = 3;
 
 
 
        //Random for the Birds
        Random rand = new Random();
 
 
        public static bool canPress = true;
 
 
 
 
 
        public Vector2 SpriteLocation;
        public Game1()
 
        {
 
            graphics = new GraphicsDeviceManager(this);
 
           ////////// Window size //////////////////
           graphics.PreferredBackBufferHeight = 1080;
           graphics.PreferredBackBufferWidth = 1920;
           graphics.IsFullScreen = true;
           ////////// Window size //////////////////
 
 
            Content.RootDirectory = "Content";
        }
 
        protected override void Initialize()
        {
 
 
          planePosition = new Vector2 (10,10);
 
 
 
 
 
 
 
            ballObjects = new Birds[Birdcount];
 
 
            for (int index = 0; index < Birdcount; index++)
            {
 
                ////Color For The Random Collor////
                byte r = (byte)rand.Next(64, 256);
                byte g = (byte)rand.Next(64, 256);
                byte b = (byte)rand.Next(64, 256);
                byte a = (byte)rand.Next(64, 256);
                Color tempColor = new Color(r, g, b, a);
   
                ballObjects[index] = new Birds(Birds.Texture, new Vector2(rand.Next(10, 600), (rand.Next(10, 600))), new Vector2(rand.Next(-10, 10), (rand.Next(-10, 10))), tempColor);
            
            }  
 
 
 
 
 
          //   PlaneObject = new Plane(Plane.Texture, new Vector2(1200f, 10f), new Vector2(-10f, 0f), Color.White);
             TruckObject = new Truck(Truck.Texture, new Vector2(2000, 800f), new Vector2(-3f, 0f), Color.White);
          //   ParachuteObject = new Parachute(Parachute.Texture, new Vector2(400f, 0f), new Vector2(0f, 3f), Color.White);
 
            base.Initialize();
 
           
        }
 
 
        protected override void LoadContent()
        {
 
            spriteBatch = new SpriteBatch(GraphicsDevice);
 
         //   Plane.Texture = Content.Load<Texture2D>("Sprites\\Plane");
       //     Plane.GraphicsViewport = graphics.GraphicsDevice.Viewport;
 
            Truck.Texture = Content.Load<Texture2D>("Sprites\\Truck");
            Truck.GraphicsViewport = graphics.GraphicsDevice.Viewport;
 
          //  Parachute.Texture = Content.Load<Texture2D>("Sprites\\Parachute");
          //  Parachute.GraphicsViewport = graphics.GraphicsDevice.Viewport;
 
            Birds.Texture = Content.Load<Texture2D>("Sprites\\Bird");
            Birds.GraphicsViewport = graphics.GraphicsDevice.Viewport;
 
            Bullet.Texture = Content.Load<Texture2D>("Sprites\\Parachute");
            Bullet.GraphicsViewport = graphics.GraphicsDevice.Viewport;
 
            planeTexture = Content.Load<Texture2D>("Sprites\\Plane");
            
       
        }
 
        protected override void UnloadContent()
        {
 
        }
 
        protected override void Update(GameTime gameTime)
        {
 
            personHit = false;
 
 
            if (planePosition.X > 2000f)
            {
                planePosition.X = -600f;
            }
 
 
            
 
 
 
 
            planePosition.X = planePosition.X + 7f;
 
 
            //////////////////////////////Movement Control For Parachute///////////////////////////////////////
 
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
 
 
            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                BulletObject.Position.X = BulletObject.Position.X + 3f;
            }
 
 
            if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                BulletObject.Position.X = BulletObject.Position.X - 3f;
            }
 
            //////////////////////////////Movement Control For Parachute///////////////////////////////////////
 
 
            foreach (Birds ball in ballObjects)
            {
                ball.Update();
 
 
                Rectangle ballRectangle = new Rectangle((int)ball.Position.X, (int)ball.Position.Y, Birds.Texture.Width, Birds.Texture.Height); 
                
                
               Rectangle planeRectangle = new Rectangle((int)planePosition.X, (int)planePosition.Y, planeTexture.Width, planeTexture.Height); 
                
                if (planeRectangle.Intersects(ballRectangle))
                {
                    personHit = true;
                    
                }
                
            }
                // PlaneObject.Update();
                TruckObject.Update();
                //  ParachuteObject.Update()
 
 
                if (canPress)
                {
 
                    if (Keyboard.GetState().IsKeyDown(Keys.Space))
                    {
 
                        Vector2 bulletPosition = planePosition;
                        BulletObject = new Bullet(Bullet.Texture, bulletPosition);
                        Bullet.alive = true;
 
                    }
 
 
                }
 
 
                if (Bullet.alive)
                {
                    canPress = false;
                    BulletObject.MovingBullet();
                }
 
 
 
                ;
 
                base.Update(gameTime);
            }
        
 
 
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.SkyBlue);
 
            spriteBatch.Begin();
 
            spriteBatch.Draw(planeTexture, planePosition, Color.White);
            if (Bullet.alive)
            {
                BulletObject.Draw(spriteBatch);
            }
 
            
 
         //   PlaneObject.Draw(spriteBatch);
            TruckObject.Draw(spriteBatch);
        //    ParachuteObject.Draw(spriteBatch);
 
 
            foreach (Birds ball in ballObjects)
            {
                ball.Draw(spriteBatch);
            }
 
 
 
 
 
 
            spriteBatch.End();
 
 
            base.Draw(gameTime);
        }
    }
}]


Bullet class(Parachute, its named bullet because i was following a tutorial):


[using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
 
namespace WindowsGame1
{
    class Bullet
    {
        public Vector2 Position;
        public static Texture2D Texture;
        public static Viewport GraphicsViewport;
        public static bool alive;
 
 
        public Bullet(Texture2D newTexture, Vector2 newPosition)
        {
            Position = newPosition;
        }
 
 
 
 
 
        public void MovingBullet()
        {
            Position.Y = Position.Y + 2;
 
            if (Position.Y > GraphicsViewport.Width - Texture.Width)
            {
                alive = false;
                Game1.canPress = true;
            } 
         }
 
 
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw (Texture, Position, Color.White);
        }
 
 
 
 
 
 
    }
 
}]

 

Birds Class:

 

[using System;

using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace WindowsGame1
{
    class Birds
    {
        public Vector2 Position;
        public Vector2 Velocity;
        Color Color;
 
 
        public static Texture2D Texture;
        public static Viewport GraphicsViewport;
 
        public Birds(Texture2D newTexture, Vector2 newPosition, Vector2 newVelocity, Color newColor)
        {
            Position = newPosition;
            Velocity = newVelocity;
            Color = newColor;
        }
 
        public void Update()
        {
 
            Position = Position + Velocity;
 
            if (Position.X < 0 || Position.X > GraphicsViewport.Width - Texture.Width)
            {
                Velocity.X = -Velocity.X;
                Position.X = Position.X + Velocity.X;
 
            }
 
            if (Position.Y < 0 || Position.Y > GraphicsViewport.Height - Texture.Height)
            {
                Velocity.Y = -Velocity.Y;
                Position.Y = Position.Y + Velocity.Y;
 
            }
        }
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(Texture, Position, Color);
        }
    
    }
}]

 

 

truck class:

 

[using System;

using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
 
 
namespace WindowsGame1
{
    class Truck
    {
        public Vector2 Position;
        public Vector2 Velocity;
        Color Color;
 
 
        public static Texture2D Texture;
        public static Viewport GraphicsViewport;
 
        public Truck(Texture2D newTexture, Vector2 newPosition, Vector2 newVelocity, Color newColor)
        {
            Position = newPosition;
            Velocity = newVelocity;
            Color = newColor;
        }
 
        public void Update()
        {
 
            Position = Position + Velocity;
 
 
            if (Position.X < -600f)
            {
 
                Position.X = 2000f;
 
            }
        }
 
        //            if (Position.Y < 0 || Position.Y > GraphicsViewport.Height - Texture.Height)
        //          {
        //            Velocity.Y = -Velocity.Y;
        //          Position.Y = Position.Y + Velocity.Y;
 
        //    }
        //}
 
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(Texture, Position, Color);
        }
 
 
 
    }
}]

PARTNERS