| 1 |
''' |
| 2 |
The MIT License (MIT) |
| 3 |
|
| 4 |
Copyright (c) 2015 David Bern |
| 5 |
|
| 6 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
of this software and associated documentation files (the "Software"), to deal |
| 8 |
in the Software without restriction, including without limitation the rights |
| 9 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
copies of the Software, and to permit persons to whom the Software is |
| 11 |
furnished to do so, subject to the following conditions: |
| 12 |
|
| 13 |
The above copyright notice and this permission notice shall be included in all |
| 14 |
copies or substantial portions of the Software. |
| 15 |
|
| 16 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 |
SOFTWARE. |
| 23 |
|
| 24 |
''' |
| 25 |
import random |
| 26 |
|
| 27 |
def grid_list(block_size): |
| 28 |
''' |
| 29 |
Generates a gridlist used shape class |
| 30 |
''' |
| 31 |
grids = [] |
| 32 |
for y in range(-5, 1): |
| 33 |
row = [] |
| 34 |
for x in range(0, 6): |
| 35 |
row.append((x * block_size, y * block_size)) |
| 36 |
grids.append(row) |
| 37 |
|
| 38 |
return grids |
| 39 |
|
| 40 |
class GameOver(Exception): |
| 41 |
''' |
| 42 |
Exception that is raised when a brick causes a game over state. |
| 43 |
''' |
| 44 |
pass |
| 45 |
|
| 46 |
|
| 47 |
class Shape(object): |
| 48 |
''' |
| 49 |
This will make the foundation for a Shape that the others shapes |
| 50 |
will inherit. |
| 51 |
''' |
| 52 |
def __init__(self, x_pos, size, color): |
| 53 |
''' |
| 54 |
Inits the shape to start at y_pos and every block is of size in size. |
| 55 |
OShape consists of 4 blocks, each block is size wide and high. |
| 56 |
''' |
| 57 |
|
| 58 |
self.block_size = size |
| 59 |
self._width = size |
| 60 |
self._height = size |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
self._x = x_pos - (self.block_size * 3) |
| 66 |
self._y = 0 |
| 67 |
self.piece = [] |
| 68 |
|
| 69 |
self.grid = grid_list(self.block_size) |
| 70 |
|
| 71 |
self.color = color |
| 72 |
|
| 73 |
def shape_grid_update(self): |
| 74 |
''' |
| 75 |
Update shapes position with the given _x and _y value. |
| 76 |
To be used after move or rotation is performed on the shape. |
| 77 |
''' |
| 78 |
for row_n, row in enumerate(self.piece): |
| 79 |
for element_n, element in enumerate(row): |
| 80 |
if element is not None: |
| 81 |
start_x, start_y = self.grid[row_n][element_n] |
| 82 |
self.piece[row_n][element_n].x = start_x + self._x |
| 83 |
self.piece[row_n][element_n].y = start_y + self._y |
| 84 |
|
| 85 |
def draw(self): |
| 86 |
''' |
| 87 |
Draws all the blocks inside the shape. |
| 88 |
''' |
| 89 |
for row in self.piece: |
| 90 |
for element in row: |
| 91 |
if element is not None: |
| 92 |
screen.draw.filled_rect(element, self.color) |
| 93 |
|
| 94 |
def pos_rotate(self): |
| 95 |
''' |
| 96 |
Rotate shape clockwise |
| 97 |
''' |
| 98 |
|
| 99 |
transposed = [new_row for new_row in zip(*self.piece)] |
| 100 |
|
| 101 |
|
| 102 |
rotated_shape = [] |
| 103 |
for row in transposed: |
| 104 |
row = list(row) |
| 105 |
reversed_row = [element for element in reversed(row)] |
| 106 |
rotated_shape.append(reversed_row) |
| 107 |
|
| 108 |
self.piece = rotated_shape |
| 109 |
self.shape_grid_update() |
| 110 |
|
| 111 |
def neg_rotate(self): |
| 112 |
''' |
| 113 |
Rotate shape clockwise |
| 114 |
''' |
| 115 |
|
| 116 |
transposed = [new_row for new_row in zip(*self.piece)] |
| 117 |
|
| 118 |
|
| 119 |
rotated_shape = [] |
| 120 |
for row in reversed(transposed): |
| 121 |
row = list(row) |
| 122 |
rotated_shape.append(row) |
| 123 |
|
| 124 |
self.piece = rotated_shape |
| 125 |
self.shape_grid_update() |
| 126 |
|
| 127 |
def move_down(self, speed): |
| 128 |
''' |
| 129 |
Moves the shape downwards in the speed passed in arg speed. |
| 130 |
speed is the number of pixels to move per update. |
| 131 |
''' |
| 132 |
self._y += speed |
| 133 |
self.shape_grid_update() |
| 134 |
|
| 135 |
def move_up(self, speed): |
| 136 |
''' |
| 137 |
Moves the shape upwards in the speed passed in arg speed. |
| 138 |
speed is the number of pixels to move per update. |
| 139 |
''' |
| 140 |
self._y -= speed |
| 141 |
self.shape_grid_update() |
| 142 |
|
| 143 |
def move_right(self): |
| 144 |
''' |
| 145 |
Moves the shape 1 step to the right. |
| 146 |
''' |
| 147 |
self._x += self.block_size |
| 148 |
self.shape_grid_update() |
| 149 |
|
| 150 |
def move_left(self): |
| 151 |
''' |
| 152 |
Moves the shape 1 step to the left |
| 153 |
''' |
| 154 |
self._x -= self.block_size |
| 155 |
self.shape_grid_update() |
| 156 |
|
| 157 |
def bottom(self): |
| 158 |
''' |
| 159 |
Gives the bottom pos of the first block in the first row. |
| 160 |
''' |
| 161 |
return self._y |
| 162 |
|
| 163 |
def left(self): |
| 164 |
''' |
| 165 |
Gives the left pos of the first block in the first row. |
| 166 |
''' |
| 167 |
x_pos = [] |
| 168 |
for row in self.piece: |
| 169 |
for element in row: |
| 170 |
if element is not None: |
| 171 |
x_pos.append(element.x) |
| 172 |
|
| 173 |
return min(x_pos) |
| 174 |
|
| 175 |
def right(self): |
| 176 |
''' |
| 177 |
Gives the right pos of the last block in the first row. |
| 178 |
''' |
| 179 |
x_pos = [] |
| 180 |
for row in self.piece: |
| 181 |
for element in row: |
| 182 |
if element is not None: |
| 183 |
x_pos.append(element.x) |
| 184 |
|
| 185 |
return max(x_pos) |
| 186 |
|
| 187 |
def top(self): |
| 188 |
''' |
| 189 |
Gives the top pos of the first block in the second row. |
| 190 |
''' |
| 191 |
|
| 192 |
return self._y + (self.block_size * 2) |
| 193 |
|
| 194 |
def gameboard_collision(self, gameboard): |
| 195 |
''' |
| 196 |
Check if this piece have collided with any other piece on the bottom. |
| 197 |
''' |
| 198 |
for row in self.piece: |
| 199 |
for element in row: |
| 200 |
if element is not None: |
| 201 |
if not gameboard.contains(element): |
| 202 |
if element.bottom > gameboard.bottom: |
| 203 |
return True |
| 204 |
elif self.right() >= gameboard.right: |
| 205 |
return True |
| 206 |
elif self.left() < gameboard.left: |
| 207 |
return True |
| 208 |
return False |
| 209 |
|
| 210 |
def shape_collision(self, other_pieces): |
| 211 |
''' |
| 212 |
Check if this piece have collided with any other piece on the bottom. |
| 213 |
''' |
| 214 |
|
| 215 |
|
| 216 |
for row in self.piece: |
| 217 |
for element in row: |
| 218 |
if element is not None: |
| 219 |
collisions = element.collidelistall(other_pieces) |
| 220 |
if collisions: |
| 221 |
return True |
| 222 |
return False |
| 223 |
|
| 224 |
def get_rect(self): |
| 225 |
''' |
| 226 |
Returns a list of rects |
| 227 |
''' |
| 228 |
rect_list = [] |
| 229 |
for row in self.piece: |
| 230 |
for element in row: |
| 231 |
if element is not None: |
| 232 |
rect_list.append(element) |
| 233 |
|
| 234 |
|
| 235 |
return rect_list |
| 236 |
|
| 237 |
def remove(self, block_list): |
| 238 |
''' |
| 239 |
Compare list of bricks against piece. If any match remove the entity. |
| 240 |
''' |
| 241 |
for block in block_list: |
| 242 |
for row in self.piece: |
| 243 |
try: |
| 244 |
row.remove(block) |
| 245 |
except ValueError: |
| 246 |
|
| 247 |
|
| 248 |
pass |
| 249 |
|
| 250 |
def __str__(self): |
| 251 |
return str(self.piece) |
| 252 |
|
| 253 |
|
| 254 |
class OShape(Shape): |
| 255 |
''' |
| 256 |
Shape: |
| 257 |
xx |
| 258 |
xx |
| 259 |
''' |
| 260 |
|
| 261 |
def __init__(self, x_pos, size, color): |
| 262 |
Shape.__init__(self, x_pos, size, color) |
| 263 |
self.piece = [[None, None, None, None, None, None], |
| 264 |
[None, None, None, None, None, None], |
| 265 |
[None, None, Rect((0, 0), (size, size)), |
| 266 |
Rect((0, 0), (size, size)), None, None], |
| 267 |
[None, None, Rect((0, 0), (size, size)), |
| 268 |
Rect((0, 0), (size, size)), None, None], |
| 269 |
[None, None, None, None, None, None], |
| 270 |
[None, None, None, None, None, None]] |
| 271 |
|
| 272 |
|
| 273 |
self.shape_grid_update() |
| 274 |
|
| 275 |
def pos_rotate(self): |
| 276 |
''' |
| 277 |
Try to rotate this one to victory is futile |
| 278 |
''' |
| 279 |
|
| 280 |
pass |
| 281 |
|
| 282 |
def neg_rotate(self): |
| 283 |
''' |
| 284 |
Try to rotate this one to victory is futile |
| 285 |
''' |
| 286 |
|
| 287 |
pass |
| 288 |
|
| 289 |
|
| 290 |
class LShape(Shape): |
| 291 |
''' |
| 292 |
Shape: |
| 293 |
x |
| 294 |
x |
| 295 |
xx |
| 296 |
''' |
| 297 |
|
| 298 |
def __init__(self, x_pos, size, color): |
| 299 |
Shape.__init__(self, x_pos, size, color) |
| 300 |
self.piece = [[None, None, None, None, None, None], |
| 301 |
[None, None, None, None, None, None], |
| 302 |
[None, None, Rect((0, 0), (size, size)), None, None, None], |
| 303 |
[None, None, Rect((0, 0), (size, size)), None, None, None], |
| 304 |
[None, None, Rect((0, 0), (size, size)), Rect((0, 0), (size, size)), None, None], |
| 305 |
[None, None, None, None, None, None]] |
| 306 |
|
| 307 |
|
| 308 |
self.shape_grid_update() |
| 309 |
|
| 310 |
|
| 311 |
class IShape(Shape): |
| 312 |
''' |
| 313 |
Shape: |
| 314 |
x |
| 315 |
x |
| 316 |
x |
| 317 |
x |
| 318 |
''' |
| 319 |
def __init__(self, x_pos, size, color): |
| 320 |
Shape.__init__(self, x_pos, size, color) |
| 321 |
self.piece = [[None, None, None, None, None, None], |
| 322 |
[None, None, Rect((0, 0), (size, size)), None, None, None], |
| 323 |
[None, None, Rect((0, 0), (size, size)), None, None, None], |
| 324 |
[None, None, Rect((0, 0), (size, size)), None, None, None], |
| 325 |
[None, None, Rect((0, 0), (size, size)), None, None, None], |
| 326 |
[None, None, None, None, None, None]] |
| 327 |
|
| 328 |
|
| 329 |
self.shape_grid_update() |
| 330 |
|
| 331 |
|
| 332 |
class ZShape(Shape): |
| 333 |
''' |
| 334 |
Shape: |
| 335 |
xx |
| 336 |
xx |
| 337 |
''' |
| 338 |
def __init__(self, x_pos, size, color): |
| 339 |
Shape.__init__(self, x_pos, size, color) |
| 340 |
self.piece = [[None, None, None, None, None, None], |
| 341 |
[None, None, None, None, None, None], |
| 342 |
[None, None, Rect((0, 0), (size, size)), Rect((0, 0), (size, size)), None, None], |
| 343 |
[None, None, None, Rect((0, 0), (size, size)), Rect((0, 0), (size, size)), None], |
| 344 |
[None, None, None, None, None, None], |
| 345 |
[None, None, None, None, None, None]] |
| 346 |
|
| 347 |
|
| 348 |
self.shape_grid_update() |
| 349 |
|
| 350 |
|
| 351 |
class SShape(Shape): |
| 352 |
''' |
| 353 |
Shape: |
| 354 |
xx |
| 355 |
xx |
| 356 |
''' |
| 357 |
def __init__(self, x_pos, size, color): |
| 358 |
Shape.__init__(self, x_pos, size, color) |
| 359 |
self.piece = [[None, None, None, None, None, None], |
| 360 |
[None, None, None, None, None, None], |
| 361 |
[None, None, None, Rect((0, 0), (size, size)), Rect((0, 0), (size, size)), None], |
| 362 |
[None, None, Rect((0, 0), (size, size)), Rect((0, 0), (size, size)), None, None], |
| 363 |
[None, None, None, None, None, None], |
| 364 |
[None, None, None, None, None, None]] |
| 365 |
|
| 366 |
|
| 367 |
self.shape_grid_update() |
| 368 |
|
| 369 |
|
| 370 |
class TShape(Shape): |
| 371 |
''' |
| 372 |
Shape: |
| 373 |
xxx |
| 374 |
x |
| 375 |
''' |
| 376 |
def __init__(self, x_pos, size, color): |
| 377 |
Shape.__init__(self, x_pos, size, color) |
| 378 |
self.piece = [[None, None, None, None, None, None], |
| 379 |
[None, None, None, None, None, None], |
| 380 |
[None, None, Rect((0, 0), (size, size)), |
| 381 |
Rect((0, 0), (size, size)), |
| 382 |
Rect((0, 0), (size, size)), None], |
| 383 |
[None, None, None, Rect((0, 0), (size, size)), None, None], |
| 384 |
[None, None, None, None, None, None], |
| 385 |
[None, None, None, None, None, None]] |
| 386 |
|
| 387 |
|
| 388 |
self.shape_grid_update() |
| 389 |
|
| 390 |
|
| 391 |
class JShape(Shape): |
| 392 |
''' |
| 393 |
Shape: |
| 394 |
x |
| 395 |
x |
| 396 |
xx |
| 397 |
''' |
| 398 |
def __init__(self, x_pos, size, color): |
| 399 |
Shape.__init__(self, x_pos, size, color) |
| 400 |
self.piece = [[None, None, None, None, None, None], |
| 401 |
[None, None, None, None, None, None], |
| 402 |
[None, None, Rect((0, 0), (size, size)), None, None, None], |
| 403 |
[None, None, Rect((0, 0), (size, size)), None, None, None], |
| 404 |
[None, Rect((0, 0), (size, size)), Rect((0, 0), (size, size)), |
| 405 |
None, None, None], |
| 406 |
[None, None, None, None, None, None]] |
| 407 |
|
| 408 |
|
| 409 |
self.shape_grid_update() |
| 410 |
|
| 411 |
|
| 412 |
class Bricks(object): |
| 413 |
''' |
| 414 |
There is the following kind of bricks |
| 415 |
1: O-shaped |
| 416 |
2: L-shaped |
| 417 |
3: I-shaped |
| 418 |
4: Z-shaped |
| 419 |
5: S-shaped |
| 420 |
6: T-shaped |
| 421 |
7: J-shaped |
| 422 |
|
| 423 |
There is the following list of colors. |
| 424 |
1: Red (0xFF, 0x0, 0x0) |
| 425 |
2: Yellow (0xF9, 0xFF, 0x0) |
| 426 |
3: Magenta (0xFF, 0x0, 0xFF) |
| 427 |
4: Blue (0x42, 0x0, 0xFF) |
| 428 |
5: Cyan (0x0, 0xFF, 0xFF) |
| 429 |
6: Lime (0x1A, 0xFF, 0x0) |
| 430 |
7: Orange (0xFF, 0xA1, 0x0) |
| 431 |
|
| 432 |
The Brick class will function as a bridge for all the shapes |
| 433 |
and as a container for bricks. |
| 434 |
''' |
| 435 |
def __init__(self, block_size): |
| 436 |
''' |
| 437 |
Valid data for parameter shape is: |
| 438 |
1, 2, 3, 4 |
| 439 |
''' |
| 440 |
self.block_size = block_size |
| 441 |
self.bricklist = [] |
| 442 |
|
| 443 |
|
| 444 |
self.shape_list = {1: OShape, 2: LShape, 3: IShape, 4: ZShape, |
| 445 |
5: SShape, 6: TShape, 7: JShape} |
| 446 |
|
| 447 |
self.color_palette = {1: (0xFF, 0x0, 0x0), 2: (0xF9, 0xFF, 0x0), |
| 448 |
3: (0xFF, 0x0, 0xFF), 4: (0x42, 0x0, 0xFF), |
| 449 |
5: (0x0, 0xFF, 0xFF), 6: (0x1A, 0xFF, 0x0), |
| 450 |
7: (0xFF, 0xA1, 0x0)} |
| 451 |
|
| 452 |
def new_brick(self, shape, color, pos, size): |
| 453 |
''' |
| 454 |
Adds a new brick to the list |
| 455 |
''' |
| 456 |
|
| 457 |
if shape not in self.shape_list: |
| 458 |
raise ValueError('Not in shape_list: {0}'.format(shape)) |
| 459 |
elif color not in self.color_palette: |
| 460 |
raise ValueError('Not in color_palette: {0}'.format(color)) |
| 461 |
else: |
| 462 |
self.bricklist.append(self.shape_list[shape](pos, size, self.color_palette[color])) |
| 463 |
|
| 464 |
@property |
| 465 |
def bottom(self): |
| 466 |
''' |
| 467 |
Returns the position of the bottom. |
| 468 |
''' |
| 469 |
return self.bricklist[-1].bottom() |
| 470 |
|
| 471 |
def move_list_down(self, move_list): |
| 472 |
''' |
| 473 |
Moves the list of blocks one step down. |
| 474 |
''' |
| 475 |
for block in move_list: |
| 476 |
block.y += self.block_size |
| 477 |
|
| 478 |
def move_active_down(self, speed): |
| 479 |
''' |
| 480 |
Move active brick down in a given speed |
| 481 |
''' |
| 482 |
self.bricklist[-1].move_down(speed) |
| 483 |
|
| 484 |
def move_active_up(self, speed): |
| 485 |
''' |
| 486 |
Move active brick up with a given speed |
| 487 |
''' |
| 488 |
self.bricklist[-1].move_up(speed) |
| 489 |
|
| 490 |
def move_active_right(self): |
| 491 |
''' |
| 492 |
Move brick right one step |
| 493 |
''' |
| 494 |
self.bricklist[-1].move_right() |
| 495 |
|
| 496 |
def move_active_left(self): |
| 497 |
''' |
| 498 |
Move active brick left one step |
| 499 |
''' |
| 500 |
self.bricklist[-1].move_left() |
| 501 |
|
| 502 |
def pos_rotate_active(self): |
| 503 |
''' |
| 504 |
Rotates active brick 90deg. |
| 505 |
''' |
| 506 |
self.bricklist[-1].pos_rotate() |
| 507 |
|
| 508 |
def neg_rotate_active(self): |
| 509 |
''' |
| 510 |
Rotates active brick -90deg. |
| 511 |
''' |
| 512 |
self.bricklist[-1].neg_rotate() |
| 513 |
|
| 514 |
def draw(self): |
| 515 |
''' |
| 516 |
Draw shape on to the screen |
| 517 |
''' |
| 518 |
for brick in self.bricklist: |
| 519 |
brick.draw() |
| 520 |
|
| 521 |
def gameboard_collision(self, gameboard): |
| 522 |
''' |
| 523 |
Check if Brick have hit gameboard bottom |
| 524 |
''' |
| 525 |
return self.bricklist[-1].gameboard_collision(gameboard) |
| 526 |
|
| 527 |
def shape_collision(self): |
| 528 |
''' |
| 529 |
other_pieces is expected to be a list of type Rect |
| 530 |
If piece have collided with other piece at bottom. |
| 531 |
returns true. |
| 532 |
''' |
| 533 |
for brick in self.bricklist[:-1]: |
| 534 |
if self.bricklist[-1].shape_collision(brick.get_rect()): |
| 535 |
return True |
| 536 |
|
| 537 |
return False |
| 538 |
|
| 539 |
def bounds_out(self, gameboard): |
| 540 |
''' |
| 541 |
Returns True if any of the active brick is out side of the |
| 542 |
gameboard when called. |
| 543 |
Else False |
| 544 |
''' |
| 545 |
block_list = self.bricklist[-1].get_rect() |
| 546 |
game_rect = gameboard.get_rect() |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
for block in block_list: |
| 551 |
if not game_rect.contains(block): |
| 552 |
return True |
| 553 |
|
| 554 |
return False |
| 555 |
|
| 556 |
def remove(self, block_list): |
| 557 |
''' |
| 558 |
If brick contains any of the bricks in list. It will remove them |
| 559 |
''' |
| 560 |
for brick in self.bricklist: |
| 561 |
brick.remove(block_list) |
| 562 |
|
| 563 |
def get_active_rect(self): |
| 564 |
''' |
| 565 |
Returns rectangle objects from brick. |
| 566 |
''' |
| 567 |
return self.bricklist[-1].get_rect() |
| 568 |
|
| 569 |
def get_rect_list(self): |
| 570 |
''' |
| 571 |
Helper method to compile a list of rects from a list of bricks |
| 572 |
''' |
| 573 |
rect_list = [] |
| 574 |
for brick in self.bricklist: |
| 575 |
rect_list.extend(brick.get_rect()) |
| 576 |
return rect_list |
| 577 |
|
| 578 |
|
| 579 |
class ScoreBoard(object): |
| 580 |
''' |
| 581 |
Shows the game the current score |
| 582 |
''' |
| 583 |
def __init__(self, pos): |
| 584 |
self.title = 'Score' |
| 585 |
self.pos_x, self.pos_y = pos |
| 586 |
self.current_score = 0 |
| 587 |
|
| 588 |
def add_to_score(self, points): |
| 589 |
''' |
| 590 |
Takes points as argument and adds it to the current score |
| 591 |
''' |
| 592 |
|
| 593 |
multiplier = points / 10 |
| 594 |
|
| 595 |
|
| 596 |
self.current_score += int(points * multiplier) |
| 597 |
|
| 598 |
def draw(self): |
| 599 |
''' |
| 600 |
Draws title and current score to surface |
| 601 |
''' |
| 602 |
screen.draw.text(self.title, |
| 603 |
(self.pos_x, self.pos_y), |
| 604 |
color=(0xDE, 0xAD, 0xFF), |
| 605 |
fontsize=20) |
| 606 |
screen.draw.text(str(self.current_score), |
| 607 |
(self.pos_x, self.pos_y + 20), |
| 608 |
color=(0x00, 0xAD, 0xFF), |
| 609 |
fontsize=20) |
| 610 |
|
| 611 |
|
| 612 |
class GameBoard(object): |
| 613 |
''' |
| 614 |
Implements the play area and is judge that decides what rectangles |
| 615 |
that should be removed and which dead bricks that should move down. |
| 616 |
''' |
| 617 |
def __init__(self, position, gameboard_size, block_size, color): |
| 618 |
pos_x, pos_y = position |
| 619 |
self._pos_x = pos_x |
| 620 |
self._pos_y = pos_y |
| 621 |
self.cols, self.rows = gameboard_size |
| 622 |
self.block_size = block_size |
| 623 |
width = self.cols * self.block_size |
| 624 |
height = self.rows * self.block_size |
| 625 |
self._board = Rect(position, (width, height)) |
| 626 |
self.color = color |
| 627 |
|
| 628 |
def center(self): |
| 629 |
''' |
| 630 |
Returns the centrum pos in the X axis. |
| 631 |
''' |
| 632 |
center = (self._board.width / 2) + self._pos_x |
| 633 |
return center |
| 634 |
|
| 635 |
def get_rect(self): |
| 636 |
''' |
| 637 |
Returns a Rect object of the GameBoard |
| 638 |
''' |
| 639 |
return self._board |
| 640 |
|
| 641 |
def draw(self): |
| 642 |
''' |
| 643 |
To be called when we want it to be drawn onto the screen surface. |
| 644 |
''' |
| 645 |
screen.draw.filled_rect(self._board, self.color) |
| 646 |
|
| 647 |
def ruler(self, rects): |
| 648 |
''' |
| 649 |
Takes a list of bricks. Returns a list of rects that should be removed |
| 650 |
from the first row that it encounters |
| 651 |
and a list of rects that should be moved down a row. |
| 652 |
|
| 653 |
If nothing to report, it returns a tuple of (None, None) |
| 654 |
''' |
| 655 |
|
| 656 |
blocks = rects |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
complete_row = self.cols |
| 661 |
|
| 662 |
|
| 663 |
ruler_rect = Rect((self._pos_x, self._board.bottom - self.block_size), |
| 664 |
(self._board.width, self.block_size)) |
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
other_rect = Rect((self._pos_x, self._pos_y), |
| 669 |
(self._board.width, self._board.height)) |
| 670 |
remove_list = [] |
| 671 |
move_down_list = [] |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
for _ in range(0, self.rows): |
| 677 |
|
| 678 |
other_rect.height -= self.block_size |
| 679 |
|
| 680 |
collision_indices = ruler_rect.collidelistall(blocks) |
| 681 |
move_down_indices = other_rect.collidelistall(blocks) |
| 682 |
if len(collision_indices) == complete_row: |
| 683 |
for index in collision_indices: |
| 684 |
remove_list.append(blocks[index]) |
| 685 |
for index in move_down_indices: |
| 686 |
move_down_list.append(blocks[index]) |
| 687 |
|
| 688 |
return (remove_list, move_down_list) |
| 689 |
|
| 690 |
|
| 691 |
ruler_rect.y -= self.block_size |
| 692 |
|
| 693 |
|
| 694 |
return (None, None) |
| 695 |
|
| 696 |
|
| 697 |
class Game(object): |
| 698 |
''' |
| 699 |
Game board, bricks and controls. |
| 700 |
''' |
| 701 |
def __init__(self, x, y, cols, rows): |
| 702 |
''' |
| 703 |
''' |
| 704 |
|
| 705 |
self.block_size = 20 |
| 706 |
|
| 707 |
self._xpos = x |
| 708 |
self._ypos = y |
| 709 |
self._cols = cols |
| 710 |
self._rows = rows |
| 711 |
|
| 712 |
self.width = cols * self.block_size |
| 713 |
self.height = rows * self.block_size |
| 714 |
self.gameboard = GameBoard((x, y), (cols, rows), self.block_size, (0x00, 0x00, 0x00)) |
| 715 |
|
| 716 |
|
| 717 |
margin = 10 |
| 718 |
self.scoreboard = ScoreBoard((self._xpos + self.width + margin, |
| 719 |
self._ypos)) |
| 720 |
|
| 721 |
|
| 722 |
self.bricks = Bricks(self.block_size) |
| 723 |
|
| 724 |
self.speed = 1 |
| 725 |
|
| 726 |
|
| 727 |
self.game_active = True |
| 728 |
self.game_pause = False |
| 729 |
|
| 730 |
self.new_brick() |
| 731 |
|
| 732 |
def new_brick(self): |
| 733 |
''' |
| 734 |
Creates a new brick when previous get locked in place |
| 735 |
''' |
| 736 |
self.bricks.new_brick(random.randint(1, 7), |
| 737 |
random.randint(1, 7), |
| 738 |
self.gameboard.center(), |
| 739 |
self.block_size) |
| 740 |
|
| 741 |
def draw(self): |
| 742 |
''' |
| 743 |
Draw game |
| 744 |
''' |
| 745 |
self.gameboard.draw() |
| 746 |
self.bricks.draw() |
| 747 |
self.scoreboard.draw() |
| 748 |
|
| 749 |
if not self.game_active: |
| 750 |
screen.draw.text('Game Over', (40, 100), color=(0xDE, 0xAD, 0xFF), fontsize=80) |
| 751 |
|
| 752 |
def remove_complete_rows(self): |
| 753 |
''' |
| 754 |
For every complete row, request to remove it. |
| 755 |
The request is sent to each brick in the game. |
| 756 |
''' |
| 757 |
points = 0 |
| 758 |
|
| 759 |
while True: |
| 760 |
remove_list, move_list = self.gameboard.ruler(self.bricks.get_rect_list()) |
| 761 |
if remove_list: |
| 762 |
self.bricks.remove(remove_list) |
| 763 |
points += len(remove_list) |
| 764 |
|
| 765 |
if move_list: |
| 766 |
self.bricks.move_list_down(move_list) |
| 767 |
|
| 768 |
else: |
| 769 |
break |
| 770 |
|
| 771 |
|
| 772 |
self.scoreboard.add_to_score(points) |
| 773 |
|
| 774 |
def toggle_pause(self): |
| 775 |
''' |
| 776 |
Pause/Un-pause the game |
| 777 |
''' |
| 778 |
self.game_pause = not self.game_pause |
| 779 |
|
| 780 |
def proceed(self): |
| 781 |
''' |
| 782 |
Is called to change the state of the game. |
| 783 |
''' |
| 784 |
|
| 785 |
if self.found_collision(): |
| 786 |
self.bricks.move_active_up(self.speed) |
| 787 |
self.remove_complete_rows() |
| 788 |
|
| 789 |
if self.bricks.bounds_out(self.gameboard): |
| 790 |
|
| 791 |
self.game_active = False |
| 792 |
raise GameOver |
| 793 |
else: |
| 794 |
self.new_brick() |
| 795 |
|
| 796 |
else: |
| 797 |
self.bricks.move_active_down(self.speed) |
| 798 |
|
| 799 |
def found_collision(self): |
| 800 |
''' |
| 801 |
Check if active brick have collided with anything. |
| 802 |
''' |
| 803 |
if self.bricks.gameboard_collision(self.gameboard.get_rect()): |
| 804 |
return True |
| 805 |
elif self.bricks.shape_collision(): |
| 806 |
return True |
| 807 |
else: |
| 808 |
return False |
| 809 |
|
| 810 |
def controls(self, direction): |
| 811 |
''' |
| 812 |
Will be called for a given intervall. This prevents the blocks from |
| 813 |
flying from left to right in high speed |
| 814 |
''' |
| 815 |
|
| 816 |
|
| 817 |
if direction == 'right': |
| 818 |
self.bricks.move_active_right() |
| 819 |
elif direction == 'left': |
| 820 |
self.bricks.move_active_left() |
| 821 |
elif direction == 'down': |
| 822 |
self.bricks.move_active_down(self.speed * 10) |
| 823 |
elif direction == 'pos_rotate': |
| 824 |
self.bricks.pos_rotate_active() |
| 825 |
elif direction == 'neg_rotate': |
| 826 |
self.bricks.neg_rotate_active() |
| 827 |
|
| 828 |
|
| 829 |
if self.found_collision(): |
| 830 |
if direction == 'right': |
| 831 |
self.bricks.move_active_left() |
| 832 |
elif direction == 'left': |
| 833 |
self.bricks.move_active_right() |
| 834 |
elif direction == 'down': |
| 835 |
self.bricks.move_active_up(self.speed * 10) |
| 836 |
elif direction == 'pos_rotate': |
| 837 |
self.bricks.neg_rotate_active() |
| 838 |
elif direction == 'neg_rotate': |
| 839 |
self.bricks.pos_rotate_active() |
| 840 |
|
| 841 |
def reset(self): |
| 842 |
''' |
| 843 |
Restarts the game |
| 844 |
''' |
| 845 |
Game.__init__(self, self._xpos, self._ypos, self._cols, self._rows) |
| 846 |
|
| 847 |
|
| 848 |
WIDTH = 500 |
| 849 |
HEIGHT = 800 |
| 850 |
|
| 851 |
music.set_volume(0.5) |
| 852 |
music.play('hhavok-main') |
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
TETRA_PUZZLE = Game(100, 50, 10, 20) |
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
SPEEDOMETER_LEFT = 0 |
| 861 |
SPEEDOMETER_RIGHT = 0 |
| 862 |
|
| 863 |
def control_tick(): |
| 864 |
''' |
| 865 |
Gets triggerad by clock. |
| 866 |
Tells the game betris what the user want to do, if any. |
| 867 |
''' |
| 868 |
global SPEEDOMETER_RIGHT |
| 869 |
global SPEEDOMETER_LEFT |
| 870 |
|
| 871 |
if TETRA_PUZZLE.game_active: |
| 872 |
if keyboard.space: |
| 873 |
TETRA_PUZZLE.controls('down') |
| 874 |
elif keyboard.right: |
| 875 |
if SPEEDOMETER_LEFT: |
| 876 |
SPEEDOMETER_LEFT = 0 |
| 877 |
SPEEDOMETER_RIGHT += 0.01 |
| 878 |
TETRA_PUZZLE.controls('right') |
| 879 |
elif keyboard.left: |
| 880 |
if SPEEDOMETER_RIGHT: |
| 881 |
SPEEDOMETER_RIGHT = 0 |
| 882 |
SPEEDOMETER_LEFT += 0.01 |
| 883 |
TETRA_PUZZLE.controls('left') |
| 884 |
|
| 885 |
def on_key_down(key): |
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
if key is keys.R: |
| 891 |
TETRA_PUZZLE.reset() |
| 892 |
elif key is keys.P: |
| 893 |
TETRA_PUZZLE.toggle_pause() |
| 894 |
elif key is keys.UP: |
| 895 |
TETRA_PUZZLE.controls('pos_rotate') |
| 896 |
elif key is keys.DOWN: |
| 897 |
TETRA_PUZZLE.controls('neg_rotate') |
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
clock.schedule_interval(control_tick, (0.05 - SPEEDOMETER_RIGHT) - SPEEDOMETER_LEFT) |
| 902 |
|
| 903 |
def update(): |
| 904 |
''' |
| 905 |
Update block position. |
| 906 |
''' |
| 907 |
if TETRA_PUZZLE.game_active and not TETRA_PUZZLE.game_pause: |
| 908 |
try: |
| 909 |
TETRA_PUZZLE.proceed() |
| 910 |
except GameOver: |
| 911 |
draw() |
| 912 |
|
| 913 |
def draw(): |
| 914 |
''' |
| 915 |
Draw game |
| 916 |
''' |
| 917 |
screen.fill((0xFF, 0xFF, 0xFF)) |
| 918 |
TETRA_PUZZLE.draw() |