forked from epai/tetris-terminal-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__Main__.py
331 lines (310 loc) · 13.4 KB
/
__Main__.py
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# Eric Pai
# Spring 2014
import os
import sys
import curses
#import locale
from __game__ import *
from __welcome__ import *
class Main:
### FIELDS and SETUP ###
nextPieceBoarder = \
("┌------------------┐\n",
"| Next Piece |\n",
"|==================|\n",
"| |\n",
"| |\n",
"| |\n",
"| |\n",
"| |\n",
"| |\n",
"└------------------┘\n")
########################
def __init__(self):
self.version = 0.92
### curses setup ###
self.stdscr = curses.initscr()
self.setupColors()
curses.noecho()
curses.cbreak()
self.stdscr.keypad(True)
self.stdscr.nodelay(True)
self.stdscr.clear()
curses.curs_set(0)
### Field Variables ###
# -- initialized in self.doRestart() -- #
self.doRestart()
### other ###
self.rows, self.columns = \
[int(x) for x in os.popen('stty size', 'r').read().split()]
def setupColors(self):
curses.start_color()
self.has_colors = curses.has_colors()
if self.has_colors:
colors = [
curses.COLOR_RED,
curses.COLOR_YELLOW,
curses.COLOR_MAGENTA,
curses.COLOR_BLUE,
curses.COLOR_CYAN,
curses.COLOR_GREEN,
curses.COLOR_WHITE,
]
for i, color in enumerate(colors):
curses.init_pair(i + 1, color, color)
curses.init_pair(10, curses.COLOR_WHITE, curses.COLOR_BLACK)
self.boardColor = curses.color_pair(10)
def doWelcome(self):
self.stdscr.addstr(0, 0, welcomeMessage[0])
start = False
blink_counter = 1
blink = True
animate_counter = 0
refresh_counter = 10
while (not start):
c = self.stdscr.getch()
for i in range(10000):
c = self.stdscr.getch()
if c != -1:
start = True
break
if refresh_counter == 10:
refresh_counter = 1
if animate_counter == len(welcomeMessage):
animate_counter = 0
self.stdscr.addstr(0, 0, welcomeMessage[animate_counter])
animate_counter += 1
if blink:
self.stdscr.addstr(14, 22, "┌--------------------------------┐")
self.stdscr.addstr(15, 22, "| |")
self.stdscr.addstr(16, 22, "| |")
self.stdscr.addstr(17, 22, "| |")
self.stdscr.addstr(18, 22, "└--------------------------------┘")
else:
self.stdscr.addstr(14, 22, "┌--------------------------------┐")
self.stdscr.addstr(15, 22, "| |")
self.stdscr.addstr(16, 22, "| Press ANY key to start! |")
self.stdscr.addstr(17, 22, "| |")
self.stdscr.addstr(18, 22, "└--------------------------------┘")
blink = not blink
blink_counter = 0
refresh_counter += 1
#self.stdscr.addstr(23, 70, "Eric Pai")
self.stdscr.addstr(23, 0, "v{0} Eric Pai ©2014".format(self.version))
curses.delay_output(5)
blink_counter += 1
self.stdscr.refresh()
def gameLoop(self):
while True:
self.doRestart()
while True:
if self.g.gameOver:
self.doGameOver()
if self.has_landed:
self.g.newPiece()
self.has_landed = False
else:
self.doMove()
if self.restart:
break
if not self.has_landed and \
self.down_counter == self.down_constant: # do fall
self.has_landed = self.g.fallPiece()
self.down_counter = 1
self.refreshAnimation()
def displayBoard(self):
boardString = self.g.toString()
if not self.has_colors:
self.stdscr.addstr(0, 0, self.g.toString())
return
for y, line in enumerate(boardString.split("\n")):
for x, ch in enumerate(line):
if ch.isdigit():
color = int(ch)
if color == 9:
self.stdscr.addstr(y, x, '░', self.boardColor)
else:
self.stdscr.addstr(y, x, ch, curses.color_pair(color))
else:
self.stdscr.addstr(y, x, ch, self.boardColor)
def doMove(self):
last_move = 0
shape_change = False
for i in range(1000): # improves reactivity
c = self.stdscr.getch()
if c == curses.KEY_DOWN: # moves piece down faster
self.down_counter = self.down_constant
curses.delay_output(self.time)
if not self.has_landed:
self.has_landed = self.g.fallPiece()
self.displayBoard()
if self.has_landed:
self.down_counter = 1
if c == curses.KEY_LEFT: # moves blocks to the left
last_move = -1
elif c == curses.KEY_RIGHT: # moves piece to the right
last_move = 1
elif not shape_change and c == curses.KEY_UP: # rotates piece
self.g.rotatePiece()
shape_change = True
elif c == ord('m'):
self.doMenu()
elif c == ord('q'):
self.doQuit()
elif c == ord('p'):
self.doPause()
self.g.movePiece(last_move)
def doMenu(self):
def printMenu():
self.stdscr.addstr(5, 24, "┌--------------------------------┐")
self.stdscr.addstr(6, 24, "| GAME PAUSED |")
self.stdscr.addstr(7, 24, "| |")
self.stdscr.addstr(8, 24, "| Controls: |")
self.stdscr.addstr(9, 24, "| ^ Rotate Piece |")
self.stdscr.addstr(10, 24, "| | |")
self.stdscr.addstr(11, 24, "| Move left <-- --> Move right |")
self.stdscr.addstr(12, 24, "| | |")
self.stdscr.addstr(13, 24, "| V Fall faster |")
self.stdscr.addstr(14, 24, "| |")
self.stdscr.addstr(15, 24, "| Type 'm' to resume, |")
self.stdscr.addstr(16, 24, "| 'q' to quit |")
self.stdscr.addstr(17, 24, "| 'r' to restart |")
self.stdscr.addstr(18, 24, "└--------------------------------┘")
self.stdscr.refresh()
printMenu()
self.stdscr.nodelay(False)
c = self.stdscr.getch()
while c not in (ord('q'), ord('r'), ord('m')):
c = self.stdscr.getch()
if c == ord('q'):
self.doQuit()
printMenu()
if c == ord('r'):
self.restart = True
self.stdscr.nodelay(True)
def doPause(self):
self.stdscr.addstr(9, 22, "┌----------------------------┐")
self.stdscr.addstr(10, 22, "| |")
self.stdscr.addstr(11, 22, "| Game is Paused |")
self.stdscr.addstr(12, 22, "| press 'p' to unpause |")
self.stdscr.addstr(13, 22, "| |")
self.stdscr.addstr(14, 22, "└----------------------------┘")
self.stdscr.nodelay(False)
c = self.stdscr.getch()
while c != ord('p'):
c = self.stdscr.getch()
self.stdscr.nodelay(True)
def refreshAnimation(self):
self.stdscr.clear()
curses.delay_output(self.time) # change so updates in real time
self.down_counter += 1
self.displayBoard()
# score
self.stdscr.addstr(20, 52, "lines completed: {0}".format(self.g.clearedLines))
self.stdscr.addstr(22, 42, "Type 'q' to quit, 'm' for menu,")
self.stdscr.addstr(23, 52, "'p' to pause.")
self.stdscr.addstr(15, 52, "level: {0}".format(self.g.level))
self.stdscr.addstr(17, 48, "--------------------------")
self.stdscr.addstr(18, 48, " Score {:,} ".format(self.g.score))
self.stdscr.addstr(19, 48, "--------------------------")
# next piece box
for i in range(len(self.nextPieceBoarder)):
self.stdscr.addstr(i + 1, 49, self.nextPieceBoarder[i])
nextPieceLines = self.g.nextPieceToString()
for i, line in enumerate(nextPieceLines):
for j, ch in enumerate(line):
if ch.isdigit():
color = int(ch)
self.stdscr.addstr(i + 5, 56 + j, ch, curses.color_pair(color))
else:
self.stdscr.addstr(i + 5, 56 + j, ch, self.boardColor)
if self.g.clearedLines - self.level_constant*self.g.level >= 0:
self.down_constant -= self.level_decrement
self.g.level += 1
if self.g.level == 11:
self.doWin()
def doGameOver(self):
#self.stdscr.clear()
#self.stdscr.addstr(11, 34, "Game Over!")
self.stdscr.addstr(10, 24, "┌--------------------------------┐")
self.stdscr.addstr(11, 24, "| |")
self.stdscr.addstr(12, 24, "| Game Over! |")
self.stdscr.addstr(13, 24, "| |")
self.stdscr.addstr(14, 24, "└--------------------------------┘")
self.stdscr.refresh()
curses.delay_output(1500)
self.stdscr.addstr(12, 24, "| Score: {:,}".format(self.g.score))
self.stdscr.refresh()
curses.delay_output(1500)
self.stdscr.addstr(9, 24, "┌--------------------------------┐")
self.stdscr.addstr(10, 24, "| Play again? |")
self.stdscr.addstr(11, 24, "| |")
self.stdscr.addstr(13, 24, "| |")
self.stdscr.addstr(14, 24, "| 'y' for yes, 'n' for no |")
self.stdscr.addstr(15, 24, "└--------------------------------┘")
# self.stdscr.addstr(11, 27, " Play again? ")
# self.stdscr.addstr(13, 27, "'y' for yes, 'n' for no")
self.stdscr.refresh()
self.stdscr.nodelay(False)
c = self.stdscr.getch()
while c not in (ord('y'), ord('n')):
c = self.stdscr.getch()
if c == ord('y'):
self.restart = True
if not self.restart:
raise ZeroDivisionError
self.stdscr.nodelay(True)
def doWin(self):
#self.stdscr.clear()
self.stdscr.addstr(10, 24, "┌--------------------------------┐")
self.stdscr.addstr(11, 24, "| |")
self.stdscr.addstr(12, 24, "| You win! |")
self.stdscr.addstr(13, 24, "| |")
self.stdscr.addstr(14, 24, "└--------------------------------┘")
self.stdscr.refresh()
curses.delay_output(1500)
self.stdscr.addstr(12, 24, "| Score: {:,}".format(self.g.score))
self.stdscr.refresh()
curses.delay_output(1500)
self.stdscr.addstr(10, 24, "┌--------------------------------┐")
self.stdscr.addstr(11, 24, "| Play again? |")
self.stdscr.addstr(11, 24, "| |")
self.stdscr.addstr(13, 24, "| |")
self.stdscr.addstr(13, 24, "| 'y' for yes, 'n' for no |")
self.stdscr.addstr(14, 24, "└--------------------------------┘")
self.stdscr.refresh()
self.stdscr.nodelay(False)
c = self.stdscr.getch()
while c not in (ord('y'), ord('n')):
c = self.stdscr.getch()
if c == ord('y'):
self.restart = True
if not self.restart:
raise ZeroDivisionError
self.stdscr.nodelay(True)
def doQuit(self):
self.stdscr.addstr(10, 24, "┌--------------------------------┐")
self.stdscr.addstr(11, 24, "| Are you sure you want to quit? |")
self.stdscr.addstr(12, 24, "| |")
self.stdscr.addstr(13, 24, "| 'y' for yes, 'n' for no |")
self.stdscr.addstr(14, 24, "└--------------------------------┘")
self.stdscr.refresh()
c = self.stdscr.getch()
while c not in (ord('y'), ord('n')):
c = self.stdscr.getch()
if c == ord('y'):
raise ZeroDivisionError
def doRestart(self):
self.time = 5
self.g = Game()
self.has_landed = True
self.down_counter = 1
self.down_constant = 100
self.level_constant = 5
self.level_decrement = 10
self.restart = False
def doFinish(self):
curses.nocbreak()
self.stdscr.keypad(False)
curses.echo()
curses.endwin()