From 17a4f0656f58a171335ad5f474f566b06bc215f8 Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 19 Oct 2021 10:20:22 -0400 Subject: it's pretty now --- main.py | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 74 insertions(+), 11 deletions(-) diff --git a/main.py b/main.py index 210d96c..46d7aab 100644 --- a/main.py +++ b/main.py @@ -18,29 +18,67 @@ maxY = 4 scaleX = 6 scaleY = 3 +offsetX = 1 +offsetY = 1 + gamestate = [[0 for mx in range(maxX)] for my in range(maxY)] +# INIT + +stdscr = curses.initscr() +curses.noecho() +curses.curs_set(0) + +height, width = stdscr.getmaxyx() + +distanceX = width - (maxX * scaleX + offsetX * 2) +distanceY = height - (maxY * scaleY + offsetY * 2) + +gamewin = curses.newwin(0, 0, distanceY, distanceX) + +gamewin.mvwin(math.floor(distanceY / 2), math.floor(distanceX / 2)) + +curses.start_color() + +# COLORS +curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK) +curses.init_pair(2, curses.COLOR_YELLOW, curses.COLOR_BLACK) +curses.init_pair(3, curses.COLOR_GREEN, curses.COLOR_BLACK) +curses.init_pair(4, curses.COLOR_CYAN, curses.COLOR_BLACK) +curses.init_pair(5, curses.COLOR_BLUE, curses.COLOR_BLACK) +curses.init_pair(6, curses.COLOR_MAGENTA, curses.COLOR_BLACK) +curses.init_pair(7, curses.COLOR_WHITE, curses.COLOR_BLACK) +curses.init_pair(8, curses.COLOR_BLACK, curses.COLOR_RED) +curses.init_pair(9, curses.COLOR_BLACK, curses.COLOR_YELLOW) +curses.init_pair(10, curses.COLOR_BLACK, curses.COLOR_GREEN) +curses.init_pair(11, curses.COLOR_BLACK, curses.COLOR_CYAN) + # GRAPHICS -def DrawSquare(x, y, val): +def DrawSquare(x, y, val, box_color): 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) + "+") + gamewin.addstr(y, x, "+" + "-" * (scaleX - 2) + "+", curses.color_pair(box_color)) + gamewin.addstr(y + 1, x, "|" + " " * math.ceil((4-val_len)/2) + str(val) + " " * math.floor((4-val_len)/2) + "|", curses.color_pair(box_color)) + gamewin.addstr(y + 2, x, "+" + "-" * (scaleX - 2) + "+", curses.color_pair(box_color)) def FullDraw(key): - stdscr.clear() + gamewin.clear() + gamewin.box() 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, "+") + color = 1 + try: + color = int(math.log(gamestate[i][j]) / math.log(2)) # Might add colors to make it easier to see whats going on + except: + pass + DrawSquare(offsetX + j * scaleX, offsetY + i * scaleY, gamestate[i][j], color) # PROCESSING @@ -151,13 +189,27 @@ def SpawnBlock(): gamestate[chosen[1]][chosen[0]] = random.choice(blocks) -# Init -stdscr = curses.initscr() -curses.noecho() +# WIN AND LOSE + +def CheckWin(): + for line in gamestate: + for item in line: + if item == 2048: + return True + return False + +def End(text): + gamewin.clear() + gamewin.refresh() + gamewin.box() + gamewin.addstr(math.ceil(maxY * scaleY / 2), math.ceil((maxX * scaleX - len(text)) / 2 ) , text) + gamewin.getch() + + userin = 0 while True: FullDraw(userin) - userin = stdscr.getch() + userin = gamewin.getch() if userin == 81: # Q break if userin in key_left: @@ -170,9 +222,20 @@ while True: MoveDown() else: continue + if CheckWin(): + End("YOU WIN!") + break + + height, width = stdscr.getmaxyx() + distanceX = width - (maxX * scaleX + offsetX * 2) + distanceY = height - (maxY * scaleY + offsetY * 2) + gamewin.mvwin(math.floor(distanceY / 2), math.floor(distanceX / 2)) + stdscr.clear() + stdscr.refresh() try: SpawnBlock() except: + End("YOU LOSE!") break curses.endwin() -- cgit v1.2.3