0% found this document useful (0 votes)
92 views16 pages

#Region

Uploaded by

János Vitéz
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views16 pages

#Region

Uploaded by

János Vitéz
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

#region File Description

//-----------------------------------------------------------------------------
// 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;

// The images we will draw


Texture2D characterTexture;
Texture2D fireballTexture;
Rectangle viewportRect;
Texture2D backgroundTexture;
Texture2D terrainTexture;
Texture2D nav1Texture;
Texture2D nav2Texture;
Texture2D life1Texture;
Texture2D life2Texture;
Texture2D life3Texture;
Texture2D shipTexture;
Texture2D starpuTexture;
Texture2D healthpuTexture;
Texture2D thousandTexture;
Texture2D hundredTexture;
Texture2D fivehundredTexture;
GameObject character;
KeyboardState previousKeyboardState = Keyboard.GetState();

//Background textures for the various screens in the game


Texture2D mGameOverScreen;
Texture2D mTitleScreenBackground;

//Screen State variables to indicate what is the current screen


bool mIsGameOverScreenShown;
bool mIsTitleScreenShown;

//player healthbar
Texture2D mhealthbar;
int mCurrentHealth = 140;

int score = 100;

int livecount = 0;
int starcount = 0;

SpriteFont font;

// The images will be drawn with this SpriteBatch


SpriteBatch spriteBatch;

//Set the postion of the terrain graphic


Vector2 scorepos = new Vector2(438.0f, 24.0f);
Vector2 terrainPosition = new Vector2(0.0f, 557.0f);
Vector2 nav1Position = new Vector2(0.2f, 21.0f);
Vector2 nav2Position = new Vector2(390.0f, 2.0f);
Vector2 life1Position = new Vector2(60.0f, 45.0f);
Vector2 life2Position = new Vector2(45.0f, 45.0f);
Vector2 life3Position = new Vector2(30.0f, 45.0f);

// 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;

Random random = new Random();

// For when a collision is detected


bool personHit = false;
bool personHit1 = false;
bool personHit2 = false;
bool personHit3 = false;
bool personHit4 = false;
bool personHit5 = false;
bool personHit6 = false;

// 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";

// Prefer a resolution suitable for both Windows and XBox 360


graphics.PreferredBackBufferWidth = 930;
graphics.PreferredBackBufferHeight = 620;
}

/// <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();

// Calculate safe bounds based on current resolution


Viewport viewport = graphics.GraphicsDevice.Viewport;
safeBounds = new Rectangle(
(int)(viewport.Width * SafeAreaPortion),
(int)(viewport.Height * SafeAreaPortion),
(int)(viewport.Width * (1 - 2 * SafeAreaPortion)),
(int)(viewport.Height * (1 - 2 * SafeAreaPortion)));

character.position.X = (safeBounds.Width - characterTexture.Width) / 2;


character.position.Y = safeBounds.Height - characterTexture.Height / 2;

/// <summary>
/// Load your graphics content.
/// </summary>
protected override void LoadContent()
{

// Initialize audio objects.


engine = new AudioEngine("Content\\Audio\\proj.xgs");
soundBank = new SoundBank(engine, "Content\\Audio\\Sound Bank.xsb");
waveBank = new WaveBank(engine, "Content\\Audio\\Wave Bank.xwb");

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");

//Initialize the screen state variables


mIsTitleScreenShown = true;
mIsGameOverScreenShown = false;

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);

// Create a sprite batch to draw those textures


spriteBatch = new SpriteBatch(graphics.GraphicsDevice);

/// <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);

//Based on the screen state variables, call the

//Update method associated with the current screen

if (mIsGameOverScreenShown)
{

UpdateGameOverScreen();

else if (mIsTitleScreenShown)
{

UpdateTitleScreen();

if (mCurrentHealth < 1 && livecount < 4)


{
livecount = livecount + 1;
mCurrentHealth = 140;
}
// Allows the game to exit
if (gamePad.Buttons.Back == ButtonState.Pressed ||
keyboard.IsKeyDown(Keys.Escape))
{
this.Exit();
}

KeyboardState keyboardState = Keyboard.GetState();


if (keyboardState.IsKeyDown(Keys.Left))
{
character.position.X -= character.speed;
}
if (keyboardState.IsKeyDown(Keys.Right))
{
character.position.X += character.speed;
}

//Force the health to remain between 0 and 100


mCurrentHealth = (int)MathHelper.Clamp(mCurrentHealth, 0, 100);

// Prevent the person from moving off of the screen


character.position.X = MathHelper.Clamp(character.position.X,
safeBounds.Left, safeBounds.Right - characterTexture.Width);

// Spawn new falling fireball


if (random.NextDouble() < fireballSpawnProbability)
{
float x = (float)random.NextDouble() *
(Window.ClientBounds.Width - fireballTexture.Width);
fireballPositions.Add(new Vector2(x, -fireballTexture.Height + 1));
}

// Spawn new ship


if (random.NextDouble() < ShipSpawnProbability)
{
float x = (float)random.NextDouble() *
(Window.ClientBounds.Width - shipTexture.Width);
shipPositions.Add(new Vector2(x, -shipTexture.Height + 1));
}

// Spawn new 1000 power-up


if (random.NextDouble() < ThousandPuSpawnProbability)
{
float x = (float)random.NextDouble() *
(Window.ClientBounds.Width - thousandTexture.Width);
thousandpuPositions.Add(new Vector2(x, -thousandTexture.Height + 1));
}

// Spawn new 100 power-up


if (random.NextDouble() < HundredPuSpawnProbability)
{
float x = (float)random.NextDouble() *
(Window.ClientBounds.Width - hundredTexture.Width);
hundredpuPositions.Add(new Vector2(x, -hundredTexture.Height + 1));
}

// Spawn new 500 power-up


if (random.NextDouble() < FiveHundredPuSpawnProbability)
{
float x = (float)random.NextDouble() *
(Window.ClientBounds.Width - fivehundredTexture.Width);
fivehundredpuPositions.Add(new Vector2(x, -fivehundredTexture.Height
+ 1));
}

// Spawn new health power-up


if (random.NextDouble() < HealthPuSpawnProbability)
{
float x = (float)random.NextDouble() *
(Window.ClientBounds.Width - healthpuTexture.Width);
healthpuPositions.Add(new Vector2(x, -healthpuTexture.Height + 1));
}

// Spawn new life power-up


if (random.NextDouble() < starpuSpawnProbability)
{
float x = (float)random.NextDouble() *
(Window.ClientBounds.Width - starpuTexture.Width);
starpuPositions.Add(new Vector2(x, -starpuTexture.Height + 1));
}

// Get the bounding rectangle of the person


Rectangle personRectangle =
new Rectangle((int)character.position.X, (int)character.position.Y,
characterTexture.Width, characterTexture.Height);

// Update each fireball


personHit = false;
for (int i = 0; i < fireballPositions.Count; i++)
{
// Animate this fireball falling
fireballPositions[i] =
new Vector2(fireballPositions[i].X,
fireballPositions[i].Y + fireballFallSpeed);

// Get the bounding rectangle of this fireball


Rectangle blockRectangle =
new Rectangle((int)fireballPositions[i].X,
(int)fireballPositions[i].Y,
fireballTexture.Width, fireballTexture.Height);

// Check collision with person


if (personRectangle.Intersects(blockRectangle))
{
{
personHit = true;
fireballPositions.RemoveAt(i);

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--;
}
}

// Update each ship


personHit6 = false;
for (int i = 0; i < shipPositions.Count; i++)
{
// Animate this fireball falling
shipPositions[i] =
new Vector2(shipPositions[i].X,
shipPositions[i].Y + ShipFallSpeed);

// Get the bounding rectangle of this fireball


Rectangle blockRectangle =
new Rectangle((int)shipPositions[i].X, (int)shipPositions[i].Y,
shipTexture.Width, shipTexture.Height);

// Check collision with person


if (personRectangle.Intersects(blockRectangle))
{
{
personHit6 = true;
shipPositions.RemoveAt(i);

// collision on ship hitting player


if (personHit6)
{
mCurrentHealth = mCurrentHealth - 2;
soundBank.PlayCue("sound4");
}

}
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--;
}
}

// Update each 1000 power-up


personHit1 = false;
for (int i = 0; i < thousandpuPositions.Count; i++)
{
// Animate this fireball falling
thousandpuPositions[i] =
new Vector2(thousandpuPositions[i].X,
thousandpuPositions[i].Y + ThousandPuFallSpeed);

// Get the bounding rectangle of this fireball


Rectangle blockRectangle =
new Rectangle((int)thousandpuPositions[i].X,
(int)thousandpuPositions[i].Y,
thousandTexture.Width, thousandTexture.Height);

// Check collision with person


if (personRectangle.Intersects(blockRectangle))
{
personHit1 = true;
thousandpuPositions.RemoveAt(i);

if (personHit1)
{
score = score + 1000;
// Play the sound.
soundBank.PlayCue("sound3");
}

// Remove this fireball if it have fallen off the screen


}

else if (thousandpuPositions[i].Y > Window.ClientBounds.Height - 99)


{
thousandpuPositions.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--;
}
}
// 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);

// Get the bounding rectangle of this fireball


Rectangle blockRectangle =
new Rectangle((int)hundredpuPositions[i].X,
(int)hundredpuPositions[i].Y,
hundredTexture.Width, hundredTexture.Height);

// Check collision with person


if (personRectangle.Intersects(blockRectangle))
{
personHit2 = true;
hundredpuPositions.RemoveAt(i);

if (personHit2)
{
score = score + 100;
// Play the sound.
soundBank.PlayCue("sound3");
}

// Remove this fireball if it have fallen off the screen


}
else if (hundredpuPositions[i].Y > Window.ClientBounds.Height - 99)
{
hundredpuPositions.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--;
}
}

// Update each 500 power-up


personHit3 = false;
for (int i = 0; i < fivehundredpuPositions.Count; i++)
{
// Animate this fireball falling
fivehundredpuPositions[i] =
new Vector2(fivehundredpuPositions[i].X,
fivehundredpuPositions[i].Y + FiveHundredPuFallSpeed);

// Get the bounding rectangle of this fireball


Rectangle blockRectangle =
new Rectangle((int)fivehundredpuPositions[i].X,
(int)fivehundredpuPositions[i].Y,
fivehundredTexture.Width, fivehundredTexture.Height);

// Check collision with person


if (personRectangle.Intersects(blockRectangle))
{
personHit3 = true;
fivehundredpuPositions.RemoveAt(i);

if (personHit3)
{
score = score + 500;
// Play the sound.
soundBank.PlayCue("sound3");
}

// Remove this fireball if it have fallen off the screen


}
else if (fivehundredpuPositions[i].Y > Window.ClientBounds.Height -
99)
{

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--;
}
}

// Update each health power-up


personHit4 = false;
for (int i = 0; i < healthpuPositions.Count; i++)
{
// Animate this fireball falling
healthpuPositions[i] =
new Vector2(healthpuPositions[i].X,
healthpuPositions[i].Y + HealthPuFallSpeed);

// Get the bounding rectangle of this fireball


Rectangle blockRectangle =
new Rectangle((int)healthpuPositions[i].X,
(int)healthpuPositions[i].Y,
healthpuTexture.Width, healthpuTexture.Height);

// Check collision with person


if (personRectangle.Intersects(blockRectangle))
{
personHit4 = true;
healthpuPositions.RemoveAt(i);

if (personHit4 && mCurrentHealth < 140)


{
mCurrentHealth = mCurrentHealth + 1;
// Play the sound.
soundBank.PlayCue("sound3");
}

}
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--;
}
}

// Update each star power-up


personHit5 = false;
for (int i = 0; i < starpuPositions.Count; i++)
{
// Animate this fireball falling
starpuPositions[i] =
new Vector2(starpuPositions[i].X,
starpuPositions[i].Y + starpuFallSpeed);

// Get the bounding rectangle of this fireball


Rectangle blockRectangle =
new Rectangle((int)starpuPositions[i].X,
(int)starpuPositions[i].Y,
starpuTexture.Width, starpuTexture.Height);

// Check collision with person


if (personRectangle.Intersects(blockRectangle))
{
starpuPositions.RemoveAt(i);
personHit5 = true;

if (personHit5)
{
starcount++;
soundBank.PlayCue("sound3");
}

else if (starpuPositions[i].Y > Window.ClientBounds.Height - 99)


{
starpuPositions.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--;
}
}

base.Update(gameTime);
}
}

private void UpdateGameOverScreen()


{

if (Keyboard.GetState().IsKeyDown(Keys.Space) == true)
{
mIsTitleScreenShown = true;

mIsGameOverScreenShown = false;

return;
}
else if (Keyboard.GetState().IsKeyDown(Keys.Escape) == true)
{
this.Exit();
}
}

private void UpdateTitleScreen()


{

//Move back to the Controller detect screen if the player moves

//back (using B) from the Title screen (this is typical game behavior

//and is used to switch to a new player one controller)

if (Keyboard.GetState().IsKeyDown(Keys.Enter) == true)
{

mIsTitleScreenShown = false;

mIsGameOverScreenShown = false;

return;

}
else if (Keyboard.GetState().IsKeyDown(Keys.Escape) == true)
{
this.Exit();
}

private void DrawGameOverScreen()


{
//Draw all of the elements that are part of the game over screen

spriteBatch.Draw(mGameOverScreen, Vector2.Zero, Color.White);

}
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)
{

GraphicsDevice device = graphics.GraphicsDevice;

spriteBatch.Begin();

spriteBatch.Draw(backgroundTexture, viewportRect, Color.White);

spriteBatch.Draw(character.sprite,
character.position,
null,
Color.White,
character.rotation,
character.center, 1.0f,
SpriteEffects.None, 0);

spriteBatch.Draw(terrainTexture, terrainPosition, Color.White);

spriteBatch.Draw(nav1Texture, nav1Position, Color.White);

spriteBatch.Draw(nav2Texture, nav2Position, Color.White);

// 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);

// Draw 1000 power-up


foreach (Vector2 thousandpuPosition in thousandpuPositions)
spriteBatch.Draw(thousandTexture, thousandpuPosition, Color.White);

// Draw 100 power-up


foreach (Vector2 hundredpuPosition in hundredpuPositions)
spriteBatch.Draw(hundredTexture, hundredpuPosition, Color.White);

// Draw 500 power-up


foreach (Vector2 fivehundredpuPosition in fivehundredpuPositions)
spriteBatch.Draw(fivehundredTexture, fivehundredpuPosition, Color.White);

// Draw health power-up


foreach (Vector2 healthpuPosition in healthpuPositions)
spriteBatch.Draw(healthpuTexture, healthpuPosition, Color.White);

// Draw star power-up


foreach (Vector2 starpuPosition in starpuPositions)
spriteBatch.Draw(starpuTexture, starpuPosition, Color.White);

//Draw the negative space for the health bar


spriteBatch.Draw(mhealthbar, new Rectangle(0, 1, mhealthbar.Width, 20), new
Rectangle(0, 45, mhealthbar.Width, 44), Color.Black);

//Draw the current health level based on the current Health


spriteBatch.Draw(mhealthbar, new Rectangle(0, 1, (int)(mhealthbar.Width *
((double)mCurrentHealth / 100)), 20), new Rectangle(0, 45, mhealthbar.Width, 44),
Color.DarkRed);

//Draw the box around the health bar


spriteBatch.Draw(mhealthbar, new Rectangle(0, 1, mhealthbar.Width, 20), new
Rectangle(0, 0, mhealthbar.Width, 44), 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;
}

spriteBatch.DrawString(font, score.ToString(), scorepos, Color.White);

//Based on the screen state variables, call the

//Draw method associated with the current screen

if (mIsGameOverScreenShown)
{

DrawGameOverScreen();

else if (mIsTitleScreenShown)
{
DrawTitleScreen();

spriteBatch.End();

base.Draw(gameTime);
}
}

You might also like