Advertisement
HeroBaga

Untitled

Aug 15th, 2021
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 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.  
  7. void draw();
  8.  
  9. void keypressed(char keyboard);
  10.  
  11. int player_one_y = 13, player_two_y = 13;
  12.  
  13. int ball_x = 4, ball_y = 13;
  14.  
  15. int ball_direct_x = 1, ball_direct_y = -1;
  16.  
  17. int score_one = 0, score_two=0;
  18.  
  19.  
  20. int main() {
  21. initscr();
  22. curs_set(0);
  23. noecho();
  24. nodelay(stdscr, TRUE);
  25. // draw();
  26. while(score_one !=21 || score_two !=21){
  27. char c = getch();
  28. keypressed(c);
  29.  
  30. if (ball_x - 1 == 2 && ball_direct_x == -1) {
  31. if (ball_y >= player_one_y - 1 && ball_y <= player_one_y + 1) {
  32. ball_direct_x = -1;
  33. }
  34. else {
  35. score_two += 1;
  36. ball_y = 13;
  37. ball_x = 3;
  38. player_one_y = 13;
  39. player_two_y = 13;
  40. ball_direct_x = 1;
  41. ball_direct_y = 1;
  42. }
  43. }
  44. if (ball_x + 1 == 77 && ball_direct_x == 1) {
  45. if (ball_y >= player_two_y - 1 && ball_y <= player_two_y + 1) {
  46. ball_direct_x *= -1;
  47. }
  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(500000);
  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