Advertisement
Guest User

Untitled

a guest
Jan 9th, 2013
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using Microsoft.Xna.Framework.Input;
  6.  
  7. namespace BadGame
  8. {
  9.     public struct PlayerData
  10.     {
  11.         public Vector2 Position;
  12.         public Vector2 Velocity;
  13.         public bool IsAlive;
  14.         public bool Win;
  15.         public bool CanJump;
  16.     }
  17.  
  18.     public class Game1 : Microsoft.Xna.Framework.Game
  19.     {
  20.         GraphicsDeviceManager graphics;
  21.         GraphicsDevice device;
  22.  
  23.         SpriteBatch spriteBatch;
  24.         SpriteFont font;
  25.  
  26.         Texture2D backgroundTexture;
  27.         Texture2D foregroundTexture;
  28.         Texture2D playerTexture;
  29.         Texture2D rect;
  30.  
  31.         int screenHeight;
  32.         int screenWidth;
  33.  
  34.         public Game1()
  35.         {
  36.             graphics = new GraphicsDeviceManager(this);
  37.             Content.RootDirectory = "Content";
  38.  
  39.             IsFixedTimeStep = false;
  40.         }
  41.  
  42.         protected override void Initialize()
  43.         {
  44.             graphics.PreferredBackBufferWidth = 800;
  45.             graphics.PreferredBackBufferHeight = 400;
  46.             graphics.IsFullScreen = false;
  47.             graphics.ApplyChanges();
  48.  
  49.             Window.Title = "Terrible Game";
  50.  
  51.             base.Initialize();
  52.         }
  53.  
  54.         PlayerData Player = new PlayerData();
  55.         void BuildPlayerData()
  56.         {
  57.             Player.Position = new Vector2(0, 250); // Start them in the air because it looks/feels cool when a player falls at the start
  58.             Player.IsAlive = true;
  59.             Player.Win = false;
  60.             //Player.Velocity = new Vector2(0.3f, 0); // It defaults to Vector2.Zero (stationary)
  61.             Player.CanJump = true;
  62.         }
  63.  
  64.  
  65.         List<Rectangle> Platform = new List<Rectangle>();
  66.         void BuildPlatformData()
  67.         {
  68.             Platform.Add(new Rectangle(100, 210, 150, 30));
  69.             Platform.Add(new Rectangle(150, 140, 100, 30));
  70.             Platform.Add(new Rectangle(150, 310, 230, 30));
  71.             Platform.Add(new Rectangle(230, 280, 150, 30));
  72.             Platform.Add(new Rectangle(310, 250, 70, 30));
  73.             Platform.Add(new Rectangle(350, 140, 100, 30));
  74.             Platform.Add(new Rectangle(400, 230, 150, 30));
  75.             Platform.Add(new Rectangle(400, 170, 30, 60));
  76.             Platform.Add(new Rectangle(500, 170, 30, 30));
  77.             Platform.Add(new Rectangle(600, 130, 30, 300));
  78.             Platform.Add(new Rectangle(600, 130, 130, 30));
  79.             Platform.Add(new Rectangle(600, 330, 200, 10));
  80.             Platform.Add(new Rectangle(700, 250, 100, 30));
  81.             Platform.Add(new Rectangle(770, 0, 30, 260));
  82.  
  83.             // Adding new platforms to keep the player confined within the level
  84.             // These others are positioned right off the screen so you won't see them
  85.             // The only downside would be that these are now DRAWN on the screen (just out of view)
  86.             // However, if you ever expand on the project, you would likely check if an element is visible on the screen, and if not, don't draw it :)
  87.             // You could do this by settings a rectangle that is the size/position of the screen.. and check if the platforms intersect this screen rect
  88.             Platform.Add(new Rectangle(0, 340, 380, 61)); // Bottom boundry (THIS USED TO BE "LEVEL")
  89.             Platform.Add(new Rectangle(0, -30, screenWidth, 30)); // Top boundry
  90.             Platform.Add(new Rectangle(-30, 0, 30, screenHeight)); // Left boundry
  91.             Platform.Add(new Rectangle(screenWidth, 0, 30, screenHeight)); // Right boundry
  92.         }
  93.  
  94.         List<Rectangle> Hazard = new List<Rectangle>();
  95.         void BuildHazardData()
  96.         {
  97.             Hazard.Add(new Rectangle(380, 340, 420, 61));
  98.         }
  99.  
  100.         protected override void LoadContent()
  101.         {
  102.             device = graphics.GraphicsDevice;
  103.             spriteBatch = new SpriteBatch(GraphicsDevice);
  104.  
  105.             backgroundTexture = Content.Load<Texture2D>("background");
  106.             playerTexture = Content.Load<Texture2D>("player");
  107.             foregroundTexture = Content.Load<Texture2D>("foreground");
  108.             font = Content.Load<SpriteFont>("gameFont");
  109.  
  110.             rect = new Texture2D(GraphicsDevice, 1, 1);
  111.             rect.SetData(new[] { Color.White });
  112.  
  113.             screenWidth = device.PresentationParameters.BackBufferWidth;
  114.             screenHeight = device.PresentationParameters.BackBufferHeight;
  115.  
  116.             BuildPlayerData();
  117.             BuildPlatformData();
  118.             BuildHazardData();
  119.         }
  120.  
  121.         protected override void UnloadContent()
  122.         {
  123.  
  124.         }
  125.  
  126.         void CollisionDetection()
  127.         {
  128.             // I removed checking collision with LEVEL and added "LEVEL" as a platform.. there is nothing special about it
  129.             // And it should really just be treated like any other platform
  130.  
  131.             // I've also removed the screen bounds checking... because if you ever add a camera and have your level "scroll"
  132.             // Your code wouldn't be useful anymore. Just add platforms to the screen edges to prevent movement! Then all your code above works with it :)
  133.             // Less, more generalized code is the way to do things!
  134.  
  135.  
  136.             // Figure out the character's destination... collision code will be run based on this position... not the current position!
  137.             Rectangle player = new Rectangle((int)Player.Position.X, (int)Player.Position.Y, 28, 28);
  138.             Player.CanJump = false; // if they're moving they can't jump until they collide with the top of an object (platform/ground)
  139.  
  140.  
  141.             // Check horizontal collision
  142.             int finalH = (int)(player.X + Player.Velocity.X);
  143.             while (true)
  144.             {
  145.                 // We want to move from point A to point B slowly to check for collisions along the way
  146.                 player.X += Interpolate(player.X, finalH, player.Width);
  147.                 bool collided = false;
  148.                 foreach (Rectangle i in Platform)
  149.                 {
  150.                     if (player.Intersects(i))
  151.                     {
  152.                         collided = true;
  153.                         if (player.Center.X > i.Center.X)
  154.                             player.X = i.Right; // Intersecting right
  155.                         else
  156.                             player.X = i.Left - player.Width; // intersecting left
  157.                     }
  158.                 }
  159.                 // If we collided with an object or reached our destination with no collision, we're done here
  160.                 if (collided || player.X == finalH)
  161.                     break;
  162.             }
  163.  
  164.             // Check Vertical collision
  165.             int finalV = (int)(player.Y + Player.Velocity.Y);
  166.             while (true)
  167.             {
  168.                 // We want to move from point A to point B slowly to check for collisions along the way
  169.                 player.Y += Interpolate(player.Y, finalV, player.Height);
  170.                 bool collided = false;
  171.                 foreach (Rectangle i in Platform)
  172.                 {
  173.                     if (player.Intersects(i))
  174.                     {
  175.                         collided = true;
  176.                         if (player.Center.Y > i.Center.Y)
  177.                             player.Y = i.Bottom; // Intersecting bottom
  178.                         else
  179.                         {
  180.                             player.Y = i.Top - player.Height; // Intersecting top
  181.                             Player.CanJump = true;
  182.                         }
  183.                     }
  184.  
  185.                 }
  186.  
  187.                 foreach (Rectangle o in Hazard)
  188.                 {
  189.                     if (player.Intersects(o))
  190.                     {
  191.                         Player.IsAlive = false;
  192.                         collided = true;
  193.                         if (player.Center.Y > o.Center.Y)
  194.                             player.Y = o.Bottom; // Intersecting bottom
  195.                         else
  196.                         {
  197.                             player.Y = o.Top ; // Intersecting top
  198.                             Player.CanJump = true;
  199.                         }
  200.                     }
  201.  
  202.                 }
  203.  
  204.                 // If we collided with an object or reached our destination with no collision, we're done here
  205.                 if (collided || player.Y == finalV)
  206.                     break;
  207.             }
  208.  
  209.             // Now that the collisions have been figured out, let's update the player's location to where it should be!
  210.             Player.Position = new Vector2(player.X, player.Y);
  211.         }
  212.  
  213.         void CheckWin()
  214.         {
  215.             if (Player.Position.X >= screenWidth - 28) Player.Win = true;
  216.         }
  217.  
  218.         int Interpolate(int start, int end, int step)
  219.         {
  220.             // This function just returns a value starting from START and moving towards END in STEP increments
  221.             int difference = Math.Abs(end - start);
  222.             int increment = Math.Min(difference, step);
  223.             if (start > end)
  224.                 return -increment;
  225.             else
  226.                 return increment;
  227.         }
  228.  
  229.         // We can make this a const since we won't be changing it
  230.         const float gravity = 1.25f;
  231.  
  232.         protected override void Update(GameTime gameTime)
  233.         {
  234.             float elapsedTime = (float)gameTime.ElapsedGameTime.TotalMilliseconds;
  235.             float speed = elapsedTime * .3f;
  236.             KeyboardState keyboard = Keyboard.GetState();
  237.  
  238.             if (Player.IsAlive && !Player.Win)
  239.             {
  240.                 // Horizontal movement
  241.                 if (keyboard.IsKeyDown(Keys.A))
  242.                     Player.Velocity.X += speed*-5;
  243.                 else if (keyboard.IsKeyDown(Keys.D))
  244.                     Player.Velocity.X += speed*5;
  245.                 else
  246.                     Player.Velocity.X = 0; // Cancel momentum... Stop moving when they let go
  247.                 // Limit their horizontal velocity
  248.                 const float maxVelocityH = 5f;
  249.                 Player.Velocity.X = MathHelper.Clamp(Player.Velocity.X, -maxVelocityH, maxVelocityH);
  250.  
  251.                 // Vertical movement
  252.                 if (keyboard.IsKeyDown(Keys.W) && Player.CanJump)
  253.                     Player.Velocity.Y += speed*-15;
  254.                 else if (keyboard.IsKeyDown(Keys.S))
  255.                     Player.Velocity.Y += speed*1;
  256.                 // Limit their vertical velocity
  257.                 const float maxVelocityV = 15f;
  258.                 Player.Velocity.Y = MathHelper.Clamp(Player.Velocity.Y, -maxVelocityV, maxVelocityV / 2);
  259.  
  260.                 // Apply Gravity (downwards force)
  261.                 Player.Velocity.Y += gravity;
  262.  
  263.                 // Collision Detection
  264.                 CollisionDetection();
  265.  
  266.                 CheckWin();
  267.             }
  268.  
  269.             if (keyboard.IsKeyDown(Keys.R)) BuildPlayerData();
  270.  
  271.             base.Update(gameTime);
  272.         }
  273.  
  274.         protected override void Draw(GameTime gameTime)
  275.         {
  276.             GraphicsDevice.Clear(Color.CornflowerBlue);
  277.  
  278.             spriteBatch.Begin();
  279.  
  280.             DrawLevel();
  281.             DrawText();
  282.             if (!Player.IsAlive) PlayerDead();
  283.             else if (Player.Win) PlayerWin();
  284.  
  285.             spriteBatch.End();
  286.  
  287.             base.Draw(gameTime);
  288.  
  289.         }
  290.  
  291.         private void PlayerDead()
  292.         {
  293.             //spriteBatch.Draw(rect, new Rectangle(screenWidth / 2, screenHeight / 2, 92, 22), Color.Red);
  294.             spriteBatch.DrawString(font, "YOU DIED!\nPress R to restart.", new Vector2(screenWidth / 2, screenHeight / 2), Color.White);
  295.         }
  296.  
  297.         private void PlayerWin()
  298.         {
  299.             spriteBatch.DrawString(font, "YOU WON!\nPress R to restart.", new Vector2(screenWidth / 2, screenHeight / 2), Color.White);
  300.         }
  301.  
  302.         private void DrawText()
  303.         {
  304.             spriteBatch.DrawString(font, String.Format("Position: {0} Gravity: {1} Velocity: {2}", Player.Position, gravity, Player.Velocity), new Vector2(0, 0), Color.White);
  305.         }
  306.  
  307.         private void DrawLevel()
  308.         {
  309.             Rectangle screenRectangle = new Rectangle(0, 0, screenWidth, screenHeight);
  310.  
  311.             spriteBatch.Draw(backgroundTexture, screenRectangle, Color.White);
  312.  
  313.             foreach (Rectangle i in Platform) spriteBatch.Draw(rect, i, Color.Green);
  314.             foreach (Rectangle i in Hazard) spriteBatch.Draw(rect, i, Color.Red);
  315.  
  316.             DrawPlayer();
  317.  
  318.            spriteBatch.Draw(foregroundTexture, new Rectangle(0, 0, 380, screenHeight), Color.White);
  319.         }
  320.  
  321.         private void DrawPlayer()
  322.         {
  323.             spriteBatch.Draw(playerTexture, Player.Position, Color.White);
  324.         }
  325.     }
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement