]> git.armaanb.net Git - pong.git/blob - pong.c
0be9fe3f28be0925eb638ba8dba4a066025e88b7
[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                 getmaxyx(stdscr, max_y_new, max_x_new);
43                 --max_x_new;
44                 --max_y_new;
45                 pad_h = 0.2 * max_y;
46                 if (max_y_new != max_y || max_x_new != max_x) {
47                         clear();
48                         max_x = max_x_new;
49                         max_y = max_y_new;
50                 }
51
52                 // Parse keypresses
53                 int key = getch();
54                 if (key == 'q') {
55                         die(0);
56                 } else if (key == 'k') {
57                         boff += 2;
58                 } else if (key == 'i') {
59                         boff -= 2;
60                 } else if (key == 's') {
61                         aoff += 2;
62                 } else if (key == 'w') {
63                         aoff -= 2;
64                 }
65
66                 // Draw paddles
67                 for (int i = 0; i < max_y + 1; i++) {
68                         mvprintw(i, 0, " ");
69                         mvprintw(i, 1, " ");
70                         mvprintw(i, max_x, " ");
71                         mvprintw(i, max_x - 1, " ");
72                 }
73                 for (int i = aoff; i < aoff + pad_h; i++) {
74                         mvprintw(i, 0, "|");
75                 }
76
77                 for (int i = boff; i < boff + pad_h; i++) {
78                         mvprintw(i, max_x, "|");
79                 }
80
81                 for (int i = 0; i < max_y; i++) {
82                         mvprintw(i, max_x/2, ":");
83                 }
84
85                 mvprintw(0, 2, "%d", a_score);
86                 mvprintw(0, max_x - 1, "%d", b_score);
87
88                 // Move ball
89                 gettimeofday(&stop, NULL);
90                 if ((double)(stop.tv_usec - start.tv_usec) / 1000000 +
91                                 (double)(stop.tv_sec - start.tv_sec) > speed) {
92                         gettimeofday(&start, NULL);
93                         index += index_dir;
94                         mvprintw(y, x, " ");
95                         x += direction;
96                         y = slope_y * index / slope_x;
97                         mvprintw(y, x, "o");
98                 }
99
100                 // Check if touching paddle or edges
101                 if (x >= max_x - 1) {
102                         if (y >= boff && y <= boff + pad_h) {
103                                 direction *= -1;
104                                 x += direction;
105                         } else {
106                                 a_score += 1;
107                                 x = max_x/2;
108                                 index = (max_y/2)*(slope_x/slope_y);
109                                 speed /= 0.8;
110                                 sleep(1);
111                         }
112                 } else if (x <= 1) {
113                         if (y >= aoff && y <= aoff + pad_h) {
114                                 direction *= -1;
115                                 x += direction;
116                         } else {
117                                 b_score += 1;
118                                 x = max_x/2;
119                                 index = (max_y/2)*(slope_x/slope_y);
120                                 speed /= 0.8;
121                                 sleep(1);
122                         }
123                 } else if (y == 0 || y == max_y) {
124                         index_dir *= -1;
125                 }
126         }
127 }