diff --git a/module/libkey.nas b/module/libkey.nas index ab0c769..48ba8df 100644 --- a/module/libkey.nas +++ b/module/libkey.nas @@ -7,30 +7,40 @@ var libkey=func(){ var nb=dylib.dlsym(lib,"nas_noblock"); var init=dylib.dlsym(lib,"nas_init"); var cls=dylib.dlsym(lib,"nas_close"); + var call=dylib.dlcall; var is_init=0; return { init:func(){ - dylib.dlcall(init); + # change io mode to no echo + call(init); is_init=1; }, kbhit:func(){ + # check if kerboard is hit + # if keyboard is hit this function will return 1 + # until getch() gets all the input characters + # and the input flow becomes empty if(!is_init) me.init(); - return dylib.dlcall(kb); + return call(kb); }, getch:func(){ + # get input one character without echo + # block until get one input if(!is_init) me.init(); - return dylib.dlcall(gt); + return call(gt); }, nonblock:func(){ + # nonblock input without echo if(!is_init) me.init(); - return dylib.dlcall(nb); + return call(nb); }, close:func(){ # must call this function before exiting the program - dylib.dlcall(cls); + # this will change terminal mode to normal io mode + call(cls); dylib.dlclose(lib); } } diff --git a/test/tetris.nas b/test/tetris.nas index 96b14a8..1578cb4 100644 --- a/test/tetris.nas +++ b/test/tetris.nas @@ -1,58 +1,176 @@ import("lib.nas"); import("module/libkey.nas"); -var brick=[ - [ - [1,1], - [1,0], - [1,0] - ], - [ - [0,1], - [1,1], - [1,0] - ], - [ - [1,1], - [0,1], - [0,1], - ], - [ - [1,0], - [1,1], - [0,1] - ], - [ - [1,1,1,1] - ], - [ - [1,1], - [1,1] - ], - [ - [0,1,0], - [1,1,1] - ] -]; +var mapgen=func(mapx,mapy){ + if(mapx<1 or mapy<1) + die("map_x or map_y must be greater than 1"); + # use in print + var table="\e[44m "; + for(var i=0;i0.9); + append(map,tmp); + } -func(){ + # color print + var front=[ + "31","32","33","34","35","36", + "91","92","93","94","95","96", + ]; + var map_print=func(){ + var s="\e[1;1H"~table; + for(var y=0;y=1;x-=1) + map[y][x]=map[y][x-1]; + map[y][0]=tmp; + } + } + + var rotate=func(block){ + + } + + var setblock=func(block){ + + } + + var checkmap=func(block){ + map_print(map); + } + return { + print:map_print, + moveleft:moveleft, + moveright:moveright, + rotate:rotate, + setblock:setblock, + checkmap:checkmap + }; +} + +var blocktype=[ + [0,1,2,3], + [4,5,6,7], + [8,9,10,11], + [12,13], + [14], + [15,16], + [17,18] +]; +var blockshape=[ + # [][] [] [][][] + # [] [] [] [] + # [] [][][] [][] + [[0,0],[1,0],[0,1],[0,2]], + [[0,0],[0,1],[1,1],[2,1]], + [[0,0],[0,1],[0,2],[-1,2]], + [[0,0],[1,0],[2,0],[2,1]], + # [][] [][][] [] + # [] [] [] [] + # [] [][] [][][] + [[0,0],[1,0],[1,1],[1,2]], + [[0,0],[],[],[]], + [[0,0],[],[],[]], + [[0,0],[],[],[]], + # [] [] [][][] [] + # [][][] [][] [] [][] + # [] [] + [[0,0],[],[],[]], + [[0,0],[],[],[]], + [[0,0],[],[],[]], + [[0,0],[],[],[]], + # [] [][][][] + # [] + # [] + # [] + [[0,0],[],[],[]], + [[0,0],[],[],[]], + # [][] + # [][] + [[0,0],[],[],[]], + # [] [][] + # [][] [][] + # [] + [[0,0],[],[],[]], + [[0,0],[],[],[]], + # [] [][] + # [][] [][] + # [] + [[0,0],[],[],[]], + [[0,0],[],[],[]] +]; +var block={ + x:0, + y:0, + type:nil, + shape:nil, + new:func(x=0,y=0){ + (me.x,me.y)=(x,y); + me.type=blocktype[rand()*size(blocktype)]; + me.shape=blockshape[me.type][0]; + return {parents:[block]}; + } +}; + +var main=func(){ + print("\ec"); + + rand(time(0)); + var map=mapgen(mapx:15,mapy:15); + var blk=block.new(); + libkey.init(); - print('\ec'); - var cnt=0; while(1){ var ch=libkey.nonblock(); - ch=ch?chr(ch)~chr(ch):"[]"; - var s="\ec+--------------+\n"; - var normal="| |\n"; - var character="| "~ch~" |\n"; - for(var i=0;i<6;i+=1) - s~=(i==cnt)?character:normal; - s~="+--------------+\n"; - print(s); - unix.sleep(0.3); - cnt+=1; - if(cnt==6) - cnt=0; + if(ch){ + if(ch=='a'[0]) # move left + map.moveleft(blk); + elsif(ch=='d'[0]) # move right + map.moveright(blk); + elsif(ch=='s'[0]) # rotate + map.rotate(blk); + elsif(ch==' '[0]) # set block + map.setblock(blk); + elsif(ch=='q'[0]) + break; + map.checkmap(blk); + unix.sleep(0.03); + } + else{ + blk.y+=1; + map.checkmap(blk); + unix.sleep(0.3); + } } libkey.close(); -}(); \ No newline at end of file + + print("\ec\e[91ms\e[92me\e[93me \e[94my\e[95mo\e[96mu \e[34m~\e[0m\n"); +}; + +main();