Advertisement
diliupg

GameProjectInc4

Oct 20th, 2017
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.24 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.             // check and resolve collisions between burger and teddy bears
  182.             foreach (TeddyBear bear in bears)
  183.             {
  184.                 if (bear.Active && burger.Health >= 0 &&
  185.                     bear.CollisionRectangle.Intersects(burger.CollisionRectangle))
  186.                 {
  187.                     bear.Active = false;
  188.                     burger.Health -= GameConstants.BearDamage;
  189.                     explosions.Add(new Explosion(explosionSpriteStrip,
  190.                         bear.CollisionRectangle.Center.X, bear.CollisionRectangle.Center.Y));
  191.                 }
  192.             }
  193.             // check and resolve collisions between burger and projectiles
  194.             foreach(Projectile projectile in projectiles)
  195.             {
  196.                 if (projectile.Type == ProjectileType.TeddyBear &&
  197.                     projectile.Active &&
  198.                     projectile.CollisionRectangle.Intersects(burger.CollisionRectangle))
  199.                 {
  200.                     projectile.Active = false;
  201.                     burger.Health -= GameConstants.TeddyBearProjectileDamage;
  202.  
  203.                 }
  204.  
  205.             }
  206.             // check and resolve collisions between teddy bears and projectiles
  207.             foreach (TeddyBear bear in bears)
  208.             {
  209.                 foreach (Projectile projectile in projectiles)
  210.                 {
  211.                     if (bear.Active && projectile.Active && projectile.Type == ProjectileType.FrenchFries &&
  212.                         bear.CollisionRectangle.Intersects(projectile.CollisionRectangle))
  213.                     {
  214.                         bear.Active = false;
  215.                         projectile.Active = false;
  216.                         explosions.Add(new Explosion(explosionSpriteStrip,
  217.                         bear.CollisionRectangle.Center.X, bear.CollisionRectangle.Center.Y));
  218.  
  219.                     }
  220.                 }
  221.             }
  222.             // clean out inactive teddy bears and add new ones as necessary
  223.             for (int i = bears.Count - 1; i >= 0; i--)
  224.             {
  225.                 if (!bears[i].Active)
  226.                 {
  227.                     bears.RemoveAt(i);
  228.                 }
  229.             }
  230.             // add new teddy bears to fill in for the ones reemoved
  231.  
  232.             while (bears.Count <= GameConstants.MaxBears-1)
  233.             {
  234.                 SpawnBear();
  235.             }
  236.             // clean out inactive projectiles
  237.             for (int i = projectiles.Count - 1; i >= 0; i--)
  238.             {
  239.                 if (!projectiles[i].Active)
  240.                 {
  241.                     projectiles.RemoveAt(i);
  242.                 }
  243.             }
  244.             // clean out finished explosions
  245.             for (int i = explosions.Count - 1; i >= 0; i--)
  246.             {
  247.                 if (explosions[i].Finished)
  248.                 {
  249.                     explosions.RemoveAt(i);
  250.                 }
  251.             }
  252.  
  253.             base.Update(gameTime);
  254.         }
  255.  
  256.         /// <summary>
  257.         /// This is called when the game should draw itself.
  258.         /// </summary>
  259.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  260.         protected override void Draw(GameTime gameTime)
  261.         {
  262.             GraphicsDevice.Clear(Color.CornflowerBlue);
  263.  
  264.             spriteBatch.Begin();
  265.  
  266.             // draw game objects
  267.             burger.Draw(spriteBatch);
  268.             foreach (TeddyBear bear in bears)
  269.             {
  270.                 bear.Draw(spriteBatch);
  271.             }
  272.             foreach (Projectile projectile in projectiles)
  273.             {
  274.                 projectile.Draw(spriteBatch);
  275.             }
  276.             foreach (Explosion explosion in explosions)
  277.             {
  278.                 explosion.Draw(spriteBatch);
  279.             }
  280.  
  281.             // draw score and health
  282.  
  283.             spriteBatch.End();
  284.  
  285.             base.Draw(gameTime);
  286.         }
  287.  
  288.         #region Public methods
  289.  
  290.         /// <summary>
  291.         /// Gets the projectile sprite for the given projectile type
  292.         /// </summary>
  293.         /// <param name="type">the projectile type</param>
  294.         /// <returns>the projectile sprite for the type</returns>
  295.         public static Texture2D GetProjectileSprite(ProjectileType type)
  296.         {
  297.             // replace with code to return correct projectile sprite based on projectile type
  298.             if (type == ProjectileType.FrenchFries)
  299.             {
  300.                 return frenchFriesSprite;
  301.             }
  302.             else
  303.             {
  304.                 return teddyBearProjectileSprite;
  305.             }
  306.         }
  307.  
  308.         /// <summary>
  309.         /// Adds the given projectile to the game
  310.         /// </summary>
  311.         /// <param name="projectile">the projectile to add</param>
  312.         public static void AddProjectile(Projectile projectile)
  313.         {
  314.             projectiles.Add(projectile);
  315.         }
  316.  
  317.         #endregion
  318.  
  319.         #region Private methods
  320.  
  321.         /// <summary>
  322.         /// Spawns a new teddy bear at a random location
  323.         /// </summary>
  324.         private void SpawnBear()
  325.         {
  326.  
  327.             // generate random location
  328.             int bearPosX = GetRandomLocation(GameConstants.SpawnBorderSize,
  329.                 graphics.PreferredBackBufferWidth - GameConstants.SpawnBorderSize * 2);
  330.             int bearPosY = GetRandomLocation(GameConstants.SpawnBorderSize,
  331.                 graphics.PreferredBackBufferHeight - GameConstants.SpawnBorderSize * 2);
  332.  
  333.             // generate random velocity
  334.             float bearSpeed = GameConstants.MinBearSpeed +
  335.                 RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange);
  336.             float angle = RandomNumberGenerator.NextFloat(2 * (float)Math.PI);
  337.             Vector2 velocity = new Vector2
  338.                 ((float)(Math.Cos(angle) * bearSpeed), (float)(Math.Sin(angle) * bearSpeed));
  339.  
  340.             // create new bear
  341.             TeddyBear newBear = new TeddyBear(Content, @"graphics/teddybear", bearPosX,
  342.                 bearPosY, velocity, null, null);
  343.  
  344.             // make sure we don't spawn into a collision
  345.             List<Rectangle> collist = new List<Rectangle>();
  346.             // get the existing bear collision rectangles into a list
  347.             foreach (TeddyBear bear in bears)
  348.             {
  349.                 collist.Add(bear.CollisionRectangle);
  350.             }
  351.             bool colloide = CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, collist);
  352.             while (colloide == false)
  353.             {
  354.                 newBear.X = GetRandomLocation(GameConstants.SpawnBorderSize,
  355.                 graphics.PreferredBackBufferWidth - GameConstants.SpawnBorderSize * 2);
  356.  
  357.                 newBear.Y = GetRandomLocation(GameConstants.SpawnBorderSize,
  358.                 graphics.PreferredBackBufferHeight - GameConstants.SpawnBorderSize * 2);
  359.  
  360.                 colloide = CollisionUtils.IsCollisionFree(newBear.CollisionRectangle, collist);
  361.             }
  362.             // add new bear to list
  363.             bears.Add(newBear);
  364.         }
  365.  
  366.         /// <summary>
  367.         /// Gets a random location using the given min and range
  368.         /// </summary>
  369.         /// <param name="min">the minimum</param>
  370.         /// <param name="range">the range</param>
  371.         /// <returns>the random location</returns>
  372.         private int GetRandomLocation(int min, int range)
  373.         {
  374.             return min + RandomNumberGenerator.Next(range);
  375.         }
  376.  
  377.         /// <summary>
  378.         /// Gets a list of collision rectangles for all the objects in the game world
  379.         /// </summary>
  380.         /// <returns>the list of collision rectangles</returns>
  381.         private List<Rectangle> GetCollisionRectangles()
  382.         {
  383.             List<Rectangle> collisionRectangles = new List<Rectangle>();
  384.             collisionRectangles.Add(burger.CollisionRectangle);
  385.             foreach (TeddyBear bear in bears)
  386.             {
  387.                 collisionRectangles.Add(bear.CollisionRectangle);
  388.             }
  389.             foreach (Projectile projectile in projectiles)
  390.             {
  391.                 collisionRectangles.Add(projectile.CollisionRectangle);
  392.             }
  393.             foreach (Explosion explosion in explosions)
  394.             {
  395.                 collisionRectangles.Add(explosion.CollisionRectangle);
  396.             }
  397.             return collisionRectangles;
  398.         }
  399.         /// <summary>
  400.         /// Checks to see if the burger has just been killed
  401.         /// </summary>
  402.         private void CheckBurgerKill()
  403.         {
  404.  
  405.         }
  406.  
  407.         #endregion
  408.     }
  409. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement