]> git.armaanb.net Git - pong.git/blob - pong.c
Add better comments
[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
8 int
9 die(int code)
10 {
11         endwin();
12         exit(code);
13 }
14
15 int
16 main(void) {
17         int x, y, aoff = 0, boff = 0;
18         int max_y, max_x;
19         int max_y_new = 0, max_x_new = 0;
20         int pad_h = 5;
21         int a_score = 0, b_score = 0;
22         int slope_x = 8;
23         int slope_y = 1;
24         int index_dir = 1;
25         int direction = 1;
26         double speed = 0.015;
27         struct timeval start, stop;
28
29         // Initialize curses
30         initscr();
31         noecho();
32         curs_set(0);
33         nodelay(stdscr, 1); // Don't wait for getch input
34         getmaxyx(stdscr, max_y, max_x);
35         --max_x;
36         --max_y;
37         int index = (max_y/2)*(slope_x/slope_y);
38         x = max_x/2;
39
40         gettimeofday(&start, NULL);
41         while(1) {
42                 // Handle resizes
43                 getmaxyx(stdscr, max_y_new, max_x_new);
44                 --max_x_new;
45                 --max_y_new;
46                 pad_h = 0.2 * max_y;
47                 if (max_y_new != max_y || max_x_new != max_x) {
48                         clear();
49                         max_x = max_x_new;
50                         max_y = max_y_new;
51                 }
52
53                 // Parse keypresses
54                 int key = getch();
55                 if (key == 'q') {
56                         die(0);
57                 } else if (key == 'k') {
58                         boff += 2;
59                 } else if (key == 'i') {
60                         boff -= 2;
61                 } else if (key == 's') {
62                         aoff += 2;
63                 } else if (key == 'w') {
64                         aoff -= 2;
65                 }
66
67                 // Clear screen
68                 for (int i = 0; i < max_y + 1; i++) {
69                         mvprintw(i, 0, " ");
70                         mvprintw(i, 1, " ");
71                         mvprintw(i, max_x, " ");
72                         mvprintw(i, max_x - 1, " ");
73                 }
74
75                 // Draw paddles
76                 for (int i = aoff; i < aoff + pad_h; i++) {
77                         mvprintw(i, 0, "|");
78                 }
79
80                 for (int i = boff; i < boff + pad_h; i++) {
81                         mvprintw(i, max_x, "|");
82                 }
83
84                 // Draw net
85                 for (int i = 0; i < max_y; i++) {
86                         mvprintw(i, max_x/2, ":");
87                 }
88
89                 mvprintw(0, 2, "%d", a_score);
90                 mvprintw(0, max_x - 1, "%d", b_score);
91
92                 // Move ball if enough time has elapsed
93                 gettimeofday(&stop, NULL);
94                 if ((double)(stop.tv_usec - start.tv_usec) / 1000000 +
95                                 (double)(stop.tv_sec - start.tv_sec) > speed) {
96                         gettimeofday(&start, NULL);
97                         index += index_dir;
98                         mvprintw(y, x, " ");
99                         x += direction;
100                         y = slope_y * index / slope_x;
101                         mvprintw(y, x, "o");
102                 }
103
104                 // Check if touching paddle or edges
105                 if (x >= max_x - 1) {
106                         if (y >= boff && y <= boff + pad_h) {
107                                 direction *= -1;
108                                 x += direction;
109                         } else {
110                                 a_score += 1;
111                                 x = max_x/2;
112                                 index = (max_y/2)*(slope_x/slope_y);
113                                 speed /= 0.8;
114                                 sleep(1);
115                         }
116                 } else if (x <= 1) {
117                         if (y >= aoff && y <= aoff + pad_h) {
118                                 direction *= -1;
119                                 x += direction;
120                         } else {
121                                 b_score += 1;
122                                 x = max_x/2;
123                                 index = (max_y/2)*(slope_x/slope_y);
124                                 speed /= 0.8;
125                                 sleep(1);
126                         }
127                 } else if (y == 0 || y == max_y) {
128                         index_dir *= -1;
129                 }
130         }
131 }