Rev 4515 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4515 | Rev 4565 | ||
---|---|---|---|
1 | /* $OpenBSD: tetris.c,v 1.21 2006/04/20 03:24:12 ray Exp $ */ |
1 | /* $OpenBSD: tetris.c,v 1.21 2006/04/20 03:24:12 ray Exp $ */ |
2 | /* $NetBSD: tetris.c,v 1.2 1995/04/22 07:42:47 cgd Exp $ */ |
2 | /* $NetBSD: tetris.c,v 1.2 1995/04/22 07:42:47 cgd Exp $ */ |
3 | 3 | ||
4 | /*- |
4 | /*- |
5 | * Copyright (c) 1992, 1993 |
5 | * Copyright (c) 1992, 1993 |
6 | * The Regents of the University of California. All rights reserved. |
6 | * The Regents of the University of California. All rights reserved. |
7 | * |
7 | * |
8 | * This code is derived from software contributed to Berkeley by |
8 | * This code is derived from software contributed to Berkeley by |
9 | * Chris Torek and Darren F. Provine. |
9 | * Chris Torek and Darren F. Provine. |
10 | * |
10 | * |
11 | * Redistribution and use in source and binary forms, with or without |
11 | * Redistribution and use in source and binary forms, with or without |
12 | * modification, are permitted provided that the following conditions |
12 | * modification, are permitted provided that the following conditions |
13 | * are met: |
13 | * are met: |
14 | * 1. Redistributions of source code must retain the above copyright |
14 | * 1. Redistributions of source code must retain the above copyright |
15 | * notice, this list of conditions and the following disclaimer. |
15 | * notice, this list of conditions and the following disclaimer. |
16 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * 2. Redistributions in binary form must reproduce the above copyright |
17 | * notice, this list of conditions and the following disclaimer in the |
17 | * notice, this list of conditions and the following disclaimer in the |
18 | * documentation and/or other materials provided with the distribution. |
18 | * documentation and/or other materials provided with the distribution. |
19 | * 3. Neither the name of the University nor the names of its contributors |
19 | * 3. Neither the name of the University nor the names of its contributors |
20 | * may be used to endorse or promote products derived from this software |
20 | * may be used to endorse or promote products derived from this software |
21 | * without specific prior written permission. |
21 | * without specific prior written permission. |
22 | * |
22 | * |
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
33 | * SUCH DAMAGE. |
33 | * SUCH DAMAGE. |
34 | * |
34 | * |
35 | * @(#)tetris.c 8.1 (Berkeley) 5/31/93 |
35 | * @(#)tetris.c 8.1 (Berkeley) 5/31/93 |
36 | */ |
36 | */ |
37 | 37 | ||
38 | /** @addtogroup tetris Tetris |
38 | /** @addtogroup tetris Tetris |
39 | * @brief Tetris ported from OpenBSD |
39 | * @brief Tetris ported from OpenBSD |
40 | * @{ |
40 | * @{ |
41 | */ |
41 | */ |
42 | /** @file |
42 | /** @file |
43 | */ |
43 | */ |
44 | 44 | ||
45 | static const char copyright[] = |
45 | static const char copyright[] = |
46 | "@(#) Copyright (c) 1992, 1993\n" |
46 | "@(#) Copyright (c) 1992, 1993\n" |
47 | "\tThe Regents of the University of California. All rights reserved.\n"; |
47 | "\tThe Regents of the University of California. All rights reserved.\n"; |
48 | 48 | ||
49 | #include <sys/time.h> |
49 | #include <sys/time.h> |
50 | #include <sys/types.h> |
50 | #include <sys/types.h> |
51 | #include <err.h> |
51 | #include <err.h> |
- | 52 | #include <errno.h> |
|
52 | #include <stdio.h> |
53 | #include <stdio.h> |
53 | #include <stdlib.h> |
54 | #include <stdlib.h> |
54 | #include <string.h> |
55 | #include <string.h> |
55 | #include <unistd.h> |
56 | #include <unistd.h> |
56 | #include <getopt.h> |
57 | #include <getopt.h> |
57 | 58 | ||
58 | #include "input.h" |
59 | #include "input.h" |
59 | #include "scores.h" |
60 | #include "scores.h" |
60 | #include "screen.h" |
61 | #include "screen.h" |
61 | #include "tetris.h" |
62 | #include "tetris.h" |
62 | 63 | ||
63 | cell board[B_SIZE]; |
64 | cell board[B_SIZE]; |
64 | 65 | ||
65 | int Rows; |
66 | int Rows; |
66 | int Cols; |
67 | int Cols; |
67 | 68 | ||
68 | const struct shape *curshape; |
69 | const struct shape *curshape; |
69 | const struct shape *nextshape; |
70 | const struct shape *nextshape; |
70 | 71 | ||
71 | long fallrate; |
72 | long fallrate; |
72 | int score; |
73 | int score; |
73 | char key_msg[100]; |
74 | char key_msg[100]; |
74 | int showpreview; |
75 | int showpreview; |
75 | int classic; |
76 | int classic; |
76 | 77 | ||
77 | static void elide(void); |
78 | static void elide(void); |
78 | static void setup_board(void); |
79 | static void setup_board(void); |
79 | static const struct shape *randshape(void); |
80 | static const struct shape *randshape(void); |
80 | 81 | ||
81 | static void usage(void); |
82 | static void usage(void); |
82 | 83 | ||
83 | static int firstgame = 1; |
84 | static int firstgame = 1; |
84 | 85 | ||
85 | /* |
86 | /* |
86 | * Set up the initial board. The bottom display row is completely set, |
87 | * Set up the initial board. The bottom display row is completely set, |
87 | * along with another (hidden) row underneath that. Also, the left and |
88 | * along with another (hidden) row underneath that. Also, the left and |
88 | * right edges are set. |
89 | * right edges are set. |
89 | */ |
90 | */ |
90 | static void setup_board(void) |
91 | static void setup_board(void) |
91 | { |
92 | { |
92 | int i; |
93 | int i; |
93 | cell *p = board; |
94 | cell *p = board; |
94 | 95 | ||
95 | for (i = B_SIZE; i; i--) |
96 | for (i = B_SIZE; i; i--) |
96 | *p++ = (i <= (2 * B_COLS) || (i % B_COLS) < 2) ? 0x0000ff : 0x000000; |
97 | *p++ = (i <= (2 * B_COLS) || (i % B_COLS) < 2) ? 0x0000ff : 0x000000; |
97 | } |
98 | } |
98 | 99 | ||
99 | /* |
100 | /* |
100 | * Elide any full active rows. |
101 | * Elide any full active rows. |
101 | */ |
102 | */ |
102 | static void elide(void) |
103 | static void elide(void) |
103 | { |
104 | { |
104 | int rows = 0; |
105 | int rows = 0; |
105 | int i; |
106 | int i; |
106 | int j; |
107 | int j; |
107 | int base; |
108 | int base; |
108 | cell *p; |
109 | cell *p; |
109 | 110 | ||
110 | for (i = A_FIRST; i < A_LAST; i++) { |
111 | for (i = A_FIRST; i < A_LAST; i++) { |
111 | base = i * B_COLS + 1; |
112 | base = i * B_COLS + 1; |
112 | p = &board[base]; |
113 | p = &board[base]; |
113 | for (j = B_COLS - 2; *p++ != 0;) { |
114 | for (j = B_COLS - 2; *p++ != 0;) { |
114 | if (--j <= 0) { |
115 | if (--j <= 0) { |
115 | /* This row is to be elided */ |
116 | /* This row is to be elided */ |
116 | rows++; |
117 | rows++; |
117 | memset(&board[base], 0, sizeof(cell) * (B_COLS - 2)); |
118 | memset(&board[base], 0, sizeof(cell) * (B_COLS - 2)); |
118 | 119 | ||
119 | scr_update(); |
120 | scr_update(); |
120 | tsleep(); |
121 | tsleep(); |
121 | 122 | ||
122 | while (--base != 0) |
123 | while (--base != 0) |
123 | board[base + B_COLS] = board[base]; |
124 | board[base + B_COLS] = board[base]; |
124 | 125 | ||
125 | scr_update(); |
126 | scr_update(); |
126 | tsleep(); |
127 | tsleep(); |
127 | 128 | ||
128 | break; |
129 | break; |
129 | } |
130 | } |
130 | } |
131 | } |
131 | } |
132 | } |
132 | 133 | ||
133 | switch (rows) { |
134 | switch (rows) { |
134 | case 1: |
135 | case 1: |
135 | score += 10; |
136 | score += 10; |
136 | break; |
137 | break; |
137 | case 2: |
138 | case 2: |
138 | score += 30; |
139 | score += 30; |
139 | break; |
140 | break; |
140 | case 3: |
141 | case 3: |
141 | score += 70; |
142 | score += 70; |
142 | break; |
143 | break; |
143 | case 4: |
144 | case 4: |
144 | score += 150; |
145 | score += 150; |
145 | break; |
146 | break; |
146 | default: |
147 | default: |
147 | break; |
148 | break; |
148 | } |
149 | } |
149 | } |
150 | } |
150 | 151 | ||
151 | const struct shape *randshape(void) |
152 | const struct shape *randshape(void) |
152 | { |
153 | { |
153 | const struct shape *tmp = &shapes[random() % 7]; |
154 | const struct shape *tmp = &shapes[random() % 7]; |
154 | int i; |
155 | int i; |
155 | int j = random() % 4; |
156 | int j = random() % 4; |
156 | 157 | ||
157 | for (i = 0; i < j; i++) |
158 | for (i = 0; i < j; i++) |
158 | tmp = &shapes[classic ? tmp->rotc : tmp->rot]; |
159 | tmp = &shapes[classic ? tmp->rotc : tmp->rot]; |
159 | 160 | ||
160 | return (tmp); |
161 | return (tmp); |
161 | } |
162 | } |
162 | 163 | ||
163 | static void srandomdev(void) |
164 | static void srandomdev(void) |
164 | { |
165 | { |
165 | struct timeval tv; |
166 | struct timeval tv; |
166 | 167 | ||
167 | gettimeofday(&tv, NULL); |
168 | gettimeofday(&tv, NULL); |
168 | srandom(tv.tv_sec + tv.tv_usec / 100000); |
169 | srandom(tv.tv_sec + tv.tv_usec / 100000); |
169 | } |
170 | } |
170 | 171 | ||
171 | static void tetris_menu_draw(int level) |
172 | static void tetris_menu_draw(int level) |
172 | { |
173 | { |
173 | clear_screen(); |
174 | clear_screen(); |
174 | moveto(5, 10); |
175 | moveto(5, 10); |
175 | puts("Tetris\n\n"); |
176 | puts("Tetris\n\n"); |
176 | 177 | ||
177 | moveto(8, 10); |
178 | moveto(8, 10); |
178 | printf("Level = %d (press keys 1 - 9 to change)", level); |
179 | printf("Level = %d (press keys 1 - 9 to change)", level); |
179 | moveto(9, 10); |
180 | moveto(9, 10); |
180 | printf("Preview is %s (press 'p' to change)", (showpreview ? "on ": "off")); |
181 | printf("Preview is %s (press 'p' to change)", (showpreview ? "on ": "off")); |
181 | moveto(12, 10); |
182 | moveto(12, 10); |
182 | printf("Press 'h' to show hiscore table."); |
183 | printf("Press 'h' to show hiscore table."); |
183 | moveto(13, 10); |
184 | moveto(13, 10); |
184 | printf("Press 's' to start game."); |
185 | printf("Press 's' to start game."); |
185 | moveto(14, 10); |
186 | moveto(14, 10); |
186 | printf("Press 'q' to quit game."); |
187 | printf("Press 'q' to quit game."); |
187 | moveto(20, 10); |
188 | moveto(20, 10); |
188 | printf("In game controls:"); |
189 | printf("In game controls:"); |
189 | moveto(21, 0); |
190 | moveto(21, 0); |
190 | puts(key_msg); |
191 | puts(key_msg); |
191 | } |
192 | } |
192 | 193 | ||
193 | static int tetris_menu(int *level) |
194 | static int tetris_menu(int *level) |
194 | { |
195 | { |
195 | tetris_menu_draw(*level); |
196 | tetris_menu_draw(*level); |
196 | while (1) { |
197 | while (1) { |
197 | int i = getchar(); |
198 | int i = getchar(); |
198 | 199 | ||
199 | switch(i) { |
200 | switch(i) { |
200 | case 'p': |
201 | case 'p': |
201 | showpreview = !showpreview; |
202 | showpreview = !showpreview; |
202 | moveto(9, 21); |
203 | moveto(9, 21); |
203 | if (showpreview) |
204 | if (showpreview) |
204 | printf("on "); |
205 | printf("on "); |
205 | else |
206 | else |
206 | printf("off"); |
207 | printf("off"); |
207 | break; |
208 | break; |
208 | case 'h': |
209 | case 'h': |
- | 210 | loadscores(); |
|
209 | showscores(firstgame); |
211 | showscores(firstgame); |
210 | tetris_menu_draw(*level); |
212 | tetris_menu_draw(*level); |
211 | break; |
213 | break; |
212 | case 's': |
214 | case 's': |
213 | firstgame = 0; |
215 | firstgame = 0; |
214 | return 1; |
216 | return 1; |
215 | case 'q': |
217 | case 'q': |
216 | return 0; |
218 | return 0; |
217 | case '1': |
219 | case '1': |
218 | case '2': |
220 | case '2': |
219 | case '3': |
221 | case '3': |
220 | case '4': |
222 | case '4': |
221 | case '5': |
223 | case '5': |
222 | case '6': |
224 | case '6': |
223 | case '7': |
225 | case '7': |
224 | case '8': |
226 | case '8': |
225 | case '9': |
227 | case '9': |
226 | *level = i - '0'; |
228 | *level = i - '0'; |
227 | moveto(8, 18); |
229 | moveto(8, 18); |
228 | printf("%d", *level); |
230 | printf("%d", *level); |
229 | break; |
231 | break; |
230 | } |
232 | } |
231 | } |
233 | } |
232 | } |
234 | } |
233 | 235 | ||
234 | int main(int argc, char *argv[]) |
236 | int main(int argc, char *argv[]) |
235 | { |
237 | { |
236 | int pos; |
238 | int pos; |
237 | int c; |
239 | int c; |
238 | char *keys; |
240 | char *keys; |
239 | int level = 2; |
241 | int level = 2; |
240 | char key_write[6][10]; |
242 | char key_write[6][10]; |
241 | int i; |
243 | int i; |
242 | int j; |
244 | int j; |
243 | int ch; |
245 | int ch; |
244 | 246 | ||
245 | keys = "jkl pq"; |
247 | keys = "jkl pq"; |
246 | 248 | ||
247 | classic = 0; |
249 | classic = 0; |
248 | showpreview = 1; |
250 | showpreview = 1; |
249 | 251 | ||
250 | while ((ch = getopt(argc, argv, "ck:ps")) != -1) |
252 | while ((ch = getopt(argc, argv, "ck:ps")) != -1) |
251 | switch(ch) { |
253 | switch(ch) { |
252 | case 'c': |
254 | case 'c': |
253 | /* |
255 | /* |
254 | * this means: |
256 | * this means: |
255 | * - rotate the other way |
257 | * - rotate the other way |
256 | * - no reverse video |
258 | * - no reverse video |
257 | */ |
259 | */ |
258 | classic = 1; |
260 | classic = 1; |
259 | break; |
261 | break; |
260 | case 'k': |
262 | case 'k': |
261 | if (str_size(keys = optarg) != 6) |
263 | if (str_size(keys = optarg) != 6) |
262 | usage(); |
264 | usage(); |
263 | break; |
265 | break; |
264 | case 'p': |
266 | case 'p': |
265 | showpreview = 1; |
267 | showpreview = 1; |
266 | break; |
268 | break; |
267 | case 's': |
269 | case 's': |
268 | showscores(0); |
270 | showscores(0); |
269 | exit(0); |
271 | exit(0); |
270 | default: |
272 | default: |
271 | usage(); |
273 | usage(); |
272 | } |
274 | } |
273 | 275 | ||
274 | argc -= optind; |
276 | argc -= optind; |
275 | argv += optind; |
277 | argv += optind; |
276 | 278 | ||
277 | if (argc) |
279 | if (argc) |
278 | usage(); |
280 | usage(); |
279 | 281 | ||
280 | for (i = 0; i <= 5; i++) { |
282 | for (i = 0; i <= 5; i++) { |
281 | for (j = i + 1; j <= 5; j++) { |
283 | for (j = i + 1; j <= 5; j++) { |
282 | if (keys[i] == keys[j]) |
284 | if (keys[i] == keys[j]) |
283 | errx(1, "duplicate command keys specified."); |
285 | errx(1, "duplicate command keys specified."); |
284 | } |
286 | } |
285 | 287 | ||
286 | if (keys[i] == ' ') |
288 | if (keys[i] == ' ') |
287 | str_cpy(key_write[i], sizeof(key_write[i]), "<space>"); |
289 | str_cpy(key_write[i], sizeof(key_write[i]), "<space>"); |
288 | else { |
290 | else { |
289 | key_write[i][0] = keys[i]; |
291 | key_write[i][0] = keys[i]; |
290 | key_write[i][1] = '\0'; |
292 | key_write[i][1] = '\0'; |
291 | } |
293 | } |
292 | } |
294 | } |
293 | 295 | ||
294 | snprintf(key_msg, sizeof(key_msg), |
296 | snprintf(key_msg, sizeof(key_msg), |
295 | "%s - left %s - rotate %s - right %s - drop %s - pause %s - quit", |
297 | "%s - left %s - rotate %s - right %s - drop %s - pause %s - quit", |
296 | key_write[0], key_write[1], key_write[2], key_write[3], |
298 | key_write[0], key_write[1], key_write[2], key_write[3], |
297 | key_write[4], key_write[5]); |
299 | key_write[4], key_write[5]); |
298 | 300 | ||
299 | scr_init(); |
301 | scr_init(); |
- | 302 | if (loadscores() != EOK) |
|
300 | initscores(); |
303 | initscores(); |
- | 304 | ||
301 | while (tetris_menu(&level)) { |
305 | while (tetris_menu(&level)) { |
302 | fallrate = 1000000 / level; |
306 | fallrate = 1000000 / level; |
303 | 307 | ||
304 | scr_clear(); |
308 | scr_clear(); |
305 | setup_board(); |
309 | setup_board(); |
306 | 310 | ||
307 | srandomdev(); |
311 | srandomdev(); |
308 | scr_set(); |
312 | scr_set(); |
309 | 313 | ||
310 | pos = A_FIRST * B_COLS + (B_COLS / 2) - 1; |
314 | pos = A_FIRST * B_COLS + (B_COLS / 2) - 1; |
311 | nextshape = randshape(); |
315 | nextshape = randshape(); |
312 | curshape = randshape(); |
316 | curshape = randshape(); |
313 | 317 | ||
314 | scr_msg(key_msg, 1); |
318 | scr_msg(key_msg, 1); |
315 | 319 | ||
316 | while (1) { |
320 | while (1) { |
317 | place(curshape, pos, 1); |
321 | place(curshape, pos, 1); |
318 | scr_update(); |
322 | scr_update(); |
319 | place(curshape, pos, 0); |
323 | place(curshape, pos, 0); |
320 | c = tgetchar(); |
324 | c = tgetchar(); |
321 | if (c < 0) { |
325 | if (c < 0) { |
322 | /* |
326 | /* |
323 | * Timeout. Move down if possible. |
327 | * Timeout. Move down if possible. |
324 | */ |
328 | */ |
325 | if (fits_in(curshape, pos + B_COLS)) { |
329 | if (fits_in(curshape, pos + B_COLS)) { |
326 | pos += B_COLS; |
330 | pos += B_COLS; |
327 | continue; |
331 | continue; |
328 | } |
332 | } |
329 | 333 | ||
330 | /* |
334 | /* |
331 | * Put up the current shape `permanently', |
335 | * Put up the current shape `permanently', |
332 | * bump score, and elide any full rows. |
336 | * bump score, and elide any full rows. |
333 | */ |
337 | */ |
334 | place(curshape, pos, 1); |
338 | place(curshape, pos, 1); |
335 | score++; |
339 | score++; |
336 | elide(); |
340 | elide(); |
337 | 341 | ||
338 | /* |
342 | /* |
339 | * Choose a new shape. If it does not fit, |
343 | * Choose a new shape. If it does not fit, |
340 | * the game is over. |
344 | * the game is over. |
341 | */ |
345 | */ |
342 | curshape = nextshape; |
346 | curshape = nextshape; |
343 | nextshape = randshape(); |
347 | nextshape = randshape(); |
344 | pos = A_FIRST * B_COLS + (B_COLS / 2) - 1; |
348 | pos = A_FIRST * B_COLS + (B_COLS / 2) - 1; |
345 | 349 | ||
346 | if (!fits_in(curshape, pos)) |
350 | if (!fits_in(curshape, pos)) |
347 | break; |
351 | break; |
348 | 352 | ||
349 | continue; |
353 | continue; |
350 | } |
354 | } |
351 | 355 | ||
352 | /* |
356 | /* |
353 | * Handle command keys. |
357 | * Handle command keys. |
354 | */ |
358 | */ |
355 | if (c == keys[5]) { |
359 | if (c == keys[5]) { |
356 | /* quit */ |
360 | /* quit */ |
357 | break; |
361 | break; |
358 | } |
362 | } |
359 | 363 | ||
360 | if (c == keys[4]) { |
364 | if (c == keys[4]) { |
361 | static char msg[] = |
365 | static char msg[] = |
362 | "paused - press RETURN to continue"; |
366 | "paused - press RETURN to continue"; |
363 | 367 | ||
364 | place(curshape, pos, 1); |
368 | place(curshape, pos, 1); |
365 | do { |
369 | do { |
366 | scr_update(); |
370 | scr_update(); |
367 | scr_msg(key_msg, 0); |
371 | scr_msg(key_msg, 0); |
368 | scr_msg(msg, 1); |
372 | scr_msg(msg, 1); |
369 | (void) fflush(stdout); |
373 | (void) fflush(stdout); |
370 | } while (rwait((struct timeval *) NULL) == -1); |
374 | } while (rwait((struct timeval *) NULL) == -1); |
371 | 375 | ||
372 | scr_msg(msg, 0); |
376 | scr_msg(msg, 0); |
373 | scr_msg(key_msg, 1); |
377 | scr_msg(key_msg, 1); |
374 | place(curshape, pos, 0); |
378 | place(curshape, pos, 0); |
375 | continue; |
379 | continue; |
376 | } |
380 | } |
377 | 381 | ||
378 | if (c == keys[0]) { |
382 | if (c == keys[0]) { |
379 | /* move left */ |
383 | /* move left */ |
380 | if (fits_in(curshape, pos - 1)) |
384 | if (fits_in(curshape, pos - 1)) |
381 | pos--; |
385 | pos--; |
382 | continue; |
386 | continue; |
383 | } |
387 | } |
384 | 388 | ||
385 | if (c == keys[1]) { |
389 | if (c == keys[1]) { |
386 | /* turn */ |
390 | /* turn */ |
387 | const struct shape *new = |
391 | const struct shape *new = |
388 | &shapes[classic ? curshape->rotc : curshape->rot]; |
392 | &shapes[classic ? curshape->rotc : curshape->rot]; |
389 | 393 | ||
390 | if (fits_in(new, pos)) |
394 | if (fits_in(new, pos)) |
391 | curshape = new; |
395 | curshape = new; |
392 | continue; |
396 | continue; |
393 | } |
397 | } |
394 | 398 | ||
395 | if (c == keys[2]) { |
399 | if (c == keys[2]) { |
396 | /* move right */ |
400 | /* move right */ |
397 | if (fits_in(curshape, pos + 1)) |
401 | if (fits_in(curshape, pos + 1)) |
398 | pos++; |
402 | pos++; |
399 | continue; |
403 | continue; |
400 | } |
404 | } |
401 | 405 | ||
402 | if (c == keys[3]) { |
406 | if (c == keys[3]) { |
403 | /* move to bottom */ |
407 | /* move to bottom */ |
404 | while (fits_in(curshape, pos + B_COLS)) { |
408 | while (fits_in(curshape, pos + B_COLS)) { |
405 | pos += B_COLS; |
409 | pos += B_COLS; |
406 | score++; |
410 | score++; |
407 | } |
411 | } |
408 | continue; |
412 | continue; |
409 | } |
413 | } |
410 | 414 | ||
411 | if (c == '\f') { |
415 | if (c == '\f') { |
412 | scr_clear(); |
416 | scr_clear(); |
413 | scr_msg(key_msg, 1); |
417 | scr_msg(key_msg, 1); |
414 | } |
418 | } |
415 | } |
419 | } |
416 | 420 | ||
417 | scr_clear(); |
421 | scr_clear(); |
- | 422 | loadscores(); |
|
418 | insertscore(score, level); |
423 | insertscore(score, level); |
- | 424 | savescores(); |
|
419 | score = 0; |
425 | score = 0; |
420 | } |
426 | } |
421 | 427 | ||
422 | scr_clear(); |
428 | scr_clear(); |
423 | printf("\nGame over.\n"); |
429 | printf("\nGame over.\n"); |
424 | scr_end(); |
430 | scr_end(); |
425 | 431 | ||
426 | return 0; |
432 | return 0; |
427 | } |
433 | } |
428 | 434 | ||
429 | void usage(void) |
435 | void usage(void) |
430 | { |
436 | { |
431 | fprintf(stderr, "usage: tetris [-ps] [-k keys]\n"); |
437 | fprintf(stderr, "usage: tetris [-ps] [-k keys]\n"); |
432 | exit(1); |
438 | exit(1); |
433 | } |
439 | } |
434 | 440 | ||
435 | /** @} |
441 | /** @} |
436 | */ |
442 | */ |
437 | 443 |