Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.03 KB | None | 0 0
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Game.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9.  
  10. #region Using Statements
  11. using System;
  12. using System.Collections.Generic;
  13. using Microsoft.Xna.Framework;
  14. using Microsoft.Xna.Framework.Audio;
  15. using Microsoft.Xna.Framework.Content;
  16. using Microsoft.Xna.Framework.Graphics;
  17. using Microsoft.Xna.Framework.Input;
  18. using Microsoft.Xna.Framework.Storage;
  19. #endregion
  20.  
  21.  
  22.  
  23. namespace RectangleCollision
  24. {
  25.     /// <summary>
  26.     /// This is the main type for your game
  27.     /// </summary>
  28.     public class RectangleCollisionGame : Microsoft.Xna.Framework.Game
  29.     {
  30.         GraphicsDeviceManager graphics;
  31.  
  32.         // The images we will draw
  33.         Texture2D personTexture;
  34.         Vector2 mPosition;
  35.         Texture2D mSpriteTexture;
  36.  
  37.  
  38.         // The images will be drawn with this SpriteBatch
  39.         SpriteBatch spriteBatch;
  40.  
  41.         // Person
  42.         Vector2 personPosition;
  43.         const int PersonMoveSpeed = 2;
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.         // The sub-rectangle of the drawable area which should be visible on all TVs
  51.         Rectangle safeBounds;
  52.         // Percentage of the screen on every side is the safe area
  53.         const float SafeAreaPortion = 0.00f;
  54.  
  55.  
  56.         public RectangleCollisionGame()
  57.         {
  58.             graphics = new GraphicsDeviceManager(this);
  59.             Content.RootDirectory = "Content";
  60.         }
  61.  
  62.  
  63.         /// <summary>
  64.         /// Allows the game to perform any initialization it needs to before starting to
  65.         /// run. This is where it can query for any required services and load any
  66.         /// non-graphic related content.  Calling base.Initialize will enumerate through
  67.         /// any components and initialize them as well.
  68.         /// </summary>
  69.         protected override void Initialize()
  70.         {
  71.             base.Initialize();
  72.  
  73.             // Calculate safe bounds based on current resolution
  74.             Viewport viewport = graphics.GraphicsDevice.Viewport;
  75.             safeBounds = new Rectangle(
  76.                 (int)(viewport.Width * SafeAreaPortion),
  77.                 (int)(viewport.Height * SafeAreaPortion),
  78.                 (int)(viewport.Width * (1 - 2 * SafeAreaPortion)),
  79.                 (int)(viewport.Height * (1 - 2 * SafeAreaPortion)));
  80.  
  81.             // Start the player in the center along the bottom of the screen
  82.             personPosition.X = (safeBounds.Width - personTexture.Width) / 2;
  83.             personPosition.Y = safeBounds.Height - personTexture.Height;
  84.         }
  85.  
  86.  
  87.         /// <summary>
  88.         /// Load your graphics content.
  89.         /// </summary>
  90.         ///
  91.  
  92.         protected override void LoadContent()
  93.         {
  94.             // Load textures
  95.  
  96.             personTexture = Content.Load<Texture2D>("Person");
  97.             mSpriteTexture = this.Content.Load<Texture2D>("Person");
  98.  
  99.  
  100.  
  101.             // Create a sprite batch to draw those textures
  102.             spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
  103.         }
  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.         ///
  112.  
  113.  
  114.         protected override void Update(GameTime gameTime)
  115.         {
  116.  
  117.             // Get input
  118.             KeyboardState keyboard = Keyboard.GetState();
  119.             GamePadState gamePad = GamePad.GetState(PlayerIndex.One);
  120.  
  121.             // Allows the game to exit
  122.             if (gamePad.Buttons.Back == ButtonState.Pressed ||
  123.                 keyboard.IsKeyDown(Keys.Escape))
  124.             {
  125.                 this.Exit();
  126.             }
  127.  
  128.             // Move the player left and right with arrow keys or d-pad
  129.             bool haut = false;
  130.             bool bas = false;
  131.             int u = 0;
  132.             int d = 0;
  133.             if (keyboard.IsKeyDown(Keys.Left) ||
  134.             gamePad.DPad.Right == ButtonState.Pressed)
  135.             {
  136.                 personPosition.X -= PersonMoveSpeed;
  137.  
  138.                 mPosition = new Vector2(personPosition.X - 10, personPosition.Y);
  139.             }
  140.  
  141.  
  142.             if (keyboard.IsKeyDown(Keys.Right) ||
  143.                 gamePad.DPad.Right == ButtonState.Pressed)
  144.             {
  145.                 personPosition.X += PersonMoveSpeed;
  146.                 mPosition = new Vector2(personPosition.X + 10, personPosition.Y);
  147.             }
  148.  
  149.             if (keyboard.IsKeyDown(Keys.Down))
  150.             {
  151.                 bas = true;
  152.                 haut = false;
  153.             }
  154.             if (keyboard.IsKeyDown(Keys.Up))
  155.             {
  156.                 bas = false;
  157.                 haut = true;
  158.             }
  159.  
  160.             if (haut == true)
  161.             {
  162.                 personPosition.Y -= PersonMoveSpeed;
  163.             }
  164.  
  165.             if (bas == true)
  166.             {
  167.                 personPosition.Y += PersonMoveSpeed;
  168.             }
  169.            
  170.            
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.             // Prevent the person from moving off of the screen
  182.             personPosition.X = MathHelper.Clamp(personPosition.X, safeBounds.Left, safeBounds.Right - personTexture.Width);
  183.             personPosition.Y = MathHelper.Clamp(personPosition.Y, safeBounds.Left, safeBounds.Height - personTexture.Height);
  184.  
  185.  
  186.         }
  187.  
  188.  
  189.  
  190.  
  191.  
  192.         /// <summary>
  193.         /// This is called when the game should draw itself.
  194.         /// </summary>
  195.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  196.         protected override void Draw(GameTime gameTime)
  197.         {
  198.             GraphicsDevice device = graphics.GraphicsDevice;
  199.  
  200.  
  201.  
  202.             spriteBatch.Begin();
  203.  
  204.             // Draw person
  205.  
  206.             spriteBatch.Draw(personTexture, personPosition, Color.White);
  207.  
  208.  
  209.  
  210.             spriteBatch.End();
  211.  
  212.  
  213.             base.Draw(gameTime);
  214.         }
  215.     }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement