locked
Having trouble with bullet/enemy collision detection in a Space Invaders-like game (using XNA 4.0). RRS feed

  • Question

  • Hey everyone,

    I have recently been working on a school project and am required to make a game. I decided to make a simple 2d shooter and the last thing I need to do is add bullet/enemy collision detection. I have tried different methods, but to no avail. Here's my code(Please note that I am a complete novice when it comes to programming so go easy on me ;) ):

    Main

    namespace Software_Design_Major_Project
    {
       
        public class Game1 : Microsoft.Xna.Framework.Game
        {
            GraphicsDeviceManager graphics;
            SpriteBatch spriteBatch;
    
            Texture2D ship; // Declaring the sprite
            Vector2 shipposition = Vector2.Zero; // Creating a vector for the sprite.
    
            List<bullets> bullets = new List<bullets>(); // a list is created here so that there can be multiple copies of this item at once without writing the extra code.
            Texture2D texture;
            
            KeyboardState pastkey;
            
    
            //bullet sound effect.
            SoundEffect effect;
            
            List<enemies> Enemies = new List<enemies>();       
            Random random2 = new Random();        
            
            float spawn = 0;
    
            public enum GameState 
            {
                MainMenu,            
                Playing,
                
            }
            GameState CurrentGameState = GameState.MainMenu;
    
            int screenWidth = 600, screenHeight = 480;
    
            cButton play;                    
    
            public Game1()
            {
                graphics = new GraphicsDeviceManager(this);
                Content.RootDirectory = "Content";
                //changes width of screen to 600px.
                graphics.PreferredBackBufferWidth = 600;
                                       
            }
    
      
    
            protected override void Initialize()
            {
                        
                base.Initialize();
            }
    
     
            protected override void LoadContent()
            {
                
                spriteBatch = new SpriteBatch(GraphicsDevice);
                 // This statement positions the ship.
                ship = Content.Load<Texture2D>("ship"); // Loads the ship into the memory.
                shipposition = new Vector2((graphics.GraphicsDevice.Viewport.Width / 2) - (ship.Width / 2), 420); 
                // loads bullet sprite
                texture = Content.Load<Texture2D>("bullet");             
    
                graphics.PreferredBackBufferWidth = screenWidth;
                graphics.PreferredBackBufferHeight = screenHeight;
    
                graphics.ApplyChanges();
                IsMouseVisible = true;
    
                play = new cButton(Content.Load<Texture2D>("play"), graphics.GraphicsDevice);
                play.setPosition(new Vector2(225, 220));
    
                effect = Content.Load<SoundEffect>("laser");            
                           
                
            }
    
         
            protected override void UnloadContent()
            {
             
            }
    
    
            protected override void Update(GameTime gameTime)
            {
                spawn += (float)gameTime.ElapsedGameTime.TotalSeconds;
                // spawns enemy every second.
                foreach (enemies enemy in Enemies)
                    enemy.update(graphics.GraphicsDevice);
    
    
                MouseState mouse = Mouse.GetState();
    
                switch (CurrentGameState)
                {
                    case GameState.MainMenu:
    
                        if (play.isClicked == true)
                            CurrentGameState = GameState.Playing;
    
                        play.Update(mouse);
    
                        break;
    
                    case GameState.Playing:
    
                        if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.E))
                        {
                            Exit();
                        }
    
                        break;
    
    
                }
    
                // Allows the ship to move left and stops the ship going off the screen.          
                if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Left) && shipposition.X >= 0)
                {
                    shipposition.X -= 7;
                }// same as above except for the right direction.
                if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Right) && shipposition.X < ((graphics.GraphicsDevice.Viewport.Width) - (ship.Width)))
                {
                    shipposition.X += 7;
                }
    
                // this prevents the player from holding down space to spam bullets.
                if (Keyboard.GetState().IsKeyDown(Keys.Space) && pastkey.IsKeyUp(Keys.Space))
                {
                    bullets bullet = new bullets(texture);
                    // the ships coordinates are gathered from the top left hand corner of the sprites rectangle therefore 'new Vector2(shipposition.X + 32, shipposition.Y)' had to
                    // be added rather than just = 'shipposition' to avoid having the bullets shoot from the wing. 
                    bullet.position = new Vector2(shipposition.X + 32, shipposition.Y);
                    bullets.Add(bullet);
                    effect.Play();
    
                }
    
    
    
                pastkey = Keyboard.GetState();
    
                //calls upon the update method from the bullets class.
    
                foreach (bullets bullet in bullets)
                    bullet.update();
    
                
                    LoadEnemies();
                base.Update(gameTime);
    
    
            }
                
                public void LoadEnemies() 
                {
                    int randX = random2.Next(10, 540);
    
                    if (spawn <= 1) 
                    {
                        spawn = 0;
                        //limits amount of enemies on screen to 5.
                        if (Enemies.Count() < 5)
                            Enemies.Add(new enemies(Content.Load<Texture2D>("enemy"), new Vector2(randX, -100)));
                    }
                    for (int i = 0; i < Enemies.Count; i++) 
                    {
                        if (!Enemies[i].enemyVisble)
                        {
                            //removes the enemies when they go off screen.
                            Enemies.RemoveAt(i);
                            i--;
                        }
                        
                    }
                }
            
            
          
                protected override void Draw(GameTime gameTime)
                {
                    GraphicsDevice.Clear(Color.CornflowerBlue);
                    // calls draw method in bullets class
                    foreach (bullets bullet in bullets)
                    {
                        bullet.Draw(spriteBatch);
                    }
                    spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
                    spriteBatch.Draw(ship, shipposition, Color.White); // draws ship sprite 
      
    
                                    
                    switch (CurrentGameState)
                    {
                        case GameState.MainMenu:
                            play.draw(spriteBatch);
                            spriteBatch.Draw(Content.Load<Texture2D>("menu"), new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
                                                                            
                            break;
    
                        case GameState.Playing:
    
                           
                            break;
    
                     
                    }
                    
    
    
                    foreach (enemies enemy in Enemies)
                    {
                        enemy.draw(spriteBatch);
                    }
    
                     spriteBatch.End();
    
                    base.Draw(gameTime);
                }            
                        
    
        }
    }
    

    Bullet Class

    namespace Software_Design_Major_Project
    {
       
              class bullets // A new class needs to be created to allow for bullets.
              {
                    public Texture2D texture;
                    public Vector2 position;     
                    public bool isvisible;
                    public Rectangle bulletRectangle
                    {
                        get 
                        {
                            return new Rectangle((int)position.X, (int)position.Y, (int)texture.Width, (int)texture.Height);
                        }        
                    }      
                    
                    
                    
                  
                    
                    
    
                    public bullets(Texture2D newtexture) 
                    {
                        texture = newtexture;
                        isvisible = false;
                    }
    
                    public void update() 
                    {
                        position.Y -= 3; // velocity of the bullet                    
    
                    } 
                                       
                    
                    public void Draw(SpriteBatch spriteBatch) 
                    {
                        spriteBatch.Begin();
                        spriteBatch.Draw(texture, position, Color.White);
                        spriteBatch.End();
                    }
              }
    }
    

    Enemy Class

    namespace Software_Design_Major_Project
    {
        class enemies
        {
            public Texture2D enemyTexture;
            public Vector2 enemyPosition;        
            public bool enemyVisble = true;
            public float enemyMoveSpeed;
            public int Value;
            Random random = new Random();
            int randY;
            public Rectangle enemyRectangle 
            {
                get
                {
                    return new Rectangle((int)enemyPosition.X, (int)enemyPosition.Y, (int)enemyTexture.Width, (int)enemyTexture.Height);
                }
            }
    
            public enemies (Texture2D newEnemyTexture, Vector2 newEnemyPosition) 
            {
                enemyTexture = newEnemyTexture;
                enemyPosition = newEnemyPosition;
                randY = random.Next(1, 4);
                enemyMoveSpeed = randY;
                enemyVisble = true;                        
                Value = 100;            
    
            }       
    
            
            public void update(GraphicsDevice graphics) 
            {
                enemyPosition.Y += enemyMoveSpeed;
    
                if (enemyPosition.Y > 500)
                    enemyVisble = false;            
                
                
            }
    
            public void draw(SpriteBatch spriteBatch) 
            {
                
                spriteBatch.Draw(enemyTexture, enemyPosition, Color.White);            
                enemyVisble = true;
                
            }
    
        }
        
    }
    
    P.S: Sorry if I posted in the wrong category or used a bad question layout (this is my first post). 

    • Moved by Brian Smith - MSFTMicrosoft employee Friday, July 27, 2012 4:15 PM Posted to wrong forum - hope someone here can help, or redirect. (From:Project Online General Questions and Answers)
    Friday, July 27, 2012 9:08 AM

All replies