Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.30 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 helloworld4
  13. {
  14.     /// <summary>
  15.     /// This is the main type for your game
  16.     /// </summary>
  17.     public class Game1 : Microsoft.Xna.Framework.Game
  18.     {
  19.         GraphicsDeviceManager graphics;
  20.         SpriteBatch spriteBatch;
  21.  
  22.         public Game1()
  23.         {
  24.             graphics = new GraphicsDeviceManager(this);
  25.             Content.RootDirectory = "Content";
  26.         }
  27.  
  28.         /// <summary>
  29.         /// Allows the game to perform any initialization it needs to before starting to run.
  30.         /// This is where it can query for any required services and load any non-graphic
  31.         /// related content.  Calling base.Initialize will enumerate through any components
  32.         /// and initialize them as well.
  33.         /// </summary>
  34.         protected override void Initialize()
  35.         {
  36.             // TODO: Add your initialization logic here
  37.  
  38.             base.Initialize();
  39.         }
  40.  
  41.         /// <summary>
  42.         /// LoadContent will be called once per game and is the place to load
  43.         /// all of your content.
  44.         /// </summary>
  45.         ///
  46.         Texture2D player;
  47.         Vector2 spritePosition = Vector2.Zero;
  48.  
  49.  
  50.         protected override void LoadContent()
  51.         {
  52.             // Create a new SpriteBatch, which can be used to draw textures.
  53.             spriteBatch = new SpriteBatch(GraphicsDevice);
  54.             player = Content.Load<Texture2D>("shipsprite");
  55.  
  56.             // TODO: use this.Content to load your game content here
  57.         }
  58.  
  59.         /// <summary>
  60.         /// UnloadContent will be called once per game and is the place to unload
  61.         /// all content.
  62.         /// </summary>
  63.         protected override void UnloadContent()
  64.         {
  65.             // TODO: Unload any non ContentManager content here
  66.         }
  67.  
  68.         /// <summary>
  69.         /// Allows the game to run logic such as updating the world,
  70.         /// checking for collisions, gathering input, and playing audio.
  71.         /// </summary>
  72.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  73.         void UpdateSprite(GameTime gameTime)
  74.         {
  75.             // Allows the game to exit
  76.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  77.                 this.Exit();
  78.  
  79.             // TODO: Add your update logic here
  80.  
  81.             spritePosition +=
  82.                 spriteSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
  83.  
  84.             int MaxX =
  85.                 graphics.GraphicsDevice.Viewport.Width - player.Width;
  86.             int MinX = 0;
  87.  
  88.             int MaxY =
  89.                 graphics.GraphicsDevice.Viewport.Height - player.Height;
  90.             int MinY = 0;
  91.  
  92.             //check for bounce
  93.             if (spritePosition.X > MaxX)
  94.             {
  95.                 spriteSpeed.X *= -1;
  96.                 spritePosition.X = MaxX;
  97.             }
  98.  
  99.             else if (spritePosition.X < MinX)
  100.             {
  101.                 spriteSpeed.X *= -1;
  102.                 spritePosition.X = MinX;
  103.             }
  104.  
  105.             if (spritePosition.Y > MaxY)
  106.             {
  107.                 spriteSpeed.Y *= -1;
  108.                 spritePosition.Y = MaxY;
  109.             }
  110.  
  111.             else if (spritePosition.Y < MinY)
  112.             {
  113.                 spriteSpeed.Y *= -1;
  114.                 spritePosition.Y = MinY;
  115.             }
  116.  
  117.                
  118.  
  119.             base.Update(gameTime);
  120.         }
  121.  
  122.         /// <summary>
  123.         /// This is called when the game should draw itself.
  124.         /// </summary>
  125.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  126.         protected override void Draw(GameTime gameTime)
  127.         {
  128.             GraphicsDevice.Clear(Color.CornflowerBlue);
  129.  
  130.             // TODO: Add your drawing code here
  131.             spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
  132.             spriteBatch.Draw(player, spritePosition, Color.White);
  133.             spriteBatch.End();
  134.  
  135.  
  136.             base.Draw(gameTime);
  137.         }
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement