Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5.  
  6. public delegate void OnStringGet(string s);
  7. public class RandomStringInvader : Control
  8. {
  9. private bool _PauseOnMouseExit = false;
  10.  
  11. public bool PauseOnMouseExit
  12. {
  13. get
  14. {
  15. return _PauseOnMouseExit;
  16. }
  17. set
  18. {
  19. _PauseOnMouseExit = value;
  20. if(_PauseOnMouseExit)
  21. {
  22. if (boardX == int.MinValue)
  23. Stop();
  24. }
  25. else
  26. {
  27. Start();
  28. }
  29. }
  30. }
  31.  
  32. Ship[] shooters;
  33. Timer drawTimer;
  34.  
  35. List<Collectable> FallingCollectables = new List<Collectable>();
  36. Random r = new Random();
  37.  
  38. int boardX = 0;
  39. int boardY = 0;
  40. int boardWidth = 0;
  41. int boardHeight = 6;
  42.  
  43. bool Stopped = false;
  44.  
  45. public event OnStringGet OnStringGet;
  46.  
  47. public string CharMap { get; set; }
  48. struct Ship
  49. {
  50. public bool reverse;
  51. public int X;
  52. public Size Size;
  53. public void Draw(Graphics g)
  54. {
  55. g.FillRectangle(Brushes.White, new Rectangle(new Point(X, 0), Size));
  56. g.FillPolygon(Brushes.White, new Point[] { new Point(X, Size.Height / 2), new Point(X + Size.Width, Size.Height / 2), new Point(X + (Size.Width / 2), Size.Height + 3) });
  57. }
  58. }
  59.  
  60. struct Collectable
  61. {
  62. public string Letter;
  63. public int X;
  64. public int Y;
  65. }
  66.  
  67. public RandomStringInvader()
  68. {
  69. CharMap = "1234567890-=qwertyuiopasdfghjklzxcvbnm,./;'[]";
  70. BackColor = Color.Black;
  71. MinimumSize = new Size(100, 100);
  72. drawTimer = new Timer();
  73. drawTimer.Interval = 50;
  74. drawTimer.Tick += DrawTimer_Tick;
  75. drawTimer.Start();
  76. Setup();
  77. }
  78.  
  79. private void DrawTimer_Tick(object sender, EventArgs e)
  80. {
  81. for (int i = 0; i < shooters.Length; i++)
  82. {
  83. Ship shooter = shooters[i];
  84. int newX = shooter.X;
  85.  
  86. if (shooter.reverse)
  87. {
  88. newX -= this.Width / 10;
  89. if ((newX - (shooter.Size.Width / 2)) < 0)
  90. {
  91. newX = newX - (shooter.Size.Width / 2);
  92. shooter.reverse = false;
  93. }
  94. }
  95. else
  96. {
  97. newX += this.Width / 10;
  98. if (newX > this.Width - shooter.Size.Width)
  99. {
  100. newX = this.Width - shooter.Size.Width;
  101. shooter.reverse = true;
  102. }
  103. }
  104.  
  105. shooter.X = newX;
  106. shooters[i] = shooter;
  107.  
  108. var newc = new Collectable();
  109. newc.Letter = CharMap[r.Next(CharMap.Length)].ToString();
  110. newc.X = shooter.X + (shooter.Size.Width / 2);
  111. newc.Y = shooter.Size.Height + 3;
  112. FallingCollectables.Add(newc);
  113. }
  114.  
  115.  
  116.  
  117. for (int i = 0; i < FallingCollectables.Count; i++)
  118. {
  119. Collectable c = FallingCollectables[i];
  120. c.Y += 10;
  121. if (c.Y > this.Height + 10)
  122. FallingCollectables.RemoveAt(i);
  123. else
  124. FallingCollectables[i] = c;
  125. }
  126.  
  127. Invalidate();
  128. }
  129.  
  130. private void Setup()
  131. {
  132. shooters = new Ship[]
  133. {
  134. new Ship()
  135. {
  136. Size = new Size(this.Width / 10, this.Height / 10),
  137. reverse = false
  138. },
  139. new Ship()
  140. {
  141. Size = new Size(this.Width / 10, this.Height / 10),
  142. reverse = true,
  143. X = this.Width
  144. },
  145. new Ship()
  146. {
  147. Size = new Size(this.Width / 10, this.Height / 10),
  148. reverse = false,
  149. X = this.Width / 2
  150. }
  151. };
  152.  
  153. boardX = int.MinValue;
  154. boardY = this.Height - (this.Height / 5);
  155. boardWidth = this.Width / 10;
  156. }
  157.  
  158. protected override void OnPaint(PaintEventArgs e)
  159. {
  160. var g = e.Graphics;
  161.  
  162. if(boardX != int.MinValue)
  163. g.FillRectangle(Brushes.Green, new Rectangle(boardX, boardY, boardWidth, boardHeight));
  164.  
  165.  
  166.  
  167. foreach (Ship s in shooters)
  168. s.Draw(g);
  169.  
  170.  
  171. for (int i = 0; i < FallingCollectables.Count; i++)
  172. {
  173. Collectable c = FallingCollectables[i];
  174. if ((c.X > boardX && c.X < boardX + boardWidth) && (c.Y > boardY))
  175. {
  176. FallingCollectables.RemoveAt(i);
  177. var callback = OnStringGet;
  178. if (callback != null)
  179. callback(c.Letter);
  180. }
  181. else
  182. {
  183. g.DrawString(c.Letter, this.Font, Brushes.Red, new Point(c.X, c.Y));
  184. }
  185. }
  186.  
  187. base.OnPaint(e);
  188. }
  189. protected override void OnSizeChanged(EventArgs e)
  190. {
  191. base.OnSizeChanged(e);
  192. Setup();
  193. }
  194.  
  195. protected override void OnMouseMove(MouseEventArgs e)
  196. {
  197. base.OnMouseMove(e);
  198. if(!Stopped)
  199. boardX = e.X;
  200. }
  201.  
  202. protected override void OnMouseLeave(EventArgs e)
  203. {
  204. boardX = int.MinValue;
  205. if (PauseOnMouseExit)
  206. Stop();
  207. base.OnMouseLeave(e);
  208. }
  209.  
  210. protected override void OnMouseEnter(EventArgs e)
  211. {
  212. if (PauseOnMouseExit)
  213. Start();
  214. base.OnMouseEnter(e);
  215. }
  216.  
  217. public void Stop()
  218. {
  219. Stopped = true;
  220. boardX = int.MinValue;
  221. drawTimer.Stop();
  222. }
  223.  
  224.  
  225. public void Start()
  226. {
  227. Stopped = false;
  228. drawTimer.Start();
  229. }
  230.  
  231. public void ClearCharacters()
  232. {
  233. FallingCollectables.Clear();
  234. Invalidate();
  235. }
  236.  
  237.  
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement