/
GameController.py
275 lines (227 loc) · 8.98 KB
/
GameController.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
#!/usr/bin/env python
"""
Tetris game controller
"""
from GameBoard import *
from Shapes import *
from random import randint
from StatusWindow import *
import tkMessageBox
from BoardStates import *
NO_OF_LEVELS = 10
SCALE = 20
OFFSET = 3
MAXX = 10
MAXY = 22
LEFT = "left"
RIGHT = "right"
DOWN = "down"
direction_d = { "left": (-1, 0), "right": (1, 0), "down": (0, 1) }
def level_thresholds( first_level, no_of_levels ):
"""
Calculates the score at which the level will change, for n levels.
"""
thresholds =[]
for x in xrange( no_of_levels ):
multiplier = 2**x
thresholds.append( first_level * multiplier )
return thresholds
class game_controller(object):
"""
Main game loop and receives GUI callback events for keypresses etc...
"""
def __init__(self, parent, lines, game_number = 0):
self.lines = lines
self.auto_mode = False
self.game_number = game_number
if lines != None:
self.lines = shape.list_from_str_list(lines)
self.lines_shape_index = 0
self.auto_mode = True
"""
Intialise the game...
"""
self.parent = parent
self.score = 0
self.level = 0
self.delay = 1000 #ms
#lookup table
self.shapes = [square_shape,
t_shape,
l_shape,
reverse_l_shape,
z_shape,
s_shape,
i_shape ]
self.thresholds = level_thresholds( 500, NO_OF_LEVELS )
self.status_bar = status_bar( parent )
self.status_bar.pack(side=TOP,fill=X)
#print "Status bar width",self.status_bar.cget("width")
self.status_bar.set("Score: %-7d\t Level: %d " % (
self.score, self.level+1)
)
#create the status window
self.status_window = StatusWindow(parent)
self.status_window.pack()
#create the game board
self.board = Board(
parent,
scale=SCALE,
max_x=MAXX,
max_y=MAXY,
offset=OFFSET
)
self.board.pack(side=BOTTOM)
self.board.focus()
self.parent.bind("<Left>", self.left_callback)
self.parent.bind("<Right>", self.right_callback)
self.parent.bind("<Up>", self.up_callback)
self.parent.bind("<Down>", self.down_callback)
self.parent.bind("a", self.a_callback)
self.parent.bind("s", self.s_callback)
self.parent.bind("p", self.p_callback)
self.parent.bind("q", self.quit_callback)
self.shape = self.get_next_shape()
self.after_id = self.parent.after( self.delay, self.move_my_shape )
def eval(self):
touching = 0
already_hit = []
for block in self.shape.blocks:
current = block.coord()
print "current:", current
#create tuples of coordinates of blocks to check
right = (current[0]+1, current[1])
left = (current[0]-1, current[1])
#TK coord system is down and to right
#that's why up and down are reversed
down = (current[0], current[1]+1)
up = (current[0], current[1]-1)
right_hit = self.board.landed.get(right)
left_hit = self.board.landed.get(left)
up_hit = self.board.landed.get(up)
down_hit = self.board.landed.get(down)
if( right_hit and (right not in already_hit)):
print "\tright hit", right
already_hit.append(right)
touching += 1
if ( left_hit and (left not in already_hit) ):
print "\tleft hit", left
already_hit.append(left)
touching += 1
if ( up_hit and (up not in already_hit) ):
print "\tup hit", up
already_hit.append(up)
touching += 1
if ( down_hit and (down not in already_hit) ):
print "\tdown hit", down
already_hit.append(down)
touching += 1
print "\tTouching: ", touching
print "-"*20
def handle_move(self, direction):
#if you can't move then you've hit something
if not self.shape.move( direction ):
self.status_window.log_text("HIT")
# if your heading down then the shape has 'landed'
if direction == DOWN:
self.eval()
tmp_score = self.score
self.score += self.board.check_for_complete_row(
self.shape.blocks
)
#check for points added
if(self.score > tmp_score):
self.status_window.log_text("POINTS " + str(self.score - tmp_score))
del self.shape
self.status_window.log_text("LANDED")
self.shape = self.get_next_shape()
# If the shape returned is None, then this indicates that
# that the check before creating it failed and the
# game is over!
if self.shape is None:
self.status_window.log_text("GAME OVER! SCORE: " + str(self.score))
tkMessageBox.showwarning(
title="GAME OVER",
message ="Score: %7d\tLevel: %d\t" % (
self.score, self.level),
)
Toplevel().quit()
self.parent.quit()
# self.status_window.quit()
# self.status_window.destroy()
# sys.exit(0)
# do we go up a level?
if (self.level < NO_OF_LEVELS and
self.score >= self.thresholds[ self.level]):
self.level+=1
self.delay-=100
self.status_bar.set("Score: %-7d\t Level: %d " % (
self.score, self.level+1)
)
# Signal that the shape has 'landed'
return False
return True
def left_callback( self, event ):
if self.shape:
self.status_window.log_text("LEFT")
self.handle_move( LEFT )
def right_callback( self, event ):
if self.shape:
self.status_window.log_text("RIGHT")
self.handle_move( RIGHT )
def up_callback( self, event ):
if self.shape:
# drop the tetrominoe to the bottom
while self.handle_move( DOWN ):
pass
def down_callback( self, event ):
if self.shape:
self.status_window.log_text("DOWN")
self.handle_move( DOWN )
def a_callback( self, event):
if self.shape:
self.shape.rotate(clockwise=True)
self.status_window.log_text("ROTATE CLOCKWISE")
def s_callback( self, event):
if self.shape:
self.shape.rotate(clockwise=False)
self.status_window.log_text("ROTATE COUNTERCLOCKWISE")
def p_callback(self, event):
self.parent.after_cancel( self.after_id )
tkMessageBox.askquestion(
title = "Paused!",
message="Continue?",
type=tkMessageBox.OK)
self.after_id = self.parent.after( self.delay, self.move_my_shape )
def quit_callback(self, event):
sys.exit(0)
def move_my_shape( self ):
if self.shape:
self.handle_move( DOWN )
self.after_id = self.parent.after( self.delay, self.move_my_shape )
def get_next_shape( self ):
"""
Select the next shape in the list
"""
if self.lines and len(self.lines) > 0 and self.lines_shape_index < len(self.lines):
the_shape = self.lines[self.lines_shape_index]
self.lines_shape_index = self.lines_shape_index + 1
else:
"""
If we were in auto mode, that means we have now ran out of pieces
Log the score, and move on to the next game
"""
if self.auto_mode == True:
print "Game #" + str(self.game_number) + " " + "Score: " + str(self.score)
return None #This will end the game
"""
Randomly select which tetrominoe will be used next.
"""
self.shapes = self.status_window.get_shapes()
the_shape = self.shapes[ randint(0,len(self.shapes)-1) ]
#check_and_create is a factory function
s = the_shape.check_and_create(self.board)
self.status_window.new_shape(s)
self.status_window.log_board(self.board)
BoardStates.generate_child_states(self.board, the_shape)
return s