Advertisement
Guest User

AntGame

a guest
Apr 16th, 2014
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.97 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 AntGame
  13. {
  14.  
  15.     /// <summary>
  16.     /// This is the main type for your game
  17.     /// </summary>
  18.     ///
  19.     public class Game1 : Microsoft.Xna.Framework.Game
  20.     {
  21.  
  22.         private KeyboardState keyCurrent;
  23.         private GamePadState myGamePad;
  24.         private GraphicsDeviceManager graphics;
  25.         private SpriteBatch spriteBatch;
  26.         static public MouseState mouseState;
  27.         static public MouseState previousState;
  28.         static public Vector2 pos;
  29.         static public Vector2 spawn;
  30.         private Texture2D foodImage;
  31.         private Texture2D hillImage;
  32.         static public bool leftClickOccurred = false;
  33.         static public bool rightClickOccurred = false;
  34.         static public bool placeFood = false;
  35.         static public bool placeHill = false;
  36.  
  37.         public Game1()
  38.         {
  39.             graphics = new GraphicsDeviceManager(this);
  40.             Content.RootDirectory = "Content";
  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.             // TODO: use this.Content to load your game content here
  66.  
  67.             foodImage = Content.Load<Texture2D>("images\\Food");
  68.             hillImage = Content.Load<Texture2D>("images\\AntHill");
  69.         }
  70.  
  71.         /// <summary>
  72.         /// UnloadContent will be called once per game and is the place to unload
  73.         /// all content.
  74.         /// </summary>
  75.         protected override void UnloadContent()
  76.         {
  77.             // TODO: Unload any non ContentManager content here
  78.         }
  79.  
  80.         /// <summary>
  81.         /// Allows the game to run logic such as updating the world,
  82.         /// checking for collisions, gathering input, and playing audio.
  83.         /// </summary>
  84.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  85.  
  86.         protected override void Update(GameTime gameTime)
  87.         {
  88.             previousState = mouseState;
  89.             mouseState = Mouse.GetState(); //Needed to find the most current mouse states.
  90.             pos.X = mouseState.X; //Change x pos to mouseX
  91.             pos.Y = mouseState.Y; //Change y pos to mouseY
  92.             Mouse.WindowHandle = Window.Handle;
  93.  
  94.             if (previousState.LeftButton == ButtonState.Released && mouseState.LeftButton == ButtonState.Pressed)
  95.             {
  96.                 // React to the click
  97.                 leftClickOccurred = true;
  98.             }
  99.             if (previousState.RightButton == ButtonState.Released && mouseState.RightButton == ButtonState.Pressed)
  100.             {
  101.                 // React to the click
  102.                 rightClickOccurred = true;
  103.             }
  104.  
  105.             myGamePad = GamePad.GetState(PlayerIndex.One);
  106.  
  107.             if (myGamePad.IsConnected)
  108.             {
  109.                 ProcessGamePad();
  110.             }
  111.             else
  112.             {
  113.                 ProcessKeyboard();
  114.             }
  115.  
  116.             base.Update(gameTime);
  117.         }
  118.  
  119.  
  120.         private void ProcessKeyboard()
  121.         {
  122.             keyCurrent = Keyboard.GetState();
  123.  
  124.             if (keyCurrent.IsKeyDown(Keys.Escape))
  125.                 this.Exit();
  126.                 // used for debugging
  127.                 // Console.WriteLine(" {0} " ,  value);
  128.         }
  129.  
  130.         private void ProcessGamePad()
  131.         {
  132.             // Allows the game to exit
  133.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  134.                 this.Exit();
  135.         }
  136.  
  137.         /// <summary>
  138.         /// This is called when the game should draw itself.
  139.         /// </summary>
  140.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  141.  
  142.         protected override void Draw(GameTime gameTime)
  143.         {
  144.             GraphicsDevice.Clear(Color.CornflowerBlue);
  145.  
  146.             if (leftClickOccurred)
  147.             {
  148.                 spawn.X = pos.X; //Change spawn position X to current mouse position
  149.                 spawn.Y = pos.Y; //Change spawn position Y to current mouse position
  150.  
  151.                 placeFood = true;
  152.  
  153.                 leftClickOccurred = false;
  154.             }
  155.  
  156.             if (placeFood)
  157.             {
  158.                 spriteBatch.Begin();
  159.  
  160.                 spriteBatch.Draw(foodImage, new Vector2(spawn.X, spawn.Y), Color.Brown);
  161.  
  162.                 spriteBatch.End();
  163.             }
  164.  
  165.             if (rightClickOccurred)
  166.             {
  167.                 spawn.X = pos.X; //Change spawn position X to current mouse position
  168.                 spawn.Y = pos.Y; //Change spawn position Y to current mouse position
  169.  
  170.                 placeHill = true;
  171.  
  172.                 rightClickOccurred = false;
  173.             }
  174.  
  175.             if (placeHill)
  176.             {
  177.                 spriteBatch.Begin();
  178.  
  179.                 spriteBatch.Draw(hillImage, new Vector2(spawn.X, spawn.Y), Color.Brown);
  180.  
  181.                 spriteBatch.End();
  182.             }
  183.  
  184.             base.Draw(gameTime);
  185.         }
  186.  
  187.  
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement