C# Projects

Singularity - 3D XNA Space Shooter

 

I recently worked on creating a sidescrolling shooter using Microsoft XNA. I co-created the game with Stefan Dieckmann. It is a simple space shooter that uses a scrolling background to give the illusion of moving through space. Rather than using sprites or 2D images for the objects in the game, we decided to make it in 3D and have a fixed camera to give the feeling of playing it in 2D. Through the use of OOP, we were able to demonstrate the use of 3D rendering and collision detection.

  • Game Loop
  • Collision Detection
  • Download Source

    Solution Screen:

    Game Loop

    View Full Code :

    #region Update
    /// <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)
    {
      soundEnginePlaying = false// Reset sound
      input.Update();    // Call input update
     
      if (input.currentKeyboardState.IsKeyDown(Keys.Escape))
        Exit();
     
      // Calls collision detection
      CheckCollisions();
     
      // Update the ship
      if (ship.Update(gameTime))
      {
        // Allow the player to make sound
        soundEnginePlaying = true;
        soundFire1.Play();
      }
     
      //Update background
      bgStars.Update(elapsed * 100);
     
      // Update each asteroid being rendered
      for (int counter = 0; counter < maxAsteroids; counter++)
      {
        // Reset asteroid
        Asteroid asteroid = null;
        asteroid = asteroids[counter];
     
        // If an asteroid exists
        if (asteroid != null)
        {
          // Reset asteroid if moves past boundary
          if (asteroid.Position.Z >= MaximumZPos + 500)
            asteroids[counter] = null;
          else
            // Update each asteroid
            asteroid.Update(gameTime);
        }
        else
          // Create random asteroid
          asteroids[counter] = new Asteroid(rand);
      }
     
      base.Update(gameTime);
    }

    Collision Detection

    View Full Code :

    public void CheckCollisions()
    {
      // Bounding sphere for ship
      BoundingSphere shipSphere =    // Bounding sphere for ship
    	new BoundingSphere(ship.Position, 100f);
     
      // Check for collisions with EACH asteroid
      for (int counter = 0; counter < maxAsteroids; counter++)
      {
        Asteroid asteroid = null// Reset asteroid
        asteroid = asteroids[counter];
        if (asteroid != null)
        {
          BoundingSphere asteroidSphere =// Bounding sphere for asteroid
           new BoundingSphere(asteroid.Position, 200f);
     
          // If there is a collision detected between Ship and Asteroid
          if(shipSphere.Contains(asteroidSphere) != ContainmentType.Disjoint)
          {
            asteroids[counter] = null// Reset asteroid
            ship.Reset();
            soundEnginePlaying = true;
            soundExplosion1.Play();
            playerScore = 0;
          }
          // Check for collision with EACH ship projectile
          for (int counter1 = 0; counter1 < ship.projectiles.Count; counter1++)
          {
            Projectile proj = null;// Reset projectile
            proj = ship.projectiles[counter1];
     
            if (proj != null)
            {
              BoundingSphere projSphere =// Bounding sphere for projectile
              new BoundingSphere(proj.projectilePosition, 50f);
     
              // If there is a collision detected between Projectile and Asteroid
              if (asteroidSphere.Contains(projSphere) != ContainmentType.Disjoint)
              {
                asteroids[counter] = null;// Reset asteroid
                ship.projectiles.Remove(proj);
                soundEnginePlaying = true;
                soundExplosion2.Play();
                playerScore += 10;
              }
            }
         }
      }
    }