Advertisement
adolciaaa

Duch2

Oct 16th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 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 Ghost
  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. Texture2D ghostPic;
  22. Texture2D background;
  23. Random rand;
  24. int ghostColour;
  25. Rectangle ghostLocation;
  26. float timeRemaining;
  27. float showTime;
  28. int score;
  29. MouseState mouseState, mouseStatePrevious;
  30.  
  31. public Game1()
  32. {
  33. graphics = new GraphicsDeviceManager(this);
  34. Content.RootDirectory = "Content";
  35. }
  36.  
  37. /// <summary>
  38. /// Allows the game to perform any initialization it needs to before starting to run.
  39. /// This is where it can query for any required services and load any non-graphic
  40. /// related content. Calling base.Initialize will enumerate through any components
  41. /// and initialize them as well.
  42. /// </summary>
  43. protected override void Initialize()
  44. {
  45. // TODO: Add your initialization logic here
  46. graphics.PreferredBackBufferHeight = 550;
  47. graphics.PreferredBackBufferWidth = 750;
  48. this.IsMouseVisible = true;
  49. this.Window.Title = "Ghost Hunt | Score: 0";
  50. this.Window.AllowUserResizing = true;
  51. showTime = 2f;
  52. rand = new Random();
  53. base.Initialize();
  54. }
  55.  
  56. /// <summary>
  57. /// LoadContent will be called once per game and is the place to load
  58. /// all of your content.
  59. /// </summary>
  60. protected override void LoadContent()
  61. {
  62. // Create a new SpriteBatch, which can be used to draw textures.
  63. spriteBatch = new SpriteBatch(GraphicsDevice);
  64. ghostPic = Content.Load<Texture2D>("GhostPic");
  65. background = Content.Load<Texture2D>("Background");
  66. // TODO: use this.Content to load your game content here
  67. }
  68.  
  69. /// <summary>
  70. /// UnloadContent will be called once per game and is the place to unload
  71. /// all content.
  72. /// </summary>
  73. protected override void UnloadContent()
  74. {
  75. // TODO: Unload any non ContentManager content here
  76. }
  77.  
  78. /// <summary>
  79. /// Allows the game to run logic such as updating the world,
  80. /// checking for collisions, gathering input, and playing audio.
  81. /// </summary>
  82. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  83. protected override void Update(GameTime gameTime)
  84. {
  85.  
  86. // Allows the game to exit
  87. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  88. this.Exit();
  89.  
  90. // TODO: Add your update logic here
  91. if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape))
  92. this.Exit();
  93.  
  94. mouseState = Mouse.GetState();
  95.  
  96. if (mouseState.LeftButton == ButtonState.Pressed && mouseStatePrevious.LeftButton == ButtonState.Released && ghostLocation.Contains(mouseState.X, mouseState.Y))
  97. {
  98. if (ghostColour == 0) score++;
  99. if (ghostColour == 1) score--;
  100. this.Window.Title = "Ghost Hunt | Score: " + score.ToString();
  101. timeRemaining = 0.0f;
  102. }
  103. else
  104. {
  105. timeRemaining = MathHelper.Max(0, timeRemaining - (float)gameTime.ElapsedGameTime.TotalSeconds);
  106. }
  107. mouseStatePrevious = mouseState;
  108. if (timeRemaining == 0.0f)
  109. {
  110. ghostColour = rand.Next(2);
  111. ghostLocation = new Rectangle(rand.Next(GraphicsDevice.Viewport.Bounds.Width - ghostPic.Width), rand.Next(GraphicsDevice.Viewport.Bounds.Height - ghostPic.Height), ghostPic.Width / 2, ghostPic.Height / 2);
  112. timeRemaining = showTime;
  113. }
  114.  
  115. base.Update(gameTime);
  116. }
  117.  
  118. /// <summary>
  119. /// This is called when the game should draw itself.
  120. /// </summary>
  121. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  122. protected override void Draw(GameTime gameTime)
  123. {
  124. GraphicsDevice.Clear(Color.CornflowerBlue);
  125.  
  126. spriteBatch.Begin(SpriteSortMode.BackToFront,BlendState.AlphaBlend);
  127. spriteBatch.Draw(background, new Rectangle(0,0,GraphicsDevice.Viewport.Bounds.Width,GraphicsDevice.Viewport.Bounds.Height), Color.White);
  128. switch (ghostColour)
  129. {
  130. case 0:
  131. spriteBatch.Draw(ghostPic, ghostLocation, Color.White * 0.7f);
  132. break;
  133. case 1:
  134. spriteBatch.Draw(ghostPic, ghostLocation, new Color(255,0,0,50));
  135. break;
  136. }
  137. spriteBatch.End();
  138.  
  139. base.Draw(gameTime);
  140. }
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement