fromkandinskyimportdraw_stringastxt,fill_rectasrec,get_pixelfromionimportkeydownfromrandomimportrandintfrommathimportsqrt,logfromtimeimportmonotonic,sleep# Tetris 2.0-alpha4 NumWorks, 25.12.2024
# Par Kevin F., Ilyas R., & Vincent ROBERT
# https://nsi.xyz/tetris <3
dark_c=(42,)*3light_c=(142,)*3bright_c=(242,)*3game_c=(148,113,222)void_c=(255,)*3color=((128,192,224),(224,128,192),(192,224,128),(192,192,192),(224,192,128),(192,128,224),(128,224,192))formes=([(1,1,2,1)],[(1,2,1),(0,0,1)],[(0,2,1),(1,1,0)],[(2,2),(2,2)],[(1,2,1),(0,1,0)],[(1,2,1),(1,0,0)],[(1,2,0),(0,1,1)])os=(148,113,222)scoreboard=[[None],[None],["Level",1],["Lines",0],["Score",0]]game_config=(["Level",1],["Vision",2],["Grid",0])config_limits=((1,9),(0,3),(0,1))state="IN_MENU"ks=0defreset():globalscoreboardrec(0,0,320,222,void_c)scoreboard=[[None],[None],["Level",1],["Lines",0],["Score",0]]defgui(e=0):rec(74,0,2,222,light_c)rec(246,0,2,222,light_c)rec(0,200,320,22,game_c)txt("Developed by nsi.xyz/tetris",25,202,bright_c,game_c)foriinrange(len(game_config)):txt(game_config[i][0],37-5*len(game_config[i][0]),200//(len(game_config)+1)*(i+1)-18,light_c)rec((74-18)//2,200//(len(game_config)+1)*(i+1),18,18,light_c)txt(str(game_config[i][1]),(74-18)//2+4,200//(len(game_config)+1)*(i+1),bright_c,light_c)ifnote:config_displayer(0,1)arrows_displayer(0,1*(e!=0))defgrid(e):foriinrange(13):rec(76+i*14,0,2,200,bright_c*(e==1)+void_c*(e==0))defconfig_displayer(conf,stt):c=game_cifsttelselight_ctxt(game_config[conf][0],37-5*len(game_config[conf][0]),200//(len(game_config)+1)*(conf+1)-18,c)rec((74-18)//2,200//(len(game_config)+1)*(conf+1),18,18,c)txt(str(game_config[conf][1]),(74-18)//2+4,200//(len(game_config)+1)*(conf+1),bright_c,c)defconfig_updater(conf,incr,f=0):globalgame_configc=light_ciffelsegame_cgame_config[conf][1]+=incrtxt(str(game_config[conf][1]),(74-18)//2+4,200//(len(game_config)+1)*(conf+1),bright_c,c)arrows_displayer(conf)ifconf==2:grid(game_config[2][1])elifconf==0:scoreboard[2][1]=game_config[0][1]scoreboard_displayer()defarrows_displayer(conf,leave=0):ifleave:foriinrange(len(game_config)):forjinrange(2):rec((74-18)//2+(25ifjelse-17),200//(len(game_config)+1)*(i+1)+3,10,10,void_c)else:ifgame_config[conf][1]>config_limits[conf][0]:txt("<",(74-18)//2-17,200//(len(game_config)+1)*(conf+1),game_c)else:txt("<",(74-18)//2-17,200//(len(game_config)+1)*(conf+1),void_c)ifgame_config[conf][1]<config_limits[conf][1]:txt(">",(74-18)//2+25,200//(len(game_config)+1)*(conf+1),game_c)else:txt(">",(74-18)//2+25,200//(len(game_config)+1)*(conf+1),void_c)defarrow(x,y,d=0,c=game_c):foriinrange(6):rec(x+(23+i)*(d==3)-(5+i)*(d==0)+(3+i)*(d%3!=0),y+(3+i)*(d%3==0)-(7+i)*(d==1)+(22+i)*(d==2),1,2,c)rec(x+(23+i)*(d==3)-(5+i)*(d==0)+(13-i)*(d%3!=0),y+(13-i)*(d%3==0)-(7+i)*(d==1)+(22+i)*(d==2),1,2,c)defconfig_manager(conf,i):config_displayer(conf,0)arrows_displayer(conf,1)conf+=iconfig_displayer(conf,1)arrows_displayer(conf)defmenu():globalgame,ksconfig=0last_key=Nonewhilenot(keydown(4)orkeydown(52))ornotks:ifkeydown(1)andconfig>0andlast_key!=1:config_manager(config,-1)config-=1last_key=1ifkeydown(2)andconfig<len(game_config)-1andlast_key!=2:config_manager(config,1)config+=1last_key=2ifkeydown(0)andgame_config[config][1]>config_limits[config][0]andlast_key!=0:config_updater(config,-1)last_key=0ifkeydown(3)andgame_config[config][1]<config_limits[config][1]andlast_key!=3:config_updater(config,1)last_key=3ifnot(keydown(0)orkeydown(1)orkeydown(2)orkeydown(3)orkeydown(4)orkeydown(52)):last_key,ks=None,1gui(1)defscoreboard_displayer():rec(255,105,49,30,void_c)foriinrange(2,5):txt(scoreboard[i][0],280-5*len(scoreboard[i][0]),200//(len(scoreboard)+1)*(i+1)-18,dark_c)rec(250,200//(len(scoreboard)+1)*(i+1),60,18,void_c)txt(str(scoreboard[i][1]),280-5*len(str(scoreboard[i][1])),200//(len(scoreboard)+1)*(i+1),light_c)defscore(li=0,i=0):scoreboard[3][1]+=liscoreboard[4][1]+=scoreboard[2][1]*(25*li+25*2**max(li-1,0))*(li!=0)+scoreboard[2][1]*iscoreboard[2][1]=max(round(sqrt(scoreboard[4][1]+(10*game_config[0][1])**2)/10),game_config[0][1])scoreboard_displayer()defrlen(l):returnrange(len(l))defancr(piece):foriinrlen(piece[1]):forjinrlen(piece[1][i]):ifpiece[1][i][j]==2:returnpiece[0][0]-14*j,piece[0][1]-14*idefdraw_tetro(piece,clr=-1):ifclr==-1:clr=piece[2]x,y=ancr(piece)foriinrlen(piece[1]):forjinrlen(piece[1][i]):ifpiece[1][i][j]:rec(x+14*j+1,y+14*i+1,12,12,clr)deftourner(piece,s,t=0):ifnott:draw_tetro(piece,(255,255,255))ifs=="h":piece[1]=list(zip(*piece[1][::-1]))ifs=="a":piece[1]=list(zip(*(list(i)[::-1]foriinpiece[1])))ifnott:draw_tetro(piece)defmove(piece,x=0,y=0):draw_tetro(piece,(248,252,248))piece[0][0]+=x*14piece[0][1]+=y*14draw_tetro(piece)deftest_rot(piece,s):x,y=ancr(piece)blocs=[]foriinrlen(piece[1]):forjinrlen(piece[1][i]):ifpiece[1][i][j]:blocs+=[[x+14*j,y+14*i]]tourner(piece,s,1)x,y=ancr(piece)foriinrlen(piece[1]):forjinrlen(piece[1][i]):ifnot[j*14+x,i*14+y]inblocs:ifnot(77<=x+14*j<=231)ornot(11<=y+14*i<=207)orget_pixel(x+7+14*j,y+7+14*i)!=(255,255,255):tourner(piece,"a"*(s=="h")+"h"*(s=="a"),1)return1tourner(piece,"a"*(s=="h")+"h"*(s=="a"),1)return0defcollision(d,piece):x,y=ancr(piece)foriinrlen(piece[1]):forjinrlen(piece[1][i]):ifpiece[1][i][j]:ifdin(0,3)and((0<=j+(d==3)-(d==0)<=len(piece[1][i])-1andnotpiece[1][i][j+(d==3)-(d==0)])or(j,d)in((0,0),(len(piece[1][i])-1,3))):ifget_pixel(x+7+14*(j+(d==3)-(d==0)),y+7+14*i)!=(255,255,255)orx+((j+1)*14)*(d!=0)in[77,245]:return1ifd==2and((i!=len(piece[1])-1andnotpiece[1][i+1][j])or(i==len(piece[1])-1andpiece[1][i][j])):ifget_pixel(x+7+14*j,y+7+14*(i+1))!=(255,255,255)ory+i*14==207:return1return0defengine():globalstateifgame_config[1][1]:tetrominos=[]foriinrange(game_config[1][1]):chx=randint(0,6)tetrominos+=[[[280,2+i*32],formes[chx],color[chx]]]whileget_pixel(154,10)==(255,255,255):chx=randint(0,6)ifgame_config[1][1]:fortetintetrominos:draw_tetro(tet,(255,255,255))tetromino=[[147,3],tetrominos[0][1],tetrominos[0][2]]tetrominos=[tetrominos[i+1]foriinrange(game_config[1][1]-1)]+[[[0,0],formes[chx],color[chx]]]foriinrange(game_config[1][1]):tetrominos[i][0]=[280,2+i*32]fortetintetrominos:draw_tetro(tet)else:tetromino=[[147,3],formes[chx],color[chx]]draw_tetro(tetromino)chute=0.120while1:depart=monotonic()whilemonotonic()<depart+round(-0.1945871*log(scoreboard[2][1])+1.01122,3):ifkeydown(0)andnotcollision(0,tetromino):move(tetromino,x=-1)sleep(0.120)ifkeydown(3)andnotcollision(3,tetromino):move(tetromino,x=1)sleep(0.120)ifkeydown(2):sleep(chute)chute/=2score(i=1)breakchute=0.120ifkeydown(16)andnottest_rot(tetromino,"h"):sleep(0.160)tourner(tetromino,"h")ifkeydown(17)andnottest_rot(tetromino,"a"):sleep(0.160)tourner(tetromino,"a")ifcollision(2,tetromino):breakscore(i=1)move(tetromino,y=1)lignes=[]foriinrange(192,4,-14):verif=0forjinrange(84,252,14):verif+=(get_pixel(j,i)!=(255,255,255))+2*(get_pixel(j,i)==(255,255,255))ifverif==12:lignes+=[i-7]ifverif==24:stop=i-7breakiflignes:score(li=len(lignes))forcinrange(7):foriinlignes:forjinrange(77,245,14):rec(j+1,i+1,12,12,notc%2and(255,255,255)oros)sleep(0.2*round(-0.1945871*log(scoreboard[2][1])+1.01122,3))lignes=[lignes[i]+i*14foriinrlen(lignes)]foriinlignes:foryinrange(i,stop-14,-14):forxinrange(77,245,14):rec(x+1,y+1,12,12,get_pixel(x+7,y-7))rec(120,100,80,42,os)txt("LOOSE",135,102,(255,255,255),os)txt(str(scoreboard[4][1]),160-5*len(str(scoreboard[4][1])),122,(255,255,255),os)state="END"#get_key()
#gl = [1, 0, 0]
#rec(77, 11, 168, 210, (248, 252, 248))
#rec(248, 0, 72, 222, (248, 252, 248))
defstart():globalstatestate="IN_GAME"gui()scoreboard_displayer()menu()engine()deftetris():globalkswhilenotkeydown(51):ifstate=="IN_MENU":start()ifstate=="END"and(keydown(4)orkeydown(52))andnotks:reset()start()ifnot(keydown(4)orkeydown(52)):ks=0tetris()
During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:
Ensure the proper functioning of the site (essential cookies); and
Track your browsing to send you personalized communications if you have created a professional account on the site and can be contacted (audience measurement cookies).
With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.