Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 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, 25);
  11. Console.SetBufferSize(80, 25);
  12. int y1 = 12;
  13. int x1 = 10;
  14. int y2 = 12;
  15. int x2 = 70;
  16. int xBall = 40, yBall = 5;
  17. int xSpeed = 1, ySpeed = 1;
  18. int conter1 = 0 ,conter2 = 0;
  19. bool finished = false;
  20. ConsoleKeyInfo userkey;
  21. do
  22. {
  23. Console.Clear();
  24.  
  25. Console.SetCursorPosition(x1, y1);
  26. Console.ForegroundColor = ConsoleColor.Yellow;
  27. Console.Write("|");
  28.  
  29. Console.SetCursorPosition(x2, y2);
  30. Console.ForegroundColor = ConsoleColor.Yellow;
  31. Console.Write("|");
  32.  
  33. Console.SetCursorPosition(xBall, yBall);
  34. Console.ForegroundColor = ConsoleColor.Cyan;
  35. Console.Write("O");
  36.  
  37. Console.SetCursorPosition(37, 2);
  38. Console.ForegroundColor = ConsoleColor.Yellow;
  39. Console.Write(conter1 + "-" + conter2);
  40.  
  41. Console.SetCursorPosition(1, 1);
  42.  
  43. if (Console.KeyAvailable)
  44. {
  45. userkey = Console.ReadKey(true);
  46.  
  47. if ((userkey.Key == ConsoleKey.W) && (y1 > 1))
  48. y1--;
  49. if ((userkey.Key == ConsoleKey.S) && (y1 < 24))
  50. y1++;
  51. if ((userkey.Key == ConsoleKey.UpArrow) && (y2 > 1))
  52. y2--;
  53. if ((userkey.Key == ConsoleKey.DownArrow) && (y2 < 24))
  54. y2++;
  55.  
  56. if (userkey.Key == ConsoleKey.Escape)
  57. finished = true;
  58. }
  59.  
  60. if (xBall == (x1-5))
  61. {
  62. conter2++;
  63. xBall = 40 ; yBall = 5;
  64. x1 = 10 ; y1 = 12;
  65. x2 = 70 ; y2 = 12;
  66. xSpeed = -xSpeed;
  67. ySpeed = -ySpeed;
  68. }
  69.  
  70. if (xBall == (x2+5))
  71. {
  72. conter1++;
  73. xBall = 40 ; yBall = 5;
  74. x1 = 10 ; y1 = 12;
  75. x2 = 70 ; y2 = 12;
  76. xSpeed = -xSpeed;
  77. ySpeed = -ySpeed;
  78. }
  79. if (xBall == 10 && yBall == y1)
  80. {
  81. xSpeed = -xSpeed;
  82. ySpeed = -ySpeed;
  83. }
  84. if (xBall == 70 && yBall == y2)
  85. {
  86. xSpeed = -xSpeed;
  87. ySpeed = -ySpeed;
  88. }
  89. xBall += xSpeed;
  90. yBall += ySpeed;
  91. if ((xBall <= 1) || (xBall >= 78))
  92. xSpeed = -xSpeed;
  93. if ((yBall <= 1) || (yBall >= 24))
  94. ySpeed = -ySpeed;
  95.  
  96.  
  97.  
  98.  
  99. Thread.Sleep(100);
  100. }
  101. while ( ! finished );
  102.  
  103. Console.ForegroundColor = ConsoleColor.White;
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement