Hyluss

[Monogame PingPong] Game.cs

Oct 30th, 2018
581
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.89 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System;
  5. using System.Collections.Generic;
  6.  
  7. namespace PingPong
  8. {
  9.     /// <summary>
  10.     /// This is the main type for your game.
  11.     /// </summary>
  12.     public class Game1 : Game
  13.     {
  14.         GraphicsDeviceManager graphics;
  15.         SpriteBatch spriteBatch;
  16.  
  17.         public static int ScreenWidth;
  18.         public static int ScreenHeight;
  19.         public static Random Random;
  20.  
  21.         private Score _score;
  22.         private List<Sprite> _sprites;
  23.  
  24.         Texture2D bcgTexture;
  25.  
  26.         public Game1()
  27.         {
  28.             graphics = new GraphicsDeviceManager(this);
  29.             Content.RootDirectory = "Content";
  30.         }
  31.  
  32.         /// <summary>
  33.         /// Allows the game to perform any initialization it needs to before starting to run.
  34.         /// This is where it can query for any required services and load any non-graphic
  35.         /// related content.  Calling base.Initialize will enumerate through any components
  36.         /// and initialize them as well.
  37.         /// </summary>
  38.         protected override void Initialize()
  39.         {
  40.             // TODO: Add your initialization logic here
  41.             ScreenWidth = graphics.PreferredBackBufferWidth;
  42.             ScreenHeight = graphics.PreferredBackBufferHeight;
  43.             Random = new Random();
  44.  
  45.             base.Initialize();
  46.         }
  47.  
  48.         /// <summary>
  49.         /// LoadContent will be called once per game and is the place to load
  50.         /// all of your content.
  51.         /// </summary>
  52.         protected override void LoadContent()
  53.         {
  54.             // Create a new SpriteBatch, which can be used to draw textures.
  55.             spriteBatch = new SpriteBatch(GraphicsDevice);
  56.  
  57.             bcgTexture = Content.Load<Texture2D>("pongBackground");
  58.  
  59.             var batTexture = Content.Load<Texture2D>("paddle1a");
  60.             var batTexture2 = Content.Load<Texture2D>("paddle2a");
  61.             var ballTexture = Content.Load<Texture2D>("ball1");
  62.  
  63.             var ballAnimTexture = Content.Load<Texture2D>("ball-anim");
  64.  
  65.             _score = new Score(Content.Load<SpriteFont>("basicFont"));
  66.  
  67.             _sprites = new List<Sprite>()
  68.       {
  69.         //new Sprite(Content.Load<Texture2D>("Background")),
  70.         new Bat(batTexture)
  71.         {
  72.           Position = new Vector2(20, (ScreenHeight / 2) - (batTexture.Height / 2)),
  73.           Input = new Input()
  74.           {
  75.             Up = Keys.W,
  76.             Down = Keys.S,
  77.           }
  78.         },
  79.         new Bat(batTexture2)
  80.         {
  81.           Position = new Vector2(ScreenWidth - 20 - batTexture.Width, (ScreenHeight / 2) - (batTexture.Height / 2)),
  82.           Input = new Input()
  83.           {
  84.             Up = Keys.Up,
  85.             Down = Keys.Down,
  86.           }
  87.         },
  88.         //new Ball(ballTexture)
  89.         //{
  90.         //  Position = new Vector2((ScreenWidth / 2) - (ballTexture.Width / 2), (ScreenHeight / 2) - (ballTexture.Height / 2)),
  91.         //  Score = _score,
  92.         //}
  93.         new Ball(ballAnimTexture,5,16)
  94.         {
  95.             Position = new Vector2((ScreenWidth / 2) - (32), (ScreenHeight / 2) - (32)),
  96.             Score = _score,
  97.         }
  98.       };
  99.  
  100.         }
  101.  
  102.         /// <summary>
  103.         /// UnloadContent will be called once per game and is the place to unload
  104.         /// game-specific content.
  105.         /// </summary>
  106.         protected override void UnloadContent()
  107.         {
  108.             // TODO: Unload any non ContentManager content here
  109.         }
  110.  
  111.         /// <summary>
  112.         /// Allows the game to run logic such as updating the world,
  113.         /// checking for collisions, gathering input, and playing audio.
  114.         /// </summary>
  115.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  116.         protected override void Update(GameTime gameTime)
  117.         {
  118.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
  119.                 Exit();
  120.  
  121.             foreach (var sprite in _sprites)
  122.             {
  123.                 sprite.Update(gameTime, _sprites);
  124.             }
  125.  
  126.             base.Update(gameTime);
  127.         }
  128.  
  129.         /// <summary>
  130.         /// This is called when the game should draw itself.
  131.         /// </summary>
  132.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  133.         protected override void Draw(GameTime gameTime)
  134.         {
  135.             GraphicsDevice.Clear(Color.CornflowerBlue);
  136.  
  137.             // TODO: Add your drawing code here
  138.             spriteBatch.Begin();
  139.  
  140.             spriteBatch.Draw(bcgTexture, GraphicsDevice.Viewport.Bounds, Color.White);
  141.  
  142.             foreach (var sprite in _sprites)
  143.                 sprite.Draw(spriteBatch);
  144.  
  145.             _score.Draw(spriteBatch);
  146.  
  147.             spriteBatch.End();
  148.  
  149.             base.Draw(gameTime);
  150.         }
  151.     }
  152. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment