Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region File Description
- //-----------------------------------------------------------------------------
- // Game.cs
- //
- // Microsoft XNA Community Game Platform
- // Copyright (C) Microsoft Corporation. All rights reserved.
- //-----------------------------------------------------------------------------
- #endregion
- #region Using Statements
- using System;
- using System.Collections.Generic;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Storage;
- #endregion
- namespace RectangleCollision
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class RectangleCollisionGame : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- // The images we will draw
- Texture2D personTexture;
- Vector2 mPosition;
- Texture2D mSpriteTexture;
- // The images will be drawn with this SpriteBatch
- SpriteBatch spriteBatch;
- // Person
- Vector2 personPosition;
- const int PersonMoveSpeed = 2;
- // The sub-rectangle of the drawable area which should be visible on all TVs
- Rectangle safeBounds;
- // Percentage of the screen on every side is the safe area
- const float SafeAreaPortion = 0.00f;
- public RectangleCollisionGame()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- }
- /// <summary>
- /// Allows the game to perform any initialization it needs to before starting to
- /// run. This is where it can query for any required services and load any
- /// non-graphic related content. Calling base.Initialize will enumerate through
- /// any components and initialize them as well.
- /// </summary>
- protected override void Initialize()
- {
- base.Initialize();
- // Calculate safe bounds based on current resolution
- Viewport viewport = graphics.GraphicsDevice.Viewport;
- safeBounds = new Rectangle(
- (int)(viewport.Width * SafeAreaPortion),
- (int)(viewport.Height * SafeAreaPortion),
- (int)(viewport.Width * (1 - 2 * SafeAreaPortion)),
- (int)(viewport.Height * (1 - 2 * SafeAreaPortion)));
- // Start the player in the center along the bottom of the screen
- personPosition.X = (safeBounds.Width - personTexture.Width) / 2;
- personPosition.Y = safeBounds.Height - personTexture.Height;
- }
- /// <summary>
- /// Load your graphics content.
- /// </summary>
- ///
- protected override void LoadContent()
- {
- // Load textures
- personTexture = Content.Load<Texture2D>("Person");
- mSpriteTexture = this.Content.Load<Texture2D>("Person");
- // Create a sprite batch to draw those textures
- spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
- }
- /// <summary>
- /// Allows the game to run logic such as updating the world,
- /// checking for collisions, gathering input and playing audio.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- ///
- protected override void Update(GameTime gameTime)
- {
- // Get input
- KeyboardState keyboard = Keyboard.GetState();
- GamePadState gamePad = GamePad.GetState(PlayerIndex.One);
- // Allows the game to exit
- if (gamePad.Buttons.Back == ButtonState.Pressed ||
- keyboard.IsKeyDown(Keys.Escape))
- {
- this.Exit();
- }
- // Move the player left and right with arrow keys or d-pad
- bool haut = false;
- bool bas = false;
- int u = 0;
- int d = 0;
- if (keyboard.IsKeyDown(Keys.Left) ||
- gamePad.DPad.Right == ButtonState.Pressed)
- {
- personPosition.X -= PersonMoveSpeed;
- mPosition = new Vector2(personPosition.X - 10, personPosition.Y);
- }
- if (keyboard.IsKeyDown(Keys.Right) ||
- gamePad.DPad.Right == ButtonState.Pressed)
- {
- personPosition.X += PersonMoveSpeed;
- mPosition = new Vector2(personPosition.X + 10, personPosition.Y);
- }
- if (keyboard.IsKeyDown(Keys.Down))
- {
- bas = true;
- haut = false;
- }
- if (keyboard.IsKeyDown(Keys.Up))
- {
- bas = false;
- haut = true;
- }
- if (haut == true)
- {
- personPosition.Y -= PersonMoveSpeed;
- }
- if (bas == true)
- {
- personPosition.Y += PersonMoveSpeed;
- }
- // Prevent the person from moving off of the screen
- personPosition.X = MathHelper.Clamp(personPosition.X, safeBounds.Left, safeBounds.Right - personTexture.Width);
- personPosition.Y = MathHelper.Clamp(personPosition.Y, safeBounds.Left, safeBounds.Height - personTexture.Height);
- }
- /// <summary>
- /// This is called when the game should draw itself.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice device = graphics.GraphicsDevice;
- spriteBatch.Begin();
- // Draw person
- spriteBatch.Draw(personTexture, personPosition, Color.White);
- spriteBatch.End();
- base.Draw(gameTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement