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 AntGame
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- ///
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- private KeyboardState keyCurrent;
- private GamePadState myGamePad;
- private GraphicsDeviceManager graphics;
- private SpriteBatch spriteBatch;
- static public MouseState mouseState;
- static public MouseState previousState;
- static public Vector2 pos;
- static public Vector2 spawn;
- private Texture2D foodImage;
- private Texture2D hillImage;
- static public bool leftClickOccurred = false;
- static public bool rightClickOccurred = false;
- static public bool placeFood = false;
- static public bool placeHill = false;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- }
- /// <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);
- // TODO: use this.Content to load your game content here
- foodImage = Content.Load<Texture2D>("images\\Food");
- hillImage = Content.Load<Texture2D>("images\\AntHill");
- }
- /// <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
- }
- /// <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)
- {
- previousState = mouseState;
- mouseState = Mouse.GetState(); //Needed to find the most current mouse states.
- pos.X = mouseState.X; //Change x pos to mouseX
- pos.Y = mouseState.Y; //Change y pos to mouseY
- Mouse.WindowHandle = Window.Handle;
- if (previousState.LeftButton == ButtonState.Released && mouseState.LeftButton == ButtonState.Pressed)
- {
- // React to the click
- leftClickOccurred = true;
- }
- if (previousState.RightButton == ButtonState.Released && mouseState.RightButton == ButtonState.Pressed)
- {
- // React to the click
- rightClickOccurred = true;
- }
- myGamePad = GamePad.GetState(PlayerIndex.One);
- if (myGamePad.IsConnected)
- {
- ProcessGamePad();
- }
- else
- {
- ProcessKeyboard();
- }
- base.Update(gameTime);
- }
- private void ProcessKeyboard()
- {
- keyCurrent = Keyboard.GetState();
- if (keyCurrent.IsKeyDown(Keys.Escape))
- this.Exit();
- // used for debugging
- // Console.WriteLine(" {0} " , value);
- }
- private void ProcessGamePad()
- {
- // Allows the game to exit
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- this.Exit();
- }
- /// <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);
- if (leftClickOccurred)
- {
- spawn.X = pos.X; //Change spawn position X to current mouse position
- spawn.Y = pos.Y; //Change spawn position Y to current mouse position
- placeFood = true;
- leftClickOccurred = false;
- }
- if (placeFood)
- {
- spriteBatch.Begin();
- spriteBatch.Draw(foodImage, new Vector2(spawn.X, spawn.Y), Color.Brown);
- spriteBatch.End();
- }
- if (rightClickOccurred)
- {
- spawn.X = pos.X; //Change spawn position X to current mouse position
- spawn.Y = pos.Y; //Change spawn position Y to current mouse position
- placeHill = true;
- rightClickOccurred = false;
- }
- if (placeHill)
- {
- spriteBatch.Begin();
- spriteBatch.Draw(hillImage, new Vector2(spawn.X, spawn.Y), Color.Brown);
- spriteBatch.End();
- }
- base.Draw(gameTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement