Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region Using Statements
- using System;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Storage;
- using Microsoft.Xna.Framework.Input;
- #endregion
- namespace eightbitsexy
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- const int WINDOW_WIDTH = 1200;
- const int WINDOW_HEIGHT = 700;
- int heroHealth = 100;
- bool isDamaged;
- //draw character stuff
- Texture2D hero0;
- Rectangle drawHeroRectangle;
- Texture2D enemy0;
- Rectangle drawEnemyRectangle;
- Vector2 enemyPosition;
- Vector2 enemyVelocity;
- Texture2D heroDamaged;
- //thumbstick movement
- const int THUMBSTICK_DEFLECTION_AMOUNT = 20;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- //change resolution
- graphics.PreferredBackBufferHeight = WINDOW_HEIGHT;
- graphics.PreferredBackBufferWidth = WINDOW_WIDTH;
- IsMouseVisible = false;
- graphics.IsFullScreen = true;
- }
- /// <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()
- {
- // TODO: Add your initialization logic here
- //give enemy velocity
- enemyPosition = new Vector2(this.GraphicsDevice.Viewport.Width / 2, this.GraphicsDevice.Viewport.Height * 0.25f);
- enemyVelocity = new Vector2(0,2);
- base.Initialize();
- }
- /// <summary>
- /// LoadContent will be called once per game and is the place to load
- /// all of your content.
- /// </summary>
- protected override void LoadContent()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(GraphicsDevice);
- // TODO: use this.Content to load your game content here
- hero0 = Content.Load<Texture2D>("HeroSprite");
- drawHeroRectangle = new Rectangle(WINDOW_WIDTH / 2 - hero0.Width / 2,
- WINDOW_HEIGHT / 2 - hero0.Height / 2, hero0.Width, hero0.Height);
- enemy0 = Content.Load<Texture2D>("enemy");
- drawEnemyRectangle = new Rectangle((int)(enemyPosition.X - enemy0.Width / 2),
- (int)(enemyPosition.Y - enemy0.Height / 2), enemy0.Width, enemy0.Height);
- heroDamaged = Content.Load<Texture2D>("HeroDamaged");
- }
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// all content.
- /// </summary>
- protected override void UnloadContent()
- {
- // TODO: Unload any non ContentManager content here
- }
- /// <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)
- {
- // Allows the game to exit
- if (GamePad.GetState (PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- this.Exit ();
- if (Keyboard.GetState ().IsKeyDown (Keys.Escape)) {
- this.Exit ();
- }
- else {
- }
- //make thumbstick move character
- GamePadState gamepad = GamePad.GetState (PlayerIndex.One);
- if (gamepad.IsConnected) {
- drawHeroRectangle.X += (int)(gamepad.ThumbSticks.Left.X * THUMBSTICK_DEFLECTION_AMOUNT);
- drawHeroRectangle.Y -= (int)(gamepad.ThumbSticks.Left.Y * THUMBSTICK_DEFLECTION_AMOUNT);
- //clamp character to window
- if (drawHeroRectangle.Left < 0) {
- drawHeroRectangle.X = 0;
- }
- if (drawHeroRectangle.Right > WINDOW_WIDTH) {
- drawHeroRectangle.X = WINDOW_WIDTH - drawHeroRectangle.Width;
- }
- if (drawHeroRectangle.Top < 0) {
- drawHeroRectangle.Y = 0;
- }
- if (drawHeroRectangle.Bottom > WINDOW_HEIGHT) {
- drawHeroRectangle.Y = WINDOW_HEIGHT - drawHeroRectangle.Height;
- }
- } else {
- //make character follow mouse
- MouseState mouse = Mouse.GetState ();
- drawHeroRectangle.X = mouse.X - hero0.Width / 2;
- drawHeroRectangle.Y = mouse.Y - hero0.Height / 2;
- //clamp character to window
- if (drawHeroRectangle.Left < 0) {
- drawHeroRectangle.X = 0;
- }
- if (drawHeroRectangle.Right > WINDOW_WIDTH) {
- drawHeroRectangle.X = WINDOW_WIDTH - drawHeroRectangle.Width;
- }
- if (drawHeroRectangle.Top < 0) {
- drawHeroRectangle.Y = 0;
- }
- if (drawHeroRectangle.Bottom > WINDOW_HEIGHT) {
- drawHeroRectangle.Y = WINDOW_HEIGHT - drawHeroRectangle.Height;
- }
- }
- //draw enemy on screen
- int enemyYPosition = 720;
- int enemyXPosition = 1200;
- drawEnemyRectangle.Y = enemyYPosition / 2;
- drawEnemyRectangle.X = enemyXPosition;
- if (enemyXPosition > 0) {
- enemyXPosition -= 50;
- }
- else {
- enemyXPosition = 1200;
- }
- //checks for collisions
- if (drawHeroRectangle.Intersects (drawEnemyRectangle)) {
- heroHealth--;
- isDamaged = true;
- enemyVelocity = -enemyVelocity;
- enemyPosition += enemyVelocity;
- } else {
- isDamaged = false;
- enemyPosition += enemyVelocity;
- }
- //end the game if health=0
- if (heroHealth == 0) {
- Exit();
- }
- base.Update(gameTime);
- }
- /// <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.Clear(Color.CornflowerBlue);
- //draw hero
- if (heroHealth > 0)
- {
- spriteBatch.Begin();
- spriteBatch.Draw(hero0, drawHeroRectangle, Color.White);
- spriteBatch.Draw(enemy0, drawEnemyRectangle, Color.White);
- spriteBatch.End();
- }
- base.Draw(gameTime);
- if (isDamaged == true & heroHealth > 0)
- {
- spriteBatch.Begin();
- spriteBatch.Draw(heroDamaged, drawHeroRectangle, Color.White);
- spriteBatch.End();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement