Game1.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System.Collections.Generic;
  5. using Tutorial023.Misc;
  6. using Tutorial023.Sprites;
  7. namespace Tutorial023
  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. public static int ScreenWidth = 1280;
  17. public static int ScreenHeight = 720;
  18. private List<ScrollingBackground> _scrollingBackgrounds;
  19. private Player _player;
  20. public Game1()
  21. {
  22. graphics = new GraphicsDeviceManager(this);
  23. Content.RootDirectory = "Content";
  24. }
  25. /// <summary>
  26. /// Allows the game to perform any initialization it needs to before starting to run.
  27. /// This is where it can query for any required services and load any non-graphic
  28. /// related content. Calling base.Initialize will enumerate through any components
  29. /// and initialize them as well.
  30. /// </summary>
  31. protected override void Initialize()
  32. {
  33. graphics.PreferredBackBufferWidth = ScreenWidth;
  34. graphics.PreferredBackBufferHeight = ScreenHeight;
  35. graphics.ApplyChanges();
  36. base.Initialize();
  37. }
  38. /// <summary>
  39. /// LoadContent will be called once per game and is the place to load
  40. /// all of your content.
  41. /// </summary>
  42. protected override void LoadContent()
  43. {
  44. // Create a new SpriteBatch, which can be used to draw textures.
  45. spriteBatch = new SpriteBatch(GraphicsDevice);
  46. var boyTexture = Content.Load<Texture2D>("boy");
  47. _player = new Player(boyTexture)
  48. {
  49. Position = new Vector2(50, (ScreenHeight - boyTexture.Height) - 20),
  50. Layer = 1f,
  51. };
  52. _scrollingBackgrounds = new List<ScrollingBackground>()
  53. {
  54. new ScrollingBackground(Content.Load<Texture2D>("ScrollingBackgrounds/Trees"), _player, 60f)
  55. {
  56. Layer = 0.99f,
  57. },
  58. new ScrollingBackground(Content.Load<Texture2D>("ScrollingBackgrounds/Floor"), _player, 60f)
  59. {
  60. Layer = 0.9f,
  61. },
  62. new ScrollingBackground(Content.Load<Texture2D>("ScrollingBackgrounds/Hills_Front"), _player, 40f)
  63. {
  64. Layer = 0.8f,
  65. },
  66. new ScrollingBackground(Content.Load<Texture2D>("ScrollingBackgrounds/Hills_Middle"), _player, 30f)
  67. {
  68. Layer = 0.79f,
  69. },
  70. new ScrollingBackground(Content.Load<Texture2D>("ScrollingBackgrounds/Clouds_Fast"), _player, 25f, true)
  71. {
  72. Layer = 0.78f,
  73. },
  74. new ScrollingBackground(Content.Load<Texture2D>("ScrollingBackgrounds/Hills_Back"), _player, 0f)
  75. {
  76. Layer = 0.77f,
  77. },
  78. new ScrollingBackground(Content.Load<Texture2D>("ScrollingBackgrounds/Clouds_Slow"), _player, 10f, true)
  79. {
  80. Layer = 0.7f,
  81. },
  82. new ScrollingBackground(Content.Load<Texture2D>("ScrollingBackgrounds/Sky"), _player, 0f)
  83. {
  84. Layer = 0.1f,
  85. },
  86. };
  87. }
  88. /// <summary>
  89. /// UnloadContent will be called once per game and is the place to unload
  90. /// game-specific content.
  91. /// </summary>
  92. protected override void UnloadContent()
  93. {
  94. // TODO: Unload any non ContentManager content here
  95. }
  96. /// <summary>
  97. /// Allows the game to run logic such as updating the world,
  98. /// checking for collisions, gathering input, and playing audio.
  99. /// </summary>
  100. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  101. protected override void Update(GameTime gameTime)
  102. {
  103. _player.Update(gameTime);
  104. foreach (var sb in _scrollingBackgrounds)
  105. sb.Update(gameTime);
  106. base.Update(gameTime);
  107. }
  108. /// <summary>
  109. /// This is called when the game should draw itself.
  110. /// </summary>
  111. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  112. protected override void Draw(GameTime gameTime)
  113. {
  114. GraphicsDevice.Clear(Color.CornflowerBlue);
  115. spriteBatch.Begin(SpriteSortMode.FrontToBack, null, SamplerState.PointClamp);
  116. _player.Draw(gameTime, spriteBatch);
  117. foreach (var sb in _scrollingBackgrounds)
  118. sb.Draw(gameTime, spriteBatch);
  119. spriteBatch.End();
  120. base.Draw(gameTime);
  121. }
  122. }
  123. }