Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using TeddyMineExplosion;
- namespace ProgrammingAssignment5
- {
- /// <summary>
- /// This is the main type for your game.
- /// </summary>
- public class Game1 : Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- const int windowWidth = 800;
- const int windowHeight = 600;
- const double lowN = -0.5;
- const double highN = 0.5;
- // game world objects and characters
- Texture2D teddySprite;
- List<TeddyBear> bearList = new List<TeddyBear>();
- Texture2D mineSprite;
- List<Mine> mineList = new List<Mine>();
- Texture2D explosionSprite;
- List<Explosion> explosionList = new List<Explosion>();
- // spawning support
- Random rand = new Random();
- int elapsedSpawnDelayMilliseconds = 0;
- int totalSpawnDelayMilliseconds = 1;
- // mouse matters
- bool leftClickStarted = false;
- bool leftButtonReleased = true;
- public Game1()
- {
- if (elapsedSpawnDelayMilliseconds==0)
- {
- int number = rand.Next(1, 4);
- totalSpawnDelayMilliseconds = number * 1000;
- }
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- graphics.PreferredBackBufferWidth = windowWidth;
- graphics.PreferredBackBufferHeight = windowHeight;
- IsMouseVisible = true;
- }
- /// <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
- 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
- mineSprite = Content.Load<Texture2D>(@"graphics/mine");
- teddySprite = Content.Load<Texture2D>(@"graphics/teddybear");
- explosionSprite = Content.Load<Texture2D>(@"graphics/explosion");
- }
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// game-specific 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)
- {
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
- Exit();
- // TODO: Add your update logic here
- elapsedSpawnDelayMilliseconds += gameTime.ElapsedGameTime.Milliseconds;
- MouseState mouse = Mouse.GetState();
- //check mouse click status
- if (mouse.LeftButton == ButtonState.Pressed && leftButtonReleased)
- {
- leftClickStarted = true;
- leftButtonReleased = false;
- }
- else if(mouse.LeftButton==ButtonState.Released)
- {
- leftButtonReleased = true;
- if (leftClickStarted)
- {
- leftClickStarted = false;
- mineList.Add(new Mine(mineSprite, mouse.X, mouse.Y));
- }
- }
- // do the bear things
- if (elapsedSpawnDelayMilliseconds>=totalSpawnDelayMilliseconds)
- {
- Vector2 velocity = new Vector2(rand.Next(-5, 5) * 0.1f, rand.Next(-5, 5) * 0.1f);
- TeddyBear teddy = new TeddyBear(teddySprite, velocity, windowWidth, windowHeight);
- bearList.Add(teddy);
- elapsedSpawnDelayMilliseconds = 0;
- int number = rand.Next(1, 4);
- totalSpawnDelayMilliseconds = number * 1000;
- }
- foreach (TeddyBear bear in bearList)
- {
- bear.Update(gameTime);
- }
- foreach(TeddyBear bear in bearList)
- {
- foreach (Mine mine in mineList)
- {
- if (bear.Active && mine.Active &&
- bear.CollisionRectangle.Intersects(mine.CollisionRectangle))
- {
- bear.Active = false;
- mine.Active = false;
- explosionList.Add(new Explosion(explosionSprite,
- mine.CollisionRectangle.Center.X, mine.CollisionRectangle.Center.Y));
- }
- }
- }
- foreach (Explosion explosion in explosionList)
- {
- explosion.Update(gameTime);
- }
- for (int i = bearList.Count-1; i>=0; i--)
- {
- if (!bearList[i].Active)
- {
- bearList.RemoveAt(i);
- }
- }
- for (int i = mineList.Count - 1; i >= 0; i--)
- {
- if (!mineList[i].Active)
- {
- mineList.RemoveAt(i);
- }
- }
- for (int i = explosionList.Count - 1; i >= 0; i--)
- {
- if (!explosionList[i].Playing)
- {
- explosionList.RemoveAt(i);
- }
- }
- 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)
- {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- // TODO: Add your drawing code here
- spriteBatch.Begin();
- // DRAW MINES, TEDDIES and EXPLOSIONS to screen
- foreach(Mine mine in mineList)
- {
- mine.Draw(spriteBatch);
- }
- foreach(TeddyBear bear in bearList)
- {
- bear.Draw(spriteBatch);
- }
- foreach (Explosion explosion in explosionList)
- {
- explosion.Draw(spriteBatch);
- }
- spriteBatch.End();
- base.Draw(gameTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement