Original Post
Hi all, First of all i'd like to say i already made a topic for this in the past, but that answer didn't help. This is the old topic: http://www.gamedev.net/community/forums/topic.asp?topic_id=496654 Now my problem is that when i play the game, it feels "laggy" and i think it is because its still not free from fixed values. Could some1 tell me if i still got fixed values here?
[source lang=csharp]
#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
#region author
// author: Frank de Kievit
// date: spring 2008
// description: fangame to apply to NHTV.
// ------------FOR PERSONAL USE ONLY!-------------
#endregion
namespace fan_game
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
#region declare attributes
GraphicsDeviceManager graphics;
Mouse mouse1 = new Mouse();
Mouse mouse2 = new Mouse();
Mouse mouse3 = new Mouse();
Mouse mouse4 = new Mouse();
Mouse mouse5 = new Mouse();
Mouse mouse6 = new Mouse();
Mouse mouse7 = new Mouse();
Mouse mouse8 = new Mouse();
Mouse mouse9 = new Mouse();
Mouse mouse10 = new Mouse();
Mouse mouse11 = new Mouse();
Mouse mouse12 = new Mouse();
Mouse mouse13 = new Mouse();
Mouse mouse14 = new Mouse();
Mouse mouse15 = new Mouse();
private List<Mouse> mice;
ContentManager content;
KeyboardState oldState;
Color backColor = Color.White;
Color yellowBar = new Color(255, 255, 0, 128); // 128 = 50% opacity
float verticalFanPosition = 1;
float horizontalFanPosition = 75;
double lastSpawnTime = 0;
double waitThreeSeconds = 3;
bool drawWind = false;
int level = 0;
int numberOfCheese = 3;
Rectangle menuRectangle = new Rectangle(350, 350, 200, 50);
Rectangle windPos = new Rectangle();
Rectangle invisWindArea = new Rectangle();
Texture2D backgroundTexture,
choiceTexture,
fanTexture,
gameoverTexture,
instructionsTexture,
level1Texture,
level2Texture,
level3Texture,
level4Texture,
level5Texture,
livesTexture,
menuTexture,
mouseTexture,
mouse2Texture,
mouse3Texture,
mouse4Texture,
mouse5Texture,
mouse6Texture,
mouse7Texture,
mouse8Texture,
mouse9Texture,
mouse10Texture,
mouse11Texture,
mouse12Texture,
mouse13Texture,
mouse14Texture,
mouse15Texture,
winTexture,
windTexture;
SpriteBatch backgroundBatch, foregroundBatch;
Vector2 defaultPosition = new Vector2(0, 0);
Random randomMousePosition = new Random();
enum GameMode
{
Menu,
Instructions,
FanGame,
GameOver,
Win
}
#endregion
GameMode gameMode = GameMode.Menu;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
content = new ContentManager(Services);
oldState = Keyboard.GetState();
// Set basic Window Properties
graphics.PreferredBackBufferFormat = SurfaceFormat.Bgr565;
graphics.PreferredBackBufferWidth = 1024;
graphics.PreferredBackBufferHeight = 768;
level = 1;
#region mouse variables
// Set mouse variables
mouse1.YPos = GenerateRandomMouseHeight();
mouse1.CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
mouse1.ShowThisMouse = true;
mouse2.YPos = GenerateRandomMouseHeight();
mouse2.CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
mouse2.ShowThisMouse = false;
mouse3.YPos = GenerateRandomMouseHeight();
mouse3.CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
mouse3.ShowThisMouse = false;
mouse4.YPos = GenerateRandomMouseHeight();
mouse4.CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
mouse4.ShowThisMouse = false;
mouse5.YPos = GenerateRandomMouseHeight();
mouse5.CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
mouse5.ShowThisMouse = false;
mouse6.YPos = GenerateRandomMouseHeight();
mouse6.CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
mouse6.ShowThisMouse = false;
mouse7.YPos = GenerateRandomMouseHeight();
mouse7.CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
mouse7.ShowThisMouse = false;
mouse8.YPos = GenerateRandomMouseHeight();
mouse8.CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
mouse8.ShowThisMouse = false;
mouse9.YPos = GenerateRandomMouseHeight();
mouse9.CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
mouse9.ShowThisMouse = false;
mouse10.YPos = GenerateRandomMouseHeight();
mouse10.CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
mouse10.ShowThisMouse = false;
mouse11.YPos = GenerateRandomMouseHeight();
mouse11.CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
mouse11.ShowThisMouse = false;
mouse12.YPos = GenerateRandomMouseHeight();
mouse12.CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
mouse12.ShowThisMouse = false;
mouse13.YPos = GenerateRandomMouseHeight();
mouse13.CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
mouse13.ShowThisMouse = false;
mouse14.YPos = GenerateRandomMouseHeight();
mouse14.CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
mouse14.ShowThisMouse = false;
mouse15.YPos = GenerateRandomMouseHeight();
mouse15.CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
mouse15.ShowThisMouse = false;
mice = new System.Collections.Generic.List<Mouse>();
mice.Add(mouse1);
mice.Add(mouse2);
mice.Add(mouse3);
mice.Add(mouse4);
mice.Add(mouse5);
mice.Add(mouse6);
mice.Add(mouse7);
mice.Add(mouse8);
mice.Add(mouse9);
mice.Add(mouse10);
mice.Add(mouse11);
mice.Add(mouse12);
mice.Add(mouse13);
mice.Add(mouse14);
mice.Add(mouse15);
#endregion
}
/// <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();
backgroundTexture = content.Load<Texture2D>("sprites/achtergrond");
backgroundBatch = new SpriteBatch(graphics.GraphicsDevice);
oldState = Keyboard.GetState();
}
/// <summary>
/// Load your graphics content. If loadAllContent is true, you should
/// load content from both ResourceManagementMode pools. Otherwise, just
/// load ResourceManagementMode.Manual content.
/// </summary>
/// <param name="loadAllContent">Which type of content to load.</param>
protected override void LoadGraphicsContent(bool loadAllContent)
{
if (loadAllContent)
{
foregroundBatch = new SpriteBatch(graphics.GraphicsDevice);
choiceTexture = content.Load<Texture2D>("sprites/yellow");
fanTexture = content.Load<Texture2D>("sprites/fansprite");
gameoverTexture = content.Load<Texture2D>("sprites/gameover");
instructionsTexture = content.Load<Texture2D>("sprites/instructions");
level1Texture = content.Load<Texture2D>("sprites/level1");
level2Texture = content.Load<Texture2D>("sprites/level2");
level3Texture = content.Load<Texture2D>("sprites/level3");
level4Texture = content.Load<Texture2D>("sprites/level4");
level5Texture = content.Load<Texture2D>("sprites/level5");
livesTexture = content.Load<Texture2D>("sprites/lives");
menuTexture = content.Load<Texture2D>("sprites/menu");
mouseTexture = content.Load<Texture2D>("sprites/mousesprite");
mouse2Texture = content.Load<Texture2D>("sprites/mousesprite");
mouse3Texture = content.Load<Texture2D>("sprites/mousesprite");
mouse4Texture = content.Load<Texture2D>("sprites/mousesprite");
mouse5Texture = content.Load<Texture2D>("sprites/mousesprite");
mouse6Texture = content.Load<Texture2D>("sprites/mousesprite");
mouse7Texture = content.Load<Texture2D>("sprites/mousesprite");
mouse8Texture = content.Load<Texture2D>("sprites/mousesprite");
mouse9Texture = content.Load<Texture2D>("sprites/mousesprite");
mouse10Texture = content.Load<Texture2D>("sprites/mousesprite");
mouse11Texture = content.Load<Texture2D>("sprites/mousesprite");
mouse12Texture = content.Load<Texture2D>("sprites/mousesprite");
mouse13Texture = content.Load<Texture2D>("sprites/mousesprite");
mouse14Texture = content.Load<Texture2D>("sprites/mousesprite");
mouse15Texture = content.Load<Texture2D>("sprites/mousesprite");
winTexture = content.Load<Texture2D>("sprites/win");
windTexture = content.Load<Texture2D>("sprites/Wind");
}
}
/// <summary>
/// Unload your graphics content. If unloadAllContent is true, you should
/// unload content from both ResourceManagementMode pools. Otherwise, just
/// unload ResourceManagementMode.Manual content. Manual content will get
/// Disposed by the GraphicsDevice during a Reset.
/// </summary>
/// <param name="unloadAllContent">Which type of content to unload.</param>
protected override void UnloadGraphicsContent(bool unloadAllContent)
{
if (unloadAllContent)
{
// TODO: Unload any ResourceManagementMode.Automatic content
content.Unload();
}
// TODO: Unload any ResourceManagementMode.Manual content
}
/// <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)
{
KeyboardState newState = Keyboard.GetState();
// Allows the game to exit
if (oldState.IsKeyDown(Keys.Escape))
{
this.Exit();
}
switch (gameMode)
{
#region gameMode == GameMode.Menu
case GameMode.Menu:
{
#region up
// Is the up-arrow key down?
if (newState.IsKeyDown(Keys.Up))
{
// If not down last update, key has just been pressed.
if (!oldState.IsKeyDown(Keys.Up))
{
if (menuRectangle.Y == 350)
{
menuRectangle.Y = 450;
}
else if (menuRectangle.Y == 400)
{
menuRectangle.Y = 350;
}
else if (menuRectangle.Y == 450)
{
menuRectangle.Y = 400;
}
}
}
#endregion
#region down
// Is the down-arrow key down?
if (newState.IsKeyDown(Keys.Down))
{
// If not down last update, key has just been pressed.
if (!oldState.IsKeyDown(Keys.Down))
{
if (menuRectangle.Y == 350)
{
menuRectangle.Y = 400;
}
else if (menuRectangle.Y == 400)
{
menuRectangle.Y = 450;
}
else if (menuRectangle.Y == 450)
{
menuRectangle.Y = 350;
}
}
}
#endregion
#region enter
// Is the enter key down?
if (newState.IsKeyDown(Keys.Enter))
{
// If not down last update, key has just been pressed.
if (!oldState.IsKeyDown(Keys.Enter))
{
if (menuRectangle.Y == 350)
{
gameMode = GameMode.FanGame;
}
else if (menuRectangle.Y == 400)
{
gameMode = GameMode.Instructions;
}
else if (menuRectangle.Y == 450)
{
this.Exit();
}
}
}
#endregion
}
break;
#endregion
#region gameMode == GameMode.Instructions
case GameMode.Instructions:
{
#region backspace
// Is the backspace key down?
if (newState.IsKeyDown(Keys.Back))
{
// If not down last update, key has just been pressed.
if (!oldState.IsKeyDown(Keys.Back))
{
gameMode = GameMode.Menu;
}
}
#endregion
}
break;
#endregion
#region gameMode == GameMode.FanGame
case GameMode.FanGame:
{
// Move half way across the screen each second
float moveFactorPerSecond = 25f * (float)gameTime.ElapsedRealTime.TotalMilliseconds / 100.0f;
foreach (Mouse mse in mice)
{
if (mse.ShowThisMouse == true)
{
float lastFrameTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
double currentGameTime = gameTime.TotalGameTime.TotalSeconds;
if ((currentGameTime - lastSpawnTime) > waitThreeSeconds)
{
#region move mouse
// Move mouse
if (mse.BlownByWind == false)
{
mse.XPos -= 100f * lastFrameTime;
}
else if (mse.BlownByWind == true)
{
mse.XPos += 100f * lastFrameTime;
}
#endregion
}
#region kill mouse at cheese or spikes
// Set status to AteCheese when mouse is at cheese
if (mse.CurMouseStatus == Mouse.CurrentMouseStatus.Alive)
{
if (mse.XPos < 30)
{
mse.CurMouseStatus = Mouse.CurrentMouseStatus.AteCheese;
}
// Set status to dead when the sprite is at the spikes
else if (mse.XPos > 680)
{
mse.CurMouseStatus = Mouse.CurrentMouseStatus.Dead;
}
}
#endregion
#region collision detection
// Collision detection with invis rectangle, if there is a collision, set mouseHit to true.
if (drawWind)
{
invisWindArea = windPos;
invisWindArea.Width = 700;
if (invisWindArea.Intersects(mse.mousePosRectangle))
{
mse.BlownByWind = true;
}
else
{
mse.BlownByWind = false;
}
}
else
{
mse.BlownByWind = false;
}
#endregion
#region swich to game.Gameover when 3 pieces of cheese are eaten
if (numberOfCheese < 1)
{
gameMode = GameMode.GameOver;
}
#endregion
}
}
#region space
// Is the SPACE key down?
if (newState.IsKeyDown(Keys.Space))
{
// If not down last update, key has just been pressed.
if (!oldState.IsKeyDown(Keys.Space))
{
drawWind = true;
}
}
else if (oldState.IsKeyDown(Keys.Space))
{
// Key was down last update, but not down now, so
// it has just been released.
drawWind = false;
}
#endregion
#region up
// Is the up-arrow key down?
if (newState.IsKeyDown(Keys.Up))
{
verticalFanPosition -= moveFactorPerSecond;
}
else if (oldState.IsKeyDown(Keys.Up))
{
// Key was down last update, but not down now, so
// it has just been released.
verticalFanPosition -= moveFactorPerSecond;
}
#endregion
#region down
// Is the down-arrow key down?
if (newState.IsKeyDown(Keys.Down))
{
verticalFanPosition += moveFactorPerSecond;
}
else if (oldState.IsKeyDown(Keys.Down))
{
// Key was down last update, but not down now, so
// it has just been released.
//backColor = new Color(255, 255, 255);
}
#endregion
#region leftctrl
// Is the ctrl key down?
if (newState.IsKeyDown(Keys.LeftControl))
{
// If not down last update, key has just been pressed.
if (!oldState.IsKeyDown(Keys.LeftControl))
{
backColor = new Color(backColor.R, backColor.G, (byte)~backColor.B);
//switchFanSpeed();
}
}
else if (oldState.IsKeyDown(Keys.LeftControl))
{
// Key was down last update, but not down now, so
// it has just been released.
//backColor = new Color(255, 255, 255);
}
#endregion
}
break;
#endregion
#region gameMode == GameMode.GameOver
case GameMode.GameOver:
{
#region leftctrl (debug)
// Is the ctrl key down?
if (newState.IsKeyDown(Keys.LeftControl))
{
// If not down last update, key has just been pressed.
if (!oldState.IsKeyDown(Keys.LeftControl))
{
backColor = new Color(backColor.R, backColor.G, (byte)~backColor.B);
}
}
else if (oldState.IsKeyDown(Keys.LeftControl))
{
// Key was down last update, but not down now, so
// it has just been released.
backColor = new Color(255, 255, 255);
}
#endregion
}
break;
#endregion
#region gameMode == GameMode.Win
case GameMode.Win:
{
#region leftctrl (debug)
// Is the ctrl key down?
if (newState.IsKeyDown(Keys.LeftControl))
{
// If not down last update, key has just been pressed.
if (!oldState.IsKeyDown(Keys.LeftControl))
{
backColor = new Color(backColor.R, backColor.G, (byte)~backColor.B);
}
}
else if (oldState.IsKeyDown(Keys.LeftControl))
{
// Key was down last update, but not down now, so
// it has just been released.
backColor = new Color(255, 255, 255);
}
#endregion
}
break;
#endregion
}
// Update saved state.
oldState = newState;
base.Update(gameTime);
}
/// <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)
{
// Clear screen
graphics.GraphicsDevice.Clear(backColor);
// draw background seperatly
#region draw background
backgroundBatch.Begin();
backgroundBatch.Draw(backgroundTexture, defaultPosition, backColor);
backgroundBatch.End();
#endregion
// Show screen depending on gamemode
switch (gameMode)
{
#region gameMode == GameMode.Menu
case GameMode.Menu:
{
foregroundBatch.Begin();
foregroundBatch.Draw(menuTexture, defaultPosition, Color.White);
foregroundBatch.Draw(choiceTexture, menuRectangle, yellowBar);
foregroundBatch.End();
if (oldState.IsKeyDown(Keys.Enter))
{
gameMode = GameMode.FanGame;
}
break;
}
#endregion
#region gameMode == GameModeInstructions
case GameMode.Instructions:
{
foregroundBatch.Begin();
foregroundBatch.Draw(instructionsTexture, defaultPosition, Color.White);
foregroundBatch.End();
}
break;
#endregion
#region gameMode == GameMode.FanGame
case GameMode.FanGame:
{
#region draw fan
// calculate fanRectangle, and draw fan in it
Rectangle fanRectangle = new Rectangle((int)horizontalFanPosition, (int)verticalFanPosition, fanTexture.Width, fanTexture.Height);
foregroundBatch.Begin();
if (level > 0 && level < 6)
{
foregroundBatch.Draw(fanTexture, fanRectangle, Color.White);
}
foregroundBatch.End();
#endregion
#region draw mouse
#region create rectangles for the mice
mice[0].mousePosRectangle = new Rectangle((int)mice[0].XPos, (int)mice[0].YPos, mouseTexture.Width, mouseTexture.Height);
mice[1].mousePosRectangle = new Rectangle((int)mice[1].XPos, (int)mice[1].YPos, mouse2Texture.Width, mouse2Texture.Height);
mice[2].mousePosRectangle = new Rectangle((int)mice[2].XPos, (int)mice[2].YPos, mouse3Texture.Width, mouse3Texture.Height);
mice[3].mousePosRectangle = new Rectangle((int)mice[3].XPos, (int)mice[3].YPos, mouse4Texture.Width, mouse4Texture.Height);
mice[4].mousePosRectangle = new Rectangle((int)mice[4].XPos, (int)mice[4].YPos, mouse5Texture.Width, mouse5Texture.Height);
mice[5].mousePosRectangle = new Rectangle((int)mice[5].XPos, (int)mice[5].YPos, mouse6Texture.Width, mouse6Texture.Height);
mice[6].mousePosRectangle = new Rectangle((int)mice[6].XPos, (int)mice[6].YPos, mouse7Texture.Width, mouse7Texture.Height);
mice[7].mousePosRectangle = new Rectangle((int)mice[7].XPos, (int)mice[7].YPos, mouse8Texture.Width, mouse8Texture.Height);
mice[8].mousePosRectangle = new Rectangle((int)mice[8].XPos, (int)mice[8].YPos, mouse9Texture.Width, mouse9Texture.Height);
mice[9].mousePosRectangle = new Rectangle((int)mice[9].XPos, (int)mice[9].YPos, mouse10Texture.Width, mouse10Texture.Height);
mice[10].mousePosRectangle = new Rectangle((int)mice[10].XPos, (int)mice[10].YPos, mouse11Texture.Width, mouse11Texture.Height);
mice[11].mousePosRectangle = new Rectangle((int)mice[11].XPos, (int)mice[11].YPos, mouse12Texture.Width, mouse12Texture.Height);
mice[12].mousePosRectangle = new Rectangle((int)mice[12].XPos, (int)mice[12].YPos, mouse13Texture.Width, mouse13Texture.Height);
mice[13].mousePosRectangle = new Rectangle((int)mice[13].XPos, (int)mice[13].YPos, mouse14Texture.Width, mouse14Texture.Height);
mice[14].mousePosRectangle = new Rectangle((int)mice[14].XPos, (int)mice[14].YPos, mouse15Texture.Width, mouse15Texture.Height);
#endregion
#region level 1
if (level == 1)
{
double currentGameTime = gameTime.TotalGameTime.TotalSeconds;
if (mice[0].CurMouseStatus != Mouse.CurrentMouseStatus.Dead)
{
if ((currentGameTime - lastSpawnTime) > waitThreeSeconds)
{
foregroundBatch.Begin();
if (mice[0].CurMouseStatus == Mouse.CurrentMouseStatus.Alive)
{
foregroundBatch.Draw(mouseTexture, mice[0].mousePosRectangle, Color.White);
}
else if(mice[0].CurMouseStatus != Mouse.CurrentMouseStatus.Alive)
{
mice[0].CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
numberOfCheese -= 1; // lower cheese amount by 1
// no foregroundbatch.draw to remove from screen
mice[0].XPos = 680;
// restart level 1
}
foregroundBatch.End();
}
else
{
foregroundBatch.Begin();
foregroundBatch.Draw(level1Texture, defaultPosition, Color.White);
foregroundBatch.End();
}
}
else
{
level = 2;
mice[1].ShowThisMouse = true;
mice[2].ShowThisMouse = true;
lastSpawnTime = currentGameTime;
}
}
#endregion
#region level 2
if (level == 2)
{
double currentGameTime = gameTime.TotalGameTime.TotalSeconds;
#region mice[1]
if (mice[1].CurMouseStatus != Mouse.CurrentMouseStatus.Dead)
{
if ((currentGameTime - lastSpawnTime) > waitThreeSeconds)
{
foregroundBatch.Begin();
if (mice[1].CurMouseStatus == Mouse.CurrentMouseStatus.Alive)
{
foregroundBatch.Draw(mouse2Texture, mice[1].mousePosRectangle, Color.White);
}
else if (mice[1].CurMouseStatus != Mouse.CurrentMouseStatus.Alive)
{
mice[1].CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
numberOfCheese -= 1;
mice[1].XPos = 680;
}
foregroundBatch.End();
}
else
{
foregroundBatch.Begin();
foregroundBatch.Draw(level2Texture, defaultPosition, Color.White);
foregroundBatch.End();
}
}
#endregion
#region mice[2]
if (mice[2].CurMouseStatus != Mouse.CurrentMouseStatus.Dead)
{
if ((currentGameTime - lastSpawnTime) > waitThreeSeconds)
{
foregroundBatch.Begin();
if (mice[2].CurMouseStatus == Mouse.CurrentMouseStatus.Alive)
{
foregroundBatch.Draw(mouse3Texture, mice[2].mousePosRectangle, Color.White);
}
else if (mice[2].CurMouseStatus != Mouse.CurrentMouseStatus.Alive)
{
mice[2].CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
numberOfCheese -= 1;
mice[2].XPos = 680;
}
foregroundBatch.End();
}
else
{
foregroundBatch.Begin();
foregroundBatch.Draw(level2Texture, defaultPosition, Color.White);
foregroundBatch.End();
}
}
#endregion
else if((mice[1].CurMouseStatus == Mouse.CurrentMouseStatus.Dead) && (mice[2].CurMouseStatus == Mouse.CurrentMouseStatus.Dead))
{
level = 3;
mice[3].ShowThisMouse = true;
mice[4].ShowThisMouse = true;
mice[5].ShowThisMouse = true;
lastSpawnTime = currentGameTime;
}
}
#endregion
#region level 3
if (level == 3)
{
double currentGameTime = gameTime.TotalGameTime.TotalSeconds;
#region mice[3]
if (mice[3].CurMouseStatus != Mouse.CurrentMouseStatus.Dead)
{
if ((currentGameTime - lastSpawnTime) > waitThreeSeconds)
{
foregroundBatch.Begin();
if (mice[3].CurMouseStatus == Mouse.CurrentMouseStatus.Alive)
{
foregroundBatch.Draw(mouse4Texture, mice[3].mousePosRectangle, Color.White);
}
else if (mice[3].CurMouseStatus != Mouse.CurrentMouseStatus.Alive)
{
mice[3].CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
numberOfCheese -= 1;
mice[3].XPos = 680;
}
foregroundBatch.End();
}
else
{
foregroundBatch.Begin();
foregroundBatch.Draw(level3Texture, defaultPosition, Color.White);
foregroundBatch.End();
}
}
#endregion
#region mice[4]
if (mice[4].CurMouseStatus != Mouse.CurrentMouseStatus.Dead)
{
if ((currentGameTime - lastSpawnTime) > waitThreeSeconds)
{
foregroundBatch.Begin();
if (mice[4].CurMouseStatus == Mouse.CurrentMouseStatus.Alive)
{
foregroundBatch.Draw(mouse5Texture, mice[4].mousePosRectangle, Color.White);
}
else if (mice[4].CurMouseStatus != Mouse.CurrentMouseStatus.Alive)
{
mice[4].CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
numberOfCheese -= 1;
mice[4].XPos = 680;
}
foregroundBatch.End();
}
else
{
foregroundBatch.Begin();
foregroundBatch.Draw(level3Texture, defaultPosition, Color.White);
foregroundBatch.End();
}
}
#endregion
#region mice[5]
if (mice[5].CurMouseStatus != Mouse.CurrentMouseStatus.Dead)
{
if ((currentGameTime - lastSpawnTime) > waitThreeSeconds)
{
foregroundBatch.Begin();
if (mice[5].CurMouseStatus == Mouse.CurrentMouseStatus.Alive)
{
foregroundBatch.Draw(mouse6Texture, mice[5].mousePosRectangle, Color.White);
}
else if (mice[5].CurMouseStatus != Mouse.CurrentMouseStatus.Alive)
{
mice[5].CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
numberOfCheese -= 1;
mice[5].XPos = 680;
}
foregroundBatch.End();
}
else
{
foregroundBatch.Begin();
foregroundBatch.Draw(level3Texture, defaultPosition, Color.White);
foregroundBatch.End();
}
}
#endregion
else if ((mice[3].CurMouseStatus == Mouse.CurrentMouseStatus.Dead) && (mice[4].CurMouseStatus == Mouse.CurrentMouseStatus.Dead) && (mice[5].CurMouseStatus == Mouse.CurrentMouseStatus.Dead))
{
level = 4;
mice[6].ShowThisMouse = true;
mice[7].ShowThisMouse = true;
mice[8].ShowThisMouse = true;
mice[9].ShowThisMouse = true;
lastSpawnTime = currentGameTime;
}
}
#endregion
#region level 4
if (level == 4)
{
double currentGameTime = gameTime.TotalGameTime.TotalSeconds;
#region mice[6]
if (mice[6].CurMouseStatus != Mouse.CurrentMouseStatus.Dead)
{
if ((currentGameTime - lastSpawnTime) > waitThreeSeconds)
{
foregroundBatch.Begin();
if (mice[6].CurMouseStatus == Mouse.CurrentMouseStatus.Alive)
{
foregroundBatch.Draw(mouse7Texture, mice[6].mousePosRectangle, Color.White);
}
else if (mice[6].CurMouseStatus != Mouse.CurrentMouseStatus.Alive)
{
mice[6].CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
numberOfCheese -= 1;
mice[6].XPos = 680;
}
foregroundBatch.End();
}
else
{
foregroundBatch.Begin();
foregroundBatch.Draw(level4Texture, defaultPosition, Color.White);
foregroundBatch.End();
}
}
#endregion
#region mice[7]
if (mice[7].CurMouseStatus != Mouse.CurrentMouseStatus.Dead)
{
if ((currentGameTime - lastSpawnTime) > waitThreeSeconds)
{
foregroundBatch.Begin();
if (mice[7].CurMouseStatus == Mouse.CurrentMouseStatus.Alive)
{
foregroundBatch.Draw(mouse8Texture, mice[7].mousePosRectangle, Color.White);
}
else if (mice[7].CurMouseStatus != Mouse.CurrentMouseStatus.Alive)
{
mice[7].CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
numberOfCheese -= 1;
mice[7].XPos = 680;
}
foregroundBatch.End();
}
else
{
foregroundBatch.Begin();
foregroundBatch.Draw(level4Texture, defaultPosition, Color.White);
foregroundBatch.End();
}
}
#endregion
#region mice[8]
if (mice[8].CurMouseStatus != Mouse.CurrentMouseStatus.Dead)
{
if ((currentGameTime - lastSpawnTime) > waitThreeSeconds)
{
foregroundBatch.Begin();
if (mice[8].CurMouseStatus == Mouse.CurrentMouseStatus.Alive)
{
foregroundBatch.Draw(mouse9Texture, mice[8].mousePosRectangle, Color.White);
}
else if (mice[8].CurMouseStatus != Mouse.CurrentMouseStatus.Alive)
{
mice[8].CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
numberOfCheese -= 1;
mice[8].XPos = 680;
}
foregroundBatch.End();
}
else
{
foregroundBatch.Begin();
foregroundBatch.Draw(level4Texture, defaultPosition, Color.White);
foregroundBatch.End();
}
}
#endregion
#region mice[9]
if (mice[9].CurMouseStatus != Mouse.CurrentMouseStatus.Dead)
{
if ((currentGameTime - lastSpawnTime) > waitThreeSeconds)
{
foregroundBatch.Begin();
if (mice[9].CurMouseStatus == Mouse.CurrentMouseStatus.Alive)
{
foregroundBatch.Draw(mouse10Texture, mice[9].mousePosRectangle, Color.White);
}
else if (mice[9].CurMouseStatus != Mouse.CurrentMouseStatus.Alive)
{
mice[9].CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
numberOfCheese -= 1;
mice[9].XPos = 680;
}
foregroundBatch.End();
}
else
{
foregroundBatch.Begin();
foregroundBatch.Draw(level4Texture, defaultPosition, Color.White);
foregroundBatch.End();
}
}
#endregion
else if ((mice[6].CurMouseStatus == Mouse.CurrentMouseStatus.Dead) && (mice[7].CurMouseStatus == Mouse.CurrentMouseStatus.Dead) && (mice[8].CurMouseStatus == Mouse.CurrentMouseStatus.Dead) && (mice[9].CurMouseStatus == Mouse.CurrentMouseStatus.Dead))
{
level = 5;
mice[10].ShowThisMouse = true;
mice[11].ShowThisMouse = true;
mice[12].ShowThisMouse = true;
mice[13].ShowThisMouse = true;
mice[14].ShowThisMouse = true;
lastSpawnTime = currentGameTime;
}
}
#endregion
#region level 5
if (level == 5)
{
double currentGameTime = gameTime.TotalGameTime.TotalSeconds;
#region mice[10]
if (mice[10].CurMouseStatus != Mouse.CurrentMouseStatus.Dead)
{
if ((currentGameTime - lastSpawnTime) > waitThreeSeconds)
{
foregroundBatch.Begin();
if (mice[10].CurMouseStatus == Mouse.CurrentMouseStatus.Alive)
{
foregroundBatch.Draw(mouse11Texture, mice[10].mousePosRectangle, Color.White);
}
else if (mice[10].CurMouseStatus != Mouse.CurrentMouseStatus.Alive)
{
mice[10].CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
numberOfCheese -= 1;
mice[10].XPos = 680;
}
foregroundBatch.End();
}
else
{
foregroundBatch.Begin();
foregroundBatch.Draw(level5Texture, defaultPosition, Color.White);
foregroundBatch.End();
}
}
#endregion
#region mice[11]
if (mice[11].CurMouseStatus != Mouse.CurrentMouseStatus.Dead)
{
if ((currentGameTime - lastSpawnTime) > waitThreeSeconds)
{
foregroundBatch.Begin();
if (mice[11].CurMouseStatus == Mouse.CurrentMouseStatus.Alive)
{
foregroundBatch.Draw(mouse12Texture, mice[11].mousePosRectangle, Color.White);
}
else if (mice[11].CurMouseStatus != Mouse.CurrentMouseStatus.Alive)
{
mice[11].CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
numberOfCheese -= 1;
mice[11].XPos = 680;
}
foregroundBatch.End();
}
else
{
foregroundBatch.Begin();
foregroundBatch.Draw(level5Texture, defaultPosition, Color.White);
foregroundBatch.End();
}
}
#endregion
#region mice[12]
if (mice[12].CurMouseStatus != Mouse.CurrentMouseStatus.Dead)
{
if ((currentGameTime - lastSpawnTime) > waitThreeSeconds)
{
foregroundBatch.Begin();
if (mice[12].CurMouseStatus == Mouse.CurrentMouseStatus.Alive)
{
foregroundBatch.Draw(mouse13Texture, mice[12].mousePosRectangle, Color.White);
}
else if (mice[12].CurMouseStatus != Mouse.CurrentMouseStatus.Alive)
{
mice[12].CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
numberOfCheese -= 1;
mice[12].XPos = 680;
}
foregroundBatch.End();
}
else
{
foregroundBatch.Begin();
foregroundBatch.Draw(level5Texture, defaultPosition, Color.White);
foregroundBatch.End();
}
}
#endregion
#region mice[13]
if (mice[13].CurMouseStatus != Mouse.CurrentMouseStatus.Dead)
{
if ((currentGameTime - lastSpawnTime) > waitThreeSeconds)
{
foregroundBatch.Begin();
if (mice[13].CurMouseStatus == Mouse.CurrentMouseStatus.Alive)
{
foregroundBatch.Draw(mouse14Texture, mice[13].mousePosRectangle, Color.White);
}
else if (mice[13].CurMouseStatus != Mouse.CurrentMouseStatus.Alive)
{
mice[13].CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
numberOfCheese -= 1;
mice[13].XPos = 680;
}
foregroundBatch.End();
}
else
{
foregroundBatch.Begin();
foregroundBatch.Draw(level5Texture, defaultPosition, Color.White);
foregroundBatch.End();
}
}
#endregion
#region mice[14]
if (mice[14].CurMouseStatus != Mouse.CurrentMouseStatus.Dead)
{
if ((currentGameTime - lastSpawnTime) > waitThreeSeconds)
{
foregroundBatch.Begin();
if (mice[14].CurMouseStatus == Mouse.CurrentMouseStatus.Alive)
{
foregroundBatch.Draw(mouse15Texture, mice[14].mousePosRectangle, Color.White);
}
else if (mice[14].CurMouseStatus != Mouse.CurrentMouseStatus.Alive)
{
mice[14].CurMouseStatus = Mouse.CurrentMouseStatus.Alive;
numberOfCheese -= 1;
mice[14].XPos = 680;
}
foregroundBatch.End();
}
else
{
foregroundBatch.Begin();
foregroundBatch.Draw(level5Texture, defaultPosition, Color.White);
foregroundBatch.End();
}
}
#endregion
else if ((mice[10].CurMouseStatus == Mouse.CurrentMouseStatus.Dead) && (mice[11].CurMouseStatus == Mouse.CurrentMouseStatus.Dead) && (mice[12].CurMouseStatus == Mouse.CurrentMouseStatus.Dead) && (mice[13].CurMouseStatus == Mouse.CurrentMouseStatus.Dead) && (mice[14].CurMouseStatus == Mouse.CurrentMouseStatus.Dead))
{
level = 6;
}
}
#endregion
#region level 6 (you won)
if (level == 6)
{
gameMode = GameMode.Win;
}
#endregion
#endregion
#region keep fan in the screen
// Prevent fan from going out of the screen
if (verticalFanPosition < 0)
{
verticalFanPosition = 0;
}
else if (verticalFanPosition >= (786 - fanTexture.Height))
{
verticalFanPosition = (786 - fanTexture.Height);
}
#endregion
#region draw wind
if (drawWind == true)
{
// determine current fanpos and add 50 to place the windsprite
windPos = fanRectangle;
windPos.X = windPos.X + 50;
windPos.Width = windTexture.Width;
windPos.Height = windTexture.Height;
foregroundBatch.Begin();
foregroundBatch.Draw(windTexture, windPos, Color.White);
foregroundBatch.End();
}
#endregion
#region draw lives
Rectangle livesRectangle = new Rectangle(900, 80, (int)livesTexture.Width, (int)livesTexture.Height);
for (int i = 0; i < numberOfCheese; i++)
{
//create rectangle
foregroundBatch.Begin();
foregroundBatch.Draw(livesTexture, livesRectangle, Color.White);
livesRectangle.Y += 100;
foregroundBatch.End();
}
#endregion
}
break;
#endregion
#region gameMode == GameMode.GameOver
case GameMode.GameOver:
{
foregroundBatch.Begin();
foregroundBatch.Draw(gameoverTexture, defaultPosition, Color.White);
foregroundBatch.End();
}
break;
#endregion
#region gameMode == GameMode.Win
case GameMode.Win:
{
foregroundBatch.Begin();
foregroundBatch.Draw(winTexture, defaultPosition, Color.White);
foregroundBatch.End();
}
break;
#endregion
}
base.Draw(gameTime);
}
protected int GenerateRandomMouseHeight()
{
int mouseHeight = randomMousePosition.Next(1, 740);
return mouseHeight;
}
}
}