Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- namespace BadGame
- {
- public struct PlayerData
- {
- public Vector2 Position;
- public Vector2 Velocity;
- public int Health;
- public bool CanJump;
- }
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- GraphicsDevice device;
- SpriteBatch spriteBatch;
- SpriteFont font;
- Texture2D backgroundTexture;
- Texture2D foregroundTexture;
- Texture2D playerTexture;
- Texture2D rect;
- int screenHeight;
- int screenWidth;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- IsFixedTimeStep = false;
- }
- protected override void Initialize()
- {
- graphics.PreferredBackBufferWidth = 800;
- graphics.PreferredBackBufferHeight = 400;
- graphics.IsFullScreen = false;
- graphics.ApplyChanges();
- Window.Title = "Terrible Game";
- base.Initialize();
- }
- public static PlayerData Save1 = new PlayerData();
- public static void BuildPlayerData()
- {
- Save1.Position = new Vector2(0, 312);
- Save1.Health = 100;
- Save1.Velocity = new Vector2((float)0.3, 0);
- Save1.CanJump = true;
- }
- protected override void LoadContent()
- {
- device = graphics.GraphicsDevice;
- spriteBatch = new SpriteBatch(GraphicsDevice);
- backgroundTexture = Content.Load<Texture2D>("background");
- playerTexture = Content.Load<Texture2D>("player");
- foregroundTexture = Content.Load<Texture2D>("foreground");
- font = Content.Load<SpriteFont>("gameFont");
- rect = new Texture2D(GraphicsDevice, 1, 1);
- rect.SetData(new[] { Color.White });
- screenWidth = device.PresentationParameters.BackBufferWidth;
- screenHeight = device.PresentationParameters.BackBufferHeight;
- BuildPlayerData();
- }
- protected override void UnloadContent()
- {
- }
- void CollisionDetection()
- {
- Rectangle player = new Rectangle((int)Save1.Position.X, (int)Save1.Position.Y, 28, 28);
- Rectangle level = new Rectangle(0, 340, 800, 61);
- Rectangle enemy1 = new Rectangle(150, 310, 10, 10);
- Rectangle enemy2 = new Rectangle(350, 310, 10, 10);
- Rectangle enemy3 = new Rectangle(650, 310, 10, 10);
- Rectangle enemy4 = new Rectangle(750, 310, 10, 10);
- if (player.Intersects(level))
- {
- Save1.Position.Y = 312;
- }
- if (player.Intersects(enemy1)) BuildPlayerData();
- if (player.Intersects(enemy2)) BuildPlayerData();
- if (player.Intersects(enemy3)) BuildPlayerData();
- if (player.Intersects(enemy4)) BuildPlayerData();
- if (Save1.Position.X > screenWidth - 28) Save1.Position.X = screenWidth - 28;
- if (Save1.Position.X < 0) Save1.Position.X = 0;
- if (Save1.Position.Y > screenHeight) Save1.Position.Y = screenHeight;
- if (Save1.Position.Y < 0) Save1.Position.Y = 0;
- }
- private const float gravity = 1f;
- void Gravity()
- {
- Save1.Velocity.Y += gravity;
- Save1.Position.Y += Save1.Velocity.Y;
- if (Save1.Position.Y >= 312)
- {
- Save1.Velocity.Y = 0;
- Save1.CanJump = true;
- }
- }
- void Jump()
- {
- if (!Save1.CanJump) return;
- Save1.Velocity.Y -= 15;
- Save1.CanJump = false;
- }
- protected override void Update(GameTime gameTime)
- {
- KeyboardState keyboard = Keyboard.GetState();
- Vector2 input = Vector2.Zero;
- double elapsedTime = gameTime.ElapsedGameTime.TotalMilliseconds;
- double distance = (Save1.Velocity.X*elapsedTime);
- if (keyboard.IsKeyDown(Keys.W)) Jump();
- if (keyboard.IsKeyDown(Keys.A)) input.X -= 1;
- if (keyboard.IsKeyDown(Keys.S)) input.Y += 1;
- if (keyboard.IsKeyDown(Keys.D)) input.X += 1;
- if (input != Vector2.Zero)
- {
- input.Normalize();
- Save1.Position += input * (float)(distance);
- }
- Gravity();
- CollisionDetection();
- base.Update(gameTime);
- }
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- spriteBatch.Begin();
- DrawLevel();
- DrawText();
- spriteBatch.End();
- base.Draw(gameTime);
- }
- private void DrawText()
- {
- spriteBatch.DrawString(font, String.Format("Position: {0} Gravity: {1} Velocity: {2}", Save1.Position, gravity, Save1.Velocity.Y), new Vector2(0, 0), Color.White);
- }
- private void DrawLevel()
- {
- Rectangle screenRectangle = new Rectangle(0, 0, screenWidth, screenHeight);
- spriteBatch.Draw(backgroundTexture, screenRectangle, Color.White);
- spriteBatch.Draw(rect, new Rectangle(0, 340, 800, 61), Color.Green);
- spriteBatch.Draw(rect, new Rectangle(150, 310, 10, 10), Color.Black);
- spriteBatch.Draw(rect, new Rectangle(350, 310, 10, 10), Color.Black);
- spriteBatch.Draw(rect, new Rectangle(650, 310, 10, 10), Color.Black);
- spriteBatch.Draw(rect, new Rectangle(750, 310, 10, 10), Color.Black);
- DrawPlayer();
- spriteBatch.Draw(foregroundTexture, screenRectangle, Color.White);
- }
- private void DrawPlayer()
- {
- spriteBatch.Draw(playerTexture, Save1.Position, Color.White);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement