Advertisement
Guest User

Untitled

a guest
Jan 5th, 2013
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.95 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 BadGame
  13. {
  14.     public struct PlayerData
  15.     {
  16.         public Vector2 Position;
  17.         public int Health;
  18.     }
  19.  
  20.     public class Game1 : Microsoft.Xna.Framework.Game
  21.     {
  22.         GraphicsDeviceManager graphics;
  23.         GraphicsDevice device;
  24.  
  25.         SpriteBatch spriteBatch;
  26.         SpriteFont font;
  27.  
  28.         Texture2D backgroundTexture;
  29.         Texture2D levelTexture;
  30.         Texture2D foregroundTexture;
  31.         Texture2D playerTexture;
  32.  
  33.         int screenHeight;
  34.         int screenWidth;
  35.  
  36.         public Game1()
  37.         {
  38.             graphics = new GraphicsDeviceManager(this);
  39.             Content.RootDirectory = "Content";
  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.         public static PlayerData Save1 = new PlayerData();
  55.         public static void BuildPlayerData()
  56.         {
  57.             Save1.Position = new Vector2(0, 312);
  58.             Save1.Health = 100;
  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.             levelTexture = Content.Load<Texture2D>("level");
  68.             playerTexture = Content.Load<Texture2D>("player");
  69.             foregroundTexture = Content.Load<Texture2D>("foreground");
  70.             font = Content.Load<SpriteFont>("gameFont");
  71.  
  72.             screenWidth = device.PresentationParameters.BackBufferWidth;
  73.             screenHeight = device.PresentationParameters.BackBufferHeight;
  74.  
  75.             BuildPlayerData();
  76.         }
  77.  
  78.         protected override void UnloadContent()
  79.         {
  80.  
  81.         }
  82.  
  83.         protected override void Update(GameTime gameTime)
  84.         {
  85.             KeyboardState keyboard = Keyboard.GetState();
  86.             Vector2 input = Vector2.Zero;
  87.  
  88.             if (keyboard.IsKeyDown(Keys.W)) input.Y -= 1;
  89.             if (keyboard.IsKeyDown(Keys.A)) input.X -= 1;
  90.             if (keyboard.IsKeyDown(Keys.S)) input.Y += 1;
  91.             if (keyboard.IsKeyDown(Keys.D)) input.X += 1;
  92.                                                        
  93.             if (input != Vector2.Zero)
  94.             {
  95.                 input.Normalize();
  96.                 Save1.Position += input * 2;
  97.  
  98.             }
  99.             base.Update(gameTime);
  100.         }
  101.  
  102.         protected override void Draw(GameTime gameTime)
  103.         {
  104.             GraphicsDevice.Clear(Color.CornflowerBlue);
  105.  
  106.             spriteBatch.Begin();
  107.             DrawLevel();
  108.             DrawPlayer();
  109.             DrawText();
  110.             spriteBatch.End();
  111.  
  112.             base.Draw(gameTime);
  113.  
  114.         }
  115.  
  116.         private void DrawText()
  117.         {
  118.             spriteBatch.DrawString(font, String.Format("Position: {0}", Save1.Position), new Vector2(0, 0), Color.White);
  119.         }
  120.  
  121.         private void DrawLevel()
  122.         {
  123.             Rectangle screenRectangle = new Rectangle(0, 0, screenWidth, screenHeight);
  124.             spriteBatch.Draw(backgroundTexture, screenRectangle, Color.White);
  125.             spriteBatch.Draw(levelTexture, screenRectangle, Color.White);
  126.             spriteBatch.Draw(foregroundTexture, screenRectangle, Color.White);
  127.         }
  128.  
  129.         private void DrawPlayer()
  130.         {
  131.             spriteBatch.Draw(playerTexture, Save1.Position, Color.White);
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement