Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input.Touch;
- using System;
- namespace AdMobTest
- {
- public interface IAdProvider
- {
- bool AdReady { get; }
- void ShowAd();
- }
- /// <summary>
- /// This is the main type for your game.
- /// </summary>
- public class Game1 : Game
- {
- private GraphicsDeviceManager _graphics;
- private SpriteBatch _spriteBatch;
- private Texture2D _pixel;
- private Rectangle _buttonRect = new Rectangle(100, 100, 500, 300);
- private bool _isTouched = false;
- private IAdProvider _adProvider;
- public Game1(IAdProvider adProvider)
- {
- _adProvider = adProvider ?? throw new ArgumentNullException();
- _graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- _graphics.IsFullScreen = true;
- _graphics.PreferredBackBufferWidth = 1280;
- _graphics.PreferredBackBufferHeight = 720;
- _graphics.SupportedOrientations = DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight;
- }
- /// <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
- 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
- _pixel = new Texture2D(_graphics.GraphicsDevice, 1, 1);
- _pixel.SetData<Color>(new Color[] { Color.White });
- }
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// game-specific 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)
- {
- bool wasTouched = _isTouched == true;
- _isTouched = false;
- foreach (var touch in TouchPanel.GetState())
- {
- _isTouched = _buttonRect.Contains(touch.Position);
- if (_isTouched == true)
- break;
- }
- if (wasTouched && !_isTouched)
- {
- _adProvider.ShowAd();
- }
- 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);
- // TODO: Add your drawing code here
- base.Draw(gameTime);
- Color mask = Color.Red;
- if (_adProvider.AdReady == true)
- {
- mask = (_isTouched) ? Color.Green : Color.LightGreen;
- }
- _spriteBatch.Begin();
- _spriteBatch.Draw(_pixel, _buttonRect, mask);
- _spriteBatch.End();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement