Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. // Pablo Vigara Fernández && Sergio Gumpert
  2.  
  3. using System;
  4. using System.Threading;
  5.  
  6. class Pong
  7. {
  8. static void Main()
  9. {
  10. Console.SetWindowSize(80, 26);
  11. Console.SetBufferSize(80, 26);
  12. int y1 = 12;
  13. int x1 = 4;
  14. int y2 = 12;
  15. int x2 = 75;
  16. int xBall = 40, yBall = 5;
  17. int xSpeed = 1, ySpeed = 1;
  18. int leftCount = 0 ,rightCount = 0;
  19. bool finished = false;
  20. ConsoleKeyInfo userkey;
  21. do
  22. {
  23. Console.Clear();
  24.  
  25. Console.SetCursorPosition(2, 1);
  26. Console.Write("______________________________________________");
  27. Console.Write("________________________________");
  28.  
  29. Console.SetCursorPosition(2, 25);
  30. Console.Write("______________________________________________");
  31. Console.Write("________________________________");
  32.  
  33. for (int i = 1; i < 25; i++)
  34. {
  35. Console.SetCursorPosition(1, i );
  36. Console.Write("|");
  37. }
  38.  
  39. for (int i = 1; i < 25; i++)
  40. {
  41. Console.SetCursorPosition(79, i );
  42. Console.Write("|");
  43. }
  44.  
  45. for (int i = 1; i < 25; i++)
  46. {
  47. Console.SetCursorPosition(40, i );
  48. Console.Write("|");
  49. }
  50.  
  51.  
  52.  
  53. Console.SetCursorPosition(x1, y1);
  54. Console.ForegroundColor = ConsoleColor.Yellow;
  55. Console.Write("||");
  56. Console.SetCursorPosition(x1, y1 + 1);
  57. Console.ForegroundColor = ConsoleColor.Yellow;
  58. Console.Write("||");
  59. Console.SetCursorPosition(x1, y1 - 1);
  60. Console.ForegroundColor = ConsoleColor.Yellow;
  61. Console.Write("||");
  62.  
  63. Console.SetCursorPosition(x2, y2);
  64. Console.ForegroundColor = ConsoleColor.Yellow;
  65. Console.Write("||");
  66. Console.SetCursorPosition(x2, y2 + 1);
  67. Console.ForegroundColor = ConsoleColor.Yellow;
  68. Console.Write("||");
  69. Console.SetCursorPosition(x2, y2 - 1);
  70. Console.ForegroundColor = ConsoleColor.Yellow;
  71. Console.Write("||");
  72.  
  73. Console.SetCursorPosition(xBall, yBall);
  74. Console.ForegroundColor = ConsoleColor.Cyan;
  75. Console.Write("O");
  76.  
  77. Console.SetCursorPosition(37, 2);
  78. Console.ForegroundColor = ConsoleColor.Yellow;
  79. Console.Write(leftCount + "-" + rightCount);
  80.  
  81. Console.SetCursorPosition(1, 1);
  82.  
  83. if (Console.KeyAvailable)
  84. {
  85. userkey = Console.ReadKey(true);
  86.  
  87. if ((userkey.Key == ConsoleKey.W) && (y1 > 2))
  88. y1--;
  89. if ((userkey.Key == ConsoleKey.S) && (y1 < 23))
  90. y1++;
  91. if ((userkey.Key == ConsoleKey.UpArrow) && (y2 > 2))
  92. y2--;
  93. if ((userkey.Key == ConsoleKey.DownArrow) && (y2 < 23))
  94. y2++;
  95.  
  96. if (userkey.Key == ConsoleKey.Escape)
  97. finished = true;
  98. }
  99.  
  100. // Gol
  101. if (xBall == 1)
  102. {
  103. rightCount++;
  104. xBall = 40 ; yBall = 12;
  105. }
  106.  
  107. if (xBall == 79)
  108. {
  109. leftCount++;
  110. xBall = 40 ; yBall = 12;
  111. }
  112.  
  113. // Rebote con pala
  114. if (xBall == 5 && (yBall == y1
  115. || yBall == y1 + 1 || yBall == y1 - 1))
  116. {
  117. xSpeed = -xSpeed;
  118. ySpeed = -ySpeed;
  119. }
  120. if (xBall == 75 && (yBall == y2
  121. || yBall == y2 + 1 || yBall == y2 - 1))
  122. {
  123. xSpeed = -xSpeed;
  124. ySpeed = -ySpeed;
  125. }
  126.  
  127. // Movimiento normal
  128. xBall += xSpeed;
  129. yBall += ySpeed;
  130.  
  131. // Rebote con borde
  132. if ((xBall <= 1) || (xBall >= 79))
  133. xSpeed = -xSpeed;
  134. if ((yBall <= 1) || (yBall >= 24))
  135. ySpeed = -ySpeed;
  136.  
  137.  
  138. Thread.Sleep(150);
  139. }
  140. while ( ! finished );
  141.  
  142. Console.ForegroundColor = ConsoleColor.White;
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement