-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtetris.py
324 lines (235 loc) · 8.77 KB
/
tetris.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
"""
tetris.py - a simple game of tetris
author : Benjamin Blundell
email : [email protected]
"""
import random, copy
from buffergame import BufferGame, loadFromFile
class Tetris(BufferGame):
''' Basic game logic for tetris. We deal in discrete states with no notion
of time, more to do with frames and discrete values '''
def __init__(self, boardX = 14, boardY = 13):
super(Tetris,self).__init__(boardX,boardY)
self.blocks = [
{ 'bits' : [[-1,0],[ 0,0],[1,0],[2,0]], 'type' : 'I' , 'pos' : [0,0] }, # Long piece
{ 'bits' : [[-1,1],[-1,0],[0,0],[1,0]], 'type' : 'J' , 'pos' : [0,0] }, # Left L
{ 'bits' : [[ 1,1],[-1,0],[0,0],[1,0]], 'type' : 'L' , 'pos' : [0,0] }, # Right L
{ 'bits' : [[ 1,1],[ 0,0],[1,0],[0,1]], 'type' : 'O' , 'pos' : [0,0] }, # Square
{ 'bits' : [[ 0,1],[-1,0],[0,0],[1,1]], 'type' : 'Z' , 'pos' : [0,0] }, # S
{ 'bits' : [[-1,1],[ 0,1],[0,0],[1,0]], 'type' : 'S' , 'pos' : [0,0] }, # Z
{ 'bits' : [[ 0,1],[-1,0],[0,0],[1,0]], 'type' : 'T' , 'pos' : [0,0] } # T
]
self.rotClock = ((0,-1),(1,0))
self.rotCounter = ((0,1),(-1,0))
self.rot180 = ((-1,0),(0,-1))
self.rotations = [self.rotCounter,self.rotClock,self.rot180]
self.level = 1
self.state = "READY"
self.eventQueue = [] # list of tuples - function and arg
self._dframes = 0 # frames passed between ticks
self._eframes = 0 # end game animation frames passed
# Game over data
gameover_data = loadFromFile(boardX, boardY, "game_over.txt")
self._gameOverFrames = gameover_data["buffers"]
self._gameOverDuration = gameover_data["duration"]
# start data
start_data = loadFromFile(boardX, boardY, "start_game.txt")
self._startFrames = start_data["buffers"]
self._startDuration = start_data["duration"]
def start(self):
''' Clear the board and start the game '''
self.clearBuffer()
self.level = 1
self.currentBlock = self.createBlock()
self.moveHere(self.currentBlock,(0,-1))
self.state = "PLAYING"
def rotate(self, matrix, block):
''' rotate the current block returning a new block - non destructive '''
new_block = copy.deepcopy(block)
# Ignore the square 'O'
if block['type'] != 'O':
for bit in new_block['bits']:
x = bit[0] # todo - presumably a copy here?
y = bit[1]
bit[0] = x * matrix[0][0] + y * matrix[1][0]
bit[1] = x * matrix[0][1] + y * matrix[1][1]
return new_block
def createBlock(self):
''' create a new block at the top of the screen '''
# Pick random block
c = random.randint(0,len(self.blocks)-1)
new_block = self.blocks[c : c + 1 ][0]
if new_block['type'] != 'O':
c = random.randint(0,len(self.rotations))
if c != len(self.rotations):
# Garbage collect here as we loose new_block and gain another
new_block = self.rotate(self.rotations[c],new_block)
# Find highest y point - if we rotate of course
topy = 0
for [x,y] in new_block['bits']:
if y > topy:
topy = y
new_block['pos'][0] = self.boardX / 2
new_block['pos'][1] = self.boardY - topy
return new_block
''' Events can occur at any time so they sort
of ruin the discrete nature of state changes really so we queue
them '''
def _move(self,offset):
self.moveIfPoss(self.currentBlock,offset)
def goLeft(self):
self.eventQueue.append( (self._move,(-1,0)))
def goRight(self):
self.eventQueue.append( (self._move,(1,0)))
def goDown(self):
self.eventQueue.append( (self._move,(0,-1)))
def goStart(self):
if self.state == "READY":
self.eventQueue = [] # clear so we don't roll over into rotate or something
self._eframes = 0 # reset the eframes ready for game over
self.start()
def _rotate(self,clockwise):
''' Rotate a block - somewhat more involved '''
tblock = self.rotate(self.rotClock, self.currentBlock)
if not clockwise:
tblock = self.rotate(self.rotCounter, self.currentBlock)
if self.canMoveHere(tblock, (0,0)):
# garbage collect
self.clearBlock(self.currentBlock)
self.currentBlock = tblock
self.moveHere(self.currentBlock, (0,0))
def goRotate(self):
self.eventQueue.append( (self._rotate, True))
def canMoveHere(self, block, offset):
''' check if we can move into the new location specified by offset '''
for [x,y] in block['bits']:
xp = x + block['pos'][0] + offset[0]
yp = y + block['pos'][1] + offset[1]
if xp < 0 or xp >= self.boardX:
return False
if yp < 0 or yp >= self.boardY:
return False
if self.buffer[yp][xp] != 0 :
# Cx its not moving into itself
xp = x + offset[0]
yp = y + offset[1]
if not [xp,yp] in block['bits']:
return False
return True
def moveIfPoss(self,block,offset):
''' convinience function '''
if self.canMoveHere(block,offset):
self.moveHere(block,offset)
return True
return False
def clearBlock(self,block):
''' clear a block from the buffer '''
for [x,y] in block['bits']:
xp = x + block['pos'][0]
yp = y + block['pos'][1]
if xp >= 0 and xp < self.boardX and yp >= 0 and yp < self.boardY:
self.buffer[yp][xp] = 0
def moveHere(self, block, offset):
''' clear where the block currently is and move it offset amount and set '''
# Clear buffer
self.clearBlock(block)
for [x,y] in block['bits']:
xp = x + block['pos'][0] + offset[0]
yp = y + block['pos'][1] + offset[1]
self.buffer[yp][xp] = block['type']
block['pos'][0] += offset[0]
block['pos'][1] += offset[1]
def check_lines(self):
''' Check to see if we've completed any lines '''
lines = []
for i in range(0,self.boardY):
complete = True
for j in range(0,self.boardX):
if self.buffer[i][j] == 0:
complete = False
break
if complete:
lines.append(i)
# Should be in order from the bottom up
for lidx in range(0,len(lines)):
line = lines[lidx] - lidx
for i in range(line,self.boardY):
if i != self.boardY - 1:
for j in range(0,self.boardX):
self.buffer[i][j] = self.buffer[i+1][j]
else:
for j in range(0,self.boardX):
self.buffer[i][j] = 0
def check_events(self):
''' Go through the events popping them '''
while len(self.eventQueue) > 0:
event = self.eventQueue.pop()
event[0](event[1])
def tick(self):
''' update the state of the game by one tick
this is a logic tick but since we have a low fps its almost
analogous to a game loop with no dt '''
self.check_lines()
if self.canMoveHere(self.currentBlock, (0,-1)):
self.moveHere(self.currentBlock,(0,-1))
else:
# Try and add another
self.currentBlock = self.createBlock()
if not self.canMoveHere( self.currentBlock, (0,-1) ):
self.state = "GAME_OVER"
else:
# garbage collect
self.moveHere(self.currentBlock,(0,-1))
def game_over(self,fps=2):
''' Game over animation '''
dd = float(self._eframes) / float(fps)
if dd >= self._gameOverDuration: # 3 second game over screen
self._eframes = 0
self.clearBuffer()
self.state = "READY"
return
else:
# draw the game over screen - find out which frame we want
self.clearBuffer()
dt = 0
for frame in self._gameOverFrames:
dt += frame["time"]
if dd <= dt:
self.copyBuffer(frame["buffer"])
break
self._eframes += 1
def ready(self,fps=2):
''' display a simple tetris anination and press space to play. Pretty
much the same as game over I figure '''
self.clearBuffer()
dt = 0
dd = float(self._eframes) / float(fps)
if dd >= self._startDuration:
self._eframes = 0
self.clearBuffer()
self.copyBuffer(self._startFrames[0]["buffer"])
for frame in self._startFrames:
dt += frame["time"]
if dd <= dt:
self.copyBuffer(frame["buffer"])
break
self._eframes += 1
def frame(self,fps=2):
''' We've had another frame. What should we do next?
We take an optional fps to get some idea of how fast
we are going for things like game start, block fall
button press etc '''
if self.state == "GAME_OVER":
self.game_over(fps)
elif self.state == "PLAYING":
self.check_events()
# self.tick is called depending on the level / difficulty of the game
# For now we just use a basis of 1 second drops
self._dframes += 1
if self._dframes / fps >= 1: # 1 second - 1 level
self.tick()
self._dframes = 0
elif self.state == "READY":
self.ready(fps)
if __name__ == "__main__" :
tetris = Tetris()