Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- namespace ProgrammingAssignment3
- {
- /// <summary>
- /// This is the main type for your game.
- /// </summary>
- public class Game1 : Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- const int WindowWidth = 800;
- const int WindowHeight = 600;
- Random rand = new Random( );
- Vector2 centerLocation = new Vector2(
- WindowWidth / 2, WindowHeight / 2 );
- // STUDENTS: declare variables for 3 rock sprites
- Texture2D rockSp0;
- Texture2D rockSp1;
- Texture2D rockSp2;
- // STUDENTS: declare variables for 3 rocks
- Rock rock0;
- Rock rock1;
- Rock rock2;
- // delay support
- const int TotalDelayMilliseconds = 1000;
- int elapsedDelayMilliseconds = 0;
- // random velocity support
- const float BaseSpeed = 0.15f;
- Vector2 upLeft = new Vector2( -BaseSpeed, -BaseSpeed );
- Vector2 upRight = new Vector2( BaseSpeed, -BaseSpeed );
- Vector2 downRight = new Vector2( BaseSpeed, BaseSpeed );
- Vector2 downLeft = new Vector2( -BaseSpeed, BaseSpeed );
- public Game1()
- {
- graphics = new GraphicsDeviceManager( this );
- Content.RootDirectory = "Content";
- // change resolution
- graphics.PreferredBackBufferWidth = WindowWidth;
- graphics.PreferredBackBufferHeight = WindowHeight;
- }
- /// <summary>
- /// Allows the game to perform any initialization it needs to before starting to run.
- /// This is where it can query for any required services and load any non-graphic
- /// related content. Calling base.Initialize will enumerate through any components
- /// and initialize them as well.
- /// </summary>
- protected override void Initialize()
- {
- base.Initialize( );
- }
- /// <summary>
- /// LoadContent will be called once per game and is the place to load
- /// all of your content.
- /// </summary>
- protected override void LoadContent()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch( GraphicsDevice );
- // Load content for 3 sprites
- rockSp0 = Content.Load<Texture2D>( @"graphics/greenrock" );
- rockSp1 = Content.Load<Texture2D>( @"graphics/magentarock" );
- rockSp2 = Content.Load<Texture2D>( @"graphics/whiterock" );
- // spawn three rock instances
- rock0 = null;
- rock1 = null;
- rock2 = null;
- }
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// game-specific content.
- /// </summary>
- protected override void UnloadContent()
- {
- // TODO: Unload any non ContentManager content here
- }
- /// <summary>
- /// Allows the game to run logic such as updating the world,
- /// checking for collisions, gathering input, and playing audio.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Update(GameTime gameTime)
- {
- if(GamePad.GetState( PlayerIndex.One ).Buttons.Back == ButtonState.Pressed ||
- Keyboard.GetState( ).IsKeyDown( Keys.Escape ))
- Exit( );
- // update rocks
- if(rock0 != null)
- rock0.Update( gameTime );
- if(rock1 != null)
- rock1.Update( gameTime );
- if(rock2 != null)
- rock2.Update( gameTime );
- // update timer
- elapsedDelayMilliseconds += gameTime.ElapsedGameTime.Milliseconds;
- if(elapsedDelayMilliseconds >= TotalDelayMilliseconds)
- {
- // timer expired, so spawn new rock if fewer than 3 rocks in window
- // Call the GetRandomRock method to do this
- if(rock0 == null)
- {
- rock0 = GetRandomRock( );
- }
- else if(rock1 == null)
- {
- rock1 = GetRandomRock( );
- }
- else if(rock2 == null)
- {
- rock2 = GetRandomRock( );
- }
- // restart timer
- elapsedDelayMilliseconds = 0;
- }
- // Check each rock to see if it's outside the window. If so
- // spawn a new random rock for it by calling the GetRandomRock method
- // Caution: Only check the property if the variable isn't null
- if(rock0 != null && rock0.OutsideWindow == true)
- {
- rock0 = GetRandomRock( );
- }
- if(rock1 != null && rock1.OutsideWindow == true)
- {
- rock1 = GetRandomRock( );
- }
- if(rock2 != null && rock2.OutsideWindow == true)
- {
- rock2 = GetRandomRock( );
- }
- base.Update( gameTime );
- }
- /// <summary>
- /// This is called when the game should draw itself.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear( Color.CornflowerBlue );
- // draw rocks
- spriteBatch.Begin( );
- if (rock0 !=null)
- {
- rock0.Draw( spriteBatch );
- }
- if (rock1 !=null)
- {
- rock1.Draw( spriteBatch );
- }
- if (rock2 !=null)
- {
- rock2.Draw( spriteBatch );
- }
- spriteBatch.End( );
- base.Draw( gameTime );
- }
- /// <summary>
- /// Gets a rock with a random sprite and velocity
- /// </summary>
- /// <returns>the rock</returns>
- private Rock GetRandomRock()
- {
- // randomly pick a rock sprite by calling the GetRandomSprite method
- Texture2D sprite = GetRandomSprite( );
- // randomly pick a velocity by calling the GetRandomVelocity method
- Vector2 velocity = GetRandomVelocity( );
- // return a new rock, centered in the window, with the random sprite and velocity
- return new Rock( sprite, centerLocation, velocity, WindowWidth, WindowHeight );
- }
- /// <summary>
- /// Gets a random sprite
- /// </summary>
- /// <returns>the sprite</returns>
- private Texture2D GetRandomSprite()
- {
- // return a random sprite
- int spriteNumber = rand.Next( 0, 4 );
- if(spriteNumber == 0)
- {
- return rockSp0;
- }
- else if(spriteNumber == 1)
- {
- return rockSp1;
- }
- else
- {
- return rockSp2;
- }
- }
- /// <summary>
- /// Gets a random velocity
- /// </summary>
- /// <returns>the velocity</returns>
- private Vector2 GetRandomVelocity()
- {
- //get a random velocity
- int velocityNumber = rand.Next( 0, 4 );
- if(velocityNumber == 0)
- {
- return upLeft;
- }
- else if(velocityNumber == 1)
- {
- return upRight;
- }
- else if(velocityNumber == 2)
- {
- return downRight;
- }
- else
- {
- return downLeft;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement