Advertisement
diliupg

PrjojectIncrement3

Sep 29th, 2017
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Graphics;
  7. using Microsoft.Xna.Framework.Input;
  8.  
  9. namespace GameProject
  10. {
  11.     /// <summary>
  12.     /// This is the main type for your game.
  13.     /// </summary>
  14.     public class Game1 : Game
  15.     {
  16.         GraphicsDeviceManager graphics;
  17.         SpriteBatch spriteBatch;
  18.  
  19.         // game objects. Using inheritance would make this easier,
  20.         //but inheritance isn't a GDD 1200 topic
  21.         Burger burger;
  22.         List<TeddyBear> bears = new List<TeddyBear>();
  23.         static List<Projectile> projectiles = new List<Projectile>();
  24.         List<Explosion> explosions = new List<Explosion>();
  25.  
  26.         // projectile and explosion sprites.
  27.         //Saved so they don't have to be loaded every time projectiles or explosions are created
  28.         static Texture2D frenchFriesSprite;
  29.         static Texture2D teddyBearProjectileSprite;
  30.         static Texture2D explosionSpriteStrip;
  31.  
  32.         // scoring support
  33.         int score = 0;
  34.         string scoreString = GameConstants.ScorePrefix + 0;
  35.  
  36.         // health support
  37.         string healthString = GameConstants.HealthPrefix +
  38.             GameConstants.BurgerInitialHealth;
  39.         bool burgerDead = false;
  40.  
  41.         // text display support
  42.         SpriteFont font;
  43.  
  44.         // sound effects
  45.         SoundEffect burgerDamage;
  46.         SoundEffect burgerDeath;
  47.         SoundEffect burgerShot;
  48.         SoundEffect explosion;
  49.         SoundEffect teddyBounce;
  50.         SoundEffect teddyShot;
  51.  
  52.         public Game1()
  53.         {
  54.             graphics = new GraphicsDeviceManager(this);
  55.             Content.RootDirectory = "Content";
  56.  
  57.             // set resolution
  58.             graphics.PreferredBackBufferWidth = GameConstants.WindowWidth;
  59.             graphics.PreferredBackBufferHeight = GameConstants.WindowHeight;
  60.         }
  61.  
  62.         /// <summary>
  63.         /// Allows the game to perform any initialization it needs to before starting to run.
  64.         /// This is where it can query for any required services and load any non-graphic
  65.         /// related content.  Calling base.Initialize will enumerate through any components
  66.         /// and initialize them as well.
  67.         /// </summary>
  68.         protected override void Initialize()
  69.         {
  70.             RandomNumberGenerator.Initialize();
  71.  
  72.             base.Initialize();
  73.         }
  74.  
  75.         /// <summary>
  76.         /// LoadContent will be called once per game and is the place to load
  77.         /// all of your content.
  78.         /// </summary>
  79.         protected override void LoadContent()
  80.         {
  81.             // Create a new SpriteBatch, which can be used to draw textures.
  82.             spriteBatch = new SpriteBatch(GraphicsDevice);
  83.  
  84.             // load audio content
  85.  
  86.             // load sprite font
  87.  
  88.             // load projectile and explosion sprites
  89.             teddyBearProjectileSprite = Content.Load<Texture2D>(@"graphics/teddybearprojectile");
  90.             frenchFriesSprite = Content.Load<Texture2D>(@"graphics/frenchfries");
  91.             explosionSpriteStrip = Content.Load<Texture2D>(@"graphics/explosion");
  92.  
  93.             // add initial game objects
  94.             burger = new Burger(Content, (@"graphics/burger"),
  95.                 graphics.PreferredBackBufferWidth / 2,
  96.                 graphics.PreferredBackBufferHeight - graphics.PreferredBackBufferHeight / 8,
  97.                 null);
  98.             for(int i = 0; i < GameConstants.MaxBears; i++)
  99.             {
  100.                 SpawnBear();
  101.             }
  102.  
  103.             // set initial health and score strings
  104.         }
  105.  
  106.         /// <summary>
  107.         /// UnloadContent will be called once per game and is the place to unload
  108.         /// game-specific content.
  109.         /// </summary>
  110.         protected override void UnloadContent()
  111.         {
  112.             // TODO: Unload any non ContentManager content here
  113.         }
  114.  
  115.         /// <summary>
  116.         /// Allows the game to run logic such as updating the world,
  117.         /// checking for collisions, gathering input, and playing audio.
  118.         /// </summary>
  119.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  120.         protected override void Update(GameTime gameTime)
  121.         {
  122.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
  123.                 Exit();
  124.  
  125.             // get current mouse state and update burger
  126.             MouseState mouse = Mouse.GetState();
  127.             burger.Update(gameTime, mouse);
  128.  
  129.             // update other game objects
  130.             foreach (TeddyBear bear in bears)
  131.             {
  132.                 bear.Update(gameTime);
  133.             }
  134.             foreach (Projectile projectile in projectiles)
  135.             {
  136.                 projectile.Update(gameTime);
  137.             }
  138.             foreach (Explosion explosion in explosions)
  139.             {
  140.                 explosion.Update(gameTime);
  141.             }
  142.  
  143.             // check and resolve collisions between teddy bears
  144.             for (int i = 0; i < bears.Count; i++)
  145.             {
  146.                 for (int j = i + 1; j < bears.Count; j++)
  147.                 {
  148.                     if (bears[i].Active && bears[j].Active &&
  149.                         bears[i].CollisionRectangle.Intersects(bears[j].CollisionRectangle))
  150.                     {
  151.                         CollisionResolutionInfo collisionresult = CollisionUtils.CheckCollision
  152.                             (gameTime.ElapsedGameTime.Milliseconds,
  153.                             GameConstants.WindowWidth, GameConstants.WindowHeight,
  154.                             bears[i].Velocity, bears[i].DrawRectangle,
  155.                             bears[j].Velocity, bears[j].DrawRectangle);
  156.  
  157.                         if(collisionresult != null)
  158.                         {
  159.                             if (collisionresult.FirstOutOfBounds == true)
  160.                             {
  161.                                 bears[i].Active = false;
  162.                             }
  163.                             else
  164.                             {
  165.                                 bears[i].Velocity = collisionresult.FirstVelocity;
  166.                                 bears[i].DrawRectangle = collisionresult.FirstDrawRectangle;
  167.                             }
  168.                             if (collisionresult.SecondOutOfBounds == true)
  169.                             {
  170.                                 bears[j].Active = false;
  171.                             }
  172.                             else
  173.                             {
  174.                                 bears[j].Velocity = collisionresult.SecondVelocity;
  175.                                 bears[j].DrawRectangle = collisionresult.SecondDrawRectangle;
  176.                             }
  177.  
  178.                         }
  179.                     }
  180.                 }
  181.             }
  182.             // check and resolve collisions between burger and teddy bears
  183.  
  184.             // check and resolve collisions between burger and projectiles
  185.  
  186.             // check and resolve collisions between teddy bears and projectiles
  187.             foreach (TeddyBear bear in bears)
  188.             {
  189.                 foreach (Projectile projectile in projectiles)
  190.                 {
  191.                     if (bear.Active && projectile.Active && projectile.Type == ProjectileType.FrenchFries &&
  192.                         bear.CollisionRectangle.Intersects(projectile.CollisionRectangle))
  193.                     {
  194.                         bear.Active = false;
  195.                         projectile.Active = false;
  196.                         explosions.Add(new Explosion(explosionSpriteStrip,
  197.                         bear.CollisionRectangle.Center.X, bear.CollisionRectangle.Center.Y));
  198.  
  199.                     }
  200.                 }
  201.             }
  202.             // clean out inactive teddy bears and add new ones as necessary
  203.             for (int i = bears.Count - 1; i >= 0; i--)
  204.             {
  205.                 if (!bears[i].Active)
  206.                 {
  207.                     bears.RemoveAt(i);
  208.                 }
  209.             }
  210.             // clean out inactive projectiles
  211.             for (int i = projectiles.Count - 1; i >= 0; i--)
  212.             {
  213.                 if (!projectiles[i].Active)
  214.                 {
  215.                     projectiles.RemoveAt(i);
  216.                 }
  217.             }
  218.             // clean out finished explosions
  219.             for (int i = explosions.Count - 1; i >= 0; i--)
  220.             {
  221.                 if (explosions[i].Finished)
  222.                 {
  223.                     explosions.RemoveAt(i);
  224.                 }
  225.             }
  226.  
  227.             base.Update(gameTime);
  228.         }
  229.  
  230.         /// <summary>
  231.         /// This is called when the game should draw itself.
  232.         /// </summary>
  233.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  234.         protected override void Draw(GameTime gameTime)
  235.         {
  236.             GraphicsDevice.Clear(Color.CornflowerBlue);
  237.  
  238.             spriteBatch.Begin();
  239.  
  240.             // draw game objects
  241.             burger.Draw(spriteBatch);
  242.             foreach (TeddyBear bear in bears)
  243.             {
  244.                 bear.Draw(spriteBatch);
  245.             }
  246.             foreach (Projectile projectile in projectiles)
  247.             {
  248.                 projectile.Draw(spriteBatch);
  249.             }
  250.             foreach (Explosion explosion in explosions)
  251.             {
  252.                 explosion.Draw(spriteBatch);
  253.             }
  254.  
  255.             // draw score and health
  256.  
  257.             spriteBatch.End();
  258.  
  259.             base.Draw(gameTime);
  260.         }
  261.  
  262.         #region Public methods
  263.  
  264.         /// <summary>
  265.         /// Gets the projectile sprite for the given projectile type
  266.         /// </summary>
  267.         /// <param name="type">the projectile type</param>
  268.         /// <returns>the projectile sprite for the type</returns>
  269.         public static Texture2D GetProjectileSprite(ProjectileType type)
  270.         {
  271.             // replace with code to return correct projectile sprite based on projectile type
  272.             if (type == ProjectileType.FrenchFries)
  273.             {
  274.                 return frenchFriesSprite;
  275.             }
  276.             else
  277.             {
  278.                 return teddyBearProjectileSprite;
  279.             }
  280.         }
  281.  
  282.         /// <summary>
  283.         /// Adds the given projectile to the game
  284.         /// </summary>
  285.         /// <param name="projectile">the projectile to add</param>
  286.         public static void AddProjectile(Projectile projectile)
  287.         {
  288.             projectiles.Add(projectile);
  289.         }
  290.  
  291.         #endregion
  292.  
  293.         #region Private methods
  294.  
  295.         /// <summary>
  296.         /// Spawns a new teddy bear at a random location
  297.         /// </summary>
  298.         private void SpawnBear()
  299.         {
  300.             // generate random location
  301.             int bearPosX = GetRandomLocation(GameConstants.SpawnBorderSize,
  302.                 graphics.PreferredBackBufferWidth - GameConstants.SpawnBorderSize * 2);
  303.             int bearPosY = GetRandomLocation(GameConstants.SpawnBorderSize,
  304.                 graphics.PreferredBackBufferHeight - GameConstants.SpawnBorderSize * 2);
  305.  
  306.             // generate random velocity
  307.             float bearSpeed = GameConstants.MinBearSpeed +
  308.                 RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange);
  309.             float angle = RandomNumberGenerator.NextFloat(2 * (float)Math.PI);
  310.             Vector2 velocity = new Vector2
  311.                 ((float)(Math.Cos(angle) * bearSpeed), (float)(Math.Sin(angle) * bearSpeed));
  312.  
  313.             // create new bear
  314.             TeddyBear newBear = new TeddyBear(Content, @"graphics/teddybear", bearPosX,
  315.                 bearPosY, velocity, null, null);
  316.  
  317.             // make sure we don't spawn into a collision
  318.  
  319.             // add new bear to list
  320.             bears.Add(newBear);
  321.         }
  322.  
  323.         /// <summary>
  324.         /// Gets a random location using the given min and range
  325.         /// </summary>
  326.         /// <param name="min">the minimum</param>
  327.         /// <param name="range">the range</param>
  328.         /// <returns>the random location</returns>
  329.         private int GetRandomLocation(int min, int range)
  330.         {
  331.             return min + RandomNumberGenerator.Next(range);
  332.         }
  333.  
  334.         /// <summary>
  335.         /// Gets a list of collision rectangles for all the objects in the game world
  336.         /// </summary>
  337.         /// <returns>the list of collision rectangles</returns>
  338.         private List<Rectangle> GetCollisionRectangles()
  339.         {
  340.             List<Rectangle> collisionRectangles = new List<Rectangle>();
  341.             collisionRectangles.Add(burger.CollisionRectangle);
  342.             foreach (TeddyBear bear in bears)
  343.             {
  344.                 collisionRectangles.Add(bear.CollisionRectangle);
  345.             }
  346.             foreach (Projectile projectile in projectiles)
  347.             {
  348.                 collisionRectangles.Add(projectile.CollisionRectangle);
  349.             }
  350.             foreach (Explosion explosion in explosions)
  351.             {
  352.                 collisionRectangles.Add(explosion.CollisionRectangle);
  353.             }
  354.             return collisionRectangles;
  355.         }
  356.         /// <summary>
  357.         /// Checks to see if the burger has just been killed
  358.         /// </summary>
  359.         private void CheckBurgerKill()
  360.         {
  361.  
  362.         }
  363.  
  364.         #endregion
  365.     }
  366. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement