Changeset a35b458 in mainline for uspace/app/tetris
- Timestamp:
- 2018-03-02T20:10:49Z (8 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)
- Location:
- uspace/app/tetris
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/tetris/scores.c
r3061bc1 ra35b458 98 98 { 99 99 int i; 100 100 101 101 clear_screen(); 102 102 moveto(10, 0); 103 103 printf("\tRank \tLevel \tName\t points\n"); 104 104 printf("\t========================================================\n"); 105 105 106 106 for (i = 0; i < NUMSPOTS - 1; i++) 107 107 printf("\t%6d %6d %-16s %20d\n", 108 108 i + 1, scores[i].hs_level, scores[i].hs_name, scores[i].hs_score); 109 109 110 110 if (!firstgame) { 111 111 printf("\t========================================================\n"); … … 113 113 scores[NUMSPOTS - 1].hs_level, scores[NUMSPOTS - 1].hs_name, scores[NUMSPOTS - 1].hs_score); 114 114 } 115 115 116 116 printf("\n\n\n\n\tPress any key to return to main menu."); 117 117 getchar(); … … 125 125 cons_event_t ev; 126 126 kbd_event_t *kev; 127 127 128 128 clear_screen(); 129 129 moveto(10, 10); … … 133 133 i = 6; 134 134 off = 6; 135 135 136 136 moveto(10 , 28); 137 137 printf("%s%.*s", scores[NUMSPOTS - 1].hs_name, MAXLOGNAME-i, 138 138 "........................................"); 139 139 140 140 while (1) { 141 141 console_flush(console); 142 142 if (!console_get_event(console, &ev)) 143 143 exit(1); 144 144 145 145 if (ev.type != CEV_KEY || ev.ev.key.type == KEY_RELEASE) 146 146 continue; 147 147 148 148 kev = &ev.ev.key; 149 149 150 150 if (kev->key == KC_ENTER || kev->key == KC_NENTER) 151 151 break; 152 152 153 153 if (kev->key == KC_BACKSPACE) { 154 154 if (i > 0) { 155 155 wchar_t uc; 156 156 157 157 --i; 158 158 while (off > 0) { … … 164 164 break; 165 165 } 166 166 167 167 scores[NUMSPOTS - 1].hs_name[off] = '\0'; 168 168 } … … 176 176 } 177 177 } 178 178 179 179 moveto(10, 28); 180 180 printf("%s%.*s", scores[NUMSPOTS - 1].hs_name, MAXLOGNAME - i, 181 181 "........................................"); 182 182 } 183 183 184 184 scores[NUMSPOTS - 1].hs_score = score; 185 185 scores[NUMSPOTS - 1].hs_level = level; 186 186 187 187 i = NUMSPOTS - 1; 188 188 while ((i > 0) && (scores[i - 1].hs_score < score)) 189 189 i--; 190 190 191 191 for (j = NUMSPOTS - 2; j > i; j--) 192 192 copyhiscore(j, j-1); 193 193 194 194 copyhiscore(i, NUMSPOTS - 1); 195 195 } … … 235 235 return; 236 236 } 237 237 238 238 cnt = fwrite(scores, sizeof(struct highscore), NUMSPOTS, f); 239 239 rc = fclose(f); -
uspace/app/tetris/screen.c
r3061bc1 ra35b458 147 147 sysarg_t ccap; 148 148 errno_t rc = console_get_color_cap(console, &ccap); 149 149 150 150 if (rc != EOK) 151 151 return false; 152 152 153 153 return ((ccap & CONSOLE_CAP_RGB) == CONSOLE_CAP_RGB); 154 154 } … … 160 160 { 161 161 winsize_t ws; 162 162 163 163 Rows = 0; 164 164 Cols = 0; 165 165 166 166 if (get_display_size(&ws) == 0) { 167 167 Rows = ws.ws_row; … … 170 170 171 171 use_color = get_display_color_sup(); 172 172 173 173 if ((Rows < MINROWS) || (Cols < MINCOLS)) { 174 174 char smallscr[55]; 175 175 176 176 snprintf(smallscr, sizeof(smallscr), 177 177 "the screen is too small (must be at least %dx%d)", … … 180 180 } 181 181 isset = 1; 182 182 183 183 scr_clear(); 184 184 } … … 196 196 if (isset) 197 197 scr_end(); 198 198 199 199 fprintf(stderr, "aborting: %s", why); 200 200 abort(); … … 213 213 int j; 214 214 int ccol; 215 215 216 216 /* Always leave cursor after last displayed point */ 217 217 curscreen[D_LAST * B_COLS - 1] = -1; 218 218 219 219 if (score != curscore) { 220 220 moveto(0, 0); … … 222 222 curscore = score; 223 223 } 224 224 225 225 /* Draw preview of next pattern */ 226 226 if ((showpreview) && (nextshape != lastshape)) { … … 228 228 static int r = 5, c = 2; 229 229 int tr, tc, t; 230 230 231 231 lastshape = nextshape; 232 232 233 233 /* Clean */ 234 234 resume_normal(); … … 241 241 moveto(r + 2, c - 1); 242 242 putstr(" "); 243 243 244 244 moveto(r - 3, c - 2); 245 245 putstr("Next shape:"); 246 246 247 247 /* Draw */ 248 248 start_standout(nextshape->color); … … 252 252 t = c + r * B_COLS; 253 253 t += nextshape->off[i]; 254 254 255 255 tr = t / B_COLS; 256 256 tc = t % B_COLS; 257 257 258 258 moveto(tr, 2*tc); 259 259 putstr(" "); … … 261 261 resume_normal(); 262 262 } 263 263 264 264 bp = &board[D_FIRST * B_COLS]; 265 265 sp = &curscreen[D_FIRST * B_COLS]; … … 269 269 if (*sp == (so = *bp)) 270 270 continue; 271 271 272 272 *sp = so; 273 273 if (i != ccol) { … … 278 278 moveto(RTOD(j), CTOD(i)); 279 279 } 280 280 281 281 if (so != cur_so) { 282 282 if (so) … … 287 287 } 288 288 putstr(" "); 289 289 290 290 ccol = i + 1; 291 291 /* … … 297 297 * the next cell is a different color. 298 298 */ 299 299 300 300 if ((i > STOP) || (sp[1] != bp[1]) || (so != bp[1])) 301 301 continue; 302 302 303 303 if (sp[2] != bp[2]) 304 304 sp[1] = -1; … … 309 309 } 310 310 } 311 311 312 312 if (cur_so) 313 313 resume_normal(); 314 314 315 315 console_flush(console); 316 316 } … … 323 323 { 324 324 int l = str_size(s); 325 325 326 326 moveto(Rows - 2, ((Cols - l) >> 1) - 1); 327 327 328 328 if (set) 329 329 putstr(s); … … 341 341 { 342 342 suseconds_t timeout = fallrate; 343 343 344 344 while (timeout > 0) { 345 345 cons_event_t event; 346 346 347 347 if (!console_get_event_timeout(console, &event, &timeout)) 348 348 break; … … 359 359 * and increase speed. 360 360 */ 361 361 362 362 if (timeleft <= 0) { 363 363 faster(); 364 364 timeleft = fallrate; 365 365 } 366 366 367 367 /* 368 368 * Wait to see if there is any input. If so, take it and … … 371 371 * make timeleft zero and return -1. 372 372 */ 373 373 374 374 wchar_t c = 0; 375 375 376 376 while (c == 0) { 377 377 cons_event_t event; 378 378 379 379 if (!console_get_event_timeout(console, &event, &timeleft)) { 380 380 timeleft = 0; 381 381 return -1; 382 382 } 383 383 384 384 if (event.type == CEV_KEY && event.ev.key.type == KEY_PRESS) 385 385 c = event.ev.key.c; 386 386 } 387 387 388 388 return (int) c; 389 389 } … … 395 395 { 396 396 wchar_t c = 0; 397 397 398 398 while (c == 0) { 399 399 cons_event_t event; 400 400 401 401 if (!console_get_event(console, &event)) 402 402 return -1; 403 403 404 404 if (event.type == CEV_KEY && event.ev.key.type == KEY_PRESS) 405 405 c = event.ev.key.c; 406 406 } 407 407 408 408 return (int) c; 409 409 } -
uspace/app/tetris/shapes.c
r3061bc1 ra35b458 96 96 { 97 97 const int *o = shape->off; 98 98 99 99 if ((board[pos]) || (board[pos + *o++]) || (board[pos + *o++]) || 100 100 (board[pos + *o])) 101 101 return 0; 102 102 103 103 return 1; 104 104 } … … 111 111 { 112 112 const int *o = shape->off; 113 113 114 114 board[pos] = onoff ? shape->color : 0x000000; 115 115 board[pos + *o++] = onoff ? shape->color : 0x000000; -
uspace/app/tetris/tetris.c
r3061bc1 ra35b458 97 97 int i; 98 98 cell *p = board; 99 99 100 100 for (i = B_SIZE; i; i--) 101 101 *p++ = (i <= (2 * B_COLS) || (i % B_COLS) < 2) ? 0x0000ff : 0x000000; … … 112 112 int base; 113 113 cell *p; 114 114 115 115 for (i = A_FIRST; i < A_LAST; i++) { 116 116 base = i * B_COLS + 1; … … 121 121 rows++; 122 122 memset(&board[base], 0, sizeof(cell) * (B_COLS - 2)); 123 123 124 124 scr_update(); 125 125 tsleep(); 126 126 127 127 while (--base != 0) 128 128 board[base + B_COLS] = board[base]; 129 129 130 130 scr_update(); 131 131 tsleep(); 132 132 133 133 break; 134 134 } 135 135 } 136 136 } 137 137 138 138 switch (rows) { 139 139 case 1: … … 159 159 int i; 160 160 int j = rand() % 4; 161 161 162 162 for (i = 0; i < j; i++) 163 163 tmp = &shapes[classic ? tmp->rotc : tmp->rot]; 164 164 165 165 return (tmp); 166 166 } … … 169 169 { 170 170 struct timeval tv; 171 171 172 172 gettimeofday(&tv, NULL); 173 173 srand(tv.tv_sec + tv.tv_usec / 100000); … … 179 179 moveto(5, 10); 180 180 puts("Tetris\n\n"); 181 181 182 182 moveto(8, 10); 183 183 printf("Level = %d (press keys 1 - 9 to change)", level); … … 201 201 while (1) { 202 202 int i = getchar(); 203 203 204 204 switch(i) { 205 205 case 'p': … … 248 248 int j; 249 249 int ch; 250 250 251 251 console = console_init(stdin, stdout); 252 252 253 253 keys = "jkl pq"; 254 254 255 255 classic = 0; 256 256 showpreview = 1; 257 257 258 258 while ((ch = getopt(argc, argv, "ck:ps")) != -1) 259 259 switch(ch) { … … 279 279 usage(); 280 280 } 281 281 282 282 argc -= optind; 283 283 argv += optind; 284 284 285 285 if (argc) 286 286 usage(); 287 287 288 288 for (i = 0; i <= 5; i++) { 289 289 for (j = i + 1; j <= 5; j++) { … … 293 293 } 294 294 } 295 295 296 296 if (keys[i] == ' ') 297 297 str_cpy(key_write[i], sizeof(key_write[i]), "<space>"); … … 301 301 } 302 302 } 303 303 304 304 snprintf(key_msg, sizeof(key_msg), 305 305 "%s - left %s - rotate %s - right %s - drop %s - pause %s - quit", 306 306 key_write[0], key_write[1], key_write[2], key_write[3], 307 307 key_write[4], key_write[5]); 308 308 309 309 scr_init(); 310 310 if (loadscores() != EOK) … … 313 313 while (tetris_menu(&level)) { 314 314 fallrate = 1000000 / level; 315 315 316 316 scr_clear(); 317 317 setup_board(); 318 318 319 319 srandomdev(); 320 320 scr_set(); 321 321 322 322 pos = A_FIRST * B_COLS + (B_COLS / 2) - 1; 323 323 nextshape = randshape(); 324 324 curshape = randshape(); 325 325 326 326 scr_msg(key_msg, 1); 327 327 328 328 while (1) { 329 329 place(curshape, pos, 1); … … 339 339 continue; 340 340 } 341 341 342 342 /* 343 343 * Put up the current shape `permanently', … … 347 347 score++; 348 348 elide(); 349 349 350 350 /* 351 351 * Choose a new shape. If it does not fit, … … 355 355 nextshape = randshape(); 356 356 pos = A_FIRST * B_COLS + (B_COLS / 2) - 1; 357 357 358 358 if (!fits_in(curshape, pos)) 359 359 break; 360 361 continue; 362 } 363 360 361 continue; 362 } 363 364 364 /* 365 365 * Handle command keys. … … 369 369 break; 370 370 } 371 371 372 372 if (c == keys[4]) { 373 373 static char msg[] = 374 374 "paused - press RETURN to continue"; 375 375 376 376 place(curshape, pos, 1); 377 377 do { … … 381 381 console_flush(console); 382 382 } while (!twait()); 383 383 384 384 scr_msg(msg, 0); 385 385 scr_msg(key_msg, 1); … … 387 387 continue; 388 388 } 389 389 390 390 if (c == keys[0]) { 391 391 /* move left */ … … 394 394 continue; 395 395 } 396 396 397 397 if (c == keys[1]) { 398 398 /* turn */ 399 399 const struct shape *new = 400 400 &shapes[classic ? curshape->rotc : curshape->rot]; 401 401 402 402 if (fits_in(new, pos)) 403 403 curshape = new; 404 404 continue; 405 405 } 406 406 407 407 if (c == keys[2]) { 408 408 /* move right */ … … 411 411 continue; 412 412 } 413 413 414 414 if (c == keys[3]) { 415 415 /* move to bottom */ … … 420 420 continue; 421 421 } 422 422 423 423 if (c == '\f') { 424 424 scr_clear(); … … 426 426 } 427 427 } 428 428 429 429 scr_clear(); 430 430 loadscores(); … … 433 433 score = 0; 434 434 } 435 435 436 436 scr_clear(); 437 437 printf("\nGame over.\n"); 438 438 scr_end(); 439 439 440 440 return 0; 441 441 }
Note:
See TracChangeset
for help on using the changeset viewer.