static void starfield(void) { static int init = 0; static char sf1[22]; int x, y; if (!init) { init ^= 1; for (y=0; y<22; y++) { sf1[y] = rnd()%79 + 1; } } bold(); setfg(white); setbg(black); for (y=2; y<=23; y++) { x = sf1[y-2]; if (! (y>=7 && y<=18 && x>=20 && x<=60) ) { moveto(x, y); printf(" "); } x++; if (x>79) x = 1; if (! (y>=7 && y<=18 && x>=20 && x<=60) ) { moveto(x, y); printf("."); } sf1[y-2] = x; } normal(); flush(); }
static void printlevel(void) { setbg(blue); setfg(white); moveto(68,1); printf(" Stage: %02d ", level); moveto(68,3); printf(" Speed: %02d ", speed); flush(); }
static void stars(void) { int a; setbg(black); setfg(white); for (a=1; a<=50; a++) { moveto(rnd()%79+1,rnd()%24+1); printf("."); } }
static void splash(void) { int r; clrscr(); setbg(blue); horizontal(1,79,1); horizontal(1,79,24); setfg(white); setbg(white); rect(20,7, 60,18); setbg(red); frect(21,8, 59,17); bold(); moveto(28,9); printf("tctetris - version 0.90"); normal(); setbg(red); setfg(white); moveto(23,10); printf("Copyright (c) 2006, Ivo van Poorten"); moveto(MENUX,12); printf("n - new game"); moveto(MENUX,14); printf("s - speed [%02d]", sspeed); moveto(MENUX,16); printf("q - quit"); moveto(1,23); flush(); }
static void gamescreen(void) { clrscr(); stars(); setbg(blue); setfg(white); moveto(1,24); printf(" [j] left [k] rotate [l] right [space] drop [p] pause [n] next [q] quit "); setbg(border); frect(40-WIDTH-2,1,40+WIDTH+1,HEIGHT+1); #define VIEWX 5 #define VIEWY 8 setbg(yellow); rect(VIEWX-3,VIEWY-2,VIEWX+8,VIEWY+5); rect(VIEWX-2,VIEWY-2,VIEWX+7,VIEWY+5); setbg(bg); frect(VIEWX-1, VIEWY-1, VIEWX+6, VIEWY+4); frect(40-WIDTH,1,40+WIDTH-1,HEIGHT); moveto(1,24); flush(); }
static void game(void) { char c; int counter = sspeed, rs, a, blocks = 0, paused = 0; gamescreen(); clearboard(); nexts = rnd()%28; nextc = rnd()%7 + 1; newshape(); score = view = 0; if (view) drawview(nexts, nextc); level = 1; speed = sspeed; printscore(); printlevel(); for(;;) { if (paused) { gametick(); c = tgetchar(); switch(c) { case 'q': goto stopgame; case 'p': paused = 0; moveto(68,5); setbg(black); printf(" "); normal(); flush(); default: break; } continue; } if (counter <= 0) { counter = 14 - speed; if (shapefits(current.s, current.x, current.y+1)) { drawshape(current.s, 0, 40-WIDTH-8+current.x*2, current.y-3); current.y++; drawshape(current.s, current.color, 40-WIDTH-8+current.x*2, current.y-3); } else { markboard(current.s, current.color, current.x, current.y); checkboard(current.y); incrscore(1); newshape(); if (!shapefits(current.s, current.x, current.y)) goto stopgame; blocks++; if (blocks == blocksperlevel) { blocks = 0; incrlevel(1); } if (view) drawview(nexts, nextc); } } counter--; moveto(1,24); flush(); gametick(); c = tgetchar(); switch(c) { case 'q': goto stopgame; case 'j': if (shapefits(current.s, current.x-1, current.y)) { drawshape(current.s, 0, 40-WIDTH-8+current.x*2, current.y-3); current.x--; drawshape(current.s, current.color, 40-WIDTH-8+current.x*2, current.y-3); } break; case 'l': if (shapefits(current.s, current.x+1, current.y)) { drawshape(current.s, 0, 40-WIDTH-8+current.x*2, current.y-3); current.x++; drawshape(current.s, current.color, 40-WIDTH-8+current.x*2, current.y-3); } break; case 'k': rs = (current.s & ~0x03) | ((current.s + 1) & 0x03); if (shapefits(rs, current.x, current.y)) { drawshape(current.s, 0, 40-WIDTH-8+current.x*2, current.y-3); current.s = rs; drawshape(current.s, current.color, 40-WIDTH-8+current.x*2, current.y-3); } break; case ' ': for (a=1; ; a++) { if (!shapefits(current.s, current.x, current.y+a)) break; } if (shapefits(current.s, current.x, current.y+a-1)) { drawshape(current.s, 0, 40-WIDTH-8+current.x*2, current.y-3); current.y += a-1; drawshape(current.s, current.color, 40-WIDTH-8+current.x*2, current.y-3); } incrscore(view ? (a-1) * 3 / 4 : a-1); break; case 'n': view ^= 1; if (view) drawview(nexts, nextc); else clearview(); break; case 'p': paused = 1; moveto(68,5); setfg(white); setbg(blue); printf(" PAUSED!!! "); moveto(1,24); flush(); default: break; } } stopgame: setbg(white); rect(20,8,60,12); setbg(red); frect(21,9,59,11); setfg(white); bold(); moveto(30,10); printf(" G A M E O V E R "); moveto(1,24); flush(); for(;;) { c = tgetchar(); if (c > 0) return; } }
static void clearview(void) { normal(); setfg(black); frect(4,7,11,11); }
static void printscore(void) { setbg(blue); setfg(white); moveto(1,1); printf(" Score: %05d ", score); flush(); }
static void printsspeed(void) { normal(); setbg(red); setfg(white); moveto(MENUX+11, 14); printf("%02d", sspeed); }
void setrgb(int fg, int bg) { setfg(fg); if(bg != -2) setbg(bg); }