Advertisement
Guest User

Untitled

a guest
Feb 18th, 2012
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.29 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 SquareChase
  13. {
  14.     /// <summary>
  15.     /// This is the main type for your game
  16.     /// </summary>
  17.     public class Game1 : Microsoft.Xna.Framework.Game
  18.     {
  19.  
  20.         Song soundEngine;
  21.  
  22.         GraphicsDeviceManager graphics;
  23.         SpriteBatch spriteBatch;
  24.         SpriteFont font;
  25.  
  26.         private Vector2 origin;
  27.         private Vector2 screenpos;
  28.  
  29.        
  30.  
  31.         private float RotationAngle;
  32.  
  33.  
  34.         enum GameStates { TitleScreen, Playing };
  35.         GameStates gameState = GameStates.TitleScreen;
  36.  
  37.         Random rand = new Random();
  38.         Texture2D titleScreen;
  39.         Texture2D squareTexture;
  40.         Rectangle currentSquare;
  41.         int playerScore = 0;
  42.         float timeRemaining = 0.0f;
  43.         float TimePerSquare = 2f;
  44.  
  45.         Color[] colors = new Color[3] { Color.Red, Color.Green, Color.Blue };
  46.  
  47.         public Game1()
  48.         {
  49.             graphics = new GraphicsDeviceManager(this);
  50.             // graphics.IsFullScreen = true;   <---------add this for full screen
  51.             Content.RootDirectory = "Content";
  52.         }
  53.  
  54.         /// <summary>
  55.         /// Allows the game to perform any initialization it needs to before starting to run.
  56.         /// This is where it can query for any required services and load any non-graphic
  57.         /// related content.  Calling base.Initialize will enumerate through any components
  58.         /// and initialize them as well.
  59.         /// </summary>
  60.         protected override void Initialize()
  61.         {
  62.             // TODO: Add your initialization logic here
  63.             this.IsMouseVisible = true;
  64.  
  65.             base.Initialize();
  66.         }
  67.  
  68.         /// <summary>
  69.         /// LoadContent will be called once per game and is the place to load
  70.         /// all of your content.
  71.         /// </summary>
  72.         /// private Texture2D SpriteTexture;
  73.  
  74.  
  75.  
  76.         protected override void LoadContent()
  77.         {
  78.             // Create a new SpriteBatch, which can be used to draw textures.
  79.             spriteBatch = new SpriteBatch(GraphicsDevice);
  80.  
  81.  
  82.  
  83.             squareTexture = Content.Load<Texture2D>(@"SQUARE");
  84.             soundEngine = Content.Load<Song>(@"chippy");
  85.  
  86.             Viewport viewport = graphics.GraphicsDevice.Viewport;
  87.             origin.X = squareTexture.Width / 2;
  88.             origin.Y = squareTexture.Height / 2;
  89.             screenpos.X = viewport.Width / 2;
  90.             screenpos.Y = viewport.Height / 2;
  91.             /*
  92.             origin.X = squareTexture.Width / 2;
  93.             origin.Y = squareTexture.Height / 2;
  94.             screenpos.X = viewport.Width / 2;
  95.             screenpos.Y = viewport.Height / 2;
  96.             */
  97.             font = Content.Load<SpriteFont>("SpriteFont1");
  98.  
  99.             MediaPlayer.IsRepeating = true;
  100.             MediaPlayer.Play(soundEngine);
  101.  
  102.             titleScreen = Content.Load<Texture2D>(@"TitleScreen");
  103.  
  104.  
  105.             // TODO: use this.Content to load your game content here
  106.         }
  107.  
  108.         /// <summary>
  109.         /// UnloadContent will be called once per game and is the place to unload
  110.         /// all content.
  111.         /// </summary>
  112.         protected override void UnloadContent()
  113.         {
  114.             // TODO: Unload any non ContentManager content here
  115.         }
  116.  
  117.         /// <summary>
  118.         /// Allows the game to run logic such as updating the world,
  119.         /// checking for collisions, gathering input, and playing audio.
  120.         /// </summary>
  121.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  122.         ///
  123.  
  124.         protected override void Update(GameTime gameTime)
  125.         {
  126.             // Allows the game to exit
  127.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  128.                 this.Exit();
  129.  
  130.  
  131.             float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  132.  
  133.             // TODO: Add your game logic here.
  134.             RotationAngle += elapsed;
  135.             float circle = MathHelper.Pi * 2;
  136.             RotationAngle = RotationAngle % circle;
  137.  
  138.             // TODO: Add your update logic here
  139.  
  140.             switch (gameState)
  141.             {
  142.                 case GameStates.TitleScreen:
  143.                     if (Keyboard.GetState().IsKeyDown(Keys.Space))
  144.                     {
  145.                         playerScore = 0;
  146.                         gameState = GameStates.Playing;
  147.                     }
  148.                     break;
  149.  
  150.                 case GameStates.Playing:
  151.  
  152.  
  153.  
  154.  
  155.                     if (timeRemaining == 0.0f && playerScore <= 3)
  156.                     {
  157.                         currentSquare = new Rectangle(
  158.                             rand.Next(0, this.Window.ClientBounds.Width - 55),
  159.                             rand.Next(0, this.Window.ClientBounds.Height - 55),
  160.                             75, 75);
  161.                         timeRemaining = TimePerSquare;
  162.                     }
  163.  
  164.                     if (timeRemaining == 0.0f && playerScore > 3 && playerScore <= 6)
  165.                     {
  166.                         currentSquare = new Rectangle(
  167.                             rand.Next(0, this.Window.ClientBounds.Width - 55),
  168.                             rand.Next(0, this.Window.ClientBounds.Height - 55),
  169.                             55, 55);
  170.                         timeRemaining = TimePerSquare - (TimePerSquare * .50f);
  171.                     }
  172.  
  173.                     if (timeRemaining == 0.0f && playerScore > 6 && playerScore <= 9)
  174.                     {
  175.                         currentSquare = new Rectangle(
  176.                             rand.Next(0, this.Window.ClientBounds.Width - 55),
  177.                             rand.Next(0, this.Window.ClientBounds.Height - 55),
  178.                             25, 25);
  179.                         timeRemaining = TimePerSquare - (TimePerSquare * .70f);
  180.                     }
  181.  
  182.                     if (timeRemaining == 0.0f && playerScore > 9)
  183.                     {
  184.                         currentSquare = new Rectangle(
  185.                             rand.Next(0, this.Window.ClientBounds.Width - 55),
  186.                             rand.Next(0, this.Window.ClientBounds.Height - 55),
  187.                             5, 5);
  188.                         timeRemaining = TimePerSquare;
  189.                     }
  190.  
  191.  
  192.                     MouseState mouse = Mouse.GetState();
  193.  
  194.                    
  195.                     if ((mouse.LeftButton == ButtonState.Pressed) &&
  196.                         currentSquare.Contains(mouse.X, mouse.Y))
  197.  
  198.                      
  199.                     {
  200.  
  201.                         playerScore++;
  202.                         timeRemaining = 0.0f;
  203.  
  204.  
  205.                         if (playerScore > 3 && playerScore <= 6)
  206.                         {
  207.                             currentSquare = new Rectangle(
  208.                                 rand.Next(0, this.Window.ClientBounds.Width - 55),
  209.                                 rand.Next(0, this.Window.ClientBounds.Height - 55),
  210.                                 55, 55);
  211.                             timeRemaining = TimePerSquare - (TimePerSquare * .50f);
  212.                         }
  213.  
  214.                         if (playerScore > 6 && playerScore <= 9)
  215.                         {
  216.                             currentSquare = new Rectangle(rand.Next(0, this.Window.ClientBounds.Width - 55),
  217.                             rand.Next(0, this.Window.ClientBounds.Height - 55),
  218.                             25, 25);
  219.                             timeRemaining = TimePerSquare - (TimePerSquare * .70f);
  220.                         }
  221.  
  222.                         else if (playerScore > 9)
  223.                         {
  224.                             currentSquare = new Rectangle(rand.Next(0, this.Window.ClientBounds.Width - 55),
  225.                             rand.Next(0, this.Window.ClientBounds.Height - 55),
  226.                             5, 5);
  227.                             timeRemaining = TimePerSquare;
  228.                         }
  229.  
  230.  
  231.  
  232.  
  233.                     }
  234.  
  235.                     timeRemaining = MathHelper.Max(0, timeRemaining -
  236.                         (float)gameTime.ElapsedGameTime.TotalSeconds);
  237.  
  238.  
  239.  
  240.                     this.Window.Title = "Score : " + playerScore.ToString();
  241.  
  242.                     break;
  243.             }
  244.             base.Update(gameTime);
  245.         }
  246.  
  247.  
  248.  
  249.         /// <summary>
  250.         /// This is called when the game should draw itself.
  251.         /// </summary>
  252.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  253.         protected override void Draw(GameTime gameTime)
  254.         {
  255.  
  256.             GraphicsDevice.Clear(Color.Black);
  257.  
  258.             origin.X = squareTexture.Width / 2;
  259.             origin.Y = squareTexture.Height / 2;
  260.             // TODO: Add your drawing code here
  261.  
  262.  
  263.  
  264.             if (gameState == GameStates.Playing)
  265.             {
  266.              /*    spriteBatch.Begin();
  267.                  spriteBatch.Draw(
  268.                      squareTexture,
  269.                      currentSquare,
  270.                      colors[playerScore % 3]);
  271.                 */
  272.  
  273.                    spriteBatch.Begin();
  274.                    spriteBatch.Draw(
  275.            squareTexture,
  276.            currentSquare,
  277.            null,
  278.            colors[playerScore % 3],
  279.            RotationAngle,
  280.            origin,
  281.            0,
  282.            0
  283.    );
  284.                
  285.  
  286.  
  287.                  if (playerScore > 9)
  288.                 {
  289.                     spriteBatch.DrawString(font, "You Win HOme Slice@!", new Vector2(5.0f, 1.0f), Color.White);
  290.                 }
  291.  
  292.                 spriteBatch.End();
  293.             }
  294.             if (gameState == GameStates.TitleScreen)
  295.             {
  296.                 if (gameState == GameStates.TitleScreen)
  297.                 {
  298.                     spriteBatch.Begin();
  299.                     spriteBatch.Draw(titleScreen,
  300.                     new Rectangle(0, 0,
  301.                     this.Window.ClientBounds.Width,
  302.                     this.Window.ClientBounds.Height),
  303.                     Color.White);
  304.  
  305.                     spriteBatch.End();
  306.                 }
  307.             }
  308.             base.Draw(gameTime);
  309.         }
  310.  
  311.  
  312.     }
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement