Advertisement
Guest User

Kod

a guest
Oct 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7.  
  8. //Uppgift1: Skriv en ny klass Enemy, som ärver Gameobject och som vandrar runt slumpmässigt på spelplanen
  9. //Uppgift2: Skriv en ny klass Player, som ärver Gameobject och som kan styras med tangenterna WASD
  10. namespace GridGame
  11. {
  12.  
  13. class Program
  14. {
  15.  
  16. static void Main(string[] args)
  17. {
  18. Meny startMeny = new Meny();
  19. startMeny.Main();
  20. Game myGame = new Game(40, 25);
  21. myGame.DrawBoard();
  22. while (true)
  23. {
  24. myGame.UpdateBoard();
  25. myGame.DrawBoard();
  26. }
  27.  
  28.  
  29. }
  30. }
  31.  
  32.  
  33.  
  34. class Meny
  35. {
  36. static int index = 0;
  37. public void Main()
  38. {
  39.  
  40.  
  41. List<string> menuItems = new List<string>() { "Start", "High Score", "Exit" };
  42.  
  43. Console.CursorVisible = false;
  44. bool itemSelected = false;
  45. while (itemSelected == false)
  46.  
  47. {
  48. string selectedMenuItem = drawMenu(menuItems);
  49. if (selectedMenuItem == "Start")
  50. {
  51. Console.Clear();
  52. itemSelected = true;
  53. }
  54. else if (selectedMenuItem == "Exit")
  55. {
  56. Environment.Exit(0);
  57. }
  58. }
  59. }
  60.  
  61. private static String drawMenu(List<string> items)
  62. {
  63. for (int i = 0; i < items.Count; i++)
  64. {
  65. if (i == index)
  66. {
  67. Console.BackgroundColor = ConsoleColor.Gray;
  68. Console.ForegroundColor = ConsoleColor.Black;
  69. Console.WriteLine(items[i]);
  70.  
  71. }
  72. else
  73. {
  74. Console.WriteLine(items[i]);
  75. }
  76.  
  77. Console.ResetColor();
  78. }
  79. ConsoleKeyInfo cKey = Console.ReadKey();
  80.  
  81. if (cKey.Key == ConsoleKey.DownArrow)
  82. {
  83. if (index == items.Count - 1)
  84. {
  85. index = 0;
  86. }
  87.  
  88. else { index++; }
  89. }
  90. else if (cKey.Key == ConsoleKey.UpArrow)
  91. {
  92. if (index <= 0)
  93. {
  94. index = items.Count - 1;
  95. }
  96.  
  97. else { index--; }
  98. }
  99. else if (cKey.Key == ConsoleKey.Enter)
  100. {
  101. return items[index];
  102. }
  103. else
  104. {
  105. Console.Clear();
  106. return "";
  107. }
  108.  
  109. Console.Clear();
  110. return "";
  111.  
  112. }
  113. }
  114.  
  115. class Game
  116. {
  117.  
  118. static List<GameObject> GameObjects = new List<GameObject>();
  119.  
  120. public Game(int xSize, int ySize)
  121. {
  122. for (int i = 0; i < ySize + 2; i++)
  123. {
  124. for (int j = 0; j < xSize + 2; j++)
  125. {
  126. if (j == 0 || i == 0 || i == ySize + 1 || j == xSize + 1)
  127. {
  128. GameObjects.Add(new Wall(j, i));
  129. }
  130.  
  131. }
  132. }
  133. Random RNG = new Random();
  134.  
  135. GameObjects.Add(new Player(7, 3));
  136.  
  137. GameObjects.Add(new Enemy(20, 15, RNG));
  138. GameObjects.Add(new Enemy(18, 19, RNG));
  139. GameObjects.Add(new Enemy(20, 20, RNG));
  140. GameObjects.Add(new Enemy(32, 5, RNG));
  141. GameObjects.Add(new Enemy(15, 22, RNG));
  142. GameObjects.Add(new Enemy(20, 25, RNG));
  143. GameObjects.Add(new Enemy(30, 25, RNG));
  144.  
  145. }
  146.  
  147. public void DrawBoard()
  148. {
  149. foreach (GameObject gameObject in GameObjects)
  150. {
  151. gameObject.Draw(2, 1);
  152. }
  153.  
  154.  
  155. }
  156.  
  157. public static void DrawTurn(int xBoxSize, int yBoxSize, int turn)
  158. {
  159. Console.SetCursorPosition(xBoxSize, yBoxSize);
  160. Console.Write("Turns: " + turn);
  161. }
  162.  
  163. public void UpdateBoard()
  164. {
  165. foreach (GameObject gameObject in GameObjects)
  166. {
  167. gameObject.Update(GameObjects);
  168. }
  169. int i = 0;
  170. while (i < GameObjects.Count)
  171. {
  172. if (GameObjects[i] is Enemy && !GameObjects[i].IsAlive)
  173. {
  174. GameObjects.RemoveAt(i);
  175. i--;
  176. }
  177. i++;
  178.  
  179.  
  180. }
  181.  
  182. }
  183. public static void RemoveEnemy(Enemy enemy)
  184. {
  185. GameObjects.Remove(enemy);
  186. }
  187. }
  188.  
  189. abstract class GameObject
  190. {
  191. public bool IsAlive = true;
  192. public int XPosition;
  193. public int YPosition;
  194. public abstract void Draw(int xBoxSize, int yBoxSize);
  195. public abstract void Update(List<GameObject> GameObjects);
  196. }
  197.  
  198. class Player : GameObject
  199. {
  200. int numTurns;
  201. public Player(int xPosition, int yPosition)
  202. {
  203. XPosition = xPosition;
  204. YPosition = yPosition;
  205.  
  206. }
  207.  
  208. public override void Draw(int xBoxSize, int yBoxSize)
  209. {
  210. int startX = XPosition * xBoxSize;
  211. int startY = YPosition * yBoxSize;
  212. Console.SetCursorPosition(startX, startY);
  213. Console.ForegroundColor = ConsoleColor.Blue;
  214. Console.Write("[]");
  215. Console.ResetColor();
  216. }
  217.  
  218.  
  219.  
  220. private void Erase(int xBoxSize, int yBoxSize)
  221. {
  222. int startX = XPosition * xBoxSize;
  223. int startY = YPosition * yBoxSize;
  224. Console.SetCursorPosition(startX, startY);
  225. Console.Write(" ");
  226. }
  227.  
  228.  
  229.  
  230. public override void Update(List<GameObject> GameObjects)
  231. {
  232. ConsoleKeyInfo KeyPress = Console.ReadKey(true);
  233. int tempXPosition = XPosition;
  234. int tempYPosition = YPosition;
  235. bool occupied = false;
  236.  
  237.  
  238.  
  239. Erase(2, 1);
  240.  
  241.  
  242. if (KeyPress.Key == ConsoleKey.W)
  243. {
  244. tempYPosition--;
  245. numTurns++;
  246.  
  247. }
  248. if (KeyPress.Key == ConsoleKey.A)
  249. {
  250. tempXPosition--;
  251. numTurns++;
  252. }
  253. if (KeyPress.Key == ConsoleKey.S)
  254. {
  255. tempYPosition++;
  256. numTurns++;
  257. }
  258. if (KeyPress.Key == ConsoleKey.D)
  259. {
  260. tempXPosition++;
  261. numTurns++;
  262. }
  263. if(KeyPress.Key == ConsoleKey.Escape)
  264. {
  265. Environment.Exit(0);
  266. }
  267.  
  268. Game.DrawTurn(95, 13, numTurns);
  269.  
  270. foreach (var item in GameObjects)
  271. {
  272. if (tempXPosition == item.XPosition && tempYPosition == item.YPosition)
  273. {
  274. if (item is Enemy)
  275. {
  276. occupied = true;
  277.  
  278.  
  279. }
  280. if (item is Wall)
  281. {
  282. occupied = true;
  283. }
  284. }
  285.  
  286.  
  287. }
  288. if (!occupied)
  289. {
  290. XPosition = tempXPosition;
  291. YPosition = tempYPosition;
  292. }
  293.  
  294.  
  295.  
  296. }
  297. }
  298.  
  299. class Enemy : GameObject
  300. {
  301. Random RNG;
  302. public Enemy(int xPosition, int yPosition, Random rNG)
  303. {
  304. XPosition = xPosition;
  305. YPosition = yPosition;
  306. RNG = rNG;
  307. }
  308.  
  309. public override void Draw(int xBoxSize, int yBoxSize)
  310. {
  311. int startX = XPosition * xBoxSize;
  312. int startY = YPosition * yBoxSize;
  313. Console.SetCursorPosition(startX, startY);
  314. Console.ForegroundColor = ConsoleColor.Red;
  315. Console.Write("()");
  316. Console.ResetColor();
  317. }
  318.  
  319. private void Erase(int xBoxSize, int yBoxSize)
  320. {
  321. int startX = XPosition * xBoxSize;
  322. int startY = YPosition * yBoxSize;
  323. Console.SetCursorPosition(startX, startY);
  324. Console.Write(" ");
  325.  
  326. }
  327.  
  328.  
  329.  
  330. public override void Update(List<GameObject> GameObjects)
  331. {
  332.  
  333. int slumptal = RNG.Next(1,5);
  334. int tempXPosition = XPosition;
  335. int tempYPosition = YPosition;
  336. bool occupied = false;
  337.  
  338.  
  339. if (IsAlive)
  340. {
  341. Erase(2, 1);
  342. if (slumptal == 1)
  343. {
  344. tempXPosition--;
  345. }
  346. if (slumptal == 2)
  347. {
  348. tempXPosition++;
  349. }
  350. if (slumptal == 3)
  351. {
  352. tempYPosition--;
  353. }
  354. if (slumptal == 4)
  355. {
  356. tempYPosition++;
  357. }
  358.  
  359.  
  360. foreach (var item in GameObjects)
  361. {
  362. if (tempXPosition == item.XPosition && tempYPosition == item.YPosition)
  363. {
  364. if(item is Wall)
  365. occupied = true;
  366.  
  367. if (item is Player)
  368. {
  369. occupied = false;
  370. IsAlive = false;
  371. }
  372.  
  373. }
  374.  
  375.  
  376. }
  377. if (!occupied)
  378. {
  379. XPosition = tempXPosition;
  380. YPosition = tempYPosition;
  381. }
  382.  
  383. }
  384. else
  385. {
  386. Game.RemoveEnemy(this);
  387. }
  388. }
  389. }
  390.  
  391. class Wall : GameObject
  392. {
  393. public Wall(int xPosition, int yPosition)
  394. {
  395. XPosition = xPosition;
  396. YPosition = yPosition;
  397. }
  398.  
  399. public override void Draw(int xBoxSize, int yBoxSize)
  400. {
  401. int startX = XPosition * xBoxSize;
  402. int startY = YPosition * yBoxSize;
  403. Console.SetCursorPosition(startX, startY);
  404. Console.Write("##");
  405.  
  406. }
  407.  
  408. public override void Update(List<GameObject> GameObjects)
  409. {
  410.  
  411. }
  412. }
  413.  
  414. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement