PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Programing/C#/OOP/08.TeamGame/TeamGame/Engine.cs

https://github.com/AssiNET/TelerikHomework
C# | 189 lines | 149 code | 32 blank | 8 comment | 4 complexity | 2c113f22e19f09ce0b9b0f12ef2bf99f MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Media;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace TeamGame
  9. {
  10. public class Engine
  11. {
  12. IRenderer renderer;
  13. IUserInterface userInterface;
  14. public List<GameObject> allObjects;
  15. public List<MovingObject> movingObjects;
  16. public List<GameObject> staticObjects;
  17. public SnakeHead snakeHead = new SnakeHead(new MatrixCoords(7, 4),
  18. new MatrixCoords(0, 1), 20);
  19. private int sleepTimeout;
  20. public Engine(IRenderer renderer, IUserInterface userInterface, int sleepTimeout = 10)
  21. {
  22. this.renderer = renderer;
  23. this.userInterface = userInterface;
  24. this.allObjects = new List<GameObject>();
  25. this.movingObjects = new List<MovingObject>();
  26. this.staticObjects = new List<GameObject>();
  27. this.sleepTimeout = sleepTimeout;
  28. //02.
  29. }
  30. private void AddStaticObject(GameObject obj)
  31. {
  32. this.staticObjects.Add(obj);
  33. this.allObjects.Add(obj);
  34. }
  35. private void AddMovingObject(MovingObject obj)
  36. {
  37. this.movingObjects.Add(obj);
  38. this.allObjects.Add(obj);
  39. }
  40. public virtual void AddObject(GameObject obj)
  41. {
  42. if (obj is MovingObject)
  43. {
  44. this.AddMovingObject(obj as MovingObject);
  45. }
  46. else
  47. {
  48. if (obj is SnakeHead)
  49. {
  50. AddSnake(obj);
  51. }
  52. else
  53. {
  54. this.AddStaticObject(obj);
  55. }
  56. }
  57. }
  58. /// <summary>
  59. /// 03.Since the player uses one racket only, we should remove the
  60. /// existing racket before introducing another one.
  61. /// </summary>
  62. private void RemoveRacket()
  63. {
  64. this.movingObjects.RemoveAll(obj => obj is SnakeHead);
  65. this.allObjects.RemoveAll(obj => obj is SnakeHead);
  66. this.staticObjects.RemoveAll(obj => obj is SnakeHead);
  67. }
  68. private void AddSnake(GameObject obj)
  69. {
  70. RemoveRacket();
  71. this.snakeHead = obj as SnakeHead;
  72. this.AddObject(obj);
  73. }
  74. public virtual void MovePlayerRacketLeft()
  75. {
  76. this.snakeHead.MoveLeft();
  77. }
  78. public virtual void MovePlayerRacketRight()
  79. {
  80. this.snakeHead.MoveRight();
  81. }
  82. public virtual void MovePlayerRacketUp()
  83. {
  84. this.snakeHead.MoveUp();
  85. }
  86. public virtual void MovePlayerRacketDown()
  87. {
  88. this.snakeHead.MoveDown();
  89. }
  90. // Game Over method
  91. public static void Over()
  92. {
  93. Console.Clear();
  94. Console.SetCursorPosition((int)(Console.WindowWidth / 2.5), (int)(Console.WindowHeight / 2.5));
  95. Console.ForegroundColor = ConsoleColor.Red;
  96. Console.WriteLine("G A M E O V E R!");
  97. SystemSounds.Hand.Play();
  98. Console.WriteLine();
  99. Console.SetCursorPosition((int)(Console.WindowWidth / 2.6), (Console.WindowHeight / 2));
  100. Console.WriteLine("Your Score: {0} points", SnakeHead.Score);
  101. Thread.Sleep(1000);
  102. Console.ReadKey();
  103. }
  104. // Start Game
  105. public static void Start(int seconds)
  106. {
  107. Console.SetCursorPosition((int)(Console.WindowWidth / 2.5), (int)(Console.WindowHeight / 2.5));
  108. Console.WriteLine("G E T R E A D Y...");
  109. Console.WriteLine();
  110. Console.SetCursorPosition((int)(Console.WindowWidth / 2.9), (Console.WindowHeight / 2));
  111. Console.WriteLine("F O R P Y T H O N K A A!");
  112. Console.ForegroundColor = ConsoleColor.White;
  113. Thread.Sleep(2000);
  114. while (seconds > 0)
  115. {
  116. Console.Clear();
  117. Console.ForegroundColor = ConsoleColor.Green;
  118. Console.SetCursorPosition(Console.WindowWidth / 3, Console.WindowHeight / 2);
  119. Console.WriteLine("Game will start in {0} seconds.", seconds);
  120. SystemSounds.Asterisk.Play();
  121. seconds--;
  122. Thread.Sleep(1000);
  123. }
  124. SystemSounds.Exclamation.Play();
  125. }
  126. public virtual void Run()
  127. {
  128. // Start Game
  129. Engine.Start(3);
  130. while (true)
  131. {
  132. this.renderer.RenderAll();
  133. System.Threading.Thread.Sleep(sleepTimeout);
  134. this.userInterface.ProcessInput();
  135. this.renderer.ClearQueue();
  136. foreach (var obj in this.allObjects)
  137. {
  138. obj.Update();
  139. this.renderer.EnqueueForRendering(obj);
  140. }
  141. CollisionDispatcher.HandleCollisions(this.movingObjects, this.staticObjects);
  142. List<GameObject> producedObjects = new List<GameObject>();
  143. foreach (var obj in this.allObjects)
  144. {
  145. producedObjects.AddRange(obj.ProduceObjects());
  146. }
  147. this.allObjects.RemoveAll(obj => obj.IsDestroyed);
  148. this.movingObjects.RemoveAll(obj => obj.IsDestroyed);
  149. this.staticObjects.RemoveAll(obj => obj.IsDestroyed);
  150. foreach (var obj in producedObjects)
  151. {
  152. this.AddObject(obj);
  153. }
  154. }
  155. }
  156. }
  157. }