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 541 self.remaining -= 1 if self.remaining == 0: game.nextState = LevelCleared(self.number) def display(self, screen): """ Displays the state after the first display (which simply wipes the screen). As opposed to firstDisplay, this method uses pygame.display.update with a list of rectangles that need to be updated, supplied from self.sprites.draw. """ screen.fill(config.background_color) updates = self.sprites.draw(screen) pygame.display.update(updates) class Paused(State): """ A simple, paused game state, which may be broken out of by pressing either a keyboard key or the mouse button. """ finished = 0 # Has the user ended the pause? image = None # Set this to a file name if you want an image text = '' # Set this to some informative text def handle(self, event): """ Handles events by delegating to State (which handles quitting in general) and by reacting to key presses and mouse clicks. If a key is pressed or the mouse is clicked, self.finished is set to true. """ State.handle(self, event) if event.type in [MOUSEBUTTONDOWN, KEYDOWN]: self.finished = 1 def update(self, game): """ Update the level. If a key has been pressed or the mouse has been clicked (i.e., self.finished is true), tell the game to move to the state represented by self.nextState() (should be implemented by subclasses). """ if self.finished: game.nextState = self.nextState()
542 CHAPTER 29 ■ PROJECT 10: DO-IT-YOURSELF ARCADE GAME def firstDisplay(self, screen): """ The first time the Paused state is displayed, draw the image (if any) and render the text. """ # First, clear the screen by filling it with the background color: screen.fill(config.background_color) # Create a Font object with the default appearance, and specified size: font = pygame.font.Font(None, config.font_size) # Get the lines of text in self.text, ignoring empty lines at # the top or bottom: lines = self.text.strip().splitlines() # Calculate the height of the text (using font.get_linesize() # to get the height of each line of text): height = len(lines) * font.get_linesize() # Calculate the placement of the text (centered on the screen): center, top = screen.get_rect().center top -= height // 2 # If there is an image to display... if self.image: # load it: image = pygame.image.load(self.image).convert() # get its rect: r = image.get_rect() # move the text down by half the image height: top += r.height // 2 # place the image 20 pixels above the text: r.midbottom = center, top - 20 # blit the image to the screen: screen.blit(image, r) antialias = 1 # Smooth the text black = 0, 0, 0 # Render it as black # Render all the lines, starting at the calculated top, and # move down font.get_linesize() pixels for each line: for line in lines: text = font.render(line.strip(), antialias, black) r = text.get_rect() r.midtop = center, top screen.blit(text, r) top += font.get_linesize()
- 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 570 and 571: 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
- Page 620 and 621: ■INDEX 589 localtime function tim
542 CHAPTER 29 ■ PROJECT 10: DO-IT-YOURSELF ARCADE GAME<br />
def firstDisplay(self, screen):<br />
"""<br />
The first time the Paused state is displayed, draw the image<br />
(if any) and render the text.<br />
"""<br />
# First, clear the screen by filling it with the background color:<br />
screen.fill(config.background_color)<br />
# Create a Font object with the default appearance, and specified size:<br />
font = pygame.font.Font(None, config.font_size)<br />
# Get the lines of text in self.text, ignoring empty lines at<br />
# the <strong>to</strong>p or bot<strong>to</strong>m:<br />
lines = self.text.strip().splitlines()<br />
# Calculate the height of the text (using font.get_linesize()<br />
# <strong>to</strong> get the height of each line of text):<br />
height = len(lines) * font.get_linesize()<br />
# Calculate the placement of the text (centered on the screen):<br />
center, <strong>to</strong>p = screen.get_rect().center<br />
<strong>to</strong>p -= height // 2<br />
# If there is an image <strong>to</strong> display...<br />
if self.image:<br />
# load it:<br />
image = pygame.image.load(self.image).convert()<br />
# get its rect:<br />
r = image.get_rect()<br />
# move the text down by half the image height:<br />
<strong>to</strong>p += r.height // 2<br />
# place the image 20 pixels above the text:<br />
r.midbot<strong>to</strong>m = center, <strong>to</strong>p - 20<br />
# blit the image <strong>to</strong> the screen:<br />
screen.blit(image, r)<br />
antialias = 1 # Smooth the text<br />
black = 0, 0, 0 # Render it as black<br />
# Render all the lines, starting at the calculated <strong>to</strong>p, and<br />
# move down font.get_linesize() pixels for each line:<br />
for line in lines:<br />
text = font.render(line.strip(), antialias, black)<br />
r = text.get_rect()<br />
r.mid<strong>to</strong>p = center, <strong>to</strong>p<br />
screen.blit(text, r)<br />
<strong>to</strong>p += font.get_linesize()