Advertisement
rohits134

Catching Squares

Jul 3rd, 2012
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Audio;
  7. using Microsoft.Xna.Framework.Content;
  8. using Microsoft.Xna.Framework.GamerServices;
  9. using Microsoft.Xna.Framework.Graphics;
  10. using Microsoft.Xna.Framework.Input;
  11. using Microsoft.Xna.Framework.Media;
  12.  
  13. namespace Square_Chase
  14. {
  15.     /// <summary>
  16.     /// This is the main type for your game
  17.     /// </summary>
  18.     public class Game1 : Microsoft.Xna.Framework.Game
  19.     {
  20.         [DllImport("user32.dll", CharSet = CharSet.Auto)]
  21.         public static extern uint MessageBox(IntPtr hWnd, String text, String caption, uint type);
  22.         GraphicsDeviceManager graphics;
  23.         SpriteBatch spriteBatch;
  24.         Random rand=new Random();
  25.         Texture2D squareTexture;
  26.         Rectangle currentSquare;
  27.         int playerScore = 0;
  28.         float timeRemaining = 0.0f;
  29.         const float TimePerSquare = 0.75f;
  30.         Color[] colors = new Color[3] { Color.Red, Color.Green,Color.Blue };
  31.         public Game1()
  32.         {
  33.             graphics = new GraphicsDeviceManager(this);
  34.             Content.RootDirectory = "Content";
  35.         }
  36.  
  37.         /// <summary>
  38.         /// Allows the game to perform any initialization it needs to before starting to run.
  39.         /// This is where it can query for any required services and load any non-graphic
  40.         /// related content.  Calling base.Initialize will enumerate through any components
  41.         /// and initialize them as well.
  42.         /// </summary>
  43.         protected override void Initialize()
  44.         {
  45.             // TODO: Add your initialization logic here
  46.             this.IsMouseVisible = true;
  47.             base.Initialize();
  48.         }
  49.  
  50.         /// <summary>
  51.         /// LoadContent will be called once per game and is the place to load
  52.         /// all of your content.
  53.         /// </summary>
  54.         protected override void LoadContent()
  55.         {
  56.             // Create a new SpriteBatch, which can be used to draw textures.
  57.             spriteBatch = new SpriteBatch(GraphicsDevice);
  58.             squareTexture = Content.Load<Texture2D>(@"SQUARE");
  59.             // TODO: use this.Content to load your game content here
  60.         }
  61.  
  62.         /// <summary>
  63.         /// UnloadContent will be called once per game and is the place to unload
  64.         /// all content.
  65.         /// </summary>
  66.         protected override void UnloadContent()
  67.         {
  68.             // TODO: Unload any non ContentManager content here
  69.         }
  70.  
  71.         /// <summary>
  72.         /// Allows the game to run logic such as updating the world,
  73.         /// checking for collisions, gathering input, and playing audio.
  74.         /// </summary>
  75.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  76.         protected override void Update(GameTime gameTime)
  77.         {
  78.             // Allows the game to exit
  79.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  80.                 this.Exit();
  81.             if (gameTime.TotalGameTime.TotalSeconds > 10)
  82.             {
  83.                 int msg = playerScore;
  84.                 MessageBox(new IntPtr(0), Convert.ToString(msg), "Score", 0);
  85.                 this.Exit();
  86.             }
  87.             // TODO: Add your update logic here
  88.             if (timeRemaining == 0.0f)
  89.             {
  90.                 currentSquare = new Rectangle(
  91.                 rand.Next(0, this.Window.ClientBounds.Width - 25),
  92.                 rand.Next(0, this.Window.ClientBounds.Height - 25),
  93.                 25, 25);
  94.                 timeRemaining = TimePerSquare;
  95.             }
  96.             MouseState mouse = Mouse.GetState();
  97.             if ((mouse.LeftButton == ButtonState.Pressed) &&
  98.             (currentSquare.Contains(mouse.X, mouse.Y)))
  99.             {
  100.                 playerScore++;
  101.                 timeRemaining = 0.0f;
  102.             }
  103.             timeRemaining = MathHelper.Max(0, timeRemaining -
  104.             (float)gameTime.ElapsedGameTime.TotalSeconds);
  105.             this.Window.Title = "Score : " + playerScore.ToString();
  106.             base.Update(gameTime);
  107.         }
  108.  
  109.         /// <summary>
  110.         /// This is called when the game should draw itself.
  111.         /// </summary>
  112.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  113.         protected override void Draw(GameTime gameTime)
  114.         {
  115.             GraphicsDevice.Clear(Color.CornflowerBlue);
  116.             spriteBatch.Begin();
  117.             spriteBatch.Draw(
  118.             squareTexture,
  119.             currentSquare,
  120.             colors[playerScore % 3]);
  121.             spriteBatch.End();
  122.             // TODO: Add your drawing code here
  123.  
  124.             base.Draw(gameTime);
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement