Advertisement
Guest User

Untitled

a guest
Dec 19th, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.85 KB | None | 0 0
  1. #region Using Statements
  2. using System;
  3.  
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Graphics;
  6. using Microsoft.Xna.Framework.Storage;
  7. using Microsoft.Xna.Framework.Input;
  8.  
  9. #endregion
  10.  
  11. namespace eightbitsexy
  12. {
  13.     /// <summary>
  14.     /// This is the main type for your game
  15.     /// </summary>
  16.     public class Game1 : Microsoft.Xna.Framework.Game
  17.     {
  18.  
  19.  
  20.         GraphicsDeviceManager graphics;
  21.         SpriteBatch spriteBatch;
  22.  
  23.         const int WINDOW_WIDTH = 1200;
  24.         const int WINDOW_HEIGHT = 700;
  25.  
  26.         int heroHealth = 100;
  27.         bool isDamaged;
  28.  
  29.         //draw character stuff
  30.         Texture2D hero0;
  31.         Rectangle drawHeroRectangle;
  32.  
  33.         Texture2D enemy0;
  34.         Rectangle drawEnemyRectangle;
  35.         Vector2 enemyPosition;
  36.         Vector2 enemyVelocity;
  37.  
  38.         Texture2D heroDamaged;
  39.  
  40.  
  41.         //thumbstick  movement
  42.         const int THUMBSTICK_DEFLECTION_AMOUNT = 20;
  43.  
  44.         public Game1()
  45.         {
  46.             graphics = new GraphicsDeviceManager(this);
  47.             Content.RootDirectory = "Content";
  48.  
  49.            
  50.             //change resolution
  51.             graphics.PreferredBackBufferHeight = WINDOW_HEIGHT;
  52.             graphics.PreferredBackBufferWidth = WINDOW_WIDTH;
  53.             IsMouseVisible = false;
  54.             graphics.IsFullScreen = true;
  55.         }
  56.  
  57.         /// <summary>
  58.         /// Allows the game to perform any initialization it needs to before starting to run.
  59.         /// This is where it can query for any required services and load any non-graphic
  60.         /// related content.  Calling base.Initialize will enumerate through any components
  61.         /// and initialize them as well.
  62.         /// </summary>
  63.         protected override void Initialize()
  64.         {
  65.             // TODO: Add your initialization logic here
  66.  
  67.             //give enemy velocity
  68.             enemyPosition = new Vector2(this.GraphicsDevice.Viewport.Width / 2, this.GraphicsDevice.Viewport.Height * 0.25f);
  69.             enemyVelocity = new Vector2(0,2);
  70.  
  71.             base.Initialize();
  72.         }
  73.  
  74.         /// <summary>
  75.         /// LoadContent will be called once per game and is the place to load
  76.         /// all of your content.
  77.         /// </summary>
  78.         protected override void LoadContent()
  79.         {
  80.             // Create a new SpriteBatch, which can be used to draw textures.
  81.             spriteBatch = new SpriteBatch(GraphicsDevice);
  82.  
  83.             // TODO: use this.Content to load your game content here
  84.             hero0 = Content.Load<Texture2D>("HeroSprite");
  85.             drawHeroRectangle = new Rectangle(WINDOW_WIDTH / 2 - hero0.Width / 2,
  86.                 WINDOW_HEIGHT / 2 - hero0.Height / 2, hero0.Width, hero0.Height);
  87.            
  88.             enemy0 = Content.Load<Texture2D>("enemy");
  89.             drawEnemyRectangle = new Rectangle((int)(enemyPosition.X - enemy0.Width / 2),
  90.             (int)(enemyPosition.Y - enemy0.Height / 2), enemy0.Width, enemy0.Height);
  91.            
  92.             heroDamaged = Content.Load<Texture2D>("HeroDamaged");
  93.  
  94.        
  95.         }
  96.  
  97.         /// <summary>
  98.         /// UnloadContent will be called once per game and is the place to unload
  99.         /// all content.
  100.         /// </summary>
  101.         protected override void UnloadContent()
  102.         {
  103.             // TODO: Unload any non ContentManager content here
  104.         }
  105.  
  106.         /// <summary>
  107.         /// Allows the game to run logic such as updating the world,
  108.         /// checking for collisions, gathering input, and playing audio.
  109.         /// </summary>
  110.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  111.         protected override void Update (GameTime gameTime)
  112.         {
  113.             // Allows the game to exit
  114.             if (GamePad.GetState (PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  115.                 this.Exit ();
  116.             if (Keyboard.GetState ().IsKeyDown (Keys.Escape)) {
  117.                 this.Exit ();
  118.             }
  119.             else {
  120.             }
  121.  
  122.             //make thumbstick move character
  123.             GamePadState gamepad = GamePad.GetState (PlayerIndex.One);
  124.             if (gamepad.IsConnected) {
  125.                 drawHeroRectangle.X += (int)(gamepad.ThumbSticks.Left.X * THUMBSTICK_DEFLECTION_AMOUNT);
  126.                 drawHeroRectangle.Y -= (int)(gamepad.ThumbSticks.Left.Y * THUMBSTICK_DEFLECTION_AMOUNT);
  127.  
  128.                 //clamp character to window
  129.                 if (drawHeroRectangle.Left < 0) {
  130.                     drawHeroRectangle.X = 0;
  131.                 }
  132.                 if (drawHeroRectangle.Right > WINDOW_WIDTH) {
  133.                     drawHeroRectangle.X = WINDOW_WIDTH - drawHeroRectangle.Width;
  134.                 }
  135.                 if (drawHeroRectangle.Top < 0) {
  136.                     drawHeroRectangle.Y = 0;
  137.                 }
  138.                 if (drawHeroRectangle.Bottom > WINDOW_HEIGHT) {
  139.                     drawHeroRectangle.Y = WINDOW_HEIGHT - drawHeroRectangle.Height;
  140.                 }
  141.  
  142.  
  143.             } else {
  144.  
  145.                 //make character follow mouse
  146.                 MouseState mouse = Mouse.GetState ();
  147.                 drawHeroRectangle.X = mouse.X - hero0.Width / 2;
  148.                 drawHeroRectangle.Y = mouse.Y - hero0.Height / 2;
  149.  
  150.                 //clamp character to window
  151.                 if (drawHeroRectangle.Left < 0) {
  152.                     drawHeroRectangle.X = 0;
  153.                 }
  154.                 if (drawHeroRectangle.Right > WINDOW_WIDTH) {
  155.                     drawHeroRectangle.X = WINDOW_WIDTH - drawHeroRectangle.Width;
  156.                 }
  157.                 if (drawHeroRectangle.Top < 0) {
  158.                     drawHeroRectangle.Y = 0;
  159.                 }
  160.                 if (drawHeroRectangle.Bottom > WINDOW_HEIGHT) {
  161.                     drawHeroRectangle.Y = WINDOW_HEIGHT - drawHeroRectangle.Height;
  162.                 }
  163.  
  164.             }
  165.  
  166.             //draw enemy on screen
  167.             int enemyYPosition = 720;
  168.             int enemyXPosition = 1200;
  169.  
  170.             drawEnemyRectangle.Y = enemyYPosition / 2;
  171.             drawEnemyRectangle.X = enemyXPosition;
  172.  
  173.             if (enemyXPosition > 0) {
  174.                 enemyXPosition -= 50;
  175.  
  176.             }
  177.             else {
  178.                 enemyXPosition = 1200;
  179.             }
  180.  
  181.             //checks for collisions
  182.             if (drawHeroRectangle.Intersects (drawEnemyRectangle)) {
  183.                 heroHealth--;
  184.                 isDamaged = true;
  185.                 enemyVelocity = -enemyVelocity;
  186.                 enemyPosition += enemyVelocity;
  187.  
  188.             } else {
  189.                 isDamaged = false;
  190.                 enemyPosition += enemyVelocity;
  191.             }
  192.          
  193.             //end the game if health=0
  194.             if (heroHealth == 0) {
  195.                 Exit();
  196.             }
  197.             base.Update(gameTime);
  198.         }
  199.  
  200.         /// <summary>
  201.         /// This is called when the game should draw itself.
  202.         /// </summary>
  203.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  204.         protected override void Draw(GameTime gameTime)
  205.         {
  206.             GraphicsDevice.Clear(Color.CornflowerBlue);
  207.  
  208.             //draw hero
  209.  
  210.             if (heroHealth > 0)
  211.             {
  212.                 spriteBatch.Begin();
  213.  
  214.                 spriteBatch.Draw(hero0, drawHeroRectangle, Color.White);
  215.  
  216.                 spriteBatch.Draw(enemy0, drawEnemyRectangle, Color.White);
  217.  
  218.                 spriteBatch.End();
  219.             }
  220.  
  221.             base.Draw(gameTime);
  222.  
  223.             if (isDamaged == true & heroHealth > 0)
  224.             {
  225.                 spriteBatch.Begin();
  226.                 spriteBatch.Draw(heroDamaged, drawHeroRectangle, Color.White);
  227.                 spriteBatch.End();
  228.             }
  229.         }
  230.     }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement