Changeset 9996ed5 in mainline for tetris/tetris.c


Ignore:
Timestamp:
2006-06-05T20:40:44Z (19 years ago)
Author:
Josef Cejka <malyzelenyhnus@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
e9073f2
Parents:
0aa024b1
Message:

Tetris has now a new menu.
Hiscore table added.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tetris/tetris.c

    r0aa024b1 r9996ed5  
    159159}
    160160
     161static void tetris_scores(int firstgame)
     162{
     163}
     164
     165static void tetris_menu_draw(int level)
     166{
     167                clear_screen();
     168                moveto(5,10);
     169                puts("Tetris\n\n");
     170                       
     171                moveto(8,10);
     172                printf("Level = %d (press keys 1 - 9 to change)",level);
     173                moveto(9,10);
     174                printf("Preview is %s (press 'p' to change)", (showpreview?"on ":"off"));
     175                moveto(12,10);
     176                printf("Press 'h' to show hiscore table.");
     177                moveto(13,10);
     178                printf("Press 's' to start game.");
     179                moveto(14,10);
     180                printf("Press 'q' to quit game.");
     181                moveto(20,10);
     182                printf("In game controls:");
     183                moveto(21,0);
     184                puts(key_msg);
     185}
     186
     187static int tetris_menu(int *level)
     188{
     189        static int firstgame = 1;
     190        int i;
     191/*      if (showpreview == 0)
     192                (void)printf("Your score:  %d point%s  x  level %d  =  %d\n",
     193                    score, score == 1 ? "" : "s", level, score * level);
     194        else {
     195                (void)printf("Your score:  %d point%s x level %d x preview penalty %0.3f = %d\n",
     196                    score, score == 1 ? "" : "s", level, (double)PRE_PENALTY,
     197                    (int)(score * level * PRE_PENALTY));
     198                score = score * PRE_PENALTY;
     199        }
     200        savescore(level);
     201
     202        showscores(level);
     203       
     204        printf("\nHit 's' to new game, 'q' to quit.\n");
     205*/
     206        tetris_menu_draw(*level);
     207        while (1) {
     208       
     209                i = getchar();
     210               
     211                switch(i) {
     212                        case 'p':
     213                                showpreview = !showpreview;
     214                                moveto(9,21);
     215                                if (showpreview)
     216                                        printf("on ");
     217                                else
     218                                        printf("off");
     219                                       
     220                                break;
     221                        case 'h':
     222                                showscores(firstgame);
     223                                tetris_menu_draw(*level);
     224                                break;
     225                        case 's':
     226                                firstgame = 0;
     227                                return 1;
     228                        case 'q':
     229                                return 0;
     230                        case '1':
     231                        case '2':
     232                        case '3':
     233                        case '4':
     234                        case '5':
     235                        case '6':               
     236                        case '7':
     237                        case '8':
     238                        case '9':
     239                                *level = i - '0';
     240                                moveto(8,18);
     241                                printf("%d", *level);
     242                                break;
     243                }
     244        }
     245       
     246}
     247
    161248int
    162249main(int argc, char *argv[])
     
    175262//      setegid(gid);
    176263
    177         classic = showpreview = 0;
     264        classic = 0;
     265        showpreview = 1;
    178266
    179267/*      while ((ch = getopt(argc, argv, "ck:l:ps")) != -1) */
     
    233321                key_write[0], key_write[1], key_write[2], key_write[3],
    234322                key_write[4], key_write[5]);
    235 newgame:
     323
    236324        scr_init();
    237         setup_board();
    238 
    239         srandomdev();
    240         scr_set();
    241 
    242         pos = A_FIRST*B_COLS + (B_COLS/2)-1;
    243         nextshape = randshape();
    244         curshape = randshape();
    245 
    246         scr_msg(key_msg, 1);
    247 
    248         for (;;) {
    249                 place(curshape, pos, 1);
    250                 scr_update();
    251                 place(curshape, pos, 0);
    252                 c = tgetchar();
    253                 if (c < 0) {
     325        initscores();
     326        while (tetris_menu(&level)) {
     327
     328
     329                scr_clear();
     330                setup_board();
     331       
     332                srandomdev();
     333                scr_set();
     334       
     335                pos = A_FIRST*B_COLS + (B_COLS/2)-1;
     336                nextshape = randshape();
     337                curshape = randshape();
     338       
     339                scr_msg(key_msg, 1);
     340       
     341                for (;;) {
     342                        place(curshape, pos, 1);
     343                        scr_update();
     344                        place(curshape, pos, 0);
     345                        c = tgetchar();
     346                        if (c < 0) {
     347                                /*
     348                                 * Timeout.  Move down if possible.
     349                                 */
     350                                if (fits_in(curshape, pos + B_COLS)) {
     351                                        pos += B_COLS;
     352                                        continue;
     353                                }
     354       
     355                                /*
     356                                 * Put up the current shape `permanently',
     357                                 * bump score, and elide any full rows.
     358                                 */
     359                                place(curshape, pos, 1);
     360                                score++;
     361                                elide();
     362       
     363                                /*
     364                                 * Choose a new shape.  If it does not fit,
     365                                 * the game is over.
     366                                 */
     367                                curshape = nextshape;
     368                                nextshape = randshape();
     369                                pos = A_FIRST*B_COLS + (B_COLS/2)-1;
     370                                if (!fits_in(curshape, pos))
     371                                        break;
     372                                continue;
     373                        }
     374       
    254375                        /*
    255                          * Timeout.  Move down if possible.
     376                         * Handle command keys.
    256377                         */
    257                         if (fits_in(curshape, pos + B_COLS)) {
    258                                 pos += B_COLS;
    259                                 continue;
    260                         }
    261 
    262                         /*
    263                          * Put up the current shape `permanently',
    264                          * bump score, and elide any full rows.
    265                          */
    266                         place(curshape, pos, 1);
    267                         score++;
    268                         elide();
    269 
    270                         /*
    271                          * Choose a new shape.  If it does not fit,
    272                          * the game is over.
    273                          */
    274                         curshape = nextshape;
    275                         nextshape = randshape();
    276                         pos = A_FIRST*B_COLS + (B_COLS/2)-1;
    277                         if (!fits_in(curshape, pos))
     378                        if (c == keys[5]) {
     379                                /* quit */
    278380                                break;
    279                         continue;
     381                        }
     382                        if (c == keys[4]) {
     383                                static char msg[] =
     384                                    "paused - press RETURN to continue";
     385       
     386                                place(curshape, pos, 1);
     387                                do {
     388                                        scr_update();
     389                                        scr_msg(key_msg, 0);
     390                                        scr_msg(msg, 1);
     391        //                              (void) fflush(stdout);
     392                                } while (rwait((struct timeval *)NULL) == -1);
     393                                scr_msg(msg, 0);
     394                                scr_msg(key_msg, 1);
     395                                place(curshape, pos, 0);
     396                                continue;
     397                        }
     398                        if (c == keys[0]) {
     399                                /* move left */
     400                                if (fits_in(curshape, pos - 1))
     401                                        pos--;
     402                                continue;
     403                        }
     404                        if (c == keys[1]) {
     405                                /* turn */
     406                                const struct shape *new = &shapes[
     407                                    classic? curshape->rotc : curshape->rot];
     408       
     409                                if (fits_in(new, pos))
     410                                        curshape = new;
     411                                continue;
     412                        }
     413                        if (c == keys[2]) {
     414                                /* move right */
     415                                if (fits_in(curshape, pos + 1))
     416                                        pos++;
     417                                continue;
     418                        }
     419                        if (c == keys[3]) {
     420                                /* move to bottom */
     421                                while (fits_in(curshape, pos + B_COLS)) {
     422                                        pos += B_COLS;
     423                                        score++;
     424                                }
     425                                continue;
     426                        }
     427                        if (c == '\f') {
     428                                scr_clear();
     429                                scr_msg(key_msg, 1);
     430                        }
    280431                }
    281 
    282                 /*
    283                  * Handle command keys.
    284                  */
    285                 if (c == keys[5]) {
    286                         /* quit */
    287                         break;
    288                 }
    289                 if (c == keys[4]) {
    290                         static char msg[] =
    291                             "paused - press RETURN to continue";
    292 
    293                         place(curshape, pos, 1);
    294                         do {
    295                                 scr_update();
    296                                 scr_msg(key_msg, 0);
    297                                 scr_msg(msg, 1);
    298 //                              (void) fflush(stdout);
    299                         } while (rwait((struct timeval *)NULL) == -1);
    300                         scr_msg(msg, 0);
    301                         scr_msg(key_msg, 1);
    302                         place(curshape, pos, 0);
    303                         continue;
    304                 }
    305                 if (c == keys[0]) {
    306                         /* move left */
    307                         if (fits_in(curshape, pos - 1))
    308                                 pos--;
    309                         continue;
    310                 }
    311                 if (c == keys[1]) {
    312                         /* turn */
    313                         const struct shape *new = &shapes[
    314                             classic? curshape->rotc : curshape->rot];
    315 
    316                         if (fits_in(new, pos))
    317                                 curshape = new;
    318                         continue;
    319                 }
    320                 if (c == keys[2]) {
    321                         /* move right */
    322                         if (fits_in(curshape, pos + 1))
    323                                 pos++;
    324                         continue;
    325                 }
    326                 if (c == keys[3]) {
    327                         /* move to bottom */
    328                         while (fits_in(curshape, pos + B_COLS)) {
    329                                 pos += B_COLS;
    330                                 score++;
    331                         }
    332                         continue;
    333                 }
    334                 if (c == '\f') {
    335                         scr_clear();
    336                         scr_msg(key_msg, 1);
    337                 }
    338         }
    339 
    340         scr_clear();
    341         scr_end();
    342 
    343         if (showpreview == 0)
    344                 (void)printf("Your score:  %d point%s  x  level %d  =  %d\n",
    345                     score, score == 1 ? "" : "s", level, score * level);
    346         else {
    347 /*              (void)printf("Your score:  %d point%s x level %d x preview penalty %0.3f = %d\n", */
    348 /*                  score, score == 1 ? "" : "s", level, (double)PRE_PENALTY, */
    349 /*                  (int)(score * level * PRE_PENALTY)); */
    350 /*              score = score * PRE_PENALTY; */
    351         }
    352         savescore(level);
    353 
    354         showscores(level);
    355        
    356         printf("\nHit 's' to new game, 'q' to quit.\n");
    357 
    358        
    359         while (i = getchar()) {
    360                 if (i == 's')
    361                         goto newgame;
    362                 if (i == 'q')
    363                         break;
     432               
     433                scr_clear();
     434                insertscore(score, level);
    364435        }
    365436       
     
    371442                        break
    372443*/
     444        scr_end();
    373445        exit(0);
    374446}
Note: See TracChangeset for help on using the changeset viewer.