From 9456a903d7358ead65ca29196147afc6e7622c9e Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Mon, 21 Feb 2022 17:10:13 +0800 Subject: [PATCH] finish tetris.nas --- README.md | 2 +- module/keyboard.cpp | 2 +- module/makefile | 4 +- test/tetris.nas | 150 +++++++++++++++++++++++++++++++------------- 4 files changed, 111 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index 323c689..cbb0ae6 100644 --- a/README.md +++ b/README.md @@ -108,7 +108,7 @@ __CAUTION__: If want to use the release zip/tar.gz file to build the interpreter Also remember to use g++ or clang++.(`mingw-w64` in __`Windows`__) -> [cpp compiler] -std=c++11 -O3 main.cpp -o nasal.exe -fno-exceptions +> [cpp compiler] -std=c++11 -O3 main.cpp -o nasal.exe -fno-exceptions -static Or use this in __`linux/macOS/Unix`__ diff --git a/module/keyboard.cpp b/module/keyboard.cpp index 93242f4..9afb8c9 100644 --- a/module/keyboard.cpp +++ b/module/keyboard.cpp @@ -4,7 +4,7 @@ #include #else #include -#include +#include #endif #ifndef _WIN32 diff --git a/module/makefile b/module/makefile index a175957..426df28 100644 --- a/module/makefile +++ b/module/makefile @@ -9,7 +9,7 @@ libkey.so: keyboard.cpp clang++ -c -O3 keyboard.cpp -fPIC -o keyboard.o clang++ -shared -o libkey.so keyboard.o libkey.dll: keyboard.cpp - g++ -c -O3 keyboard.cpp -fPIC -o keyboard.o - g++ -shared -o libkey.dll keyboard.o + g++ -c -O3 keyboard.cpp -fPIC -o keyboard.o -static + g++ -shared -o libkey.dll keyboard.o -static clean: rm *.o *.so *.dll *.dylib \ No newline at end of file diff --git a/test/tetris.nas b/test/tetris.nas index 6137ddf..b030590 100644 --- a/test/tetris.nas +++ b/test/tetris.nas @@ -1,14 +1,18 @@ import("lib.nas"); -import("module/libkey.nas"); +import("./module/libkey.nas"); +var color=[ + "\e[31m","\e[32m","\e[33m","\e[34m","\e[35m","\e[36m", + "\e[91m","\e[92m","\e[93m","\e[94m","\e[95m","\e[96m", +]; var blocktype=[ - [0,1,2,3], - [4,5,6,7], + [0,1,2,3 ], + [4,5,6,7 ], [8,9,10,11], - [12,13], - [14], - [15,16], - [17,18] + [12,13 ], + [14 ], + [15,16 ], + [17,18 ] ]; var blockshape=[ # [][] [] [][][] @@ -52,32 +56,45 @@ var blockshape=[ [[0,0],[0,1],[1,1],[1,2]], [[0,0],[1,0],[0,1],[-1,1]] ]; + +var stick_count=0; # make sure one stick in 10 tries var block={ x:0, y:0, rotate:0, type:nil, shape:nil, + color:nil, new:func(x=0,y=0){ (me.x,me.y)=(x,y); me.rotate=0; - me.type=blocktype[rand()*size(blocktype)]; + var t=int(rand()*size(blocktype)); + if(t!=3){ + stick_count+=1; + if(stick_count==10) + (t,stick_count)=(3,0); + }else + stick_count=0; + me.type=blocktype[t]; me.shape=blockshape[me.type[me.rotate]]; + me.color=int(rand()*size(color)); return {parents:[block]}; } }; var mapgen=func(mapx,mapy){ - var score=0; + var (score,gameover)=(0,0); var (empty,unset,full)=(0,1,2); if(mapx<1 or mapy<1) die("map_x or map_y must be greater than 1"); + # use in print - var table="\e[44m "; + var table="\e[32m+"; for(var i=0;i=full){ + gameover=1; + return; + } + # update map foreach(var i;blk.shape) map[blk.y+i[1]][blk.x+i[0]]=unset; } - new_block(); + new_block(); # initialize the first block # color print - var front=[ - "31","32","33","34","35","36", - "91","92","93","94","95","96", - ]; var map_print=func(){ - print("\e[1;1Hscore: ",score,"\n"); - var s=table; + var s="\e[1;1H"~table; for(var y=0;y=full) + s~=color[c-full]~"██\e[0m"; } - s~="\e[44m \e[0m\n"; + s~="\e[32m|\e[0m\n"; } s~=table; - print(s); + print(s," score: ",score,'\n'); } var moveleft=func(){ @@ -120,9 +145,10 @@ var mapgen=func(mapx,mapy){ foreach(var i;blk.shape){ if(x+i[0]<0) return; - if(map[y+i[1]][x+i[0]]==full) + if(map[y+i[1]][x+i[0]]>=full) return; } + # update block state and map foreach(var i;blk.shape) map[blk.y+i[1]][blk.x+i[0]]=empty; blk.x=x; @@ -136,9 +162,10 @@ var mapgen=func(mapx,mapy){ foreach(var i;blk.shape){ if(x+i[0]>=mapx) return; - if(map[y+i[1]][x+i[0]]==full) + if(map[y+i[1]][x+i[0]]>=full) return; } + # update block state and map foreach(var i;blk.shape) map[blk.y+i[1]][blk.x+i[0]]=empty; blk.x=x; @@ -154,9 +181,11 @@ var mapgen=func(mapx,mapy){ foreach(var i;shape){ if(x+i[0]>=mapx or x+i[0]<0 or y+i[1]>=mapy or y+i[1]<0) return; - if(map[y+i[1]][x+i[0]]==full) + if(map[y+i[1]][x+i[0]]>=full) return; } + + # update block state and map foreach(var i;blk.shape) map[blk.y+i[1]][blk.x+i[0]]=empty; blk.rotate=r; @@ -168,21 +197,23 @@ var mapgen=func(mapx,mapy){ var fall=func(){ var (x,y)=(blk.x,blk.y+1); + # check if falls to the edge of other blocks or map var sethere=0; - foreach(var i;blk.shape){ - if(y+i[1]>=mapy or map[y+i[1]][x+i[0]]==full){ + foreach(var i;blk.shape) + if(y+i[1]>=mapy or map[y+i[1]][x+i[0]]>=full){ sethere=1; break; } - } + # set block here and generate a new block if(sethere){ foreach(var i;blk.shape) - map[blk.y+i[1]][blk.x+i[0]]=full; + map[blk.y+i[1]][blk.x+i[0]]=blk.color+full; checkmap(); new_block(); map_print(); return; } + # update block state and map foreach(var i;blk.shape) map[blk.y+i[1]][blk.x+i[0]]=empty; blk.y=y; @@ -193,11 +224,17 @@ var mapgen=func(mapx,mapy){ var checkmap=func(){ for(var y=mapy-1;y>=0;y-=1){ - for(var x=0;x=1;t-=1) for(var x=0;x