Advertisement
Daemonion

for random student

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