Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Explorer
  4. {
  5. class MainClass
  6. {
  7. public static void Main(string[] args)
  8. {
  9. //Color console
  10. Console.ForegroundColor = ConsoleColor.Black;
  11. Console.BackgroundColor = ConsoleColor.White;
  12. Console.Clear();
  13.  
  14. //Generate & display the map
  15. Map map = new Map(20, 60);
  16. map.display();
  17. Map.Character hero = new Map.Character(10, 1, 0);
  18.  
  19. //Random objective position
  20. int r_x = map.randomnumber_x();
  21. int r_y = map.randomnumber_y();
  22.  
  23. //Play
  24. while (map.is_over(r_x,r_y) == 2)
  25. {
  26. Console.SetCursorPosition(r_x, r_y);
  27. Console.Write("O");
  28. map.update();
  29.  
  30. }
  31. if (map.is_over(r_x, r_y) == 0)
  32. {
  33. Console.Clear();
  34. string win = "YOU WIN, YOU REACH THE OBJECTIVE";
  35. Console.SetCursorPosition((Console.WindowWidth - win.Length) / 2, (Console.WindowHeight - 1) / 2);
  36. Console.WriteLine(win);
  37. Console.Read();
  38. }
  39. if (map.is_over(r_x, r_y) == 1)
  40. {
  41. Console.Clear();
  42. string lose = "YOU DIE :(";
  43. Console.SetCursorPosition((Console.WindowWidth - lose.Length) / 2, (Console.WindowHeight - 1) / 2);
  44. Console.WriteLine(lose);
  45. Console.Read();
  46. }
  47.  
  48.  
  49.  
  50. }
  51. public class Map
  52. {
  53. char[,] map;
  54. int width;
  55. int height;
  56. //int obj_x;
  57. //int obj_y;
  58. public Character hero = new Character(10,1,0);
  59.  
  60.  
  61.  
  62.  
  63. // random objective
  64. public int randomnumber_x()
  65. {
  66. Random rnum = new Random();
  67. int r_x = rnum.Next(1, height);
  68. return (r_x);
  69. }
  70.  
  71.  
  72. public int randomnumber_y()
  73. {
  74. Random rnum = new Random();
  75. int r_y = rnum.Next(1, width);
  76. return (r_y);
  77. }
  78.  
  79.  
  80.  
  81.  
  82. //build the map
  83. public Map(int width,int height)
  84. {
  85. this.width = width;
  86. this.height = height;
  87. hero.x = 1;
  88. hero.y = 0;
  89.  
  90. map = new char[width, height];
  91. for (int i = 0; i < map.GetLength(0);i++)
  92. {
  93. for (int j = 0; j < map.GetLength(1);j++)
  94. {
  95. map[i, j] = ' ';
  96. }
  97. }
  98. int c = 0;
  99. Random rand = new Random();
  100.  
  101. while (c<=15)
  102. {
  103.  
  104. int xpos = rand.Next(0, width);
  105. int ypos = rand.Next(0, height);
  106. map[xpos, ypos] = 'X';
  107. c += 1;
  108. }
  109.  
  110. }
  111.  
  112.  
  113.  
  114. public void display()
  115. {
  116.  
  117. for (int i = 0; i < map.GetLength(0);i++)
  118. {
  119. Console.Write("#");
  120. for (int j = 0; j < map.GetLength(1);j++)
  121. {
  122.  
  123. Console.Write(map[i, j]);
  124.  
  125.  
  126. }
  127. Console.WriteLine("#");
  128.  
  129. }
  130.  
  131.  
  132.  
  133. Console.WriteLine("Life: " + hero.life + "HP");
  134. Console.WriteLine("RULES : \n O is the objective to reach. \n x are the ennemies \n + is your character");
  135.  
  136. Console.SetCursorPosition(hero.x, hero.y);
  137. Console.Write("+");
  138. }
  139.  
  140.  
  141. public void update()
  142. {
  143. //wait for User Enter
  144. var UserEnter = Console.ReadKey();
  145. if (UserEnter.Key == ConsoleKey.UpArrow)
  146. {
  147. hero.move(0, -1);
  148.  
  149. }
  150. if (UserEnter.Key == ConsoleKey.DownArrow)
  151. {
  152. hero.move(0, 1);
  153.  
  154.  
  155. }
  156. if (UserEnter.Key == ConsoleKey.LeftArrow)
  157. {
  158. hero.move(-1, 0);
  159.  
  160.  
  161. }
  162. if (UserEnter.Key == ConsoleKey.RightArrow)
  163. {
  164. hero.move(1, 0);
  165.  
  166.  
  167. }
  168. /*if (map[hero.x+1,hero.y]=='X' || map[hero.x,hero.y+1]=='X' || map[hero.x-1,hero.y]=='X'||map[hero.x,hero.y-1]=='X' && UserEnter.Key !=ConsoleKey.Enter)
  169. {
  170. hero.life -= 1;
  171.  
  172. }*/
  173. /*if (map[hero.x+1,hero.y]=='X' || map[hero.x,hero.y+1]=='X' || map[hero.x-1,hero.y]=='X'||map[hero.x,hero.y-1]=='X' && UserEnter.Key ==ConsoleKey.Enter)
  174. {
  175. map[hero.x + 1, hero.y] = map[hero.x, hero.y + 1] = map[hero.x - 1, hero.y] = map[hero.x,hero.y-1]=' ';
  176. }*/
  177.  
  178.  
  179.  
  180. //Draw map
  181. Console.Clear();
  182. for (int i = 0; i < map.GetLength(0);i++)
  183. {
  184. Console.Write("#");
  185. for (int j = 0; j < map.GetLength(1);j++)
  186. {
  187.  
  188. Console.Write(map[i, j]);
  189.  
  190. }
  191. Console.WriteLine("#");
  192.  
  193. }
  194.  
  195.  
  196. // Stay in the grid
  197. if (hero.x <= 0)
  198. {
  199. hero.x += 1;
  200. }
  201. if (hero.x > height)
  202. {
  203. hero.x -= 1;
  204. }
  205. if (hero.y < 0)
  206. {
  207. hero.y += 1;
  208. }
  209. if (hero.y >= width)
  210. {
  211. hero.y -= 1;
  212. }
  213.  
  214. //write info
  215. Console.WriteLine("Life: " + hero.life +"HP");
  216. Console.WriteLine("RULES : \n O is the objective to reach. \n x are the ennemies \n + is your character");
  217. Console.WriteLine("x= " + hero.x + "y= " + hero.y);
  218.  
  219. //move the character
  220. Console.SetCursorPosition(hero.x , hero.y);
  221. Console.Write("+");
  222.  
  223.  
  224. }
  225.  
  226.  
  227. public int is_over(int r_x, int r_y)
  228. {
  229. if (hero.x == r_x && hero.y == r_y )
  230. {
  231. return (0);
  232. }
  233. if (hero.life == 0)
  234. {
  235. return (1);
  236. }
  237. else
  238. {
  239. return (2);
  240. }
  241.  
  242. }
  243.  
  244.  
  245.  
  246. public class Character
  247. {
  248. public int life { get; set; }
  249. public int x{ get; set;}
  250. public int y { get; set;}
  251.  
  252.  
  253.  
  254. public Character(int life, int x, int y)
  255. {
  256. this.x = x;
  257. this.y = y;
  258. this.life = life;
  259. }
  260.  
  261. public void move(int dx, int dy)
  262. {
  263. x += dx;
  264. y += dy;
  265.  
  266. }
  267.  
  268.  
  269. }
  270.  
  271.  
  272. public class Ennemy : Character
  273. {
  274. public Ennemy(int life, int x, int y): base(life,x,y)
  275. {
  276.  
  277. }
  278.  
  279. }
  280.  
  281.  
  282. }
  283. }
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement