Changeset a35b458 in mainline for uspace/dist/src/c/demos/tetris/tetris.c
- Timestamp:
- 2018-03-02T20:10:49Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- f1380b7
- Parents:
- 3061bc1
- git-author:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-02-28 17:38:31)
- git-committer:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-03-02 20:10:49)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/dist/src/c/demos/tetris/tetris.c
r3061bc1 ra35b458 98 98 int i; 99 99 cell *p = board; 100 100 101 101 for (i = B_SIZE; i; i--) 102 102 *p++ = (i <= (2 * B_COLS) || (i % B_COLS) < 2) ? 0x0000ff : 0x000000; … … 113 113 int base; 114 114 cell *p; 115 115 116 116 for (i = A_FIRST; i < A_LAST; i++) { 117 117 base = i * B_COLS + 1; … … 122 122 rows++; 123 123 memset(&board[base], 0, sizeof(cell) * (B_COLS - 2)); 124 124 125 125 scr_update(); 126 126 tsleep(); 127 127 128 128 while (--base != 0) 129 129 board[base + B_COLS] = board[base]; 130 130 131 131 scr_update(); 132 132 tsleep(); 133 133 134 134 break; 135 135 } 136 136 } 137 137 } 138 138 139 139 switch (rows) { 140 140 case 1: … … 160 160 int i; 161 161 int j = rand() % 4; 162 162 163 163 for (i = 0; i < j; i++) 164 164 tmp = &shapes[classic ? tmp->rotc : tmp->rot]; 165 165 166 166 return (tmp); 167 167 } … … 170 170 { 171 171 struct timeval tv; 172 172 173 173 gettimeofday(&tv, NULL); 174 174 srand(tv.tv_sec + tv.tv_usec / 100000); … … 180 180 moveto(5, 10); 181 181 puts("Tetris\n\n"); 182 182 183 183 moveto(8, 10); 184 184 printf("Level = %d (press keys 1 - 9 to change)", level); … … 202 202 while (1) { 203 203 int i = getchar(); 204 204 205 205 switch(i) { 206 206 case 'p': … … 249 249 int j; 250 250 int ch; 251 251 252 252 console = console_init(stdin, stdout); 253 253 254 254 keys = "jkl pq"; 255 255 256 256 classic = 0; 257 257 showpreview = 1; 258 258 259 259 while ((ch = getopt(argc, argv, "ck:ps")) != -1) 260 260 switch(ch) { … … 280 280 usage(); 281 281 } 282 282 283 283 argc -= optind; 284 284 argv += optind; 285 285 286 286 if (argc) 287 287 usage(); 288 288 289 289 for (i = 0; i <= 5; i++) { 290 290 for (j = i + 1; j <= 5; j++) { … … 292 292 errx(1, "%s", "duplicate command keys specified."); 293 293 } 294 294 295 295 if (keys[i] == ' ') 296 296 str_cpy(key_write[i], sizeof(key_write[i]), "<space>"); … … 300 300 } 301 301 } 302 302 303 303 snprintf(key_msg, sizeof(key_msg), 304 304 "%s - left %s - rotate %s - right %s - drop %s - pause %s - quit", 305 305 key_write[0], key_write[1], key_write[2], key_write[3], 306 306 key_write[4], key_write[5]); 307 307 308 308 scr_init(); 309 309 if (loadscores() != EOK) … … 312 312 while (tetris_menu(&level)) { 313 313 fallrate = 1000000 / level; 314 314 315 315 scr_clear(); 316 316 setup_board(); 317 317 318 318 srandomdev(); 319 319 scr_set(); 320 320 321 321 pos = A_FIRST * B_COLS + (B_COLS / 2) - 1; 322 322 nextshape = randshape(); 323 323 curshape = randshape(); 324 324 325 325 scr_msg(key_msg, 1); 326 326 327 327 while (1) { 328 328 place(curshape, pos, 1); … … 338 338 continue; 339 339 } 340 340 341 341 /* 342 342 * Put up the current shape `permanently', … … 346 346 score++; 347 347 elide(); 348 348 349 349 /* 350 350 * Choose a new shape. If it does not fit, … … 354 354 nextshape = randshape(); 355 355 pos = A_FIRST * B_COLS + (B_COLS / 2) - 1; 356 356 357 357 if (!fits_in(curshape, pos)) 358 358 break; 359 360 continue; 361 } 362 359 360 continue; 361 } 362 363 363 /* 364 364 * Handle command keys. … … 368 368 break; 369 369 } 370 370 371 371 if (c == keys[4]) { 372 372 static char msg[] = 373 373 "paused - press RETURN to continue"; 374 374 375 375 place(curshape, pos, 1); 376 376 do { … … 380 380 console_flush(console); 381 381 } while (!twait()); 382 382 383 383 scr_msg(msg, 0); 384 384 scr_msg(key_msg, 1); … … 386 386 continue; 387 387 } 388 388 389 389 if (c == keys[0]) { 390 390 /* move left */ … … 393 393 continue; 394 394 } 395 395 396 396 if (c == keys[1]) { 397 397 /* turn */ 398 398 const struct shape *new = 399 399 &shapes[classic ? curshape->rotc : curshape->rot]; 400 400 401 401 if (fits_in(new, pos)) 402 402 curshape = new; 403 403 continue; 404 404 } 405 405 406 406 if (c == keys[2]) { 407 407 /* move right */ … … 410 410 continue; 411 411 } 412 412 413 413 if (c == keys[3]) { 414 414 /* move to bottom */ … … 419 419 continue; 420 420 } 421 421 422 422 if (c == '\f') { 423 423 scr_clear(); … … 425 425 } 426 426 } 427 427 428 428 scr_clear(); 429 429 loadscores(); … … 432 432 score = 0; 433 433 } 434 434 435 435 scr_clear(); 436 436 printf("\nGame over.\n"); 437 437 scr_end(); 438 438 439 439 return 0; 440 440 }
Note:
See TracChangeset
for help on using the changeset viewer.