Advertisement
diliupg

Programming Assignment-Game1.cs

Sep 16th, 2017
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.12 KB | None | 0 0
  1. using System.Collections.Generic;
  2.  
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using Microsoft.Xna.Framework.Input;
  6.  
  7. namespace ProgrammingAssignment4
  8. {
  9.     /// <summary>
  10.     /// This is the main type for your game.
  11.     /// </summary>
  12.     public class Game1 : Game
  13.     {
  14.         GraphicsDeviceManager graphics;
  15.         SpriteBatch spriteBatch;
  16.  
  17.         const int WindowWidth = 800;
  18.         const int WindowHeight = 600;
  19.  
  20.         // teddy support
  21.         Texture2D teddySprite;
  22.         TeddyBear teddy;
  23.  
  24.         // pickup support
  25.         Texture2D pickupSprite;
  26.         List<Pickup> pickups = new List<Pickup>();
  27.  
  28.         // click processing
  29.         bool rightClickStarted = false;
  30.         bool rightButtonReleased = true;
  31.  
  32.         public Game1()
  33.         {
  34.             graphics = new GraphicsDeviceManager(this);
  35.             Content.RootDirectory = "Content";
  36.  
  37.             // STUDENTS: set resolution and make mouse visible
  38.             graphics.PreferredBackBufferWidth = WindowWidth;
  39.             graphics.PreferredBackBufferHeight = WindowHeight;
  40.  
  41.             IsMouseVisible = true;
  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 teddy and pickup sprites
  67.             teddySprite = Content.Load<Texture2D>(@"graphics/teddybear");
  68.             pickupSprite = Content.Load<Texture2D>(@"graphics/pickup");
  69.  
  70.             // STUDENTS: create teddy object centered in window
  71.             Vector2 TedCenterLoc = new Vector2(WindowWidth / 2 - teddySprite.Width / 2,
  72.                 WindowHeight / 2 - teddySprite.Height / 2);
  73.  
  74.             teddy = new TeddyBear(teddySprite, TedCenterLoc);
  75.         }
  76.  
  77.         /// <summary>
  78.         /// UnloadContent will be called once per game and is the place to unload
  79.         /// game-specific content.
  80.         /// </summary>
  81.         protected override void UnloadContent()
  82.         {
  83.             // TODO: Unload any non ContentManager content here
  84.         }
  85.  
  86.         /// <summary>
  87.         /// Allows the game to run logic such as updating the world,
  88.         /// checking for collisions, gathering input, and playing audio.
  89.         /// </summary>
  90.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  91.         protected override void Update(GameTime gameTime)
  92.         {
  93.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
  94.                 Exit();
  95.  
  96.             // STUDENTS: get current mouse state and update teddy
  97.             MouseState mouse = Mouse.GetState();
  98.  
  99.             teddy.Update(gameTime,mouse);
  100.  
  101.             // check for right click started
  102.             if (mouse.RightButton == ButtonState.Pressed &&
  103.                 rightButtonReleased)
  104.             {
  105.                 rightClickStarted = true;
  106.                 rightButtonReleased = false;
  107.             }
  108.             else if (mouse.RightButton == ButtonState.Released)
  109.             {
  110.                 rightButtonReleased = true;
  111.  
  112.                 // if right click finished, add new pickup to list
  113.                 if (rightClickStarted)
  114.                 {
  115.                     rightClickStarted = false;
  116.  
  117.                     // STUDENTS: add a new pickup to the end of the list of pickups
  118.                     Vector2 pickupLocCenter = new Vector2(mouse.X, mouse.Y);
  119.  
  120.                     pickups.Add(new Pickup(pickupSprite, pickupLocCenter));
  121.  
  122.                     // STUDENTS: if this is the first pickup in the list, set teddy
  123.                     if (pickups.Count<2)
  124.  
  125.                     {
  126.                         teddy.SetTarget(pickupLocCenter);
  127.                     }
  128.  
  129.                 }
  130.             }
  131.  
  132.             // check for collision between collecting teddy and targeted pickup
  133.             if (teddy.Collecting &&
  134.                 teddy.CollisionRectangle.Intersects(pickups[0].CollisionRectangle))
  135.             {
  136.                 // STUDENTS: remove targeted pickup from list (it's always at location 0)
  137.                 pickups.RemoveAt(0);
  138.  
  139.  
  140.                 // STUDENTS: if there's another pickup to collect, set teddy target
  141.                 // If not, clear teddy target and stop the teddy from collecting
  142.  
  143.                 if (pickups.Count != 0)
  144.                 {
  145.                     Vector2 pickupLocCenter = new Vector2(pickups[0].CollisionRectangle.X,
  146.                         pickups[0].CollisionRectangle.Y);
  147.                     teddy.SetTarget(pickupLocCenter);
  148.                 }
  149.                 else
  150.                 {
  151.                     teddy.ClearTarget();
  152.                     teddy.Collecting = false;
  153.                 }
  154.  
  155.             }
  156.  
  157.             base.Update(gameTime);
  158.         }
  159.  
  160.         /// <summary>
  161.         /// This is called when the game should draw itself.
  162.         /// </summary>
  163.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  164.         protected override void Draw(GameTime gameTime)
  165.         {
  166.             GraphicsDevice.Clear(Color.CornflowerBlue);
  167.  
  168.             // draw game objects
  169.             spriteBatch.Begin();
  170.  
  171.             teddy.Draw(spriteBatch);
  172.  
  173.             foreach (Pickup pickup in pickups)
  174.             {
  175.                 pickup.Draw(spriteBatch);
  176.             }
  177.  
  178.             spriteBatch.End();
  179.  
  180.             base.Draw(gameTime);
  181.         }
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement