List floatingValueList;
SpriteFont floatingValueFont;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = “Content”;
Window.Title = "Grab It ";
graphics.PreferredBackBufferWidth = 850;
graphics.PreferredBackBufferHeight = 600;
}
/// <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()
{
// TODO: Add your initialization logic here
ScreenBounds = GraphicsDevice.Viewport.Bounds;
playerClickTime = TimeSpan.FromSeconds(2.6);
playerPreviousClickTime = TimeSpan.Zero;
foodList = new List<Food>();
spawnFoodTime = TimeSpan.FromSeconds(2.0f);
previousSpawnFoodTime = TimeSpan.Zero;
// Initialize the random number generator.
random = new Random();
currentGameState = GameStates.TitleScreen;
highScore = 0;
floatingValueList = new List<FloatingValue>();
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
background = Content.Load<Texture2D>("Backgrounds/background");
// Texture2D playerTxr = Content.Load<Texture2D>("player");
player = new Player(Content.Load<Texture2D>("Sprites/player"));
Song song = Content.Load<Song>("Sounds/Enjoy_the_silence"); // Put the name of your song in instead of "song_title"
MediaPlayer.Play(song);
MediaPlayer.IsRepeating = true;
soundEffect = Content.Load<SoundEffect>("Sounds/bucket_plastic");
soundEffect = Content.Load<SoundEffect>("Sounds/impact_rock");
soundEffect.Play () ;
MediaPlayer.IsRepeating = true;
foodTexures = new Texture2D[16];
foodTexures[0] = Content.Load<Texture2D>("Sprites/alb1");
foodTexures[1] = Content.Load<Texture2D>("Sprites/appler");
foodTexures[2] = Content.Load<Texture2D>("Sprites/grapes");
foodTexures[3] = Content.Load<Texture2D>("Sprites/tomato");
foodTexures[4] = Content.Load<Texture2D>("Sprites/apricot");
foodTexures[5] = Content.Load<Texture2D>("Sprites/pera");
foodTexures[6] = Content.Load<Texture2D>("Sprites/strawberry1");
foodTexures[7] = Content.Load<Texture2D>("Sprites/prunes");
foodTexures[8] = Content.Load<Texture2D>("Sprites/Kiwi1");
foodTexures[9] = Content.Load<Texture2D>("Sprites/cucumber1");
foodTexures[10] = Content.Load<Texture2D>("Sprites/pineapple1");
foodTexures[11] = Content.Load<Texture2D>("Sprites/spinach");
foodTexures[12] = Content.Load<Texture2D>("Sprites/popcorn");
foodTexures[13] = Content.Load<Texture2D>("Sprites/Cola");
foodTexures[14] = Content.Load<Texture2D>("Sprites/Sweets");
foodTexures[15] = Content.Load<Texture2D>("Sprites/chips");
scoreFont = Content.Load<SpriteFont>("Fonts/ScoreFont");
// Screens
gameOverScreen = Content.Load<Texture2D>("Screens/GameOverScreen");
instructionsScreen = Content.Load<Texture2D>("Screens/InstructionsScreen");
playingScreen = Content.Load<Texture2D>("Screens/PlayingScreen");
quitScreen = Content.Load<Texture2D>("Screens/QuitScreen");
titleScreen = Content.Load<Texture2D>("Screens/TitleScreen");
// health bar
healthFont = Content.Load<SpriteFont>("Fonts/HealthFont");
// Health Bar - Demo 1
healthBarTxr = Content.Load<Texture2D>("Sprites/health-bar");
healthBarRec = new Rectangle(300, 10, healthBarWidth, 32);
healthBarPos = new Vector2(healthBarRec.X + 10, healthBarRec.Y);
floatingValueFont = Content.Load<SpriteFont>("Fonts/HealthFont");
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <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)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
{
Exit();
}
currentKeyboardState = Keyboard.GetState();
switch (currentGameState)
{
case GameStates.GameOver:
UpdateGameOver();
break;
case GameStates.Instructions:
UpdateInstructions();
break;
case GameStates.Playing:
UpdatePlaying(gameTime);
break;
case GameStates.Quit:
UpdateQuit();
break;
case GameStates.TitleScreen:
UpdateTitle();
break;
}
base.Update(gameTime);
}
private void UpdateGameOver()
{
if (currentKeyboardState.IsKeyDown(Keys.Q))
{
QuitGame(); // Exit();
}
if (currentKeyboardState.IsKeyDown(Keys.Space))
{
// Remove any existing food from list
foodList.Clear();
StartGame();
}
}
private void UpdateInstructions()
{
if (currentKeyboardState.IsKeyDown(Keys.Q))
{
QuitGame(); // Exit();
}
if (currentKeyboardState.IsKeyDown(Keys.Space))
{
currentGameState = GameStates.Playing;
}
}
private void UpdateQuit()
{
if (currentKeyboardState.IsKeyDown(Keys.Y))
{
QuitGame();
}
if (currentKeyboardState.IsKeyDown(Keys.N))
{
currentGameState = GameStates.Playing;
}
}
private void UpdatePlaying(GameTime gameTime)
{
UpdatePlayer(gameTime);
UpdateFood(gameTime);
UpdateFloatingValues(gameTime);
healthBarRec.Width = healthBarWidth * health / 100;
if (currentKeyboardState.IsKeyDown(Keys.Q))
{
// we should probably add some code to confirm decision
QuitGame();
}
}
private void UpdateFloatingValues(GameTime gameTime)
{
foreach (FloatingValue fv in floatingValueList)
{
fv.Update(gameTime);
}
// Remove all inactive floating values from list
floatingValueList.RemoveAll(fv => fv.IsActive == false);
}
private void UpdateFood(GameTime gameTime)
{
if (gameTime.TotalGameTime - previousSpawnFoodTime > spawnFoodTime)
{
previousSpawnFoodTime = gameTime.TotalGameTime;
AddFood();
}
// update each food in our list
foodList.ForEach(f => f.Update(gameTime));
// check if player missed a food
// if so, then they lose some healthy
foreach (Food f in foodList)
{
if (f.Escaped == true)
{
health -= f.Value;
}
}
// Remove all inactive food from list
foodList.RemoveAll(f => f.IsActive == false);
// game over check
if (health <= 0)
{
currentGameState = GameStates.GameOver;
}
}
private void AddFood()
{
int rnd = random.Next(1, foodTexures.Length);
int value = 0;
float speed = 0;
// pick a texture for our food
foodTexture = foodTexures[rnd];
if(rnd<13)
{
value = 2;
}
else
{
value=-2;
}
// get start position of the food
Vector2 position = new Vector2(random.Next(0, ScreenBounds.Width - foodTexture.Width), -foodTexture.Height);
// create a new food
Food food = new Food(foodTexture, position, 4, value);
// add food to the food list
foodList.Add(food);
}
private void UpdatePlayer(GameTime gameTime)
{
player.Update(gameTime);
if (gameTime.TotalGameTime - playerPreviousClickTime > playerClickTime)
{
player.IsActive = true;
if (player.MouseState.LeftButton == ButtonState.Pressed)
{
// health-=5; // loss of health for clicking
playerPreviousClickTime = gameTime.TotalGameTime;
// check is player has clicked on a food
foreach (Food f in foodList)
{
if (f.BoundingBox.Contains(player.MouseState.X, player.MouseState.Y))
{
score += f.Value; // add food value to player score
f.IsActive = false; // flag food to be removed from list
// health+=5;
AddFloatingValue(f.ValuePosition, f.Value);
}
}
player.IsActive = false;
}
}
}
private void AddFloatingValue(Vector2 pos, int value)
{
FloatingValue fv = new FloatingValue(pos, value);
// add food to the food list
floatingValueList.Add(fv);
}
/// <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.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin();
switch (currentGameState)
{
case GameStates.GameOver:
DrawGameOver();
break;
case GameStates.Instructions:
DrawInstructions();
break;
case GameStates.Playing:
DrawPlaying();
break;
case GameStates.Quit:
DrawQuit();
break;
case GameStates.TitleScreen:
DrawTitle();
break;
}
spriteBatch.End();
base.Draw(gameTime);
}
private void DrawGameOver()
{
// Draw the game over screen
spriteBatch.Draw(gameOverScreen, Vector2.Zero, Color.White);
}
private void DrawInstructions()
{
// Draw the instructions screen
spriteBatch.Draw(instructionsScreen, Vector2.Zero, Color.White);
}
private void DrawQuit()
{
// Draw the quit screen
spriteBatch.Draw(quitScreen, Vector2.Zero, Color.White);
}
private void UpdateTitle()
{
if (currentKeyboardState.IsKeyDown(Keys.Space))
{
StartGame();
}
if (currentKeyboardState.IsKeyDown(Keys.Q))
{
QuitGame();
}
if (currentKeyboardState.IsKeyDown(Keys.I))
{
currentGameState = GameStates.Instructions;
}
}
private void QuitGame()
{
Exit();
}
private void StartGame()
{
currentGameState = GameStates.Playing;
score = 0;
health = MAX_HEALTH;
}
private void DrawTitle()
{
// Draw the title screen
spriteBatch.Draw(titleScreen, Vector2.Zero, Color.White);
}
private void DrawPlaying()
{
// Draw the background
spriteBatch.Draw(background, Vector2.Zero, Color.White);
// Draw each food in the list
foodList.ForEach(f => f.Draw(spriteBatch));
// draw player
player.Draw(spriteBatch);
// HUD STUFF
DrawHUD();
}
private void DrawHUD()
{
DrawScore();
DrawHealthBar();
DrawFloatingValues();
}
private void DrawFloatingValues()
{
// draw each floating value in our list
foreach (FloatingValue fv in floatingValueList)
{
spriteBatch.DrawString(floatingValueFont, fv.ValueText, fv.Position, Color.White * fv.Opacity);
}
}
private void DrawHealthBar()
{
// Health Bar - Example 1
spriteBatch.Draw(healthBarTxr, healthBarRec, Color.Red);
spriteBatch.DrawString(healthFont, health.ToString() + "%", healthBarPos, Color.White);
}
private void DrawScore()
{
spriteBatch.DrawString(scoreFont, "Score: " + score, Vector2.One, Color.Yellow);
}
}
internal class Sprite
{
private Texture2D texture2D;
public Sprite(Texture2D texture2D)
{
this.texture2D = texture2D;
}
}
}