diliupg

GameProject-Game1.cs

Sep 7th, 2017
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.62 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
  20.         // easier, 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. Saved so they don't have to
  27.         // 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.             SpawnBear( );
  85.  
  86.             // load burger
  87.  
  88.             burger = new Burger( Content, (@"graphics/burger"), GameConstants.WindowWidth / 2,
  89.                 GameConstants.WindowHeight / 8 * 7, null );
  90.  
  91.             // load audio content
  92.  
  93.             // load sprite font
  94.  
  95.             // load projectile and explosion sprites
  96.  
  97.             // add initial game objects
  98.  
  99.             // set initial health and score strings
  100.         }
  101.  
  102.         /// <summary>
  103.         /// UnloadContent will be called once per game and is the place to unload
  104.         /// game-specific content.
  105.         /// </summary>
  106.         protected override void UnloadContent()
  107.         {
  108.             // TODO: Unload any non ContentManager content here
  109.         }
  110.  
  111.         /// <summary>
  112.         /// Allows the game to run logic such as updating the world,
  113.         /// checking for collisions, gathering input, and playing audio.
  114.         /// </summary>
  115.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  116.         protected override void Update(GameTime gameTime)
  117.         {
  118.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed
  119.                 || Keyboard.GetState().IsKeyDown(Keys.Escape))
  120.                 Exit();
  121.  
  122.             // get current mouse state and update burger
  123.  
  124.             // update other game objects
  125.             foreach (TeddyBear bear in bears)
  126.             {
  127.                 bear.Update(gameTime);
  128.             }
  129.             foreach (Projectile projectile in projectiles)
  130.             {
  131.                 projectile.Update(gameTime);
  132.             }
  133.             foreach (Explosion explosion in explosions)
  134.             {
  135.                 explosion.Update(gameTime);
  136.             }
  137.  
  138.             // check and resolve collisions between teddy bears
  139.  
  140.             // check and resolve collisions between burger and teddy bears
  141.  
  142.             // check and resolve collisions between burger and projectiles
  143.  
  144.             // check and resolve collisions between teddy bears and projectiles
  145.  
  146.             // clean out inactive teddy bears and add new ones as necessary
  147.  
  148.             // clean out inactive projectiles
  149.  
  150.             // clean out finished explosions
  151.  
  152.             base.Update(gameTime);
  153.         }
  154.  
  155.         /// <summary>
  156.         /// This is called when the game should draw itself.
  157.         /// </summary>
  158.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  159.         protected override void Draw(GameTime gameTime)
  160.         {
  161.             GraphicsDevice.Clear(Color.CornflowerBlue);
  162.  
  163.             spriteBatch.Begin();
  164.  
  165.             // draw game objects
  166.             burger.Draw(spriteBatch);
  167.  
  168.             foreach (TeddyBear bear in bears)
  169.             {
  170.                 bear.Draw(spriteBatch);
  171.             }
  172.             foreach (Projectile projectile in projectiles)
  173.             {
  174.                 projectile.Draw(spriteBatch);
  175.             }
  176.             foreach (Explosion explosion in explosions)
  177.             {
  178.                 explosion.Draw(spriteBatch);
  179.             }
  180.  
  181.             // draw score and health
  182.  
  183.             spriteBatch.End();
  184.  
  185.             base.Draw(gameTime);
  186.         }
  187.  
  188.         #region Public methods
  189.  
  190.         /// <summary>
  191.         /// Gets the projectile sprite for the given projectile type
  192.         /// </summary>
  193.         /// <param name="type">the projectile type</param>
  194.         /// <returns>the projectile sprite for the type</returns>
  195.         public static Texture2D GetProjectileSprite(ProjectileType type)
  196.         {
  197.             // replace with code to return correct projectile sprite based on projectile type
  198.             return frenchFriesSprite;
  199.         }
  200.  
  201.         /// <summary>
  202.         /// Adds the given projectile to the game
  203.         /// </summary>
  204.         /// <param name="projectile">the projectile to add</param>
  205.         public static void AddProjectile(Projectile projectile)
  206.         {
  207.  
  208.         }
  209.  
  210.         #endregion
  211.  
  212.         #region Private methods
  213.  
  214.         /// <summary>
  215.         /// Spawns a new teddy bear at a random location
  216.         /// </summary>
  217.         private void SpawnBear()
  218.         {
  219.             // generate random location
  220.             int bearPosX = GetRandomLocation(GameConstants.SpawnBorderSize,
  221.                 GameConstants.WindowWidth - GameConstants.SpawnBorderSize * 2);
  222.  
  223.             int bearPosY = GetRandomLocation(GameConstants.SpawnBorderSize,
  224.                 GameConstants.WindowHeight - GameConstants.SpawnBorderSize * 2);
  225.  
  226.             // generate random speed
  227.             float bearSpeed = GameConstants.MinBearSpeed +
  228.                 RandomNumberGenerator.NextFloat(GameConstants.BearSpeedRange);
  229.  
  230.             // generate random angle
  231.             float angle = RandomNumberGenerator.NextFloat(360) * (float)Math.PI;
  232.  
  233.             // generate vector2 velocity
  234.             float XVel =System.Convert.ToSingle(bearSpeed * Math.Cos(angle));
  235.             float YVel = System.Convert.ToSingle(bearSpeed * Math.Sin(angle));
  236.             Vector2 velocity =new Vector2(XVel,YVel);
  237.  
  238.             // create new bear
  239.             TeddyBear newBear = new TeddyBear(Content, @"graphics/teddybear", bearPosX,
  240.                 bearPosY, velocity, null, null);
  241.  
  242.             bears.Add(newBear);
  243.  
  244.             // make sure we don't spawn into a collision
  245.  
  246.             // add new bear to list
  247.  
  248.         }
  249.  
  250.         /// <summary>
  251.         /// Gets a random location using the given min and range
  252.         /// </summary>
  253.         /// <param name="min">the minimum</param>
  254.         /// <param name="range">the range</param>
  255.         /// <returns>the random location</returns>
  256.         private int GetRandomLocation(int min, int range)
  257.         {
  258.             return min + RandomNumberGenerator.Next(range);
  259.         }
  260.  
  261.         /// <summary>
  262.         /// Gets a list of collision rectangles for all the objects in the game world
  263.         /// </summary>
  264.         /// <returns>the list of collision rectangles</returns>
  265.         private List<Rectangle> GetCollisionRectangles()
  266.         {
  267.             List<Rectangle> collisionRectangles = new List<Rectangle>();
  268.             collisionRectangles.Add(burger.CollisionRectangle);
  269.             foreach (TeddyBear bear in bears)
  270.             {
  271.                 collisionRectangles.Add(bear.CollisionRectangle);
  272.             }
  273.             foreach (Projectile projectile in projectiles)
  274.             {
  275.                 collisionRectangles.Add(projectile.CollisionRectangle);
  276.             }
  277.             foreach (Explosion explosion in explosions)
  278.             {
  279.                 collisionRectangles.Add(explosion.CollisionRectangle);
  280.             }
  281.             return collisionRectangles;
  282.         }
  283.  
  284.         /// <summary>
  285.         /// Checks to see if the burger has just been killed
  286.         /// </summary>
  287.         private void CheckBurgerKill()
  288.         {
  289.  
  290.         }
  291.  
  292.         #endregion
  293.     }
  294. }
Add Comment
Please, Sign In to add comment