Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Graphics;
  6. using Microsoft.Xna.Framework.Input;
  7.  
  8. namespace Ghosts
  9. {
  10. public class Game1 : Game
  11. {
  12. GraphicsDeviceManager graphics;
  13. SpriteBatch spriteBatch;
  14. Texture2D Ghost;
  15. Texture2D Background;
  16. int posX, posY;
  17. Random random;
  18. int GhostColor;
  19. float GhostTime = 0;
  20. float GhostSleep = 2f;
  21. Rectangle posOfGhost;
  22. int WhiteScore = 0;
  23. int OtherScore = 0;
  24. bool isGhostVisible = false;
  25. List<Color> GhostColors = new List<Color>() { Color.White, Color.Red, Color.Blue };
  26. public Game1()
  27. {
  28. graphics = new GraphicsDeviceManager(this);
  29. Content.RootDirectory = "Content";
  30. graphics.PreferredBackBufferWidth = 800;
  31. graphics.PreferredBackBufferHeight = 600;
  32. IsMouseVisible = true;
  33. Window.AllowUserResizing = true;
  34. }
  35.  
  36. protected override void Initialize()
  37. {
  38. random = new Random();
  39. base.Initialize();
  40. }
  41.  
  42. protected override void LoadContent()
  43. {
  44. spriteBatch = new SpriteBatch(GraphicsDevice);
  45. Background = Content.Load<Texture2D>("street");
  46. Ghost = Content.Load<Texture2D>("ghost");
  47.  
  48. }
  49.  
  50. protected override void UnloadContent()
  51. {
  52.  
  53. }
  54.  
  55. protected override void Update(GameTime gameTime)
  56. {
  57.  
  58. var keyboard = new KeyboardState();
  59.  
  60.  
  61. var mouseState = Mouse.GetState();
  62.  
  63. var position1 = new Point(mouseState.X, mouseState.Y);
  64.  
  65. GhostTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
  66. if (Keyboard.GetState().IsKeyDown(Keys.Escape))
  67. Exit();
  68.  
  69. if (GhostTime >= GhostSleep || isGhostVisible == false && mouseState.LeftButton == ButtonState.Released)
  70. {
  71. posX = random.Next(50, GraphicsDevice.Viewport.Width - 50);
  72. posY = random.Next(50, GraphicsDevice.Viewport.Height - 50);
  73.  
  74. GhostColor = random.Next(0, 3);
  75. isGhostVisible = true;
  76. GhostTime = 0;
  77. }
  78.  
  79.  
  80. if (posOfGhost.Contains(position1) && isGhostVisible == true && mouseState.LeftButton == ButtonState.Pressed)
  81. {
  82. if (GhostColor == 0)
  83. WhiteScore++;
  84. else
  85. OtherScore++;
  86.  
  87. Window.Title = "Białe duszki: " + WhiteScore + " Inne duszki: " + OtherScore;
  88. isGhostVisible = false;
  89. }
  90.  
  91.  
  92.  
  93.  
  94. base.Update(gameTime);
  95. }
  96.  
  97. protected override void Draw(GameTime gameTime)
  98. {
  99. GraphicsDevice.Clear(Color.CornflowerBlue);
  100. spriteBatch.Begin();
  101.  
  102. spriteBatch.Draw(Background, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height),Color.White);
  103.  
  104. spriteBatch.Draw(Ghost, new Rectangle(posX, posY, 50, 50), GhostColors[GhostColor]);
  105.  
  106. posOfGhost = new Rectangle(posX, posY, 50, 50);
  107. spriteBatch.End();
  108.  
  109. base.Draw(gameTime);
  110. }
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement