int main() { Piece *one = buildpiece(0,0,0,1,1,0,1,1); Piece *two = buildpiece(0,0,0,1,0,2,1,1); initscr(); drawpiece(0,0,one); drawpiece(0,3,two); refresh(); getch(); endwin(); return 0; }
void rleft(void) { if(canfit(rotl(piece))) { setpiece(rotl(piece)); drawpiece(); flushimage(display, 1); } }
void mleft(void) { if(!collide(Pt(pos.x-pcsz, pos.y), piece)) if(!collide(Pt(pos.x-pcsz, pos.y+pcsz-DY), piece)) { undrawpiece(); pos.x -= pcsz; drawpiece(); flushimage(display, 1); } }
void suspend(int s) { suspended = s; if (suspended) setcursor(mousectl, &whitearrow); else setcursor(mousectl, nil); if (!suspended) drawpiece(); drawboard(); flushimage(display, 1); }
void choosepiece(void) { int i; do { i = nrand(NP); setpiece(&pieces[i]); pos = rboard.min; pos.x += nrand(NX)*pcsz; } while(collide(Pt(pos.x, pos.y+pcsz-DY), piece)); drawpiece(); flushimage(display, 1); }
int play () { BRICKTYPE brick, shadow; int level, next, lines, in, c, d, shadcol, nextop, shadop; double delay; clock_t start, check; level = 1; nextop = 1; shadop = 0; lines = 0; for (c = 0; c < HEIGHT + 1; c++) for (d = 0; d < col_width + 2; d++) buf[c][d] = (c < HEIGHT && d > 0 && d < col_width + 1) ? 0 : 1; brick.piece = rand () % PIECES; brick.x = col_width / 2; brick.y = 0; do { /* Next piece */ delay = 1.0 - level * .05; if (delay < .10) delay = 0.10 - (level - 20) * .01; if (delay < .06) delay = 0.6; if (shadop) brick.y = -2; next = rand () % PIECES; if (brick.piece % (COLORS / 2) == level % (COLORS / 2)) shadcol = (brick.piece + 1) % (COLORS / 2); else shadcol = brick.piece % (COLORS / 2); drawplay (level); mvwprintw (iwin, 1, 2, "Level : %d", level); mvwprintw (iwin, 2, 2, "Lines : %d", lines); wrefresh (iwin); start = clock (); shadow.piece = brick.piece; do { shadow.x = brick.x; shadow.y = brick.y; shadow.rot = brick.rot; while (!clip(&shadow)) shadow.y++; shadow.y--; if (nextop) drawpiece (nwin, 1, 2, brick.rot, next, BCOL(next)); if (shadop) drawpiece (pwin, shadow.y, shadow.x, shadow.rot, shadow.piece, shadcol); drawpiece (pwin, brick.y, brick.x, brick.rot, brick.piece, BCOL(brick.piece)); wrefresh (pwin); wrefresh (nwin); do { in = getch (); check = clock (); } while ((in == ERR)&&((double)(check - start) / CLOCKS_PER_SEC < delay)); if (nextop) drawpiece (nwin, 1, 2, brick.rot, next, (level + 1) % (COLORS / 2)); if (shadop) drawpiece (pwin, shadow.y, shadow.x, shadow.rot, shadow.piece, level % (COLORS / 2)); drawpiece (pwin, brick.y, brick.x, brick.rot, brick.piece, level % (COLORS / 2)); if ((double)(check - start) / CLOCKS_PER_SEC > delay) { brick.y++; start = clock (); } else switch (in) { case KEY_RIGHT : brick.x++; if (clip(&brick)) brick.x--; else start = clock (); break; case KEY_LEFT : brick.x--; if (clip(&brick)) brick.x++; else start = clock (); break; case KEY_UP : brick.rot++; brick.rot %= 4; if (clip(&brick)) { brick.x--; if (clip (&brick)) { brick.x += 2; if (clip (&brick)) { brick.x--; brick.rot += 3; brick.rot %= 4; } else start = clock (); } else start = clock (); } else start = clock (); break; case KEY_DOWN : brick.y++; start = clock (); break; case ' ' : while (!clip(&brick)) brick.y++; brick.y--; start = clock (); break; case 'n' : case 'N' : nextop = !nextop; break; case 's' : case 'S' : shadop = !shadop; break; case 'p' : case 'P' : wattrset (pwin, COLOR_PAIR(level % (COLORS / 2))); wfillrect (pwin, 0, 1, HEIGHT, col_width * 2); drawmsg (pwin, HEIGHT / 2, 2, 4, pausemsg); wrefresh (pwin); while (getch() == ERR) {} drawplay (level); wrefresh (pwin); break; case 'q' : case 'Q' : return (lines); } } while (!clip (&brick)); /* end of brickfall */ brick.y--; if (brick.y < 0) return (lines); for (d = 0; d < 4; d++) for (c = 0; c < 4; c++) /* commit piece to buffer */ if (blocks[rx(brick.rot % rots[brick.piece], d, c)][brick.piece][ry(brick.rot % rots[brick.piece], d, c)] != '.') buf[d + brick.y][c + brick.x - 1] = BCOL(brick.piece); drawplay (level); lines += clearedlines (level); level = lines / 10 + 1; brick.piece = next; brick.x = col_width / 2; brick.y = 0; next = rand () % PIECES; } while (!clip (&brick)); /* end of game */ drawpiece (pwin, brick.y, brick.x, brick.rot, brick.piece, BCOL(brick.piece)); wrefresh (pwin); return (lines); }