|
| 1 | +from Tkinter import * |
| 2 | +import random |
| 3 | +import time |
| 4 | + |
| 5 | +class Tetris: |
| 6 | + def __init__(self, parent): |
| 7 | + self.parent = parent |
| 8 | + self.squares = self.importColors() |
| 9 | + self.the_pile = [[0 for x in range(10)] for x in range(18)] |
| 10 | + self.master_frame = Frame(self.parent) |
| 11 | + self.master_frame.pack() |
| 12 | + self.makePiece() |
| 13 | + self.lost = False |
| 14 | + self.score = 0 |
| 15 | + self.speed = 300 |
| 16 | + self.level = 1 |
| 17 | + |
| 18 | + def __repr__(self): |
| 19 | + string = '' |
| 20 | + for row in self.the_pile: |
| 21 | + string += (str(row)) |
| 22 | + string += '\n' |
| 23 | + return string |
| 24 | + |
| 25 | + def refresh(self): |
| 26 | + self.master_frame.destroy() |
| 27 | + self.master_frame = Frame(self.parent) |
| 28 | + self.master_frame.pack() |
| 29 | + self.row_frames = [Frame(self.master_frame) for x in range(18)] |
| 30 | + self.current_piece.on = self.current_piece.getOn() |
| 31 | + for row in range(18): |
| 32 | + self.row_frames[row].pack( side = TOP ) |
| 33 | + for col in range(10): |
| 34 | + if self.the_pile[row][col] != 0: |
| 35 | + Label(self.row_frames[row], image = self.squares[self.the_pile[row][col]]).pack( side = LEFT ) |
| 36 | + elif (row,col) in self.current_piece.on: |
| 37 | + Label(self.row_frames[row], image = self.squares[self.current_piece.color]).pack( side = LEFT ) |
| 38 | + else: |
| 39 | + Label(self.row_frames[row], image = self.squares[0]).pack( side = LEFT ) |
| 40 | + |
| 41 | + def makePiece(self): |
| 42 | + piece_types = ["square","I","L1","L2","N1","N2","T"] |
| 43 | + color_options = {"square":4,"I":2,"L1":7,"L2":1,"N1":5,"N2":6,"T":3} |
| 44 | + rand_type = random.choice(piece_types) |
| 45 | + color = color_options[rand_type] |
| 46 | + if rand_type != "square": |
| 47 | + row = 4 |
| 48 | + col = 1 |
| 49 | + else: |
| 50 | + row = 4 |
| 51 | + col = 0 |
| 52 | + self.current_piece = Piece(rand_type, "north", col, row, color) |
| 53 | + for cell in self.current_piece.on: |
| 54 | + if self.the_pile[cell[0]][cell[1]] != 0: |
| 55 | + self.lost = True |
| 56 | + self.current_piece.on = [] |
| 57 | + |
| 58 | + def rotate(self): |
| 59 | + self.current_piece.rotateClockwise() |
| 60 | + self.refresh() |
| 61 | + |
| 62 | + def moveDown(self): |
| 63 | + for cell in self.current_piece.on: |
| 64 | + row = cell[0] |
| 65 | + col = cell[1] |
| 66 | + if row == 17 or self.the_pile[row+1][col] != 0: |
| 67 | + self.addToPile() |
| 68 | + return |
| 69 | + self.current_piece.row += 1 |
| 70 | + self.current_piece.on = self.current_piece.getOn() |
| 71 | + self.refresh() |
| 72 | + |
| 73 | + def moveLeft(self): |
| 74 | + for cell in self.current_piece.on: |
| 75 | + row = cell[0] |
| 76 | + col = cell[1] |
| 77 | + if col == 0: return |
| 78 | + if self.the_pile[row][col-1] != 0: return |
| 79 | + self.current_piece.col -= 1 |
| 80 | + self.refresh() |
| 81 | + |
| 82 | + def moveRight(self): |
| 83 | + for cell in self.current_piece.on: |
| 84 | + row = cell[0] |
| 85 | + col = cell[1] |
| 86 | + if col == 9: return |
| 87 | + if self.the_pile[row][col+1] != 0: return |
| 88 | + self.current_piece.col += 1 |
| 89 | + self.refresh() |
| 90 | + |
| 91 | + def addToPile(self): |
| 92 | + self.score += 1 |
| 93 | + self.level = self.score % 100 |
| 94 | + self.speed = (self.level)/1000 |
| 95 | + for cell in self.current_piece.on: |
| 96 | + self.the_pile[cell[0]][cell[1]] = self.current_piece.color |
| 97 | + for row in range(18): |
| 98 | + if 0 in self.the_pile[row]: pass |
| 99 | + else: |
| 100 | + new_board = [[0 for x in range(10)]] |
| 101 | + new_board += (self.the_pile[0:row]) |
| 102 | + new_board += (self.the_pile[row+1:18]) |
| 103 | + self.the_pile = new_board |
| 104 | + self.makePiece() |
| 105 | + self.refresh() |
| 106 | + |
| 107 | + def importColors(self): |
| 108 | + squares = [] |
| 109 | + for i in range(8): |
| 110 | + filename = "square_"+str(i)+".gif" |
| 111 | + squares.append(PhotoImage(file=filename)) |
| 112 | + return squares |
| 113 | + |
| 114 | + def quit(self): |
| 115 | + time.sleep(0.5) |
| 116 | + self.master_frame.destroy() |
| 117 | + |
| 118 | +class Piece: |
| 119 | + def __init__(self, typeName, orientation, row, col, color): |
| 120 | + self.typeName = typeName |
| 121 | + self.color = color |
| 122 | + self.orientation = orientation |
| 123 | + self.layout = self.getLayout() |
| 124 | + self.row = row |
| 125 | + self.col = col |
| 126 | + self.on = self.getOn() |
| 127 | + |
| 128 | + def getOn(self): |
| 129 | + offset = [-2,-1,0,1,2] |
| 130 | + on = [] |
| 131 | + for row in range(5): |
| 132 | + for col in range(5): |
| 133 | + if self.layout[row][col] != 0: on.append((self.row + offset[row],self.col + offset[col])) |
| 134 | + return on |
| 135 | + |
| 136 | + def getLayout(self): |
| 137 | + # Square- only has 1 orientation |
| 138 | + if self.typeName == "square": |
| 139 | + return [ |
| 140 | + [0, 0, 0, 0, 0], |
| 141 | + [0, 0, 0, 0, 0], |
| 142 | + [0, 0, 1, 1, 0], |
| 143 | + [0, 0, 1, 1, 0], |
| 144 | + [0, 0, 0, 0, 0] |
| 145 | + ] |
| 146 | + #I |
| 147 | + if self.typeName == "I": |
| 148 | + if self.orientation == "north": |
| 149 | + return [ |
| 150 | + [0, 0, 0, 0, 0], |
| 151 | + [0, 0, 1, 0, 0], |
| 152 | + [0, 0, 1, 0, 0], |
| 153 | + [0, 0, 1, 0, 0], |
| 154 | + [0, 0, 1, 0, 0] |
| 155 | + ] |
| 156 | + if self.orientation == "east": |
| 157 | + return [ |
| 158 | + [0, 0, 0, 0, 0], |
| 159 | + [0, 0, 0, 0, 0], |
| 160 | + [0, 1, 1, 1, 1], |
| 161 | + [0, 0, 0, 0, 0], |
| 162 | + [0, 0, 0, 0, 0] |
| 163 | + ] |
| 164 | + if self.orientation == "south": |
| 165 | + return [ |
| 166 | + [0, 0, 0, 0, 0], |
| 167 | + [0, 0, 1, 0, 0], |
| 168 | + [0, 0, 1, 0, 0], |
| 169 | + [0, 0, 1, 0, 0], |
| 170 | + [0, 0, 1, 0, 0] |
| 171 | + ] |
| 172 | + if self.orientation == "west": |
| 173 | + return [ |
| 174 | + [0, 0, 0, 0, 0], |
| 175 | + [0, 0, 0, 0, 0], |
| 176 | + [0, 1, 1, 1, 1], |
| 177 | + [0, 0, 0, 0, 0], |
| 178 | + [0, 0, 0, 0, 0] |
| 179 | + ] |
| 180 | + #L1 |
| 181 | + if self.typeName == "L1": |
| 182 | + if self.orientation == "north": |
| 183 | + return [ |
| 184 | + [0, 0, 0, 0, 0], |
| 185 | + [0, 0, 1, 0, 0], |
| 186 | + [0, 0, 1, 0, 0], |
| 187 | + [0, 0, 1, 1, 0], |
| 188 | + [0, 0, 0, 0, 0] |
| 189 | + ] |
| 190 | + if self.orientation == "east": |
| 191 | + return [ |
| 192 | + [0, 0, 0, 0, 0], |
| 193 | + [0, 0, 0, 0, 0], |
| 194 | + [0, 1, 1, 1, 0], |
| 195 | + [0, 1, 0, 0, 0], |
| 196 | + [0, 0, 0, 0, 0] |
| 197 | + ] |
| 198 | + if self.orientation == "south": |
| 199 | + return [ |
| 200 | + [0, 0, 0, 0, 0], |
| 201 | + [0, 1, 1, 0, 0], |
| 202 | + [0, 0, 1, 0, 0], |
| 203 | + [0, 0, 1, 0, 0], |
| 204 | + [0, 0, 0, 0, 0] |
| 205 | + ] |
| 206 | + if self.orientation == "west": |
| 207 | + return [ |
| 208 | + [0, 0, 0, 0, 0], |
| 209 | + [0, 0, 0, 1, 0], |
| 210 | + [0, 1, 1, 1, 0], |
| 211 | + [0, 0, 0, 0, 0], |
| 212 | + [0, 0, 0, 0, 0] |
| 213 | + ] |
| 214 | + #L2 |
| 215 | + if self.typeName == "L2": |
| 216 | + if self.orientation == "north": |
| 217 | + return [ |
| 218 | + [0, 0, 0, 0, 0], |
| 219 | + [0, 0, 1, 0, 0], |
| 220 | + [0, 0, 1, 0, 0], |
| 221 | + [0, 1, 1, 0, 0], |
| 222 | + [0, 0, 0, 0, 0] |
| 223 | + ] |
| 224 | + if self.orientation == "east": |
| 225 | + return [ |
| 226 | + [0, 0, 0, 0, 0], |
| 227 | + [0, 1, 0, 0, 0], |
| 228 | + [0, 1, 1, 1, 0], |
| 229 | + [0, 0, 0, 0, 0], |
| 230 | + [0, 0, 0, 0, 0] |
| 231 | + ] |
| 232 | + if self.orientation == "south": |
| 233 | + return [ |
| 234 | + [0, 0, 0, 0, 0], |
| 235 | + [0, 0, 1, 1, 0], |
| 236 | + [0, 0, 1, 0, 0], |
| 237 | + [0, 0, 1, 0, 0], |
| 238 | + [0, 0, 0, 0, 0] |
| 239 | + ] |
| 240 | + if self.orientation == "west": |
| 241 | + return [ |
| 242 | + [0, 0, 0, 0, 0], |
| 243 | + [0, 0, 0, 0, 0], |
| 244 | + [0, 1, 1, 1, 0], |
| 245 | + [0, 0, 0, 1, 0], |
| 246 | + [0, 0, 0, 0, 0] |
| 247 | + ] |
| 248 | + #N1 |
| 249 | + if self.typeName == "N1": |
| 250 | + if self.orientation == "north": |
| 251 | + return [ |
| 252 | + [0, 0, 0, 0, 0], |
| 253 | + [0, 0, 0, 1, 0], |
| 254 | + [0, 0, 1, 1, 0], |
| 255 | + [0, 0, 1, 0, 0], |
| 256 | + [0, 0, 0, 0, 0] |
| 257 | + ] |
| 258 | + if self.orientation == "east": |
| 259 | + return [ |
| 260 | + [0, 0, 0, 0, 0], |
| 261 | + [0, 0, 0, 0, 0], |
| 262 | + [0, 1, 1, 0, 0], |
| 263 | + [0, 0, 1, 1, 0], |
| 264 | + [0, 0, 0, 0, 0] |
| 265 | + ] |
| 266 | + if self.orientation == "south": |
| 267 | + return [ |
| 268 | + [0, 0, 0, 0, 0], |
| 269 | + [0, 0, 0, 1, 0], |
| 270 | + [0, 0, 1, 1, 0], |
| 271 | + [0, 0, 1, 0, 0], |
| 272 | + [0, 0, 0, 0, 0] |
| 273 | + ] |
| 274 | + if self.orientation == "west": |
| 275 | + return [ |
| 276 | + [0, 0, 0, 0, 0], |
| 277 | + [0, 0, 0, 0, 0], |
| 278 | + [0, 1, 1, 0, 0], |
| 279 | + [0, 0, 1, 1, 0], |
| 280 | + [0, 0, 0, 0, 0] |
| 281 | + ] |
| 282 | + #N2 |
| 283 | + if self.typeName == "N2": |
| 284 | + if self.orientation == "north": |
| 285 | + return [ |
| 286 | + [0, 0, 0, 0, 0], |
| 287 | + [0, 0, 1, 0, 0], |
| 288 | + [0, 0, 1, 1, 0], |
| 289 | + [0, 0, 0, 1, 0], |
| 290 | + [0, 0, 0, 0, 0] |
| 291 | + ] |
| 292 | + if self.orientation == "east": |
| 293 | + return [ |
| 294 | + [0, 0, 0, 0, 0], |
| 295 | + [0, 0, 0, 0, 0], |
| 296 | + [0, 0, 1, 1, 0], |
| 297 | + [0, 1, 1, 0, 0], |
| 298 | + [0, 0, 0, 0, 0] |
| 299 | + ] |
| 300 | + if self.orientation == "south": |
| 301 | + return [ |
| 302 | + [0, 0, 0, 0, 0], |
| 303 | + [0, 0, 1, 0, 0], |
| 304 | + [0, 0, 1, 1, 0], |
| 305 | + [0, 0, 0, 1, 0], |
| 306 | + [0, 0, 0, 0, 0] |
| 307 | + ] |
| 308 | + if self.orientation == "west": |
| 309 | + return [ |
| 310 | + [0, 0, 0, 0, 0], |
| 311 | + [0, 0, 0, 0, 0], |
| 312 | + [0, 0, 1, 1, 0], |
| 313 | + [0, 1, 1, 0, 0], |
| 314 | + [0, 0, 0, 0, 0] |
| 315 | + ] |
| 316 | + #T |
| 317 | + if self.typeName == "T": |
| 318 | + if self.orientation == "north": |
| 319 | + return [ |
| 320 | + [0, 0, 0, 0, 0], |
| 321 | + [0, 0, 1, 0, 0], |
| 322 | + [0, 0, 1, 1, 0], |
| 323 | + [0, 0, 1, 0, 0], |
| 324 | + [0, 0, 0, 0, 0] |
| 325 | + ] |
| 326 | + if self.orientation == "east": |
| 327 | + return [ |
| 328 | + [0, 0, 0, 0, 0], |
| 329 | + [0, 0, 0, 0, 0], |
| 330 | + [0, 1, 1, 1, 0], |
| 331 | + [0, 0, 1, 0, 0], |
| 332 | + [0, 0, 0, 0, 0] |
| 333 | + ] |
| 334 | + if self.orientation == "south": |
| 335 | + return [ |
| 336 | + [0, 0, 0, 0, 0], |
| 337 | + [0, 0, 1, 0, 0], |
| 338 | + [0, 1, 1, 0, 0], |
| 339 | + [0, 0, 1, 0, 0], |
| 340 | + [0, 0, 0, 0, 0] |
| 341 | + ] |
| 342 | + if self.orientation == "west": |
| 343 | + return [ |
| 344 | + [0, 0, 0, 0, 0], |
| 345 | + [0, 0, 1, 0, 0], |
| 346 | + [0, 1, 1, 1, 0], |
| 347 | + [0, 0, 0, 0, 0], |
| 348 | + [0, 0, 0, 0, 0] |
| 349 | + ] |
| 350 | + |
| 351 | + def rotateClockwise(self): |
| 352 | + if self.orientation == "north": |
| 353 | + self.orientation = "east" |
| 354 | + |
| 355 | + elif self.orientation == "east": |
| 356 | + self.orientation = "south" |
| 357 | + |
| 358 | + elif self.orientation == "south": |
| 359 | + self.orientation = "west" |
| 360 | + |
| 361 | + elif self.orientation == "west": |
| 362 | + self.orientation = "north" |
| 363 | + self.layout = self.getLayout() |
0 commit comments