Advertisement
diliupg

Coursera Programming Assignment 2

Aug 27th, 2017
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.06 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 ProgrammingAssignment2
  8. {
  9.     /// <summary>
  10.     /// This is the main type for your game
  11.     /// </summary>
  12.     public class Game1 : Microsoft.Xna.Framework.Game
  13.     {
  14.         const int WindowWidth = 800;
  15.         const int WindowHeight = 600;
  16.  
  17.         GraphicsDeviceManager graphics;
  18.         SpriteBatch spriteBatch;
  19.  
  20.         // STUDENTS: declare variables for three sprites
  21.         Texture2D AirPlane;
  22.         Texture2D Bomb;
  23.         Texture2D Robot;
  24.  
  25.  
  26.         // STUDENTS: declare variables for x and y speeds
  27.         int RandomX;
  28.         int RandomY;
  29.  
  30.         // used to handle generating random values
  31.         Random rand = new Random();
  32.         const int ChangeDelayTime = 1000;
  33.         int elapsedTime = 0;
  34.  
  35.         // used to keep track of current sprite and location
  36.         Texture2D currentSprite;
  37.         Rectangle drawRectangle = new Rectangle();
  38.  
  39.         public Game1()
  40.         {
  41.             graphics = new GraphicsDeviceManager(this);
  42.             Content.RootDirectory = "Content";
  43.  
  44.             graphics.PreferredBackBufferWidth = WindowWidth;
  45.             graphics.PreferredBackBufferHeight = WindowHeight;
  46.         }
  47.  
  48.         /// <summary>
  49.         /// Allows the game to perform any initialization it needs to before starting to run.
  50.         /// This is where it can query for any required services and load any non-graphic
  51.         /// related content.  Calling base.Initialize will enumerate through any components
  52.         /// and initialize them as well.
  53.         /// </summary>
  54.         protected override void Initialize()
  55.         {
  56.             // TODO: Add your initialization logic here
  57.  
  58.             base.Initialize();
  59.         }
  60.  
  61.         /// <summary>
  62.         /// LoadContent will be called once per game and is the place to load
  63.         /// all of your content.
  64.         /// </summary>
  65.         protected override void LoadContent()
  66.         {
  67.             // Create a new SpriteBatch, which can be used to draw textures.
  68.             spriteBatch = new SpriteBatch(GraphicsDevice);
  69.  
  70.             // STUDENTS: load the sprite images here
  71.             AirPlane = Content.Load<Texture2D>( @"graphics\AirPlane1" );
  72.             Bomb = Content.Load<Texture2D>( @"graphics\Bomb1" );
  73.             Robot = Content.Load<Texture2D>( @"graphics\Robot1" );
  74.  
  75.  
  76.             // STUDENTS: set the currentSprite variable to one of your sprite variables
  77.             currentSprite = AirPlane;
  78.         }
  79.  
  80.         /// <summary>
  81.         /// UnloadContent will be called once per game and is the place to unload
  82.         /// all content.
  83.         /// </summary>
  84.         protected override void UnloadContent()
  85.         {
  86.             // TODO: Unload any non ContentManager content here
  87.         }
  88.  
  89.         /// <summary>
  90.         /// Allows the game to run logic such as updating the world,
  91.         /// checking for collisions, gathering input, and playing audio.
  92.         /// </summary>
  93.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  94.         protected override void Update(GameTime gameTime)
  95.         {
  96.             // Allows the game to exit
  97.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  98.                 this.Exit();
  99.  
  100.             elapsedTime += gameTime.ElapsedGameTime.Milliseconds;
  101.             if (elapsedTime > ChangeDelayTime)
  102.             {
  103.                 elapsedTime = 0;
  104.  
  105.                 // STUDENTS: uncomment the code below and make it generate a random number
  106.                 // between 0 and 2 inclusive using the rand field I provided
  107.                 int spriteNumber = rand.Next( 0, 3 );
  108.  
  109.                 // sets current sprite
  110.                 // STUDENTS: uncomment the lines below and change sprite0, sprite1, and sprite2
  111.                 //      to the three different names of your sprite variables
  112.                 if (spriteNumber == 0)
  113.                 {
  114.                     currentSprite = AirPlane;
  115.                 }
  116.                 else if (spriteNumber == 1)
  117.                 {
  118.                     currentSprite = Bomb;
  119.                 }
  120.                 else if (spriteNumber == 2)
  121.                 {
  122.                     currentSprite = Robot;
  123.                 }
  124.  
  125.                 // STUDENTS: set the drawRectangle.Width and drawRectangle.Height to match the width and
  126.                 // height of currentSprite
  127.                 drawRectangle.Width = currentSprite.Width;
  128.                 drawRectangle.Height = currentSprite.Height;
  129.  
  130.                 // STUDENTS: center the draw rectangle in the window. Note that the X and Y properties of the
  131.                 //rectangle are for the upper left corner of the rectangle, not the center of the rectangle
  132.                 drawRectangle.X = WindowWidth / 2 - drawRectangle.Width / 2;
  133.                 drawRectangle.Y = WindowHeight / 2 - drawRectangle.Height / 2;
  134.  
  135.                 // STUDENTS: write code below to generate random numbers  between -4 and 4 inclusive for the x
  136.                 // and y speed using the rand field I provided
  137.                 // CAUTION: Don't redeclare the x speed and y speed variables here!
  138.                 RandomX = rand.Next( -4, 5 );
  139.                 RandomY = rand.Next( -4, 5 );
  140.             }
  141.  
  142.             // STUDENTS: move the drawRectangle by the x speed and the y speed
  143.             drawRectangle.X += RandomX;
  144.             drawRectangle.Y += RandomY;
  145.  
  146.  
  147.             base.Update(gameTime);
  148.         }
  149.  
  150.         /// <summary>
  151.         /// This is called when the game should draw itself.
  152.         /// </summary>
  153.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  154.         protected override void Draw(GameTime gameTime)
  155.         {
  156.             GraphicsDevice.Clear(Color.CornflowerBlue);
  157.  
  158.             // STUDENTS: draw current sprite here
  159.             spriteBatch.Begin( );
  160.  
  161.             spriteBatch.Draw( currentSprite, drawRectangle, Color.White);
  162.  
  163.             spriteBatch.End( );
  164.  
  165.             base.Draw(gameTime);
  166.         }
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement