Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.GamerServices;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- namespace ProgrammingAssignment2
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- //Defining our game's "dimensions"
- const int WINDOW_WIDTH = 800;
- const int WINDOW_HEIGHT = 600;
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- // STUDENTS: add your sprite variables here
- Texture2D dKong;
- Texture2D ghost;
- Texture2D mario;
- Texture2D megaman;
- Texture2D sonic;
- // used to handle generating random values
- Random rand = new Random();
- const int CHANGE_DELAY_TIME = 1000;
- int elapsedTime = 0;
- // used to keep track of current sprite and location
- Texture2D currentSprite;
- Rectangle drawRectangle = new Rectangle();
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- //giving the backbuffer our game's "dimensions"
- graphics.PreferredBackBufferWidth = WINDOW_WIDTH;
- graphics.PreferredBackBufferHeight = WINDOW_HEIGHT;
- }
- /// <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()
- {
- // TODO: Add your initialization logic here
- 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);
- // STUDENTS: load the images here
- // loading PNGs, using their specified asset names
- dKong = Content.Load<Texture2D>("dKong");
- ghost = Content.Load<Texture2D>("ghost");
- mario = Content.Load<Texture2D>("mario");
- megaman = Content.Load<Texture2D>("megaman");
- sonic = Content.Load<Texture2D>("sonic");
- // STUDENTS: set the currentSprite variable to one of your sprite variables
- //defining the current sprite to be displayed. further ahead, we will change this variable to display all other sprites randomly
- currentSprite = sonic;
- }
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// all content.
- /// </summary>
- protected override void UnloadContent()
- {
- // TODO: Unload any non ContentManager content here
- //we're not unloading anything this time.
- }
- /// <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)
- {
- // Allows the game to exit
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- this.Exit();
- elapsedTime += gameTime.ElapsedGameTime.Milliseconds;
- if (elapsedTime > CHANGE_DELAY_TIME)
- {
- //start counting the elapsed time from 0
- elapsedTime = 0;
- // STUDENTS: uncomment the code below and make it generate a random number between 0 and 4
- // using the rand field I provided
- //generating a random number to be used ahead, to change the current sprite to be displayed
- int spriteNumber = rand.Next(0, 4);
- // sets current sprite
- // STUDENTS: uncomment the lines below and change sprite0, sprite1, sprite2, sprite 3, and sprite 4
- // to the five different names of your sprite variables
- // using the above generated number to defined the new current sprite to be displayed
- if (spriteNumber == 0)
- {
- currentSprite = sonic;
- }
- else if (spriteNumber == 1)
- {
- currentSprite = mario;
- }
- else if (spriteNumber == 2)
- {
- currentSprite = megaman;
- }
- else if (spriteNumber == 3)
- {
- currentSprite = ghost;
- }
- else if (spriteNumber == 4)
- {
- currentSprite = dKong;
- }
- // 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
- // using the rand field I provided
- //defining a random horizontal position of rectangle
- drawRectangle.X = rand.Next(0, graphics.PreferredBackBufferWidth - currentSprite.Width);
- // 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
- // using the rand field I provided
- //defining a random vertical position of rectangle
- drawRectangle.Y = rand.Next(0, graphics.PreferredBackBufferHeight - currentSprite.Height);
- // STUDENTS: set the drawRectangle.Width and drawRectangle.Height to match the width and height of currentSprite
- //defining rectangle's size to match sprite's size
- drawRectangle = new Rectangle(drawRectangle.X, drawRectangle.Y, currentSprite.Width, currentSprite.Height);
- }
- 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);
- // STUDENTS: draw current sprite here
- //drawing current sprite in the current rectangle
- spriteBatch.Begin();
- spriteBatch.Draw(currentSprite, drawRectangle, Color.White);
- spriteBatch.End();
- base.Draw(gameTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement