Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //I WANT TO PLAY WITH YOU
- // YOUR FRIEND, AI
- #include <stdio.h>
- #include <stdlib.h>
- 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);
- int main() {
- int score_1 = 0;
- int score_2 = 0;
- int pos_rock1_x = 1;
- int pos_rock1_y = 13;
- int pos_rock2_x = 78;
- int pos_rock2_y = 13;
- int pos_ball_x = 2;
- int pos_ball_y = 13;
- int ball_direct_x = 1;
- int ball_direct_y = -1;
- draw(pos_rock1_x, pos_rock1_y, pos_rock2_x, pos_rock2_y, pos_ball_x, pos_ball_y, score_1, score_2);
- while (score_1 != 21 || score_2 != 21) {
- system("/bin/stty raw");
- char inp = getchar();
- if (inp == 'a' && pos_rock1_y > 2) {
- pos_rock1_y -= 1;
- } else if (inp == 'z' && pos_rock1_y < 22) {
- pos_rock1_y += 1;
- } else if (inp == 'k' && pos_rock2_y > 2) {
- pos_rock2_y -= 1;
- } else if (inp == 'm' && pos_rock2_y < 22) {
- pos_rock2_y += 1;
- } else if(inp == ' '){
- pos_ball_x += ball_direct_x;
- pos_ball_y += ball_direct_y;
- }else if (inp == 'q') {
- break;
- }
- system("/bin/stty cooked");
- if (pos_ball_x - 1 == pos_rock1_x && ball_direct_x == -1) {
- if (pos_ball_y >= pos_rock1_y - 1 && pos_ball_y <= pos_rock1_y + 1) {
- ball_direct_x = -1;
- }
- else {
- score_2 += 1;
- pos_ball_y = 13;
- pos_ball_x = 2;
- pos_rock1_y = 13;
- pos_rock2_y = 13;
- ball_direct_x = 1;
- ball_direct_y = 1;
- }
- }
- if (pos_ball_x + 1 == pos_rock2_x && ball_direct_x == 1) {
- if (pos_ball_y >= pos_rock2_y - 1 && pos_ball_y <= pos_rock2_y + 1) {
- ball_direct_x *= -1;
- }
- else {
- score_1 += 1;
- pos_ball_y = 13;
- pos_ball_x = 78;
- pos_rock1_y = 13;
- pos_rock2_y = 13;
- ball_direct_x = -1;
- ball_direct_y = -1;
- }
- }
- if (pos_ball_y == 1 || pos_ball_y == 23){
- ball_direct_y *= -1;
- }
- printf ("\33c\e[3J");
- draw(pos_rock1_x, pos_rock1_y, pos_rock2_x, pos_rock2_y, pos_ball_x, pos_ball_y, score_1, score_2);
- }
- if (score_1 == 21)
- printf("Player 1 won!\n");
- else if (score_2 == 21)
- printf("Player 2 won!\n");
- system("/bin/stty sane");
- }
- 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) {
- const char rock1 = ']';
- const char rock2 = '[';
- const char ball = '*';
- const char vert = '|';
- const char hor = '-';
- const int size_x = 80;
- const int size_y = 25;
- char space = ' ';
- for (int i = 0; i < size_y; i++) {
- for (int j = 0; j < size_x; j++) {
- if (i == 0 || i == 24)
- printf("%c", hor);
- else if (j == 0 || j == 79)
- printf("%c", vert);
- else if ((i <= (pos_rock1_y + 1) && i >= pos_rock1_y - 1) && j == pos_rock1_x)
- printf("%c", rock1);
- else if (i == pos_ball_y && j == pos_ball_x)
- printf("%c", ball);
- else if (j == 39 || j == 40)
- printf("%c", vert);
- else if ((i <= (pos_rock2_y + 1) && i >= pos_rock2_y - 1) && j == pos_rock2_x)
- printf("%c", rock2);
- else
- printf("%c", space);
- }
- printf("\n");
- }
- printf("player 1 %d - %d player 2\n", score_1, score_2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement