Back to main page
summaryrefslogtreecommitdiff
path: root/main.py
diff options
context:
space:
mode:
Diffstat (limited to 'main.py')
-rw-r--r--main.py149
1 files changed, 127 insertions, 22 deletions
diff --git a/main.py b/main.py
index 9b3f06a..49b58a9 100644
--- a/main.py
+++ b/main.py
@@ -1,50 +1,154 @@
import curses
import math
+import random
-key_left = ['h', 'a']
-key_right = ['l', 'd']
-key_down = ['j', 's']
-key_up = ['k', 'w']
+# h a
+key_left = [104, 97]
+# l d
+key_right = [108, 100]
+# j s
+key_down = [39, 115]
+# k w
+key_up = [107, 119]
maxX = 4
maxY = 4
+scaleX = 6
+scaleY = 3
+
gamestate = [[0 for mx in range(maxX)] for my in range(maxY)]
-gamestate[0][0] = 128
-gamestate[2][2] = 2048
-gamestate[1][2] = 64
-gamestate[3][2] = 2
+
+
+# GRAPHICS
def DrawSquare(x, y, val):
val_len = len(str(val))
if val_len > 4:
val_len = 4
- stdscr.addstr(y, x, "+====+")
+ 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, "+====+")
+ stdscr.addstr(y + 2, x, "+" + "-" * (scaleX - 2) + "+")
def FullDraw():
+ stdscr.clear()
+ stdscr.addstr(maxY * scaleY, maxX * scaleX, "+")
for i in range(maxY):
for j in range(maxX):
if gamestate[i][j] == 0:
continue
- DrawSquare(j * 5, i * 3, gamestate[i][j])
+ DrawSquare(j * scaleX, i * scaleY, gamestate[i][j])
+
+
+# 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():
- pass
+ for i in range(maxY):
+ PasteLeft(i, Process(ScrapeLeft(i)))
def MoveRight():
- pass
+ for i in range(maxY):
+ PasteRight(i, Process(ScrapeRight(i)))
+
+def MoveUp():
+ for i in range(maxY):
+ PasteUp(i, Process(ScrapeUp(i)))
def MoveDown():
- pass
+ for i in range(maxY):
+ PasteDown(i, Process(ScrapeDown(i)))
-def MoveUp():
- pass
+# 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
@@ -53,19 +157,20 @@ curses.noecho()
while True:
FullDraw()
userin = stdscr.getch()
- if userin == 81:
+ if userin == 81: # Q
break
if userin in key_left:
MoveLeft()
elif userin in key_right:
MoveRight()
- elif userin in key_down:
- MoveDown()
elif userin in key_up:
MoveUp()
-
-
-
+ elif userin in key_down:
+ MoveDown()
+ try:
+ SpawnBlock()
+ except:
+ break
curses.endwin()