Guest User

Untitled

a guest
Jun 24th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.28 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.        
  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.  
  90.             /*
  91.             origin.X = squareTexture.Width / 2;
  92.             origin.Y = squareTexture.Height / 2;
  93.             screenpos.X = viewport.Width / 2;
  94.             screenpos.Y = viewport.Height / 2;
  95.             */
  96.             font = Content.Load<SpriteFont>("SpriteFont1");
  97.  
  98.             MediaPlayer.IsRepeating = true;
  99.             MediaPlayer.Play(soundEngine);
  100.  
  101.             titleScreen = Content.Load<Texture2D>(@"TitleScreen");
  102.  
  103.  
  104.             // TODO: use this.Content to load your game content here
  105.         }
  106.  
  107.         /// <summary>
  108.         /// UnloadContent will be called once per game and is the place to unload
  109.         /// all content.
  110.         /// </summary>
  111.         protected override void UnloadContent()
  112.         {
  113.             // TODO: Unload any non ContentManager content here
  114.         }
  115.  
  116.         /// <summary>
  117.         /// Allows the game to run logic such as updating the world,
  118.         /// checking for collisions, gathering input, and playing audio.
  119.         /// </summary>
  120.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  121.         ///
  122.  
  123.         protected override void Update(GameTime gameTime)
  124.         {
  125.             // Allows the game to exit
  126.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  127.                 this.Exit();
  128.  
  129.  
  130.             float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  131.  
  132.             // TODO: Add your game logic here.
  133.             RotationAngle += elapsed;
  134.             float circle = MathHelper.Pi * 2;
  135.             RotationAngle = RotationAngle % circle;
  136.  
  137.             // TODO: Add your update logic here
  138.  
  139.             switch (gameState)
  140.             {
  141.                 case GameStates.TitleScreen:
  142.                     if (Keyboard.GetState().IsKeyDown(Keys.Space))
  143.                     {
  144.                         playerScore = 0;
  145.                         gameState = GameStates.Playing;
  146.                     }
  147.                     break;
  148.  
  149.                 case GameStates.Playing:
  150.  
  151.  
  152.  
  153.  
  154.                     if (timeRemaining == 0.0f && playerScore <= 3)
  155.                     {
  156.                         currentSquare = new Rectangle(
  157.                             rand.Next(0, this.Window.ClientBounds.Width - 55),
  158.                             rand.Next(0, this.Window.ClientBounds.Height - 55),
  159.                             75, 75);
  160.                         timeRemaining = TimePerSquare;
  161.                     }
  162.  
  163.                     if (timeRemaining == 0.0f && playerScore > 3 && playerScore <= 6)
  164.                     {
  165.                         currentSquare = new Rectangle(
  166.                             rand.Next(0, this.Window.ClientBounds.Width - 55),
  167.                             rand.Next(0, this.Window.ClientBounds.Height - 55),
  168.                             55, 55);
  169.                         timeRemaining = TimePerSquare - (TimePerSquare * .50f);
  170.                     }
  171.  
  172.                     if (timeRemaining == 0.0f && playerScore > 6 && playerScore <= 9)
  173.                     {
  174.                         currentSquare = new Rectangle(
  175.                             rand.Next(0, this.Window.ClientBounds.Width - 55),
  176.                             rand.Next(0, this.Window.ClientBounds.Height - 55),
  177.                             25, 25);
  178.                         timeRemaining = TimePerSquare - (TimePerSquare * .70f);
  179.                     }
  180.  
  181.                     if (timeRemaining == 0.0f && playerScore > 9)
  182.                     {
  183.                         currentSquare = new Rectangle(
  184.                             rand.Next(0, this.Window.ClientBounds.Width - 55),
  185.                             rand.Next(0, this.Window.ClientBounds.Height - 55),
  186.                             5, 5);
  187.                         timeRemaining = TimePerSquare;
  188.                     }
  189.  
  190.  
  191.                     MouseState mouse = Mouse.GetState();
  192.  
  193.                     if ((mouse.LeftButton == ButtonState.Pressed) &&
  194.                       (currentSquare.Contains(mouse.X, mouse.Y)))
  195.                     {
  196.  
  197.                         playerScore++;
  198.                         timeRemaining = 0.0f;
  199.  
  200.  
  201.                         if (playerScore > 3 && playerScore <= 6)
  202.                         {
  203.                             currentSquare = new Rectangle(
  204.                                 rand.Next(0, this.Window.ClientBounds.Width - 55),
  205.                                 rand.Next(0, this.Window.ClientBounds.Height - 55),
  206.                                 55, 55);
  207.                             timeRemaining = TimePerSquare - (TimePerSquare * .50f);
  208.                         }
  209.  
  210.                         if (playerScore > 6 && playerScore <= 9)
  211.                         {
  212.                             currentSquare = new Rectangle(rand.Next(0, this.Window.ClientBounds.Width - 55),
  213.                             rand.Next(0, this.Window.ClientBounds.Height - 55),
  214.                             25, 25);
  215.                             timeRemaining = TimePerSquare - (TimePerSquare * .70f);
  216.                         }
  217.  
  218.                         else if (playerScore > 9)
  219.                         {
  220.                             currentSquare = new Rectangle(rand.Next(0, this.Window.ClientBounds.Width - 55),
  221.                             rand.Next(0, this.Window.ClientBounds.Height - 55),
  222.                             5, 5);
  223.                             timeRemaining = TimePerSquare;
  224.                         }
  225.  
  226.  
  227.  
  228.  
  229.                     }
  230.  
  231.                     timeRemaining = MathHelper.Max(0, timeRemaining -
  232.                         (float)gameTime.ElapsedGameTime.TotalSeconds);
  233.  
  234.  
  235.  
  236.                     this.Window.Title = "Score : " + playerScore.ToString();
  237.  
  238.                     break;
  239.             }
  240.             base.Update(gameTime);
  241.         }
  242.  
  243.  
  244.  
  245.         /// <summary>
  246.         /// This is called when the game should draw itself.
  247.         /// </summary>
  248.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  249.         protected override void Draw(GameTime gameTime)
  250.         {
  251.  
  252.             GraphicsDevice.Clear(Color.Black);
  253.  
  254.             origin.X = squareTexture.Width / 2;
  255.             origin.Y = squareTexture.Height / 2;
  256.             // TODO: Add your drawing code here
  257.  
  258.            
  259.  
  260.             if (gameState == GameStates.Playing)
  261.             {
  262.                /* spriteBatch.Begin();
  263.                 spriteBatch.Draw(
  264.                     squareTexture,
  265.                     currentSquare,
  266.                     colors[playerScore % 3]);
  267.                                  */
  268.                 spriteBatch.Begin();
  269.                  spriteBatch.Draw (
  270.          squareTexture,
  271.          currentSquare,
  272.          null,
  273.          colors[playerScore % 3],
  274.          RotationAngle,
  275.          origin,
  276.          0,
  277.          0
  278. );
  279.  
  280.                 /*spriteBatch.Draw(squareTexture, screenpos, null, Color.White, RotationAngle,
  281.         origin, 1.0f, SpriteEffects.None, 0f); */
  282.  
  283.                 if (playerScore > 9)
  284.                 {
  285.                     spriteBatch.DrawString(font, "You Win HOme Slice@!", new Vector2(5.0f, 1.0f), Color.White);
  286.                 }
  287.  
  288.                 spriteBatch.End();
  289.             }
  290.             if (gameState == GameStates.TitleScreen)
  291.             {
  292.                 if (gameState == GameStates.TitleScreen)
  293.                 {
  294.                     spriteBatch.Begin();
  295.                     spriteBatch.Draw(titleScreen,
  296.                     new Rectangle(0, 0,
  297.                     this.Window.ClientBounds.Width,
  298.                     this.Window.ClientBounds.Height),
  299.                     Color.White);
  300.                    
  301.                     spriteBatch.End();
  302.                 }
  303.             }
  304.             base.Draw(gameTime);
  305.         }
  306.  
  307.  
  308.     }
  309. }
Add Comment
Please, Sign In to add comment