#Region
#Region
//-----------------------------------------------------------------------------
// Game.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion
namespace RectangleCollision
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class RectangleCollisionGame : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
// Audio objects
AudioEngine engine;
SoundBank soundBank;
WaveBank waveBank;
//player healthbar
Texture2D mhealthbar;
int mCurrentHealth = 140;
int livecount = 0;
int starcount = 0;
SpriteFont font;
// Fireball
List<Vector2> fireballPositions = new List<Vector2>();
float fireballSpawnProbability = 0.093f;
const int fireballFallSpeed = 5;
// Ship
List<Vector2> shipPositions = new List<Vector2>();
float ShipSpawnProbability = 0.0016f;
const int ShipFallSpeed = 3;
// 1000 power up
List<Vector2> thousandpuPositions = new List<Vector2>();
float ThousandPuSpawnProbability = 0.002f;
const int ThousandPuFallSpeed = 4;
// 100 power up
List<Vector2> hundredpuPositions = new List<Vector2>();
float HundredPuSpawnProbability = 0.007f;
const int HundredPuFallSpeed = 3;
// 500 powerup
List<Vector2> fivehundredpuPositions = new List<Vector2>();
float FiveHundredPuSpawnProbability = 0.004f;
const int FiveHundredPuFallSpeed = 3;
// Health powerup
List<Vector2> healthpuPositions = new List<Vector2>();
float HealthPuSpawnProbability = 0.002f;
const int HealthPuFallSpeed = 3;
// Star powerup
List<Vector2> starpuPositions = new List<Vector2>();
float starpuSpawnProbability = 0.001f;
const int starpuFallSpeed = 3;
// The sub-rectangle of the drawable area which should be visible on all TVs
Rectangle safeBounds;
// Percentage of the screen on every side is the safe area
const float SafeAreaPortion = 0.05f;
public RectangleCollisionGame()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to
/// run. This is where it can query for any required services and load any
/// non-graphic related content. Calling base.Initialize will enumerate through
/// any components and initialize them as well.
/// </summary>
protected override void Initialize()
{
base.Initialize();
/// <summary>
/// Load your graphics content.
/// </summary>
protected override void LoadContent()
{
soundBank.PlayCue("melody");
// Load textures
character = new GameObject(Content.Load<Texture2D>("character"));
character.position = new Vector2(120, graphics.GraphicsDevice.Viewport.Height
- 85);
mGameOverScreen = Content.Load<Texture2D>("GameOverScreen");
mTitleScreenBackground = Content.Load<Texture2D>("TitleScreen");
fireballTexture = Content.Load<Texture2D>("fireball");
shipTexture = Content.Load<Texture2D>("ship");
characterTexture = Content.Load<Texture2D>("character");
backgroundTexture = Content.Load<Texture2D>("moonbg");
terrainTexture = Content.Load<Texture2D>("terrain");
mhealthbar = Content.Load<Texture2D>("healthbar");
nav1Texture = Content.Load<Texture2D>("nav1");
nav2Texture = Content.Load<Texture2D>("nav2");
life1Texture = Content.Load<Texture2D>("life1");
life2Texture = Content.Load<Texture2D>("life2");
life3Texture = Content.Load<Texture2D>("life3");
starpuTexture = Content.Load<Texture2D>("starpu");
healthpuTexture = Content.Load<Texture2D>("healthpu");
thousandTexture = Content.Load<Texture2D>("1000pu");
hundredTexture = Content.Load<Texture2D>("100pu");
fivehundredTexture = Content.Load<Texture2D>("500pu");
font = Content.Load<SpriteFont>("myFont");
// Draw in the background
viewportRect = new Rectangle(0, 0,
graphics.GraphicsDevice.Viewport.Width,
graphics.GraphicsDevice.Viewport.Height);
/// <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)
{
if (!mIsTitleScreenShown)
{
score++;
// Get input
KeyboardState keyboard = Keyboard.GetState();
GamePadState gamePad = GamePad.GetState(PlayerIndex.One);
if (mIsGameOverScreenShown)
{
UpdateGameOverScreen();
else if (mIsTitleScreenShown)
{
UpdateTitleScreen();
if (personHit)
{
mCurrentHealth = mCurrentHealth - 3;
// Play the sound.
soundBank.PlayCue("sound1");
}
}
else if (fireballPositions[i].Y > Window.ClientBounds.Height - 99)
{
fireballPositions.RemoveAt(i);
// When removing a fireball, the next fireball will have the same
index
// as the current fireball. Decrement i to prevent skipping a
fireball.
i--;
}
}
}
else if (shipPositions[i].Y > Window.ClientBounds.Height - 99)
{
shipPositions.RemoveAt(i);
// When removing a fireball, the next fireball will have the same
index
// as the current fireball. Decrement i to prevent skipping a
fireball.
i--;
}
}
if (personHit1)
{
score = score + 1000;
// Play the sound.
soundBank.PlayCue("sound3");
}
// When removing a fireball, the next fireball will have the same
index
// as the current fireball. Decrement i to prevent skipping a
fireball.
i--;
}
}
// Update each 100 power-up
personHit2 = false;
for (int i = 0; i < hundredpuPositions.Count; i++)
{
// Animate this fireball falling
hundredpuPositions[i] =
new Vector2(hundredpuPositions[i].X,
hundredpuPositions[i].Y + HundredPuFallSpeed);
if (personHit2)
{
score = score + 100;
// Play the sound.
soundBank.PlayCue("sound3");
}
// When removing a fireball, the next fireball will have the same
index
// as the current fireball. Decrement i to prevent skipping a
fireball.
i--;
}
}
if (personHit3)
{
score = score + 500;
// Play the sound.
soundBank.PlayCue("sound3");
}
fivehundredpuPositions.RemoveAt(i);
// When removing a fireball, the next fireball will have the same
index
// as the current fireball. Decrement i to prevent skipping a
fireball.
i--;
}
}
}
else if (healthpuPositions[i].Y > Window.ClientBounds.Height - 99)
{
healthpuPositions.RemoveAt(i);
// When removing a fireball, the next fireball will have the same
index
// as the current fireball. Decrement i to prevent skipping a
fireball.
i--;
}
}
if (personHit5)
{
starcount++;
soundBank.PlayCue("sound3");
}
// When removing a fireball, the next fireball will have the same
index
// as the current fireball. Decrement i to prevent skipping a
fireball.
i--;
}
}
base.Update(gameTime);
}
}
if (Keyboard.GetState().IsKeyDown(Keys.Space) == true)
{
mIsTitleScreenShown = true;
mIsGameOverScreenShown = false;
return;
}
else if (Keyboard.GetState().IsKeyDown(Keys.Escape) == true)
{
this.Exit();
}
}
//back (using B) from the Title screen (this is typical game behavior
if (Keyboard.GetState().IsKeyDown(Keys.Enter) == true)
{
mIsTitleScreenShown = false;
mIsGameOverScreenShown = false;
return;
}
else if (Keyboard.GetState().IsKeyDown(Keys.Escape) == true)
{
this.Exit();
}
}
private void DrawTitleScreen()
{ //Draw all of the elements that are part of the Title screen
spriteBatch.Draw(mTitleScreenBackground, Vector2.Zero, Color.White);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
spriteBatch.Begin();
spriteBatch.Draw(character.sprite,
character.position,
null,
Color.White,
character.rotation,
character.center, 1.0f,
SpriteEffects.None, 0);
// Draw fireballs
foreach (Vector2 fireballPosition in fireballPositions)
spriteBatch.Draw(fireballTexture, fireballPosition, Color.White);
//Draw ships
foreach (Vector2 shipPosition in shipPositions)
spriteBatch.Draw(shipTexture, shipPosition, Color.White);
if (livecount <= 0)
{
spriteBatch.Draw(life1Texture, life1Position, Color.White);
spriteBatch.Draw(life2Texture, life2Position, Color.White);
spriteBatch.Draw(life3Texture, life3Position, Color.White);
}
else if (livecount == 1)
{
spriteBatch.Draw(life2Texture, life2Position, Color.White);
spriteBatch.Draw(life3Texture, life3Position, Color.White);
}
else if (livecount == 2)
{
spriteBatch.Draw(life3Texture, life3Position, Color.White);
}
else if (livecount == 4)
{
mIsGameOverScreenShown = true;
}
if (mIsGameOverScreenShown)
{
DrawGameOverScreen();
else if (mIsTitleScreenShown)
{
DrawTitleScreen();
spriteBatch.End();
base.Draw(gameTime);
}
}