Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.00 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.             IsMouseVisible = true;
  41.         }
  42.  
  43.         /// <summary>
  44.         /// Allows the game to perform any initialization it needs to before starting to run.
  45.         /// This is where it can query for any required services and load any non-graphic
  46.         /// related content.  Calling base.Initialize will enumerate through any components
  47.         /// and initialize them as well.
  48.         /// </summary>
  49.         protected override void Initialize()
  50.         {
  51.             // TODO: Add your initialization logic here
  52.  
  53.             base.Initialize();
  54.         }
  55.  
  56.         /// <summary>
  57.         /// LoadContent will be called once per game and is the place to load
  58.         /// all of your content.
  59.         /// </summary>
  60.         protected override void LoadContent()
  61.         {
  62.             // Create a new SpriteBatch, which can be used to draw textures.
  63.             spriteBatch = new SpriteBatch(GraphicsDevice);
  64.  
  65.             // +STUDENTS: load teddy and pickup sprites
  66.             teddySprite = Content.Load<Texture2D>(@"graphics\teddybear");
  67.             pickupSprite = Content.Load<Texture2D>(@"graphics\pickup");
  68.  
  69.             // +STUDENTS: create teddy object centered in window
  70.             teddy = new TeddyBear(teddySprite, new Vector2(WindowWidth / 2, WindowHeight / 2));
  71.         }
  72.  
  73.         /// <summary>
  74.         /// UnloadContent will be called once per game and is the place to unload
  75.         /// game-specific content.
  76.         /// </summary>
  77.         protected override void UnloadContent()
  78.         {
  79.             // TODO: Unload any non ContentManager content here
  80.         }
  81.  
  82.         /// <summary>
  83.         /// Allows the game to run logic such as updating the world,
  84.         /// checking for collisions, gathering input, and playing audio.
  85.         /// </summary>
  86.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  87.         protected override void Update(GameTime gameTime)
  88.         {
  89.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
  90.                 Exit();
  91.  
  92.             // +STUDENTS: get current mouse state and update teddy
  93.             MouseState mouse = Mouse.GetState();
  94.             teddy.Update(gameTime, mouse);
  95.            
  96.             // check for right click started
  97.             if (mouse.RightButton == ButtonState.Pressed &&
  98.                 rightButtonReleased)
  99.             {
  100.                 rightClickStarted = true;
  101.                 rightButtonReleased = false;
  102.             }
  103.             else if (mouse.RightButton == ButtonState.Released)
  104.             {
  105.                 rightButtonReleased = true;
  106.  
  107.                 // if right click finished, add new pickup to list
  108.                 if (rightClickStarted)
  109.                 {
  110.                     rightClickStarted = false;
  111.  
  112.                     // +STUDENTS: add a new pickup to the end of the list of pickups
  113.                     Pickup newPickup = new Pickup(pickupSprite, new Vector2(mouse.X, mouse.Y));
  114.                     pickups.Add(newPickup);
  115.  
  116.                     // +STUDENTS: if this is the first pickup in the list, set teddy target
  117.                     if (pickups.Count == 1)
  118.                     {
  119.                         teddy.SetTarget(new Vector2(pickups[0].CollisionRectangle.Center.X, pickups[0].CollisionRectangle.Center.Y));
  120.                     }
  121.                 }
  122.             }
  123.  
  124.             // check for collision between collecting teddy and targeted pickup
  125.             if (teddy.Collecting &&
  126.                 teddy.CollisionRectangle.Intersects(pickups[0].CollisionRectangle))
  127.             {
  128.                 // +STUDENTS: remove targeted pickup from list (it's always at location 0)
  129.                 pickups.RemoveAt(0);
  130.  
  131.                 // +STUDENTS: if there's another pickup to collect, set teddy target
  132.                 // If not, clear teddy target and stop the teddy from collecting
  133.                 if (pickups.Count > 0)
  134.                 {
  135.                     teddy.SetTarget(new Vector2(pickups[0].CollisionRectangle.Center.X, pickups[0].CollisionRectangle.Center.Y));
  136.                 }
  137.                 else
  138.                 {
  139.                     teddy.ClearTarget();
  140.                     teddy.Collecting = false;
  141.                 }
  142.  
  143.             }
  144.  
  145.             base.Update(gameTime);
  146.         }
  147.  
  148.         /// <summary>
  149.         /// This is called when the game should draw itself.
  150.         /// </summary>
  151.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  152.         protected override void Draw(GameTime gameTime)
  153.         {
  154.             GraphicsDevice.Clear(Color.CornflowerBlue);
  155.  
  156.             // draw game objects
  157.             spriteBatch.Begin();
  158.             teddy.Draw(spriteBatch);
  159.             foreach (Pickup pickup in pickups)
  160.             {
  161.                 pickup.Draw(spriteBatch);
  162.             }
  163.             spriteBatch.End();
  164.  
  165.             base.Draw(gameTime);
  166.         }
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement