Advertisement
Guest User

Programming assignment 2

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