Advertisement
HeroBaga

Untitled

Aug 15th, 2021
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. // I WANT TO PLAY WITH YOU
  2. // YOUR FRIEND, AI
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <curses.h>
  6. //#include <conio.h>
  7.  
  8. void draw();
  9.  
  10. void keypressed(char keyboard);
  11.  
  12. int player_one_y = 13, player_two_y = 13;
  13.  
  14. int ball_x = 4, ball_y = 13;
  15.  
  16. int ball_direct_x = 1, ball_direct_y = -1;
  17.  
  18. int score_one = 0, score_two = 0;
  19.  
  20. WINDOW *win;
  21.  
  22. int main() {
  23. win = initscr();
  24. noecho();
  25. nodelay(win, true);
  26. curs_set(0);
  27. // draw();
  28. while (score_one != 21 || score_two != 21) {
  29. char c = getch();
  30. keypressed(c);
  31.  
  32. if (ball_x - 1 == 2 && ball_direct_x == -1) {
  33. if (ball_y >= player_one_y - 1 && ball_y <= player_one_y + 1) {
  34. ball_direct_x *= -1;
  35. } else {
  36. score_two += 1;
  37. ball_y = 13;
  38. ball_x = 3;
  39. player_one_y = 13;
  40. player_two_y = 13;
  41. ball_direct_x = 1;
  42. ball_direct_y = 1;
  43. }
  44. }
  45. if (ball_x + 1 == 77 && ball_direct_x == 1) {
  46. if (ball_y >= player_two_y - 1 && ball_y <= player_two_y + 1) {
  47. ball_direct_x *= -1;
  48. } else {
  49. score_one += 1;
  50. ball_y = 13;
  51. ball_x = 76;
  52. player_two_y = 13;
  53. player_one_y = 13;
  54. ball_direct_x = -1;
  55. ball_direct_y = -1;
  56. }
  57. }
  58. if (ball_y == 1 || ball_y == 23) {
  59. ball_direct_y *= -1;
  60. }
  61. ball_x += ball_direct_x;
  62. ball_y += ball_direct_y;
  63.  
  64. draw();
  65.  
  66. usleep(50000);
  67. }
  68.  
  69. }
  70.  
  71. void draw() {
  72. // move(0,0);
  73. // system("@cls||clear");
  74. printf("\33c\e[3J");
  75. for (int y = 0; y < 25; y++) {
  76. for (int x = 0; x < 80; x++) {
  77. if (y == 0 || y == 24)
  78. printf("%c", '-');
  79. else if (x == 0 || x == 79)
  80. printf("%c", '|');
  81. else if ((y <= (player_one_y + 1) && y >= player_one_y - 1) && x == 2)
  82. printf("%c", ']');
  83. else if (y == ball_y && x == ball_x)
  84. printf("%c", '*');
  85. else if ((y <= (player_two_y + 1) && y >= player_two_y - 1) && x == 77)
  86. printf("%c", '[');
  87. else
  88. printf("%c", ' ');
  89. }
  90. printf("\n");
  91. }
  92. printf("player 1 %d - %d player 2\n", score_one, score_two);
  93. }
  94.  
  95. void keypressed(char keyboard) {
  96. if (keyboard == 'z' && player_one_y < 24) {
  97. player_one_y++;
  98. } else if (keyboard == 'a' && player_one_y > 3) {
  99. player_one_y--;
  100. } else if (keyboard == 'm' && player_two_y < 24) {
  101. player_two_y++;
  102. } else if (keyboard == 'k' && player_two_y > 3) {
  103. player_two_y--;
  104. }
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement