Advertisement
diliupg

Programming Assignment5-Game1.cs

Sep 27th, 2017
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Graphics;
  6. using Microsoft.Xna.Framework.Input;
  7.  
  8. using TeddyMineExplosion;
  9.  
  10. namespace ProgrammingAssignment5
  11. {
  12.     /// <summary>
  13.     /// This is the main type for your game.
  14.     /// </summary>
  15.     public class Game1 : Game
  16.     {
  17.         GraphicsDeviceManager graphics;
  18.         SpriteBatch spriteBatch;
  19.  
  20.         const int windowWidth = 800;
  21.         const int windowHeight = 600;
  22.  
  23.         const double lowN = -0.5;
  24.         const double highN = 0.5;
  25.  
  26.         // game world objects and characters
  27.         Texture2D teddySprite;
  28.         List<TeddyBear> bearList = new List<TeddyBear>();
  29.  
  30.         Texture2D mineSprite;
  31.         List<Mine> mineList = new List<Mine>();
  32.  
  33.         Texture2D explosionSprite;
  34.         List<Explosion> explosionList = new List<Explosion>();
  35.  
  36.         // spawning support
  37.         Random rand = new Random();
  38.  
  39.         int elapsedSpawnDelayMilliseconds = 0;
  40.         int totalSpawnDelayMilliseconds = 1;
  41.  
  42.         // mouse matters
  43.         bool leftClickStarted = false;
  44.         bool leftButtonReleased = true;
  45.  
  46.         public Game1()
  47.         {
  48.             if (elapsedSpawnDelayMilliseconds==0)
  49.             {
  50.                 int number = rand.Next(1, 4);
  51.                 totalSpawnDelayMilliseconds = number * 1000;
  52.             }
  53.  
  54.             graphics = new GraphicsDeviceManager(this);
  55.             Content.RootDirectory = "Content";
  56.  
  57.             graphics.PreferredBackBufferWidth = windowWidth;
  58.             graphics.PreferredBackBufferHeight = windowHeight;
  59.  
  60.             IsMouseVisible = true;
  61.         }
  62.  
  63.         /// <summary>
  64.         /// Allows the game to perform any initialization it needs to before starting to run.
  65.         /// This is where it can query for any required services and load any non-graphic
  66.         /// related content.  Calling base.Initialize will enumerate through any components
  67.         /// and initialize them as well.
  68.         /// </summary>
  69.         protected override void Initialize()
  70.         {
  71.             // TODO: Add your initialization logic here
  72.  
  73.             base.Initialize();
  74.         }
  75.  
  76.         /// <summary>
  77.         /// LoadContent will be called once per game and is the place to load
  78.         /// all of your content.
  79.         /// </summary>
  80.         protected override void LoadContent()
  81.         {
  82.             // Create a new SpriteBatch, which can be used to draw textures.
  83.             spriteBatch = new SpriteBatch(GraphicsDevice);
  84.  
  85.             // TODO: use this.Content to load your game content here
  86.             mineSprite = Content.Load<Texture2D>(@"graphics/mine");
  87.             teddySprite = Content.Load<Texture2D>(@"graphics/teddybear");
  88.             explosionSprite = Content.Load<Texture2D>(@"graphics/explosion");
  89.         }
  90.  
  91.         /// <summary>
  92.         /// UnloadContent will be called once per game and is the place to unload
  93.         /// game-specific content.
  94.         /// </summary>
  95.         protected override void UnloadContent()
  96.         {
  97.             // TODO: Unload any non ContentManager content here
  98.         }
  99.  
  100.         /// <summary>
  101.         /// Allows the game to run logic such as updating the world,
  102.         /// checking for collisions, gathering input, and playing audio.
  103.         /// </summary>
  104.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  105.         protected override void Update(GameTime gameTime)
  106.         {
  107.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
  108.                 Exit();
  109.  
  110.             // TODO: Add your update logic here
  111.             elapsedSpawnDelayMilliseconds += gameTime.ElapsedGameTime.Milliseconds;
  112.             MouseState mouse = Mouse.GetState();
  113.  
  114.             //check mouse click status
  115.             if (mouse.LeftButton == ButtonState.Pressed && leftButtonReleased)
  116.             {
  117.                 leftClickStarted = true;
  118.                 leftButtonReleased = false;
  119.             }
  120.             else if(mouse.LeftButton==ButtonState.Released)
  121.             {
  122.                 leftButtonReleased = true;
  123.  
  124.                 if (leftClickStarted)
  125.                 {
  126.                     leftClickStarted = false;
  127.                     mineList.Add(new Mine(mineSprite, mouse.X, mouse.Y));
  128.                 }
  129.             }
  130.             // do the bear things
  131.             if (elapsedSpawnDelayMilliseconds>=totalSpawnDelayMilliseconds)
  132.             {
  133.                 Vector2 velocity = new Vector2(rand.Next(-5, 5) * 0.1f, rand.Next(-5, 5) * 0.1f);
  134.                 TeddyBear teddy = new TeddyBear(teddySprite, velocity, windowWidth, windowHeight);
  135.  
  136.                 bearList.Add(teddy);
  137.  
  138.                 elapsedSpawnDelayMilliseconds = 0;
  139.                 int number = rand.Next(1, 4);
  140.                 totalSpawnDelayMilliseconds = number * 1000;
  141.             }
  142.             foreach (TeddyBear bear in bearList)
  143.             {
  144.                 bear.Update(gameTime);
  145.             }
  146.             foreach(TeddyBear bear in bearList)
  147.             {
  148.                 foreach (Mine mine in mineList)
  149.                 {
  150.                     if (bear.Active && mine.Active &&
  151.                         bear.CollisionRectangle.Intersects(mine.CollisionRectangle))
  152.                     {
  153.                         bear.Active = false;
  154.                         mine.Active = false;
  155.                         explosionList.Add(new Explosion(explosionSprite,
  156.                             mine.CollisionRectangle.Center.X, mine.CollisionRectangle.Center.Y));
  157.                     }
  158.                 }
  159.             }
  160.             foreach (Explosion explosion in explosionList)
  161.             {
  162.                 explosion.Update(gameTime);
  163.             }
  164.             for (int i = bearList.Count-1; i>=0; i--)
  165.             {
  166.                 if (!bearList[i].Active)
  167.                 {
  168.                     bearList.RemoveAt(i);
  169.                 }
  170.             }
  171.             for (int i = mineList.Count - 1; i >= 0; i--)
  172.             {
  173.                 if (!mineList[i].Active)
  174.                 {
  175.                     mineList.RemoveAt(i);
  176.                 }
  177.             }
  178.             for (int i = explosionList.Count - 1; i >= 0; i--)
  179.             {
  180.                 if (!explosionList[i].Playing)
  181.                 {
  182.                     explosionList.RemoveAt(i);
  183.                 }
  184.             }
  185.             base.Update(gameTime);
  186.         }
  187.  
  188.         /// <summary>
  189.         /// This is called when the game should draw itself.
  190.         /// </summary>
  191.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  192.         protected override void Draw(GameTime gameTime)
  193.         {
  194.             GraphicsDevice.Clear(Color.CornflowerBlue);
  195.  
  196.             // TODO: Add your drawing code here
  197.             spriteBatch.Begin();
  198.  
  199.             // DRAW MINES, TEDDIES and EXPLOSIONS to screen
  200.             foreach(Mine mine in mineList)
  201.             {
  202.                 mine.Draw(spriteBatch);
  203.             }
  204.             foreach(TeddyBear bear in bearList)
  205.             {
  206.                 bear.Draw(spriteBatch);
  207.             }
  208.             foreach (Explosion explosion in explosionList)
  209.             {
  210.                 explosion.Draw(spriteBatch);
  211.             }
  212.  
  213.             spriteBatch.End();
  214.  
  215.  
  216.             base.Draw(gameTime);
  217.         }
  218.  
  219.  
  220.     }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement