]> git.armaanb.net Git - pong.git/blob - pong.c
Draw scores in center
[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                 // Draw scrores
90                 mvprintw(0, max_x/2 - 2, "%d", a_score);
91                 mvprintw(0, max_x/2 + 2, "%d", b_score);
92
93                 // Move ball if enough time has elapsed
94                 gettimeofday(&stop, NULL);
95                 if ((double)(stop.tv_usec - start.tv_usec) / 1000000 +
96                                 (double)(stop.tv_sec - start.tv_sec) > speed) {
97                         gettimeofday(&start, NULL);
98                         index += index_dir;
99                         mvprintw(y, x, " ");
100                         x += direction;
101                         y = slope_y * index / slope_x;
102                         mvprintw(y, x, "o");
103                 }
104
105                 // Check if touching paddle or edges
106                 if (x >= max_x - 1) {
107                         if (y >= boff && y <= boff + pad_h) {
108                                 direction *= -1;
109                                 x += direction;
110                         } else {
111                                 a_score += 1;
112                                 x = max_x/2;
113                                 index = (max_y/2)*(slope_x/slope_y);
114                                 speed /= 0.8;
115                                 sleep(1);
116                         }
117                 } else if (x <= 1) {
118                         if (y >= aoff && y <= aoff + pad_h) {
119                                 direction *= -1;
120                                 x += direction;
121                         } else {
122                                 b_score += 1;
123                                 x = max_x/2;
124                                 index = (max_y/2)*(slope_x/slope_y);
125                                 speed /= 0.8;
126                                 sleep(1);
127                         }
128                 } else if (y == 0 || y == max_y) {
129                         index_dir *= -1;
130                 }
131         }
132 }