finish map drawing in test/tetris.nas

This commit is contained in:
ValKmjolnir 2022-02-20 17:58:13 +08:00
parent 9c055a9a23
commit 557cb2ebcf
2 changed files with 182 additions and 54 deletions

View File

@ -7,30 +7,40 @@ var libkey=func(){
var nb=dylib.dlsym(lib,"nas_noblock"); var nb=dylib.dlsym(lib,"nas_noblock");
var init=dylib.dlsym(lib,"nas_init"); var init=dylib.dlsym(lib,"nas_init");
var cls=dylib.dlsym(lib,"nas_close"); var cls=dylib.dlsym(lib,"nas_close");
var call=dylib.dlcall;
var is_init=0; var is_init=0;
return { return {
init:func(){ init:func(){
dylib.dlcall(init); # change io mode to no echo
call(init);
is_init=1; is_init=1;
}, },
kbhit:func(){ 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) if(!is_init)
me.init(); me.init();
return dylib.dlcall(kb); return call(kb);
}, },
getch:func(){ getch:func(){
# get input one character without echo
# block until get one input
if(!is_init) if(!is_init)
me.init(); me.init();
return dylib.dlcall(gt); return call(gt);
}, },
nonblock:func(){ nonblock:func(){
# nonblock input without echo
if(!is_init) if(!is_init)
me.init(); me.init();
return dylib.dlcall(nb); return call(nb);
}, },
close:func(){ close:func(){
# must call this function before exiting the program # 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); dylib.dlclose(lib);
} }
} }

View File

@ -1,58 +1,176 @@
import("lib.nas"); import("lib.nas");
import("module/libkey.nas"); import("module/libkey.nas");
var brick=[ var mapgen=func(mapx,mapy){
[ if(mapx<1 or mapy<1)
[1,1], die("map_x or map_y must be greater than 1");
[1,0], # use in print
[1,0] var table="\e[44m ";
], for(var i=0;i<mapx;i+=1)
[ table~=" ";
[0,1], table~=" \e[0m\n";
[1,1], # generate new map
[1,0] var map=[];
], for(var y=0;y<mapy;y+=1){
[ var tmp=[];
[1,1], for(var x=0;x<mapx;x+=1)
[0,1], append(tmp,rand()>0.9);
[0,1], append(map,tmp);
], }
[
[1,0],
[1,1],
[0,1]
],
[
[1,1,1,1]
],
[
[1,1],
[1,1]
],
[
[0,1,0],
[1,1,1]
]
];
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<mapy;y+=1){
s~="\e[44m \e[0m";
for(var x=0;x<mapx;x+=1)
s~=map[y][x]?"\e["~front[rand()*12]~"m██\e[0m":" ";
s~="\e[44m \e[0m\n";
}
s~=table;
print(s);
}
var moveleft=func(block){
for(var y=0;y<mapy;y+=1){
var tmp=map[y][0];
for(var x=1;x<mapx;x+=1)
map[y][x-1]=map[y][x];
map[y][mapx-1]=tmp;
}
}
var moveright=func(block){
for(var y=0;y<mapy;y+=1){
var tmp=map[y][mapx-1];
for(var x=mapx-1;x>=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(); libkey.init();
print('\ec');
var cnt=0;
while(1){ while(1){
var ch=libkey.nonblock(); var ch=libkey.nonblock();
ch=ch?chr(ch)~chr(ch):"[]"; if(ch){
var s="\ec+--------------+\n"; if(ch=='a'[0]) # move left
var normal="| |\n"; map.moveleft(blk);
var character="| "~ch~" |\n"; elsif(ch=='d'[0]) # move right
for(var i=0;i<6;i+=1) map.moveright(blk);
s~=(i==cnt)?character:normal; elsif(ch=='s'[0]) # rotate
s~="+--------------+\n"; map.rotate(blk);
print(s); elsif(ch==' '[0]) # set block
unix.sleep(0.3); map.setblock(blk);
cnt+=1; elsif(ch=='q'[0])
if(cnt==6) break;
cnt=0; map.checkmap(blk);
unix.sleep(0.03);
}
else{
blk.y+=1;
map.checkmap(blk);
unix.sleep(0.3);
}
} }
libkey.close(); libkey.close();
}();
print("\ec\e[91ms\e[92me\e[93me \e[94my\e[95mo\e[96mu \e[34m~\e[0m\n");
};
main();