Advertisement
Guest User

Untitled

a guest
Jan 6th, 2013
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.12 KB | None | 0 0
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. using Microsoft.Xna.Framework.Input;
  5.  
  6. namespace BadGame
  7. {
  8.     public struct PlayerData
  9.     {
  10.         public Vector2 Position;
  11.         public Vector2 Velocity;
  12.         public int Health;
  13.         public bool CanJump;
  14.     }
  15.  
  16.     public class Game1 : Microsoft.Xna.Framework.Game
  17.     {
  18.         GraphicsDeviceManager graphics;
  19.         GraphicsDevice device;
  20.  
  21.         SpriteBatch spriteBatch;
  22.         SpriteFont font;
  23.  
  24.         Texture2D backgroundTexture;
  25.         Texture2D foregroundTexture;
  26.         Texture2D playerTexture;
  27.         Texture2D rect;
  28.  
  29.         int screenHeight;
  30.         int screenWidth;
  31.  
  32.         public Game1()
  33.         {
  34.             graphics = new GraphicsDeviceManager(this);
  35.             Content.RootDirectory = "Content";
  36.  
  37.             IsFixedTimeStep = false;
  38.         }
  39.  
  40.         protected override void Initialize()
  41.         {
  42.             graphics.PreferredBackBufferWidth = 800;
  43.             graphics.PreferredBackBufferHeight = 400;
  44.             graphics.IsFullScreen = false;
  45.             graphics.ApplyChanges();
  46.  
  47.             Window.Title = "Terrible Game";
  48.  
  49.             base.Initialize();
  50.         }
  51.  
  52.         public static PlayerData Save1 = new PlayerData();
  53.         public static void BuildPlayerData()
  54.         {
  55.             Save1.Position = new Vector2(0, 312);
  56.             Save1.Health = 100;
  57.             Save1.Velocity = new Vector2((float)0.3, 0);
  58.             Save1.CanJump = true;
  59.         }
  60.  
  61.         protected override void LoadContent()
  62.         {
  63.             device = graphics.GraphicsDevice;
  64.             spriteBatch = new SpriteBatch(GraphicsDevice);
  65.  
  66.             backgroundTexture = Content.Load<Texture2D>("background");
  67.             playerTexture = Content.Load<Texture2D>("player");
  68.             foregroundTexture = Content.Load<Texture2D>("foreground");
  69.             font = Content.Load<SpriteFont>("gameFont");
  70.  
  71.             rect = new Texture2D(GraphicsDevice, 1, 1);
  72.             rect.SetData(new[] { Color.White });
  73.  
  74.             screenWidth = device.PresentationParameters.BackBufferWidth;
  75.             screenHeight = device.PresentationParameters.BackBufferHeight;
  76.  
  77.             BuildPlayerData();
  78.         }
  79.  
  80.         protected override void UnloadContent()
  81.         {
  82.  
  83.         }
  84.  
  85.         void CollisionDetection()
  86.         {
  87.             Rectangle player = new Rectangle((int)Save1.Position.X, (int)Save1.Position.Y, 28, 28);
  88.             Rectangle level = new Rectangle(0, 340, 800, 61);
  89.             Rectangle enemy1 = new Rectangle(150, 310, 10, 10);
  90.             Rectangle enemy2 = new Rectangle(350, 310, 10, 10);
  91.             Rectangle enemy3 = new Rectangle(650, 310, 10, 10);
  92.             Rectangle enemy4 = new Rectangle(750, 310, 10, 10);
  93.  
  94.             if (player.Intersects(level))
  95.             {
  96.                 Save1.Position.Y = 312;
  97.             }
  98.             if (player.Intersects(enemy1)) BuildPlayerData();
  99.             if (player.Intersects(enemy2)) BuildPlayerData();
  100.             if (player.Intersects(enemy3)) BuildPlayerData();
  101.             if (player.Intersects(enemy4)) BuildPlayerData();
  102.  
  103.             if (Save1.Position.X > screenWidth - 28) Save1.Position.X = screenWidth - 28;
  104.             if (Save1.Position.X < 0) Save1.Position.X = 0;
  105.             if (Save1.Position.Y > screenHeight) Save1.Position.Y = screenHeight;
  106.             if (Save1.Position.Y < 0) Save1.Position.Y = 0;
  107.         }
  108.  
  109.         private const float gravity = 1f;
  110.  
  111.         void Gravity()
  112.         {
  113.             Save1.Velocity.Y += gravity;
  114.             Save1.Position.Y += Save1.Velocity.Y;
  115.  
  116.             if (Save1.Position.Y >= 312)
  117.             {
  118.                 Save1.Velocity.Y = 0;
  119.                 Save1.CanJump = true;
  120.             }
  121.  
  122.         }
  123.  
  124.         void Jump()
  125.         {
  126.             if (!Save1.CanJump) return;
  127.             Save1.Velocity.Y -= 15;
  128.             Save1.CanJump = false;
  129.         }
  130.  
  131.         protected override void Update(GameTime gameTime)
  132.         {
  133.  
  134.             KeyboardState keyboard = Keyboard.GetState();
  135.             Vector2 input = Vector2.Zero;
  136.             double elapsedTime = gameTime.ElapsedGameTime.TotalMilliseconds;
  137.             double distance = (Save1.Velocity.X*elapsedTime);
  138.  
  139.             if (keyboard.IsKeyDown(Keys.W)) Jump();
  140.             if (keyboard.IsKeyDown(Keys.A)) input.X -= 1;
  141.             if (keyboard.IsKeyDown(Keys.S)) input.Y += 1;
  142.             if (keyboard.IsKeyDown(Keys.D)) input.X += 1;
  143.  
  144.             if (input != Vector2.Zero)
  145.             {
  146.                 input.Normalize();
  147.                 Save1.Position += input * (float)(distance);
  148.             }
  149.  
  150.             Gravity();
  151.             CollisionDetection();
  152.  
  153.             base.Update(gameTime);
  154.         }
  155.  
  156.         protected override void Draw(GameTime gameTime)
  157.         {
  158.             GraphicsDevice.Clear(Color.CornflowerBlue);
  159.  
  160.             spriteBatch.Begin();
  161.             DrawLevel();
  162.             DrawText();
  163.             spriteBatch.End();
  164.  
  165.             base.Draw(gameTime);
  166.  
  167.         }
  168.  
  169.         private void DrawText()
  170.         {
  171.             spriteBatch.DrawString(font, String.Format("Position: {0} Gravity: {1} Velocity: {2}", Save1.Position, gravity, Save1.Velocity.Y), new Vector2(0, 0), Color.White);
  172.         }
  173.  
  174.         private void DrawLevel()
  175.         {
  176.             Rectangle screenRectangle = new Rectangle(0, 0, screenWidth, screenHeight);
  177.  
  178.             spriteBatch.Draw(backgroundTexture, screenRectangle, Color.White);
  179.             spriteBatch.Draw(rect, new Rectangle(0, 340, 800, 61), Color.Green);
  180.             spriteBatch.Draw(rect, new Rectangle(150, 310, 10, 10), Color.Black);
  181.             spriteBatch.Draw(rect, new Rectangle(350, 310, 10, 10), Color.Black);
  182.             spriteBatch.Draw(rect, new Rectangle(650, 310, 10, 10), Color.Black);
  183.             spriteBatch.Draw(rect, new Rectangle(750, 310, 10, 10), Color.Black);
  184.             DrawPlayer();
  185.             spriteBatch.Draw(foregroundTexture, screenRectangle, Color.White);
  186.         }
  187.  
  188.         private void DrawPlayer()
  189.         {
  190.             spriteBatch.Draw(playerTexture, Save1.Position, Color.White);
  191.         }
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement