import curses import math import random # h a left key_left = [104, 97, 68] # l d key_right = [108, 100, 67] # j s key_down = [106, 115, 66] # k w key_up = [107, 119, 65] maxX = 4 maxY = 4 scaleX = 6 scaleY = 3 gamestate = [[0 for mx in range(maxX)] for my in range(maxY)] # GRAPHICS def DrawSquare(x, y, val): val_len = len(str(val)) if val_len > 4: val_len = 4 stdscr.addstr(y, x, "+" + "-" * (scaleX - 2) + "+") stdscr.addstr(y + 1, x, "|" + " " * math.ceil((4-val_len)/2) + str(val) + " " * math.floor((4-val_len)/2) + "|") stdscr.addstr(y + 2, x, "+" + "-" * (scaleX - 2) + "+") def FullDraw(key): stdscr.clear() for i in range(maxY): for j in range(maxX): if gamestate[i][j] == 0: continue DrawSquare(j * scaleX, i * scaleY, gamestate[i][j]) stdscr.addstr(maxY * scaleY, maxX * scaleX, "+") # PROCESSING def Process(list_in): i = 0 while i < len(list_in) - 1: if list_in[i] == list_in[i + 1]: list_in[i] = 2 * list_in[i] del list_in[i + 1] i += 1 return list_in def ScrapeLeft(line): out_list = [] for num in gamestate[line]: if num == 0: continue out_list.append(num) return out_list def ScrapeRight(line): out_list = [] for num in gamestate[line][::-1]: if num == 0: continue out_list.append(num) return out_list def ScrapeUp(line): out_list = [] for num in range(maxY): if gamestate[num][line] == 0: continue out_list.append(gamestate[num][line]) return out_list def ScrapeDown(line): out_list = [] for num in range(maxY): if gamestate[maxY - 1 - num][line] == 0: continue out_list.append(gamestate[maxY - 1 - num][line]) return out_list def PasteLeft(line, list_in): for i in range(maxX): if i < len(list_in): gamestate[line][i] = list_in[i] else: gamestate[line][i] = 0 def PasteRight(line, list_in): for i in range(maxX): if i < len(list_in): gamestate[line][maxX - 1 - i] = list_in[i] else: gamestate[line][maxX - 1 - i] = 0 def PasteUp(line, list_in): for i in range(maxY): if i < len(list_in): gamestate[i][line] = list_in[i] else: gamestate[i][line] = 0 def PasteDown(line, list_in): for i in range(maxY): if i < len(list_in): gamestate[maxY - 1 - i][line] = list_in[i] else: gamestate[maxY - 1 - i][line] = 0 def MoveLeft(): for i in range(maxY): PasteLeft(i, Process(ScrapeLeft(i))) def MoveRight(): for i in range(maxY): PasteRight(i, Process(ScrapeRight(i))) def MoveUp(): for i in range(maxY): PasteUp(i, Process(ScrapeUp(i))) def MoveDown(): for i in range(maxY): PasteDown(i, Process(ScrapeDown(i))) # GAMEPLAY def GetEmptySpace(): zero_list = [] for y in range(maxY): for x in range(maxX): if gamestate[y][x] == 0: coord = [x, y] zero_list.append(coord) return zero_list blocks = [2, 2, 2, 4, 4, 8] def SpawnBlock(): chosen = random.choice(GetEmptySpace()) gamestate[chosen[1]][chosen[0]] = random.choice(blocks) # Init stdscr = curses.initscr() curses.noecho() userin = 0 while True: FullDraw(userin) userin = stdscr.getch() if userin == 81: # Q break if userin in key_left: MoveLeft() elif userin in key_right: MoveRight() elif userin in key_up: MoveUp() elif userin in key_down: MoveDown() else: continue try: SpawnBlock() except: break curses.endwin()