Guest User

Untitled

a guest
Apr 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.98 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 TEST_0000
  13. {
  14.  
  15.     public class Game1 : Microsoft.Xna.Framework.Game
  16.     {
  17.         GraphicsDeviceManager graphics;
  18.         SpriteBatch spriteBatch;
  19.  
  20.         Texture2D Player;
  21.         Vector2 PlayerPosition;
  22.  
  23.         Texture2D Portal;
  24.         Vector2 PortalPosition;
  25.  
  26.         Rectangle PlayerBoundingBox;
  27.         Rectangle PortalBoundingBox;
  28.  
  29.         bool Collision;
  30.         public Game1()
  31.         {
  32.             graphics = new GraphicsDeviceManager(this);
  33.             Content.RootDirectory = "Content";
  34.         }
  35.  
  36.      
  37.         protected override void Initialize()
  38.         {
  39.             PlayerPosition = new Vector2(100,100);
  40.             PortalPosition = new Vector2(600,100);
  41.             Collision = false;
  42.  
  43.             base.Initialize();
  44.         }
  45.  
  46.         /// <summary>
  47.         /// LoadContent will be called once per game and is the place to load
  48.         /// all of your content.
  49.         /// </summary>
  50.         protected override void LoadContent()
  51.         {
  52.             // Create a new SpriteBatch, which can be used to draw textures.
  53.             spriteBatch = new SpriteBatch(GraphicsDevice);
  54.  
  55.             Portal = Content.Load<Texture2D>("player");
  56.             Player = Content.Load<Texture2D>("player");
  57.  
  58.             PlayerBoundingBox = new Rectangle ((int)PlayerPosition.X, (int)PlayerPosition.Y , Player.Width , Player.Height );
  59.             PortalBoundingBox = new Rectangle ((int)PortalPosition.X , (int)PortalPosition.Y , Portal.Width , Portal.Height );
  60.         }
  61.  
  62.         /// <summary>
  63.         /// UnloadContent will be called once per game and is the place to unload
  64.         /// all content.
  65.         /// </summary>
  66.         protected override void UnloadContent()
  67.         {
  68.             // TODO: Unload any non ContentManager content here
  69.         }
  70.  
  71.         /// <summary>
  72.         /// Allows the game to run logic such as updating the world,
  73.         /// checking for collisions, gathering input, and playing audio.
  74.         /// </summary>
  75.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  76.         protected override void Update(GameTime gameTime)
  77.         {
  78.             KeyboardState input = Keyboard.GetState();
  79.             if ( Keyboard.GetState().IsKeyDown(Keys.Escape))
  80.                 Exit();
  81.  
  82.             if ( input.IsKeyDown(Keys.Right))
  83.                 PlayerPosition.X += 2;
  84.             if ( input.IsKeyDown(Keys.Left))
  85.                 PlayerPosition.X -= 2;
  86.             if ( input.IsKeyDown(Keys.Up))
  87.                 PlayerPosition.Y -= 2;
  88.             if ( input.IsKeyDown(Keys.Down))
  89.                 PlayerPosition.Y += 2;
  90.  
  91.             if ( PlayerBoundingBox.Intersects(PortalBoundingBox) )
  92.             {
  93.                 Collision = true;
  94.             }
  95.             else{ Collision = false; }
  96.  
  97.  
  98.             base.Update(gameTime);
  99.         }
  100.  
  101.         /// <summary>
  102.         /// This is called when the game should draw itself.
  103.         /// </summary>
  104.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  105.         protected override void Draw(GameTime gameTime)
  106.         {
  107.             if ( Collision )
  108.             {
  109.                 GraphicsDevice.Clear(Color.Orange);
  110.             }
  111.             else { GraphicsDevice.Clear(Color.CornflowerBlue); }
  112.  
  113.  
  114.             spriteBatch.Begin();
  115.  
  116.             PlayerBoundingBox = new Rectangle((int)PlayerPosition.X , (int)PlayerPosition.Y , Player.Width , Player.Height );
  117.             spriteBatch.Draw(Player,PlayerPosition,Color.White);
  118.  
  119.             spriteBatch.Draw(Portal,PortalBoundingBox,Color.Blue);
  120.  
  121.             spriteBatch.End();
  122.            
  123.  
  124.             base.Draw(gameTime);
  125.         }
  126.     }
  127. }
Add Comment
Please, Sign In to add comment