Advertisement
diliupg

ProgrammingAssigenment3_Game1.cs

Sep 4th, 2017
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.97 KB | None | 0 0
  1. using System;
  2.  
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using Microsoft.Xna.Framework.Input;
  6.  
  7. namespace ProgrammingAssignment3
  8. {
  9.     /// <summary>
  10.     /// This is the main type for your game.
  11.     /// </summary>
  12.     public class Game1 : Game
  13.     {
  14.         GraphicsDeviceManager graphics;
  15.         SpriteBatch spriteBatch;
  16.  
  17.         const int WindowWidth = 800;
  18.         const int WindowHeight = 600;
  19.  
  20.         Random rand = new Random( );
  21.         Vector2 centerLocation = new Vector2(
  22.             WindowWidth / 2, WindowHeight / 2 );
  23.  
  24.         // STUDENTS: declare variables for 3 rock sprites
  25.         Texture2D rockSp0;
  26.         Texture2D rockSp1;
  27.         Texture2D rockSp2;
  28.  
  29.         // STUDENTS: declare variables for 3 rocks
  30.         Rock rock0;
  31.         Rock rock1;
  32.         Rock rock2;
  33.  
  34.         // delay support
  35.         const int TotalDelayMilliseconds = 1000;
  36.         int elapsedDelayMilliseconds = 0;
  37.  
  38.         // random velocity support
  39.         const float BaseSpeed = 0.15f;
  40.         Vector2 upLeft = new Vector2( -BaseSpeed, -BaseSpeed );
  41.         Vector2 upRight = new Vector2( BaseSpeed, -BaseSpeed );
  42.         Vector2 downRight = new Vector2( BaseSpeed, BaseSpeed );
  43.         Vector2 downLeft = new Vector2( -BaseSpeed, BaseSpeed );
  44.  
  45.         public Game1()
  46.         {
  47.             graphics = new GraphicsDeviceManager( this );
  48.             Content.RootDirectory = "Content";
  49.  
  50.             // change resolution
  51.             graphics.PreferredBackBufferWidth = WindowWidth;
  52.             graphics.PreferredBackBufferHeight = WindowHeight;
  53.         }
  54.  
  55.         /// <summary>
  56.         /// Allows the game to perform any initialization it needs to before starting to run.
  57.         /// This is where it can query for any required services and load any non-graphic
  58.         /// related content.  Calling base.Initialize will enumerate through any components
  59.         /// and initialize them as well.
  60.         /// </summary>
  61.         protected override void Initialize()
  62.         {
  63.             base.Initialize( );
  64.         }
  65.  
  66.         /// <summary>
  67.         /// LoadContent will be called once per game and is the place to load
  68.         /// all of your content.
  69.         /// </summary>
  70.         protected override void LoadContent()
  71.         {
  72.             // Create a new SpriteBatch, which can be used to draw textures.
  73.             spriteBatch = new SpriteBatch( GraphicsDevice );
  74.  
  75.             // Load content for 3 sprites
  76.             rockSp0 = Content.Load<Texture2D>( @"graphics/greenrock" );
  77.             rockSp1 = Content.Load<Texture2D>( @"graphics/magentarock" );
  78.             rockSp2 = Content.Load<Texture2D>( @"graphics/whiterock" );
  79.  
  80.             // spawn three rock instances
  81.             rock0 = null;
  82.             rock1 = null;
  83.             rock2 = null;
  84.         }
  85.  
  86.         /// <summary>
  87.         /// UnloadContent will be called once per game and is the place to unload
  88.         /// game-specific content.
  89.         /// </summary>
  90.         protected override void UnloadContent()
  91.         {
  92.             // TODO: Unload any non ContentManager content here
  93.         }
  94.  
  95.         /// <summary>
  96.         /// Allows the game to run logic such as updating the world,
  97.         /// checking for collisions, gathering input, and playing audio.
  98.         /// </summary>
  99.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  100.         protected override void Update(GameTime gameTime)
  101.         {
  102.             if(GamePad.GetState( PlayerIndex.One ).Buttons.Back == ButtonState.Pressed ||
  103.                 Keyboard.GetState( ).IsKeyDown( Keys.Escape ))
  104.                 Exit( );
  105.  
  106.             // update rocks
  107.             if(rock0 != null)
  108.                 rock0.Update( gameTime );
  109.  
  110.             if(rock1 != null)
  111.                 rock1.Update( gameTime );
  112.  
  113.             if(rock2 != null)
  114.                 rock2.Update( gameTime );
  115.  
  116.  
  117.             // update timer
  118.             elapsedDelayMilliseconds += gameTime.ElapsedGameTime.Milliseconds;
  119.             if(elapsedDelayMilliseconds >= TotalDelayMilliseconds)
  120.             {
  121.                 // timer expired, so spawn new rock if fewer than 3 rocks in window
  122.                 // Call the GetRandomRock method to do this
  123.  
  124.                 if(rock0 == null)
  125.                 {
  126.                     rock0 = GetRandomRock( );
  127.                 }
  128.  
  129.                 else if(rock1 == null)
  130.                 {
  131.                     rock1 = GetRandomRock( );
  132.                 }
  133.  
  134.                 else if(rock2 == null)
  135.                 {
  136.                     rock2 = GetRandomRock( );
  137.                 }
  138.                 // restart timer
  139.                 elapsedDelayMilliseconds = 0;
  140.             }
  141.  
  142.             // Check each rock to see if it's outside the window. If so
  143.             // spawn a new random rock for it by calling the GetRandomRock method
  144.             // Caution: Only check the property if the variable isn't null
  145.             if(rock0 != null && rock0.OutsideWindow == true)
  146.             {
  147.                 rock0 = GetRandomRock( );
  148.             }
  149.             if(rock1 != null && rock1.OutsideWindow == true)
  150.             {
  151.                 rock1 = GetRandomRock( );
  152.             }
  153.             if(rock2 != null && rock2.OutsideWindow == true)
  154.             {
  155.                 rock2 = GetRandomRock( );
  156.             }
  157.  
  158.             base.Update( gameTime );
  159.         }
  160.  
  161.         /// <summary>
  162.         /// This is called when the game should draw itself.
  163.         /// </summary>
  164.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  165.         protected override void Draw(GameTime gameTime)
  166.         {
  167.             GraphicsDevice.Clear( Color.CornflowerBlue );
  168.  
  169.             // draw rocks
  170.             spriteBatch.Begin( );
  171.  
  172.             if (rock0 !=null)
  173.             {
  174.                 rock0.Draw( spriteBatch );
  175.             }
  176.  
  177.             if (rock1 !=null)
  178.             {
  179.                 rock1.Draw( spriteBatch );
  180.             }
  181.  
  182.             if (rock2 !=null)
  183.             {
  184.                 rock2.Draw( spriteBatch );
  185.             }
  186.  
  187.             spriteBatch.End( );
  188.  
  189.             base.Draw( gameTime );
  190.         }
  191.  
  192.         /// <summary>
  193.         /// Gets a rock with a random sprite and velocity
  194.         /// </summary>
  195.         /// <returns>the rock</returns>
  196.         private Rock GetRandomRock()
  197.         {
  198.             // randomly pick a rock sprite by calling the GetRandomSprite method
  199.             Texture2D sprite = GetRandomSprite( );
  200.  
  201.             // randomly pick a velocity by calling the GetRandomVelocity method
  202.             Vector2 velocity = GetRandomVelocity( );
  203.  
  204.             // return a new rock, centered in the window, with the random sprite and velocity
  205.             return new Rock( sprite, centerLocation, velocity, WindowWidth, WindowHeight );
  206.         }
  207.  
  208.         /// <summary>
  209.         /// Gets a random sprite
  210.         /// </summary>
  211.         /// <returns>the sprite</returns>
  212.         private Texture2D GetRandomSprite()
  213.         {
  214.             // return a random sprite
  215.             int spriteNumber = rand.Next( 0, 4 );
  216.             if(spriteNumber == 0)
  217.             {
  218.                 return rockSp0;
  219.             }
  220.             else if(spriteNumber == 1)
  221.             {
  222.                 return rockSp1;
  223.             }
  224.             else
  225.             {
  226.                 return rockSp2;
  227.             }
  228.  
  229.         }
  230.  
  231.         /// <summary>
  232.         /// Gets a random velocity
  233.         /// </summary>
  234.         /// <returns>the velocity</returns>
  235.         private Vector2 GetRandomVelocity()
  236.         {
  237.             //get a random velocity
  238.             int velocityNumber = rand.Next( 0, 4 );
  239.             if(velocityNumber == 0)
  240.             {
  241.                 return upLeft;
  242.             }
  243.             else if(velocityNumber == 1)
  244.             {
  245.                 return upRight;
  246.             }
  247.             else if(velocityNumber == 2)
  248.             {
  249.                 return downRight;
  250.             }
  251.             else
  252.             {
  253.                 return downLeft;
  254.             }
  255.  
  256.         }
  257.     }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement