diff options
author | Tom Smeding <tom.smeding@gmail.com> | 2018-02-08 19:50:36 +0100 |
---|---|---|
committer | Tom Smeding <tom.smeding@gmail.com> | 2018-02-08 19:50:42 +0100 |
commit | 533777f424242d5da36228d5e874d53d4973cfa9 (patch) | |
tree | e8ce306458c6bbb9644fd1e4a69d266cbf753182 | |
parent | 6783c591072e0e5985451dcf33fe174ce41329de (diff) |
Levels and num cleared lines
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | main.c | 51 | ||||
m--------- | termio | 0 |
3 files changed, 39 insertions, 13 deletions
@@ -1 +1,2 @@ tetris +*.swp @@ -16,9 +16,6 @@ #define USE_UNICODE -int delay_msec=700; - - const int stones[7]={ 0b0000000001110001, 0b0000000001110100, @@ -39,10 +36,18 @@ static int rotateR(int bl){ return dest; } +static int leveldelay(int level){ + int d=700-(level-1)*35; + if(d<50)d=50; + return d; +} + struct board { int lines[HEI]; int stone,stx,sty; + + int ncleared,level,delay; }; static bool boardvalid(const struct board *bd){ @@ -80,6 +85,14 @@ static void show(const struct board *bd){ memcpy(&bd2,bd,sizeof bd2); persist(&bd2); clearscreen(); + +#ifdef USE_UNICODE + assert(WID%2==0); + int boardwid=WID*3/2; +#else + int boardwid=WID; +#endif + for(int y=0;y<HEI;y++){ moveto(0,y); tputc('|'); @@ -103,15 +116,15 @@ static void show(const struct board *bd){ } tputc('|'); } + moveto(0,HEI); tputc('+'); -#ifdef USE_UNICODE - assert(WID%2==0); - for(int x=0;x<WID*3/2;x++)tputc('-'); -#else - for(int x=0;x<WID;x++)tputc('-'); -#endif + for(int x=0;x<boardwid;x++)tputc('-'); tputc('+'); + + moveto(boardwid+3,1); + tprintf("Lines: %d\n\nLevel: %d",bd->ncleared,bd->level); + redraw(); } @@ -121,6 +134,11 @@ static void checklines(struct board *bd){ memmove(bd->lines+1,bd->lines,y*sizeof(int)); bd->lines[0]=0; y++; + bd->ncleared++; + if(bd->ncleared%10==0){ + bd->level++; + bd->delay=leveldelay(bd->level); + } } } } @@ -164,9 +182,13 @@ int main(void){ initscreen(); atexit(endscreen); installCLhandler(true); + cursorvisible(false); struct board bd; memset(&bd,0,sizeof bd); + bd.ncleared=0; + bd.level=1; + bd.delay=leveldelay(bd.level); show(&bd); struct timeval timeleft; @@ -193,8 +215,8 @@ int main(void){ if(ret==0){ if(!advance(&bd))break; - timeleft.tv_sec=delay_msec/1000; - timeleft.tv_usec=delay_msec%1000*1000; + timeleft.tv_sec=bd.delay/1000; + timeleft.tv_usec=bd.delay%1000*1000; continue; } end.tv_sec-=start.tv_sec; @@ -221,11 +243,14 @@ int main(void){ if(key=='Q'||key=='q')break; else if(key=='S'||key=='s'||key==KEY_DOWN){ if(!advance(&bd))break; - timeleft.tv_sec=delay_msec/1000; - timeleft.tv_usec=delay_msec%1000*1000; + timeleft.tv_sec=bd.delay/1000; + timeleft.tv_usec=bd.delay%1000*1000; } else if(key=='A'||key=='a'||key==KEY_LEFT)move(&bd,-1); else if(key=='D'||key=='d'||key==KEY_RIGHT)move(&bd,1); else if(key=='W'||key=='w'||key==KEY_UP)rotR(&bd); else bel(); } + + endscreen(); + printf("Game Over!\nLines cleared: %d\nLevel reached: %d\n",bd.ncleared,bd.level); } diff --git a/termio b/termio -Subproject 1835b24c4b546e2eeec9c0f7a4af1604a775ad2 +Subproject c6fa2768ce7082daed428b8abe930e92adb1d5f |