Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.10 KB | None | 0 0
  1. public static string sqr = "■";
  2. public static int[,] grid = new int[23, 10];
  3. public static int[,] droppedtetrominoeLocationGrid = new int[23, 10];
  4. public static Stopwatch timer = new Stopwatch();
  5. public static Stopwatch dropTimer = new Stopwatch();
  6. public static Stopwatch inputTimer = new Stopwatch();
  7. public static int dropTime, dropRate = 300;
  8. public static bool isDropped = false;
  9. static Tetrominoe tet;
  10. static Tetrominoe nexttet;
  11. public static ConsoleKeyInfo key;
  12. public static bool isKeyPressed = false;
  13. public static int linesCleared = 0, score = 0, level = 1;
  14.  
  15. static void Main()
  16. {
  17. SoundPlayer sp = new SoundPlayer();
  18. sp.SoundLocation = Environment.CurrentDirectory + "\\01_-_Tetris_Tengen_-_NES_-_Introduction.wav";
  19. sp.PlayLooping();
  20.  
  21. drawBorder();
  22. Console.SetCursorPosition(4, 5);
  23. Console.WriteLine("Press any key");
  24. Console.ReadKey(true);
  25. sp.Stop();
  26. sp.SoundLocation = Environment.CurrentDirectory + "\\music.wav";
  27. sp.PlayLooping();
  28. timer.Start();
  29. dropTimer.Start();
  30. long time = timer.ElapsedMilliseconds;
  31. Console.SetCursorPosition(25, 0);
  32. Console.WriteLine("Level " + level);
  33. Console.SetCursorPosition(25, 1);
  34. Console.WriteLine("Score " + score);
  35. Console.SetCursorPosition(25, 2);
  36. Console.WriteLine("LinesCleared " + linesCleared);
  37. nexttet = new Tetrominoe();
  38. tet = nexttet;
  39. tet.Spawn();
  40. nexttet = new Tetrominoe();
  41.  
  42. Update();
  43.  
  44. sp.Stop();
  45. sp.SoundLocation = Environment.CurrentDirectory + "\\08_-_Tetris_Tengen_-_NES_-_Game_Over.wav";
  46. sp.Play();
  47. Console.SetCursorPosition(0, 0);
  48. Console.WriteLine("Game Over \n Replay? (Y/N)");
  49. string input = Console.ReadLine();
  50.  
  51. if (input == "y" || input == "Y")
  52. {
  53. int[,] grid = new int[23, 10];
  54. droppedtetrominoeLocationGrid = new int[23, 10];
  55. timer = new Stopwatch();
  56. dropTimer = new Stopwatch();
  57. inputTimer = new Stopwatch();
  58. dropRate = 300;
  59. isDropped = false;
  60. isKeyPressed = false;
  61. linesCleared = 0;
  62. score = 0;
  63. level = 1;
  64. GC.Collect();
  65. Console.Clear();
  66. Main();
  67. }
  68. else return;
  69.  
  70. }
  71.  
  72. private static void fillGrid()
  73. {
  74. for (int i = 0; i < 23; ++i)
  75. {
  76. Console.SetCursorPosition(1, i);
  77. for (int j = 0; j < 10; j++)
  78. {
  79. Console.Write(sqr);
  80. }
  81. Console.WriteLine();
  82. }
  83. }
  84.  
  85. private static void Update()
  86. {
  87. while (true)//Update Loop
  88. {
  89. dropTime = (int)dropTimer.ElapsedMilliseconds;
  90. if (dropTime > dropRate)
  91. {
  92. dropTime = 0;
  93. dropTimer.Restart();
  94. tet.Drop();
  95. }
  96. if (isDropped == true)
  97. {
  98. tet = nexttet;
  99. nexttet = new Tetrominoe();
  100. tet.Spawn();
  101.  
  102. isDropped = false;
  103. }
  104. int j;
  105. for (j = 0; j < 10; j++)
  106. {
  107. if (droppedtetrominoeLocationGrid[0, j] == 1)
  108. return;
  109. }
  110.  
  111. Input();
  112. ClearBlock();
  113. } //end Update
  114. }
  115. private static void ClearBlock()
  116. {
  117. int combo = 0;
  118. for (int i = 0; i < 23; i++)
  119. {
  120. int j;
  121. for (j = 0; j < 10; j++)
  122. {
  123. if (droppedtetrominoeLocationGrid[i, j] == 0)
  124. break;
  125. }
  126. if (j == 10)
  127. {
  128. linesCleared++;
  129. combo++;
  130. for (j = 0; j < 10; j++)
  131. {
  132. droppedtetrominoeLocationGrid[i, j] = 0;
  133. }
  134. int[,] newdroppedtetrominoeLocationGrid = new int[23, 10];
  135. for (int k = 1; k < i; k++)
  136. {
  137. for (int l = 0; l < 10; l++)
  138. {
  139. newdroppedtetrominoeLocationGrid[k + 1, l] = droppedtetrominoeLocationGrid[k, l];
  140. }
  141. }
  142. for (int k = 1; k < i; k++)
  143. {
  144. for (int l = 0; l < 10; l++)
  145. {
  146. droppedtetrominoeLocationGrid[k, l] = 0;
  147. }
  148. }
  149. for (int k = 0; k < 23; k++)
  150. for (int l = 0; l < 10; l++)
  151. if (newdroppedtetrominoeLocationGrid[k, l] == 1)
  152. droppedtetrominoeLocationGrid[k, l] = 1;
  153. Draw();
  154. }
  155. }
  156. if (combo == 1)
  157. score += 40 * level;
  158. else if (combo == 2)
  159. score += 100 * level;
  160. else if (combo == 3)
  161. score += 300 * level;
  162. else if (combo > 3)
  163. score += 300 * combo * level;
  164.  
  165. if (linesCleared < 5) level = 1;
  166. else if (linesCleared < 10) level = 2;
  167. else if (linesCleared < 15) level = 3;
  168. else if (linesCleared < 25) level = 4;
  169. else if (linesCleared < 35) level = 5;
  170. else if (linesCleared < 50) level = 6;
  171. else if (linesCleared < 70) level = 7;
  172. else if (linesCleared < 90) level = 8;
  173. else if (linesCleared < 110) level = 9;
  174. else if (linesCleared < 150) level = 10;
  175.  
  176.  
  177. if (combo>0)
  178. {
  179. Console.SetCursorPosition(25, 0);
  180. Console.WriteLine("Level " + level);
  181. Console.SetCursorPosition(25, 1);
  182. Console.WriteLine("Score " + score);
  183. Console.SetCursorPosition(25, 2);
  184. Console.WriteLine("LinesCleared " + linesCleared);
  185. }
  186.  
  187. dropRate = 300 - 22 * level;
  188.  
  189. }
  190. private static void Input()
  191. {
  192. if (Console.KeyAvailable)
  193. {
  194. key = Console.ReadKey();
  195. isKeyPressed = true;
  196. }
  197. else
  198. isKeyPressed = false;
  199.  
  200. if (Program.key.Key == ConsoleKey.LeftArrow & !tet.isSomethingLeft() & isKeyPressed)
  201. {
  202. for (int i = 0; i < 4; i++)
  203. {
  204. tet.location[i][1] -= 1;
  205. }
  206. tet.Update();
  207. // Console.Beep();
  208. }
  209. else if (Program.key.Key == ConsoleKey.RightArrow & !tet.isSomethingRight() & isKeyPressed)
  210. {
  211. for (int i = 0; i < 4; i++)
  212. {
  213. tet.location[i][1] += 1;
  214. }
  215. tet.Update();
  216. }
  217. if (Program.key.Key == ConsoleKey.DownArrow & isKeyPressed)
  218. {
  219. tet.Drop();
  220. }
  221. if (Program.key.Key == ConsoleKey.UpArrow & isKeyPressed)
  222. {
  223. for (; tet.isSomethingBelow()!=true; )
  224. {
  225. tet.Drop();
  226. }
  227. }
  228. if (Program.key.Key == ConsoleKey.Spacebar & isKeyPressed)
  229. {
  230. //rotate
  231. tet.Rotate();
  232. tet.Update();
  233. }
  234. }
  235. public static void Draw()
  236. {
  237. for (int i = 0; i < 23; ++i)
  238. {
  239. for (int j = 0; j < 10; j++)
  240. {
  241. Console.SetCursorPosition(1 + 2 * j, i);
  242. if (grid[i, j] == 1 | droppedtetrominoeLocationGrid[i, j] == 1)
  243. {
  244. Console.SetCursorPosition(1 + 2 * j, i);
  245. Console.Write(sqr);
  246. }
  247. else
  248. {
  249. Console.Write(" ");
  250. }
  251. }
  252.  
  253. }
  254. }
  255.  
  256. public static void drawBorder()
  257. {
  258. for (int lengthCount = 0; lengthCount <= 22; ++lengthCount)
  259. {
  260. Console.SetCursorPosition(0, lengthCount);
  261. Console.Write("*");
  262. Console.SetCursorPosition(21, lengthCount);
  263. Console.Write("*");
  264. }
  265. Console.SetCursorPosition(0, 23);
  266. for (int widthCount = 0; widthCount <= 10; widthCount++)
  267. {
  268. Console.Write("*-");
  269. }
  270.  
  271. }
  272.  
  273. }
  274.  
  275. public class Tetrominoe
  276. {
  277. public static int[,] I = new int[1, 4] { { 1, 1, 1, 1 } };//3
  278. public static int[,] O = new int[2, 2] { { 1, 1 }, { 1, 1 } };
  279. public static int[,] T = new int[2, 3] { { 0, 1, 0 }, { 1, 1, 1 } };//3
  280. public static int[,] S = new int[2, 3] { { 0, 1, 1 }, { 1, 1, 0 } };//4
  281. public static int[,] Z = new int[2, 3] { { 1, 1, 0 }, { 0, 1, 1 } };//3
  282. public static int[,] J = new int[2, 3] { { 1, 0, 0 }, { 1, 1, 1 } };//3
  283. public static int[,] L = new int[2, 3] { { 0, 0, 1 }, { 1, 1, 1 } };//3
  284. public static List<int[,]> tetrominoes = new List<int[,]>() { I, O, T, S, Z, J, L };
  285.  
  286. private bool isErect = false;
  287. private int[,] shape;
  288. private int[] pix = new int[2];
  289. public List<int[]> location = new List<int[]>();
  290.  
  291. public Tetrominoe()
  292. {
  293. Random rnd = new Random();
  294. shape = tetrominoes[rnd.Next(0, 7)];
  295. for (int i = 23; i < 33; ++i)
  296. {
  297. for (int j = 3; j < 10; j++)
  298. {
  299. Console.SetCursorPosition(i,j);
  300. Console.Write(" ");
  301. }
  302.  
  303. }
  304. Program.drawBorder();
  305. for (int i = 0; i < shape.GetLength(0); i++)
  306. {
  307. for (int j = 0; j < shape.GetLength(1); j++)
  308. {
  309. if (shape[i, j] == 1)
  310. {
  311. Console.SetCursorPosition(((10 - shape.GetLength(1)) / 2 + j)*2+20, i+5);
  312. Console.Write(Program.sqr);
  313. }
  314. }
  315. }
  316. }
  317.  
  318. public void Spawn()
  319. {
  320. for (int i = 0; i < shape.GetLength(0); i++)
  321. {
  322. for (int j = 0; j < shape.GetLength(1); j++)
  323. {
  324. if (shape[i, j] == 1)
  325. {
  326. location.Add(new int[] { i, (10 - shape.GetLength(1)) / 2 + j });
  327. }
  328. }
  329. }
  330. Update();
  331. }
  332.  
  333. public void Drop()
  334. {
  335.  
  336. if (isSomethingBelow())
  337. {
  338. for (int i = 0; i < 4; i++)
  339. {
  340. Program.droppedtetrominoeLocationGrid[location[i][0], location[i][1]] = 1;
  341. }
  342. Program.isDropped = true;
  343. // SoundPlayer sp = new SoundPlayer();
  344. // sp.SoundLocation = Environment.CurrentDirectory + "\\wood_hit_plastic_1.wav";
  345. // sp.Play();
  346.  
  347. }
  348. else
  349. {
  350. for (int numCount = 0; numCount < 4; numCount++)
  351. {
  352. location[numCount][0] += 1;
  353. }
  354. Update();
  355. }
  356. }
  357.  
  358. public void Rotate()
  359. {
  360. List<int[]> templocation = new List<int[]>();
  361. for (int i = 0; i < shape.GetLength(0); i++)
  362. {
  363. for (int j = 0; j < shape.GetLength(1); j++)
  364. {
  365. if (shape[i, j] == 1)
  366. {
  367. templocation.Add(new int[] { i, (10 - shape.GetLength(1)) / 2 + j });
  368. }
  369. }
  370. }
  371.  
  372. if (shape == tetrominoes[0])
  373. {
  374. if (isErect == false)
  375. {
  376. for (int i = 0; i < location.Count; i++)
  377. {
  378. templocation[i] = TransformMatrix(location[i], location[2], "Clockwise");
  379. }
  380. }
  381. else
  382. {
  383. for (int i = 0; i < location.Count; i++)
  384. {
  385. templocation[i] = TransformMatrix(location[i], location[2], "Counterclockwise");
  386. }
  387. }
  388. }
  389.  
  390. else if (shape == tetrominoes[3])
  391. {
  392. for (int i = 0; i < location.Count; i++)
  393. {
  394. templocation[i] = TransformMatrix(location[i], location[3], "Clockwise");
  395. }
  396. }
  397.  
  398. else if (shape == tetrominoes[1]) return;
  399. else
  400. {
  401. for (int i = 0; i < location.Count; i++)
  402. {
  403. templocation[i] = TransformMatrix(location[i], location[2], "Clockwise");
  404. }
  405. }
  406.  
  407.  
  408. for (int count = 0; isOverlayLeft(templocation) != false | isOverlayRight(templocation) != false | isOverlayBelow(templocation) != false; count++)
  409. {
  410. if (isOverlayLeft(templocation) == true)
  411. {
  412. for (int i = 0; i < location.Count; i++)
  413. {
  414. templocation[i][1] += 1;
  415. }
  416. }
  417.  
  418. if (isOverlayRight(templocation) == true)
  419. {
  420. for (int i = 0; i < location.Count; i++)
  421. {
  422. templocation[i][1] -= 1;
  423. }
  424. }
  425. if (isOverlayBelow(templocation) == true)
  426. {
  427. for (int i = 0; i < location.Count; i++)
  428. {
  429. templocation[i][0] -= 1;
  430. }
  431. }
  432. if (count == 3)
  433. {
  434. return;
  435. }
  436. }
  437.  
  438. location = templocation;
  439.  
  440. }
  441.  
  442. public int[] TransformMatrix(int[] coord, int[] axis, string dir)
  443. {
  444. int[] pcoord = { coord[0] - axis[0], coord[1] - axis[1] };
  445. if (dir == "Counterclockwise")
  446. {
  447. pcoord = new int[] { -pcoord[1], pcoord[0] };
  448. }
  449. else if (dir == "Clockwise")
  450. {
  451. pcoord = new int[] { pcoord[1], -pcoord[0] };
  452. }
  453.  
  454. return new int[] { pcoord[0] + axis[0], pcoord[1] + axis[1] };
  455. }
  456.  
  457. public bool isSomethingBelow()
  458. {
  459. for (int i = 0; i < 4; i++)
  460. {
  461. if (location[i][0] + 1 >= 23)
  462. return true;
  463. if (location[i][0] + 1 < 23)
  464. {
  465. if (Program.droppedtetrominoeLocationGrid[location[i][0] + 1, location[i][1]] == 1)
  466. {
  467. return true;
  468. }
  469. }
  470. }
  471. return false;
  472. }
  473. public bool? isOverlayBelow(List<int[]> location)
  474. {
  475. List<int> ycoords = new List<int>();
  476. for (int i = 0; i < 4; i++)
  477. {
  478. ycoords.Add(location[i][0])...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement