A 2048 game. You can change the grid size.
you can custom the colors by changing the rgb values in the colors list, you can change the gradient of color used for the color of the squares of the number by replacing the second argument’s rgb values when creating the gradient variable.
How to play:
Enter a number between 4 and 8 in the console when asked, it’s the gride size.
Arrows to play.
from kandinsky import fill_rect as f, draw_string as d from ion import * from time import * from random import * def rgb(v): return tuple([GRADIENT_COLORS[int(v/N)][x]+(GRADIENT_COLORS[int(v/N)+1][x]-GRADIENT_COLORS[int(v/N)][x])*(v%N)/N for x in range(3)]) def number(num,x,y,size,col,sp=1): a=[1+size[1]//12,1+size[0]//(len(str(num))*12)] a=a[a[0]>a[1]] b=[(size[1]-3*a)//2,(size[0]+sp)//len(str(num))-sp-2*a] b=b[b[0]>b[1]] if b>1 and num>=0: num=[int(a) for a in str(num)] l=[[1,1,1,0,1,1,1],[1,0,0,0,1,0,0],[0,1,1,1,1,1,0],[0,1,1,1,0,1,1],[1,0,1,1,0,0,1],[1,1,0,1,0,1,1],[1,1,0,1,1,1,1],[0,1,1,0,0,0,1],[1,1,1,1,1,1,1],[1,1,1,1,0,0,1]] coo=[[x,y+a,a,b],[x+a,y,b,a],[x+a+b,y+a,a,b],[x+a,y+a+b,b,a],[x,y+2*a+b,a,b],[x+a,y+2*a+2*b,b,a],[x+a+b,y+2*a+b,a,b]] for n,k in enumerate(num): for x in range(7): if l[k][x]: f(coo[x][0]+n*(b+2*a+sp),coo[x][1],coo[x][2],coo[x][3],col) def check_move(): global run mov=[0 for _ in range(4)] l=((-1,0),(0,-1),(1,0),(0,1)) for x in range(grid_size): for y in range(grid_size): if grid[x][y]: for a in range(4): if grid_size>x+l[a][0]>-1 and grid_size>y+l[a][1]>-1: if grid[x+l[a][0]][y+l[a][1]]==grid[x][y] or not grid[x+l[a][0]][y+l[a][1]]: mov[a]=1 return mov def place_number(a): global grid while a: rand_coo=[randrange(0,grid_size),randrange(0,grid_size)] if not grid[rand_coo[0]][rand_coo[1]]: a-=1 grid[rand_coo[0]][rand_coo[1]]=1+randint(1,4)//4 BACKGROUND=(125,125,125) GRID=(50,50,50) NUMBER=(220,170,80) GRADIENT_COLORS=(BACKGROUND,(0,147,255),(50,50,50)) N=1/(len(GRADIENT_COLORS)-1) key_pressing={KEY_LEFT:0,KEY_UP:0,KEY_RIGHT:0,KEY_DOWN:0} index={KEY_LEFT:0,KEY_UP:1,KEY_RIGHT:2,KEY_DOWN:3} grid_size=int(input("enter grid size \n(between 4 and 8): ")) if not 9>grid_size>3: grid_size=4 size=222//grid_size grid=[[0 for _ in range(grid_size)]for _ in range(grid_size)] place_number(1) f(0,0,320,222,BACKGROUND) f(0,0,size*grid_size+1,size*grid_size+1,GRID) for x in range(grid_size): for y in range(grid_size): f(x*size+1,y*size+1,size-1,size-1,BACKGROUND) change=1 run=1 while run: key_pressed={k:0 for k in key_pressing} for k in key_pressing: if keydown(k): if not key_pressing[k]: key_pressed[k]=1 key_pressing[k]=1 else: key_pressing[k]=0 for k,n in key_pressed.items(): if n and mov[index[k]]: change=1 if k==KEY_LEFT: for y in range(grid_size): stop=0 for x in range(grid_size): if grid[x][y]!=0: for x1 in range(x,0,-1): if not grid[x1-1][y]: grid[x1-1][y]=grid[x1][y] grid[x1][y]=0 elif grid[x1-1][y]==grid[x1][y] and x1>stop: grid[x1-1][y]+=1 grid[x1][y]=0 stop=x1 else: break elif k==KEY_RIGHT: for y in range(grid_size): stop=grid_size for x in range(grid_size-1,-1,-1): if grid[x][y]!=0: for x1 in range(x,grid_size-1): if not grid[x1+1][y]: grid[x1+1][y]=grid[x1][y] grid[x1][y]=0 elif grid[x1+1][y]==grid[x1][y] and x1<stop: grid[x1+1][y]+=1 grid[x1][y]=0 stop=x1 else: break elif k==KEY_DOWN: for x in range(grid_size): stop=grid_size for y in range(grid_size-1,-1,-1): if grid[x][y]!=0: for y1 in range(y,grid_size-1): if not grid[x][y1+1]: grid[x][y1+1]=grid[x][y1] grid[x][y1]=0 elif grid[x][y1+1]==grid[x][y1] and y1<stop: grid[x][y1+1]+=1 grid[x][y1]=0 stop=y1 else: break elif k==KEY_UP: for x in range(grid_size): stop=0 for y in range(grid_size): if grid[x][y]!=0: for y1 in range(y,0,-1): if not grid[x][y1-1]: grid[x][y1-1]=grid[x][y1] grid[x][y1]=0 elif grid[x][y1-1]==grid[x][y1] and y1>stop: grid[x][y1-1]+=1 grid[x][y1]=0 stop=y1 else: break if change: if all([all(grid[x]) for x in range(grid_size)]): run= 0 else: place_number(1) mov=check_move() for x in range(grid_size): for y in range(grid_size): f(x*size+1,y*size+1,size-1,size-1,rgb(1-1.1**(-grid[x][y]))) for x in range(len(grid)): for y in range(len(grid[0])): if grid[x][y]: number(2**grid[x][y],x*size+2,y*size+2,[size-2,size-2],NUMBER) if all([k==0 for k in mov]): run=0 d("you lost",222,0,NUMBER,BACKGROUND) change=0