]> git.armaanb.net Git - pong.git/blob - pong.c
Randomize direction of index
[pong.git] / pong.c
1 #include <ncurses.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <sys/time.h>
6 #include <time.h>
7 #include <math.h>
8
9 int
10 die(int code)
11 {
12         endwin();
13         exit(code);
14 }
15
16 int
17 randslope(void)
18 {
19         const int low = 2;
20         const int high = 10;
21         return (rand() % (high - low + 1)) + low;
22 }
23
24 int
25 randsign(void)
26 {
27         return pow(-1, rand());
28 }
29
30 int
31 main(void) {
32         srand((unsigned int)time(NULL));
33         int x, y, aoff = 0, boff = 0;
34         int max_y, max_x;
35         int max_y_new = 0, max_x_new = 0;
36         int pad_h = 5;
37         int a_score = 0, b_score = 0;
38         int slope_x = randslope();
39         int index_dir = randsign();
40         int direction = 1;
41         double speed = 0.02;
42         struct timeval start, stop;
43
44         // Initialize curses
45         initscr();
46         noecho();
47         curs_set(0);
48         nodelay(stdscr, 1); // Don't wait for getch input
49         getmaxyx(stdscr, max_y, max_x);
50         --max_x;
51         --max_y;
52         int index = slope_x*max_y/2;
53         x = max_x/2;
54
55         gettimeofday(&start, NULL);
56         while(1) {
57                 // Handle resizes
58                 getmaxyx(stdscr, max_y_new, max_x_new);
59                 --max_x_new;
60                 --max_y_new;
61                 pad_h = 0.2 * max_y;
62                 if (max_y_new != max_y || max_x_new != max_x) {
63                         clear();
64                         max_x = max_x_new;
65                         max_y = max_y_new;
66                 }
67
68                 // Parse keypresses
69                 int key = getch();
70                 if (key == 'q') {
71                         die(0);
72                 } else if (key == 'k') {
73                         boff += 2;
74                 } else if (key == 'i') {
75                         boff -= 2;
76                 } else if (key == 's') {
77                         aoff += 2;
78                 } else if (key == 'w') {
79                         aoff -= 2;
80                 }
81
82                 // Clear screen
83                 for (int i = 0; i < max_y + 1; i++) {
84                         mvprintw(i, 0, " ");
85                         mvprintw(i, 1, " ");
86                         mvprintw(i, max_x, " ");
87                         mvprintw(i, max_x - 1, " ");
88                 }
89
90                 // Draw paddles
91                 for (int i = aoff; i < aoff + pad_h; i++) {
92                         mvprintw(i, 0, "|");
93                 }
94
95                 for (int i = boff; i < boff + pad_h; i++) {
96                         mvprintw(i, max_x, "|");
97                 }
98
99                 // Draw net
100                 for (int i = 0; i < max_y; i++) {
101                         mvprintw(i, max_x/2, ":");
102                 }
103
104                 // Draw scrores
105                 mvprintw(0, max_x/2 - 2, "%d", a_score);
106                 mvprintw(0, max_x/2 + 2, "%d", b_score);
107
108                 // Move ball if enough time has elapsed
109                 gettimeofday(&stop, NULL);
110                 if ((double)(stop.tv_usec - start.tv_usec) / 1000000 +
111                                 (double)(stop.tv_sec - start.tv_sec) > speed) {
112                         gettimeofday(&start, NULL);
113                         index += index_dir;
114                         mvprintw(y, x, " ");
115                         x += direction;
116                         y = index / slope_x;
117                         mvprintw(y, x, "o");
118                 }
119
120                 if ((x >= max_x - 1 && y >= boff && y <= boff + pad_h) ||
121                                 (x <= 1 && y >= aoff && y <= aoff + pad_h)) {
122                         // Ball hits paddle
123                         direction *= -1;
124                         x += direction;
125                         speed *= 0.9;
126                 } else if (x >= max_x - 1 || x <= 1) {
127                         // Ball goes out of bounds
128                         if (x <=1) {
129                                 b_score += 1;
130                         } else {
131                                 a_score += 1;
132                         }
133                         x = max_x/2;
134                         index = slope_x*max_y/2;
135                         slope_x = randslope();
136                         index_dir = randsign();
137                         speed = 0.02;
138                         sleep(1);
139                 } else if (y == 0 || y == max_y) {
140                         // Ball hits top or bottom wall
141                         index_dir *= -1;
142                         mvprintw(y, x, " ");
143                         y = max_y - 1;
144                 }
145         }
146 }