Advertisement
HeroBaga

Untitled

Aug 15th, 2021
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. //I WANT TO PLAY WITH YOU
  2. // YOUR FRIEND, AI
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. void draw (int pos_rock1_x, int pos_rock1_y, int pos_rock2_x, int pos_rock2_y, int pos_ball_x, int pos_ball_y, int score_1, int score_2);
  7.  
  8. int main() {
  9. int score_1 = 0;
  10. int score_2 = 0;
  11. int pos_rock1_x = 1;
  12. int pos_rock1_y = 13;
  13. int pos_rock2_x = 78;
  14. int pos_rock2_y = 13;
  15. int pos_ball_x = 2;
  16. int pos_ball_y = 13;
  17. int ball_direct_x = 1;
  18. int ball_direct_y = -1;
  19. draw(pos_rock1_x, pos_rock1_y, pos_rock2_x, pos_rock2_y, pos_ball_x, pos_ball_y, score_1, score_2);
  20. while (score_1 != 21 || score_2 != 21) {
  21. system("/bin/stty raw");
  22. char inp = getchar();
  23. if (inp == 'a' && pos_rock1_y > 2) {
  24. pos_rock1_y -= 1;
  25. } else if (inp == 'z' && pos_rock1_y < 22) {
  26. pos_rock1_y += 1;
  27. } else if (inp == 'k' && pos_rock2_y > 2) {
  28. pos_rock2_y -= 1;
  29. } else if (inp == 'm' && pos_rock2_y < 22) {
  30. pos_rock2_y += 1;
  31. } else if(inp == ' '){
  32. pos_ball_x += ball_direct_x;
  33. pos_ball_y += ball_direct_y;
  34. }else if (inp == 'q') {
  35. break;
  36. }
  37. system("/bin/stty cooked");
  38. if (pos_ball_x - 1 == pos_rock1_x && ball_direct_x == -1) {
  39. if (pos_ball_y >= pos_rock1_y - 1 && pos_ball_y <= pos_rock1_y + 1) {
  40. ball_direct_x = -1;
  41. }
  42. else {
  43. score_2 += 1;
  44. pos_ball_y = 13;
  45. pos_ball_x = 2;
  46. pos_rock1_y = 13;
  47. pos_rock2_y = 13;
  48. ball_direct_x = 1;
  49. ball_direct_y = 1;
  50. }
  51. }
  52. if (pos_ball_x + 1 == pos_rock2_x && ball_direct_x == 1) {
  53. if (pos_ball_y >= pos_rock2_y - 1 && pos_ball_y <= pos_rock2_y + 1) {
  54. ball_direct_x *= -1;
  55. }
  56. else {
  57. score_1 += 1;
  58. pos_ball_y = 13;
  59. pos_ball_x = 78;
  60. pos_rock1_y = 13;
  61. pos_rock2_y = 13;
  62. ball_direct_x = -1;
  63. ball_direct_y = -1;
  64. }
  65. }
  66. if (pos_ball_y == 1 || pos_ball_y == 23){
  67. ball_direct_y *= -1;
  68. }
  69.  
  70. printf ("\33c\e[3J");
  71. draw(pos_rock1_x, pos_rock1_y, pos_rock2_x, pos_rock2_y, pos_ball_x, pos_ball_y, score_1, score_2);
  72. }
  73. if (score_1 == 21)
  74. printf("Player 1 won!\n");
  75. else if (score_2 == 21)
  76. printf("Player 2 won!\n");
  77. system("/bin/stty sane");
  78. }
  79.  
  80. void draw (int pos_rock1_x, int pos_rock1_y, int pos_rock2_x, int pos_rock2_y, int pos_ball_x, int pos_ball_y, int score_1, int score_2) {
  81. const char rock1 = ']';
  82. const char rock2 = '[';
  83. const char ball = '*';
  84. const char vert = '|';
  85. const char hor = '-';
  86. const int size_x = 80;
  87. const int size_y = 25;
  88. char space = ' ';
  89. for (int i = 0; i < size_y; i++) {
  90. for (int j = 0; j < size_x; j++) {
  91. if (i == 0 || i == 24)
  92. printf("%c", hor);
  93. else if (j == 0 || j == 79)
  94. printf("%c", vert);
  95. else if ((i <= (pos_rock1_y + 1) && i >= pos_rock1_y - 1) && j == pos_rock1_x)
  96. printf("%c", rock1);
  97. else if (i == pos_ball_y && j == pos_ball_x)
  98. printf("%c", ball);
  99. else if (j == 39 || j == 40)
  100. printf("%c", vert);
  101. else if ((i <= (pos_rock2_y + 1) && i >= pos_rock2_y - 1) && j == pos_rock2_x)
  102. printf("%c", rock2);
  103. else
  104. printf("%c", space);
  105. }
  106. printf("\n");
  107. }
  108. printf("player 1 %d - %d player 2\n", score_1, score_2);
  109. }
  110.  
  111.  
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement