コード例 #1
0
ファイル: tetris.c プロジェクト: seL4/refos
int
main()
{
    /* Future Work 3:
       How the selfloader bootstraps user processes needs to be modified further. Changes were
       made to accomodate the new way that muslc expects process's stacks to be set up when
       processes start, but the one part of this that still needs to changed is how user processes
       find their system call table. Currently the selfloader sets up user processes so that
       the selfloader's system call table is used by user processes by passing the address of the
       selfloader's system call table to the user processes via the user process's environment
       variables. Ideally, user processes would use their own system call table.
    */

    uintptr_t address = strtoll(getenv("SYSTABLE"), NULL, 16);
    refos_init_selfload_child(address);
    int c, i, *ptr;
    int delay = INITIAL_DELAY_MS;
    refos_initialise();

    peekShape = NULL;

    /* Initialise board. */
    ptr = board;
    for (i = B_SIZE - 1; i>=0; i--) {
        *ptr++ = i < 25 || i % B_COLS < 2 ? A_BG_W : RESETATTR;
    }

    srand((unsigned int) RANDOM_SEED);

    clrscr();

    print_welcome_message();
    printf("      Press the space bar to continue...\n");
    show_online_help();

    while (1) {
        c = io_nonblock_getkey();
        rand();
        if (c == ' ') break;
    }
    clrscr();
    show_online_help();

    /* Main game loop. */
    shape = next_shape();
    while (!exitGame) {
        c = io_nonblock_getkey();
        rand();
        if (c >= 0) {
            game_frame(c);
        } else {
            game_frame(0);
            usleep((delay - level * LEVEL_DECREASE_DELAY_MS) * 1000);
        }
    }

    gotoxy (0, 25);
    printf("Game over! You reached %d points.\n", points);
}
コード例 #2
0
ファイル: tetris.c プロジェクト: seL4/refos
/*! @brief Update game state forward a frame. */
void
game_frame(int c)
{
    int j;
    int *backup;

    if (c == 0) {
        if (fits_in (shape, pos + B_COLS)) {
            pos += B_COLS;
        } else {
            place(shape, pos, get_col(shape));
            ++points;
            for (j = 0; j < 252; j = B_COLS * (j / B_COLS + 1)) {
                for (; board[++j];) {
                    if (j % B_COLS == 10) {
                        linesCleared++;

                        for (; j % B_COLS; board[j--] = 0);
                        update();
                        for (; --j; board[j + B_COLS] = board[j]);
                        update();
                    }
                }
            }
            shape = next_shape();
            if (!fits_in (shape, pos = 17)) c = keys[KEY_QUIT];
        }
    }

    if (c == keys[KEY_LEFT]) {
        if (!fits_in (shape, --pos)) ++pos;
    }
    if (c == keys[KEY_ROTATE]) {
        backup = shape;
        shape = &shapes[4 * *shape]; /* Rotate */
        /* Check if it fits, if not restore shape from backup. */
        if (!fits_in (shape, pos)) shape = backup;
    }

    if (c == keys[KEY_RIGHT]) {
        if (!fits_in (shape, ++pos)) --pos;
    }
    if (c == keys[KEY_DROP]) {
        for (; fits_in (shape, pos + B_COLS); ++points) pos += B_COLS;
    }
    if (c == keys[KEY_PAUSE] || c == keys[KEY_QUIT]) {
        exitGame = 1;
        return;
    }

    place(shape, pos, get_col(shape));
    update();
    place(shape, pos, RESETATTR);
}
コード例 #3
0
ファイル: tetris.cpp プロジェクト: LiftnLearn/tetris
int *next_shape (void)
{
   int *next = peek_shape;

   peek_shape = &shapes[rand () % 7 * 4];
   if (!next)
   {
      return next_shape ();
   }

   return next;
}