Back to main page
summaryrefslogtreecommitdiff
path: root/main.py
blob: 447ca950e213836021584ef49689d95b8f4c5988 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import curses
import math
import random

#           h    a
key_left = [104, 97]
#            l    d
key_right = [108, 100]
#           j    s
key_down = [106, 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)]



# 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():
    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 * 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():
    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()
while True:
    FullDraw()
    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()
    
    try:
        SpawnBlock()
    except:
        break

curses.endwin()