Advertisement
SuperLemrick

Cat and Mouse

Mar 27th, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.81 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////
  2. // Copyright 2013, CompuScholar, Inc.
  3. //
  4. // This source code is for use by the students and teachers who
  5. // have purchased the corresponding TeenCoder or KidCoder product.
  6. // It may not be transmitted to other parties for any reason
  7. // without the written consent of CompuScholar, Inc.
  8. // This source is provided as-is for educational purposes only.
  9. // CompuScholar, Inc. makes no warranty and assumes
  10. // no liability regarding the functionality of this program.
  11. //
  12. ////////////////////////////////////////////////////////////////
  13.  
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using Microsoft.Xna.Framework;
  18. using Microsoft.Xna.Framework.Audio;
  19. using Microsoft.Xna.Framework.Content;
  20. using Microsoft.Xna.Framework.GamerServices;
  21. using Microsoft.Xna.Framework.Graphics;
  22. using Microsoft.Xna.Framework.Input;
  23. using Microsoft.Xna.Framework.Media;
  24. using Microsoft.Xna.Framework.Net;
  25. using Microsoft.Xna.Framework.Storage;
  26.  
  27. namespace CatAndMouse
  28. {
  29.     /// <summary>
  30.     /// This is the main type for your game
  31.     /// </summary>
  32.     public class CatAndMouse : Microsoft.Xna.Framework.Game
  33.     {
  34.         GraphicsDeviceManager graphics;
  35.         SpriteBatch spriteBatch;
  36.  
  37.         // old keyboard, mouse, and gamepad states track the device states
  38.         // from the previous call to Update()
  39.         KeyboardState oldKeyState;
  40.         MouseState oldMouseState;
  41.  
  42.         GamePadState oldGamePadState1;
  43.         GamePadState oldGamePadState2;
  44.  
  45.         // these textures will hold the images of the cat and mouse
  46.         Texture2D catTexture;
  47.         Texture2D mouseTexture;
  48.  
  49.         // initialize new random number generator for teleport feature
  50.         Random random = new System.Random();
  51.  
  52.         // these vectors will hold the X,Y coordinate pairs for the cat and mouse
  53.         Vector2 catLocation;
  54.         Vector2 mouseLocation;
  55.  
  56.         // these variables will hold the client window's width and height
  57.         int screenWidth;
  58.         int screenHeight;
  59.  
  60.         // This method is provided complete as part of the activity starter
  61.         public CatAndMouse()
  62.         {
  63.             graphics = new GraphicsDeviceManager(this);
  64.             Content.RootDirectory = "Content";
  65.         }
  66.  
  67.         /// <summary>
  68.         /// Allows the game to perform any initialization it needs to before starting to run.
  69.         /// This is where it can query for any required services and load any non-graphic
  70.         /// related content.  Calling base.Initialize will enumerate through any components
  71.         /// and initialize them as well.
  72.         /// </summary>
  73.         // This method is provided complete as part of the activity starter
  74.         protected override void Initialize()
  75.         {
  76.             // initialize screen width and screen height variables for future reference
  77.             screenWidth = GraphicsDevice.Viewport.Width;
  78.             screenHeight = GraphicsDevice.Viewport.Height;
  79.            
  80.             this.IsMouseVisible = true; // make the mouse visible on the screen
  81.  
  82.             // set starting positions of cat and mouse
  83.             catLocation = new Vector2(random.Next(0, screenWidth), random.Next(0, screenHeight));
  84.             mouseLocation = new Vector2(random.Next(0, screenWidth), random.Next(0, screenHeight));
  85.  
  86.             base.Initialize();
  87.         }
  88.  
  89.         /// <summary>
  90.         /// LoadContent will be called once per game and is the place to load
  91.         /// all of your content.
  92.         /// </summary>
  93.         // This method is provided complete as part of the activity starter
  94.         protected override void LoadContent()
  95.         {
  96.             // Create a new SpriteBatch, which can be used to draw textures.
  97.             spriteBatch = new SpriteBatch(GraphicsDevice);
  98.  
  99.             // load the cat and mouse textures
  100.             catTexture = Content.Load<Texture2D>("cat");
  101.             mouseTexture = Content.Load<Texture2D>("mouse");
  102.  
  103.         }
  104.  
  105.         /// <summary>
  106.         /// UnloadContent will be called once per game and is the place to unload
  107.         /// all content.
  108.         /// </summary>
  109.         // This method is provided complete as part of the activity starter
  110.         protected override void UnloadContent()
  111.         {
  112.             // TODO: Unload any non ContentManager content here
  113.         }
  114.  
  115.         /// <summary>
  116.         /// Allows the game to run logic such as updating the world,
  117.         /// checking for collisions, gathering input, and playing audio.
  118.         /// </summary>
  119.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  120.         // This method is provided complete as part of the activity starter
  121.         protected override void Update(GameTime gameTime)
  122.         {
  123.             // Allows the game to exit
  124.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  125.                 this.Exit();
  126.  
  127.             // process all keyboard input
  128.             checkKeyboardInput();
  129.  
  130.             // process all mouse input
  131.             checkMouseInput();
  132.  
  133.             // optionally process input from Xbox Controllers
  134.             checkGamepad1Input();
  135.             checkGamepad2Input();
  136.  
  137.             base.Update(gameTime);
  138.         }
  139.  
  140.         // This method will process all keyboard input and move the cat accordingly.
  141.         // The student will complete this method.
  142.         private void checkKeyboardInput()
  143.         {
  144.             KeyboardState currentKeyState = Keyboard.GetState();
  145.             if (oldKeyState == null)
  146.                 oldKeyState = currentKeyState;
  147.  
  148.             if (currentKeyState.IsKeyDown(Keys.A))
  149.             {
  150.                 catLocation.X--;
  151.             }
  152.  
  153.             if (currentKeyState.IsKeyDown(Keys.D))
  154.             {
  155.                 catLocation.X++;
  156.             }
  157.  
  158.             if (currentKeyState.IsKeyDown(Keys.S))
  159.             {
  160.                 catLocation.Y++;
  161.             }
  162.  
  163.             if (currentKeyState.IsKeyDown(Keys.W))
  164.             {
  165.                 catLocation.Y--;
  166.             }
  167.         }
  168.  
  169.         // This method will process all mouse input and move the mouse accordingly.
  170.         // The student will complete this method.
  171.         private void checkMouseInput()
  172.         {
  173.             MouseState currentMouseState = Mouse.GetState();
  174.             if (oldMouseState == null)
  175.                 oldMouseState = currentMouseState;
  176.  
  177.             Vector2 mouseCenter = new Vector2(mouseLocation.X + mouseTexture.Width / 2, mouseLocation.Y + mouseTexture.Height / 2);
  178.  
  179.             if (currentMouseState.X > mouseCenter.X)
  180.             {
  181.                 mouseLocation.X++;
  182.             }
  183.  
  184.             if (currentMouseState.X < mouseLocation.X)
  185.             {
  186.                 mouseLocation.X--;
  187.             }
  188.             if (currentMouseState.Y < mouseCenter.Y)
  189.             {
  190.                 mouseLocation.Y--;
  191.             }
  192.  
  193.             if (currentMouseState.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton == ButtonState.Released)
  194.             {
  195.                 mouseLocation.X = random.Next(0, screenWidth);
  196.                 mouseLocation.Y = random.Next(0, screenHeight);
  197.             }
  198.  
  199.             oldMouseState = currentMouseState;
  200.         }
  201.  
  202.         //***************************************************************************************************
  203.         // The following two methods can be completed by the student if they would like to use
  204.         // one or two Xbox Controller gamepads. If the student is not using gamepads, they will not
  205.         // need to complete these methods
  206.  
  207.         // This method will be completed if the student is using a gamepad for player 1 (the cat)
  208.         private void checkGamepad1Input()
  209.         {
  210.         }
  211.  
  212.  
  213.         // This method will be completed if the student is using a gamepad for player 2 (the mouse)
  214.         private void checkGamepad2Input()
  215.         {
  216.         }
  217.         //****************************************************************************************************************************
  218.  
  219.                 /// <summary>
  220.         /// This is called when the game should draw itself.
  221.         /// </summary>
  222.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  223.         // This method is provided complete as part of the activity starter
  224.         protected override void Draw(GameTime gameTime)
  225.         {
  226.             GraphicsDevice.Clear(Color.LightBlue);//CornflowerBlue);
  227.  
  228.             // start the spritebatch
  229.             spriteBatch.Begin();
  230.  
  231.             // draw the cat and mouse textures at their current location
  232.             spriteBatch.Draw(mouseTexture, mouseLocation, Color.White);
  233.             spriteBatch.Draw(catTexture, catLocation, Color.White);
  234.  
  235.             // all done drawing
  236.             spriteBatch.End();
  237.  
  238.             base.Draw(gameTime);
  239.         }
  240.     }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement