Advertisement
Guest User

Assignment 2

a guest
Sep 29th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.17 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.         //Defining our game's "dimensions"
  20.         const int WINDOW_WIDTH = 800;
  21.         const int WINDOW_HEIGHT = 600;
  22.  
  23.         GraphicsDeviceManager graphics;
  24.         SpriteBatch spriteBatch;
  25.  
  26.         // STUDENTS: add your sprite variables here
  27.         Texture2D dKong;
  28.         Texture2D ghost;
  29.         Texture2D mario;
  30.         Texture2D megaman;
  31.         Texture2D sonic;
  32.  
  33.         // used to handle generating random values
  34.         Random rand = new Random();
  35.         const int CHANGE_DELAY_TIME = 1000;
  36.         int elapsedTime = 0;
  37.  
  38.         // used to keep track of current sprite and location
  39.         Texture2D currentSprite;
  40.         Rectangle drawRectangle = new Rectangle();
  41.  
  42.         public Game1()
  43.         {
  44.             graphics = new GraphicsDeviceManager(this);
  45.             Content.RootDirectory = "Content";
  46.  
  47.             //giving the backbuffer our game's "dimensions"
  48.             graphics.PreferredBackBufferWidth = WINDOW_WIDTH;
  49.             graphics.PreferredBackBufferHeight = WINDOW_HEIGHT;
  50.         }
  51.  
  52.         /// <summary>
  53.         /// Allows the game to perform any initialization it needs to before starting to run.
  54.         /// This is where it can query for any required services and load any non-graphic
  55.         /// related content.  Calling base.Initialize will enumerate through any components
  56.         /// and initialize them as well.
  57.         /// </summary>
  58.         protected override void Initialize()
  59.         {
  60.             // TODO: Add your initialization logic here
  61.  
  62.             base.Initialize();
  63.         }
  64.  
  65.         /// <summary>
  66.         /// LoadContent will be called once per game and is the place to load
  67.         /// all of your content.
  68.         /// </summary>
  69.         protected override void LoadContent()
  70.         {
  71.             // Create a new SpriteBatch, which can be used to draw textures.
  72.             spriteBatch = new SpriteBatch(GraphicsDevice);
  73.  
  74.             // STUDENTS: load the images here
  75.             // loading PNGs, using their specified asset names
  76.             dKong = Content.Load<Texture2D>("dKong");
  77.             ghost = Content.Load<Texture2D>("ghost");
  78.             mario = Content.Load<Texture2D>("mario");
  79.             megaman = Content.Load<Texture2D>("megaman");
  80.             sonic = Content.Load<Texture2D>("sonic");
  81.  
  82.             // STUDENTS: set the currentSprite variable to one of your sprite variables
  83.             //defining the current sprite to be displayed. further ahead, we will change this variable to display all other sprites randomly
  84.             currentSprite = sonic;
  85.         }
  86.  
  87.         /// <summary>
  88.         /// UnloadContent will be called once per game and is the place to unload
  89.         /// all content.
  90.         /// </summary>
  91.         protected override void UnloadContent()
  92.         {
  93.             // TODO: Unload any non ContentManager content here
  94.             //we're not unloading anything this time.
  95.         }
  96.  
  97.         /// <summary>
  98.         /// Allows the game to run logic such as updating the world,
  99.         /// checking for collisions, gathering input, and playing audio.
  100.         /// </summary>
  101.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  102.         protected override void Update(GameTime gameTime)
  103.         {
  104.             // Allows the game to exit
  105.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  106.                 this.Exit();
  107.  
  108.             elapsedTime += gameTime.ElapsedGameTime.Milliseconds;
  109.             if (elapsedTime > CHANGE_DELAY_TIME)
  110.             {
  111.                 //start counting the elapsed time from 0
  112.                 elapsedTime = 0;
  113.  
  114.                 // STUDENTS: uncomment the code below and make it generate a random number between 0 and 4
  115.                 // using the rand field I provided
  116.                 //generating a random number to be used ahead, to change the current sprite to be displayed
  117.                 int spriteNumber = rand.Next(0, 4);
  118.  
  119.                 // sets current sprite
  120.                 // STUDENTS: uncomment the lines below and change sprite0, sprite1, sprite2, sprite 3, and sprite 4
  121.                 //      to the five different names of your sprite variables
  122.                 // using the above generated number to defined the new current sprite to be displayed
  123.                 if (spriteNumber == 0)
  124.                 {
  125.                     currentSprite = sonic;
  126.                 }
  127.                 else if (spriteNumber == 1)
  128.                 {
  129.                     currentSprite = mario;
  130.                 }
  131.                 else if (spriteNumber == 2)
  132.                 {
  133.                     currentSprite = megaman;
  134.                 }
  135.                 else if (spriteNumber == 3)
  136.                 {
  137.                     currentSprite = ghost;
  138.                 }
  139.                 else if (spriteNumber == 4)
  140.                 {
  141.                     currentSprite = dKong;
  142.                 }
  143.  
  144.                 // 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
  145.                 // using the rand field I provided
  146.                 //defining a random horizontal position of rectangle
  147.                 drawRectangle.X = rand.Next(0, graphics.PreferredBackBufferWidth - currentSprite.Width);
  148.  
  149.                 // 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
  150.                 // using the rand field I provided
  151.                 //defining a random vertical position of rectangle
  152.                 drawRectangle.Y = rand.Next(0, graphics.PreferredBackBufferHeight - currentSprite.Height);
  153.  
  154.                 // STUDENTS: set the drawRectangle.Width and drawRectangle.Height to match the width and height of currentSprite
  155.                 //defining rectangle's size to match sprite's size
  156.                 drawRectangle = new Rectangle(drawRectangle.X, drawRectangle.Y, currentSprite.Width, currentSprite.Height);
  157.             }
  158.  
  159.             base.Update(gameTime);
  160.         }
  161.  
  162.         /// <summary>
  163.         /// This is called when the game should draw itself.
  164.         /// </summary>
  165.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  166.         protected override void Draw(GameTime gameTime)
  167.         {
  168.             GraphicsDevice.Clear(Color.CornflowerBlue);
  169.  
  170.             // STUDENTS: draw current sprite here
  171.             //drawing current sprite in the current rectangle
  172.             spriteBatch.Begin();
  173.             spriteBatch.Draw(currentSprite, drawRectangle, Color.White);
  174.             spriteBatch.End();
  175.  
  176.             base.Draw(gameTime);
  177.         }
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement