Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 29 ■ PROJECT 10: DO-IT-YOURSELF ARCADE GAME 539 """ self.rect.centerx = pygame.mouse.get_pos()[0] self.rect = self.rect.clamp(self.area) def touches(self, other): """ Determines whether the banana touches another sprite (e.g., a Weight). Instead of just using the rect method colliderect, a new rectangle is first calculated (using the rect method inflate with the side and top paddings) that does not include the 'empty' areas on the top and sides of the banana. """ # Deflate the bounds with the proper padding: bounds = self.rect.inflate(-self.pad_side, -self.pad_top) # Move the bounds so they are placed at the bottom of the Banana: bounds.bottom = self.rect.bottom # Check whether the bounds intersect with the other object's rect: return bounds.colliderect(other.rect) Listing 29-4. The Main Game Module (squish.py) import os, sys, pygame from pygame.locals import * import objects, config "This module contains the main game logic of the Squish game." class State: """ A generic game state class that can handle events and display itself on a given surface. """ def handle(self, event): """ Default event handling only deals with quitting. """ if event.type == QUIT: sys.exit() if event.type == KEYDOWN and event.key == K_ESCAPE: sys.exit() def firstDisplay(self, screen): """ Used to display the State for the first time. Fills the screen with the background color. """
540 CHAPTER 29 ■ PROJECT 10: DO-IT-YOURSELF ARCADE GAME screen.fill(config.background_color) # Remember to call flip, to make the changes visible: pygame.display.flip() def display(self, screen): """ Used to display the State after it has already been displayed once. The default behavior is to do nothing. """ pass class Level(State): """ A game level. Takes care of counting how many weights have been dropped, moving the sprites around, and other tasks relating to game logic. """ def __init__(self, number=1): self.number = number # How many weights remain to dodge in this level? self.remaining = config.weights_per_level speed = config.drop_speed # One speed_increase added for each level above 1: speed += (self.number-1) * config.speed_increase # Create the weight and banana: self.weight = weight = objects.Weight(speed) self.banana = banana = objects.Banana() both = self.weight, self.banana # This could contain more sprites... self.sprites = pygame.sprite.RenderUpdates(both) def update(self, game): "Updates the game state from the previous frame." # Update all sprites: self.sprites.update() # If the banana touches the weight, tell the game to switch to # a GameOver state: if self.banana.touches(self.weight): game.nextState = GameOver() # Otherwise, if the weight has landed, reset it. If all the # weights of this level have been dodged, tell the game to # switch to a LevelCleared state: elif self.weight.landed: self.weight.reset()
- Page 519 and 520: 488 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 521 and 522: 490 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 523 and 524: 492 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 525 and 526: 494 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 527 and 528: 496 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 529 and 530: 498 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 531 and 532: 500 CHAPTER 27 ■ PROJECT 8: FILE
- Page 533 and 534: 502 CHAPTER 27 ■ PROJECT 8: FILE
- Page 535 and 536: 504 CHAPTER 27 ■ PROJECT 8: FILE
- Page 537 and 538: 506 CHAPTER 27 ■ PROJECT 8: FILE
- Page 539 and 540: 508 CHAPTER 27 ■ PROJECT 8: FILE
- Page 541 and 542: 510 CHAPTER 27 ■ PROJECT 8: FILE
- Page 543 and 544: 512 CHAPTER 27 ■ PROJECT 8: FILE
- Page 545 and 546: 514 CHAPTER 27 ■ PROJECT 8: FILE
- Page 547 and 548: 516 CHAPTER 27 ■ PROJECT 8: FILE
- Page 549 and 550: 518 CHAPTER 28 ■ PROJECT 9: FILE
- Page 551 and 552: 520 CHAPTER 28 ■ PROJECT 9: FILE
- Page 553 and 554: 522 CHAPTER 28 ■ PROJECT 9: FILE
- Page 555 and 556: 524 CHAPTER 28 ■ PROJECT 9: FILE
- Page 558 and 559: CHAPTER 29 ■ ■ ■ Project 10:
- Page 560 and 561: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 562 and 563: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 564 and 565: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 566 and 567: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 568 and 569: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 572 and 573: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 574 and 575: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 576 and 577: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 578 and 579: APPENDIX A ■ ■ ■ The Short Ve
- Page 580 and 581: APPENDIX A ■ THE SHORT VERSION 54
- Page 582 and 583: APPENDIX A ■ THE SHORT VERSION 55
- Page 584 and 585: APPENDIX A ■ THE SHORT VERSION 55
- Page 586: APPENDIX A ■ THE SHORT VERSION 55
- Page 589 and 590: 558 APPENDIX B ■ PYTHON REFERENCE
- Page 591 and 592: 560 APPENDIX B ■ PYTHON REFERENCE
- Page 593 and 594: 562 APPENDIX B ■ PYTHON REFERENCE
- Page 595 and 596: 564 APPENDIX B ■ PYTHON REFERENCE
- Page 597 and 598: 566 APPENDIX B ■ PYTHON REFERENCE
- Page 599 and 600: 568 APPENDIX B ■ PYTHON REFERENCE
- Page 601 and 602: 570 APPENDIX B ■ PYTHON REFERENCE
- Page 603 and 604: 572 APPENDIX C ■ ONLINE RESOURCES
- Page 606 and 607: Index ■Symbols - operator 558 !=
- Page 608 and 609: ■INDEX 577 assertEqual method Tes
- Page 610 and 611: ■INDEX 579 cmd module 252, 501 Cm
- Page 612 and 613: ■INDEX 581 get method 74-75 has_k
- Page 614 and 615: ■INDEX 583 ■F Factory class twi
- Page 616 and 617: ■INDEX 585 finding conflicts 197
- Page 618 and 619: ■INDEX 587 initialization 38 nami
540 CHAPTER 29 ■ PROJECT 10: DO-IT-YOURSELF ARCADE GAME<br />
screen.fill(config.background_color)<br />
# Remember <strong>to</strong> call flip, <strong>to</strong> make the changes visible:<br />
pygame.display.flip()<br />
def display(self, screen):<br />
"""<br />
Used <strong>to</strong> display the State after it has already been displayed<br />
once. The default behavior is <strong>to</strong> do nothing.<br />
"""<br />
pass<br />
class Level(State):<br />
"""<br />
A game level. Takes care of counting how many weights have been<br />
dropped, moving the sprites around, and other tasks relating <strong>to</strong><br />
game logic.<br />
"""<br />
def __init__(self, number=1):<br />
self.number = number<br />
# How many weights remain <strong>to</strong> dodge in this level?<br />
self.remaining = config.weights_per_level<br />
speed = config.drop_speed<br />
# One speed_increase added for each level above 1:<br />
speed += (self.number-1) * config.speed_increase<br />
# Create the weight and banana:<br />
self.weight = weight = objects.Weight(speed)<br />
self.banana = banana = objects.Banana()<br />
both = self.weight, self.banana # This could contain more sprites...<br />
self.sprites = pygame.sprite.RenderUpdates(both)<br />
def update(self, game):<br />
"Updates the game state from the previous frame."<br />
# Update all sprites:<br />
self.sprites.update()<br />
# If the banana <strong>to</strong>uches the weight, tell the game <strong>to</strong> switch <strong>to</strong><br />
# a GameOver state:<br />
if self.banana.<strong>to</strong>uches(self.weight):<br />
game.nextState = GameOver()<br />
# Otherwise, if the weight has landed, reset it. If all the<br />
# weights of this level have been dodged, tell the game <strong>to</strong><br />
# switch <strong>to</strong> a LevelCleared state:<br />
elif self.weight.landed:<br />
self.weight.reset()