Advertisement
Guest User

MonoGame_AdMobCrash_Game1.cs

a guest
Nov 22nd, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.08 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input.Touch;
  4. using System;
  5.  
  6. namespace AdMobTest
  7. {
  8.     public interface IAdProvider
  9.     {
  10.         bool AdReady { get; }
  11.         void ShowAd();
  12.     }
  13.  
  14.     /// <summary>
  15.     /// This is the main type for your game.
  16.     /// </summary>
  17.     public class Game1 : Game
  18.     {
  19.         private GraphicsDeviceManager _graphics;
  20.         private SpriteBatch _spriteBatch;
  21.         private Texture2D _pixel;
  22.         private Rectangle _buttonRect = new Rectangle(100, 100, 500, 300);
  23.         private bool _isTouched = false;
  24.         private IAdProvider _adProvider;
  25.  
  26.         public Game1(IAdProvider adProvider)
  27.         {
  28.             _adProvider = adProvider ?? throw new ArgumentNullException();
  29.  
  30.             _graphics = new GraphicsDeviceManager(this);
  31.             Content.RootDirectory = "Content";
  32.  
  33.             _graphics.IsFullScreen = true;
  34.             _graphics.PreferredBackBufferWidth = 1280;
  35.             _graphics.PreferredBackBufferHeight = 720;
  36.             _graphics.SupportedOrientations = DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight;
  37.         }
  38.  
  39.         /// <summary>
  40.         /// Allows the game to perform any initialization it needs to before starting to run.
  41.         /// This is where it can query for any required services and load any non-graphic
  42.         /// related content.  Calling base.Initialize will enumerate through any components
  43.         /// and initialize them as well.
  44.         /// </summary>
  45.         protected override void Initialize()
  46.         {
  47.             // TODO: Add your initialization logic here
  48.  
  49.             base.Initialize();
  50.         }
  51.  
  52.         /// <summary>
  53.         /// LoadContent will be called once per game and is the place to load
  54.         /// all of your content.
  55.         /// </summary>
  56.         protected override void LoadContent()
  57.         {
  58.             // Create a new SpriteBatch, which can be used to draw textures.
  59.             _spriteBatch = new SpriteBatch(GraphicsDevice);
  60.  
  61.             // TODO: use this.Content to load your game content here
  62.             _pixel = new Texture2D(_graphics.GraphicsDevice, 1, 1);
  63.             _pixel.SetData<Color>(new Color[] { Color.White });
  64.         }
  65.  
  66.         /// <summary>
  67.         /// UnloadContent will be called once per game and is the place to unload
  68.         /// game-specific content.
  69.         /// </summary>
  70.         protected override void UnloadContent()
  71.         {
  72.             // TODO: Unload any non ContentManager content here
  73.         }
  74.  
  75.         /// <summary>
  76.         /// Allows the game to run logic such as updating the world,
  77.         /// checking for collisions, gathering input, and playing audio.
  78.         /// </summary>
  79.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  80.         protected override void Update(GameTime gameTime)
  81.         {
  82.             bool wasTouched = _isTouched == true;
  83.             _isTouched = false;
  84.             foreach (var touch in TouchPanel.GetState())
  85.             {
  86.                 _isTouched = _buttonRect.Contains(touch.Position);
  87.  
  88.                 if (_isTouched == true)
  89.                     break;
  90.             }
  91.  
  92.             if (wasTouched && !_isTouched)
  93.             {
  94.                 _adProvider.ShowAd();
  95.             }
  96.  
  97.             base.Update(gameTime);
  98.         }
  99.  
  100.         /// <summary>
  101.         /// This is called when the game should draw itself.
  102.         /// </summary>
  103.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  104.         protected override void Draw(GameTime gameTime)
  105.         {
  106.             GraphicsDevice.Clear(Color.CornflowerBlue);
  107.  
  108.             // TODO: Add your drawing code here
  109.  
  110.             base.Draw(gameTime);
  111.  
  112.             Color mask = Color.Red;
  113.             if (_adProvider.AdReady == true)
  114.             {
  115.                 mask = (_isTouched) ? Color.Green : Color.LightGreen;
  116.             }
  117.  
  118.             _spriteBatch.Begin();
  119.             _spriteBatch.Draw(_pixel, _buttonRect, mask);
  120.             _spriteBatch.End();
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement