Advertisement
Guest User

Left 4 Cow

a guest
May 17th, 2012
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.76 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 RectangleCollision
  13. {
  14.     /// <summary>
  15.     /// This is the main type for your game
  16.     /// </summary>
  17.     public class RectangleCollisionGame : Microsoft.Xna.Framework.Game
  18.     {
  19.         GraphicsDeviceManager graphics;
  20.        
  21.        
  22.         // The images we will draw
  23.         Texture2D personTexture;
  24.         Texture2D blockTexture;
  25.         Texture2D StartScreen;
  26.         Texture2D GameOver;
  27.         Texture2D mMessageBox;
  28.         Texture2D Pause;
  29.         Color mPauseColor = Color.MediumSlateBlue;
  30.        
  31.  
  32.         // The images will be drawn with this SpriteBatch
  33.         SpriteBatch spriteBatch;
  34.         SpriteBatch spriteBatchIntro;
  35.         SpriteBatch spriteBatchPaused;
  36.         SpriteBatch spriteBatchEnd;
  37.        
  38.  
  39.         // Person
  40.         public Vector2 personPosition = new Vector2(600, 600);
  41.         const int PersonMoveSpeed = 5;
  42.  
  43.         // Blocks
  44.         List<Vector2> blockPositions = new List<Vector2>();
  45.  
  46.         public Vector2 IntroStartScreen = new Vector2(200, 0);
  47.        
  48.         float BlockSpawnProbability = 0.07f;
  49.        
  50.        
  51.        
  52.         const int BlockFallSpeed = 3;
  53.         private SpriteFont mText;
  54.         private SpriteFont wText;
  55.        
  56.        
  57.  
  58.         private enum GameState
  59.         {
  60.             Intro,
  61.             Playing,
  62.             Paused,
  63.             GameOver
  64.         }
  65.  
  66.         private GameState mCurrentState = GameState.Intro;
  67.  
  68.         Random random = new Random();
  69.  
  70.         // For when a collision is detected
  71.         bool personHit = false;
  72.  
  73.         // The sub-rectangle of the drawable area which should be visible on all TVs
  74.         Rectangle safeBounds;
  75.         // Percentage of the screen on every side is the safe area
  76.         const float SafeAreaPortion = 0.05f;
  77.  
  78.  
  79.         public RectangleCollisionGame()
  80.         {
  81.             graphics = new GraphicsDeviceManager(this);
  82.             Content.RootDirectory = "Content";
  83.             graphics.PreferredBackBufferWidth = 1024;  // set this value to the desired width of your window
  84.             graphics.PreferredBackBufferHeight = 800;   // set this value to the desired height of your window
  85.             graphics.ApplyChanges();
  86.         }
  87.  
  88.  
  89.         /// <summary>
  90.         /// Allows the game to perform any initialization it needs to before starting to
  91.         /// run. This is where it can query for any required services and load any
  92.         /// non-graphic related content.  Calling base.Initialize will enumerate through
  93.         /// any components and initialize them as well.
  94.         /// </summary>
  95.         protected override void Initialize()
  96.         {
  97.             base.Initialize();
  98.  
  99.             mCurrentState = GameState.Intro;
  100.             blockTexture.GraphicsDevice.Reset();        
  101.  
  102.             if (mCurrentState == GameState.Intro)
  103.             {
  104.                 mText = Content.Load<SpriteFont>("FYF");
  105.                 wText = Content.Load<SpriteFont>("FYF2");
  106.             }
  107.  
  108.            
  109.            
  110.                 // Calculate safe bounds based on current resolution
  111.                 Viewport viewport = graphics.GraphicsDevice.Viewport;
  112.                 safeBounds = new Rectangle(
  113.                     (int)(viewport.Width * SafeAreaPortion),
  114.                     (int)(viewport.Height * SafeAreaPortion),
  115.                     (int)(viewport.Width * (1 - 2 * SafeAreaPortion)),
  116.                     (int)(viewport.Height * (1 - 2 * SafeAreaPortion)));
  117.         }
  118.  
  119.            
  120.  
  121.         /// <summary>
  122.         /// Load your graphics content.
  123.         /// </summary>
  124.         protected override void LoadContent()
  125.         {
  126.             // Load textures
  127.              StartScreen = Content.Load<Texture2D>("StartScreen");
  128.              blockTexture = Content.Load<Texture2D>("CowMan");
  129.              personTexture = Content.Load<Texture2D>("Main character");
  130.              GameOver = Content.Load<Texture2D>("GameOver");
  131.              mMessageBox = Content.Load<Texture2D>("MessageBox");
  132.              Pause = Content.Load<Texture2D>("Pause");
  133.  
  134.  
  135.                 // Create a sprite batch to draw those textures
  136.                 spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
  137.                 spriteBatchIntro = new SpriteBatch(graphics.GraphicsDevice);
  138.                 spriteBatchPaused = new SpriteBatch(graphics.GraphicsDevice);
  139.                 spriteBatchEnd = new SpriteBatch(graphics.GraphicsDevice);
  140.            
  141.         }
  142.  
  143.  
  144.         /// <summary>
  145.         /// Allows the game to run logic such as updating the world,
  146.         /// checking for collisions, gathering input and playing audio.
  147.         /// </summary>
  148.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  149.         protected override void Update(GameTime gameTime)
  150.         {
  151.             // Get input
  152.             KeyboardState keyboard = Keyboard.GetState();
  153.             GamePadState gamePad = GamePad.GetState(PlayerIndex.One);
  154.  
  155.             // Allows the game to exit
  156.             if (mCurrentState == GameState.Playing)
  157.             {
  158.                 if (gamePad.Buttons.Back == ButtonState.Pressed ||
  159.                     keyboard.IsKeyDown(Keys.Escape))
  160.                 {
  161.                     this.Exit();
  162.                 }
  163.  
  164.  
  165.                 // Move the player left and right with arrow keys or d-pad
  166.  
  167.                 if (keyboard.IsKeyDown(Keys.Left) ||
  168.                 gamePad.DPad.Left == ButtonState.Pressed)
  169.                 {
  170.                     personPosition.X -= PersonMoveSpeed;
  171.                 }
  172.  
  173.                 if (keyboard.IsKeyDown(Keys.Right) ||
  174.                     gamePad.DPad.Right == ButtonState.Pressed)
  175.                 {
  176.                     personPosition.X += PersonMoveSpeed;
  177.                 }
  178.                 if (keyboard.IsKeyDown(Keys.Up))
  179.                 {
  180.                     personPosition.Y -= PersonMoveSpeed;
  181.                 }
  182.                 if (keyboard.IsKeyDown(Keys.Down))
  183.                 {
  184.                     personPosition.Y += PersonMoveSpeed;
  185.                 }
  186.  
  187.  
  188.  
  189.                 // Prevent the person from moving off of the screen
  190.                 personPosition.X = MathHelper.Clamp(personPosition.X,
  191.                     safeBounds.Left, safeBounds.Right - personTexture.Width);
  192.  
  193.                 // Spawn new falling blocks
  194.                 if (random.NextDouble() < BlockSpawnProbability)
  195.                 {
  196.                     float x = (float)random.NextDouble() *
  197.                         (Window.ClientBounds.Width - blockTexture.Width);
  198.                     blockPositions.Add(new Vector2(x, -blockTexture.Height));
  199.                 }
  200.  
  201.  
  202.                 // Get the bounding rectangle of the person
  203.                 Rectangle personRectangle =
  204.                     new Rectangle((int)personPosition.X, (int)personPosition.Y,
  205.                     personTexture.Width, personTexture.Height);
  206.  
  207.                 // Update each block
  208.                 personHit = false;
  209.                 for (int i = 0; i < blockPositions.Count; i++)
  210.                 {
  211.                     // Animate this block falling
  212.                     blockPositions[i] =
  213.                         new Vector2(blockPositions[i].X,
  214.                                     blockPositions[i].Y + BlockFallSpeed);
  215.  
  216.                     Rectangle blockRectangle =
  217.                             new Rectangle((int)blockPositions[i].X, (int)blockPositions[i].Y,
  218.                             blockTexture.Width, blockTexture.Height);
  219.  
  220.                     // Check collision with person
  221.                     if (personRectangle.Intersects(blockRectangle))
  222.                         personHit = true;
  223.  
  224.  
  225.  
  226.                     // Remove this block if it have fallen off the screen
  227.                     if (blockPositions[i].Y > Window.ClientBounds.Height)
  228.                     {
  229.                         blockPositions.RemoveAt(i);
  230.                         // When removing a block, the next block will have the same index
  231.                         // as the current block. Decrement i to prevent skipping a block.
  232.                         i--;
  233.                     }
  234.                 }
  235.             }
  236.  
  237.             base.Update(gameTime);
  238.  
  239.             if (keyboard.IsKeyDown(Keys.Space) == true)
  240.             {
  241.                 if (mCurrentState == GameState.Intro)
  242.                 {
  243.                    
  244.                     mCurrentState = GameState.Playing;
  245.                 }
  246.                 else if (mCurrentState == GameState.GameOver)
  247.                 {
  248.                     Initialize();
  249.                    
  250.                     mCurrentState = GameState.Intro;
  251.                 }
  252.             }
  253.  
  254.  
  255.             if (keyboard.IsKeyDown(Keys.P))
  256.             {
  257.                 if (mCurrentState == GameState.Playing)
  258.                 {
  259.                     blockTexture.GraphicsDevice.Reset();
  260.                     mCurrentState = GameState.Paused;
  261.                 }
  262.             }
  263.  
  264.             if (keyboard.IsKeyDown(Keys.Space))
  265.             {
  266.                 if (mCurrentState == GameState.Paused)
  267.                 {
  268.  
  269.                     mCurrentState = GameState.Playing;
  270.                 }
  271.             }
  272.  
  273.             if (keyboard.IsKeyDown(Keys.Escape))
  274.             {
  275.                 if (mCurrentState == GameState.Intro)
  276.                 {
  277.                     this.Exit();
  278.                 }
  279.  
  280.             }
  281.         }
  282.        
  283.        
  284.                
  285.  
  286.  
  287.         /// <summary>
  288.         /// This is called when the game should draw itself.
  289.         /// </summary>
  290.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  291.         protected override void Draw(GameTime gameTime)
  292.         {
  293.             GraphicsDevice device = graphics.GraphicsDevice;
  294.  
  295.             // Change the background to red when the person was hit by a block
  296.  
  297.  
  298.             if (mCurrentState == GameState.Intro)
  299.             {
  300.                 spriteBatchIntro.Begin();
  301.  
  302.                 spriteBatchIntro.Draw(StartScreen, IntroStartScreen, Color.White);
  303.                 spriteBatchIntro.DrawString(mText, "Press 'Space' to start the Infection!", new Vector2(150, 700), Color.White);
  304.                 spriteBatchIntro.End();
  305.             }
  306.  
  307.             if (mCurrentState == GameState.GameOver)
  308.             {
  309.                 spriteBatchEnd.Begin();
  310.                 spriteBatchEnd.Draw(GameOver, new Vector2(0, 0), Color.White);
  311.                 spriteBatchEnd.DrawString(mText, "HAHAHAHA you lost", new Vector2(150, 700), Color.White);
  312.                 device.Clear(Color.Black);
  313.                 spriteBatchEnd.End();
  314.             }
  315.  
  316.             if (mCurrentState == GameState.Paused)
  317.             {
  318.                 spriteBatchPaused.Begin();
  319.                 spriteBatchPaused.Draw(Pause, new Vector2(200, 300), Color.White);
  320.                 spriteBatchPaused.End();
  321.             }
  322.  
  323.            if (mCurrentState == GameState.Playing)
  324.             {
  325.                 spriteBatch.Begin();
  326.                 if (personHit)
  327.                 {
  328.                  mCurrentState = GameState.GameOver;
  329.                 }
  330.                 else
  331.                 {
  332.                     device.Clear(Color.CornflowerBlue);
  333.                 }
  334.                 // Draw person
  335.                 spriteBatch.Draw(personTexture, personPosition, Color.White);
  336.  
  337.                 // Draw blocks
  338.                 foreach (Vector2 blockPosition in blockPositions)
  339.                     spriteBatch.Draw(blockTexture, blockPosition, Color.White);
  340.  
  341.                 spriteBatch.End();
  342.             }
  343.  
  344.             base.Draw(gameTime);
  345.         }
  346.     }
  347. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement