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 543 # Display all the changes: pygame.display.flip() class Info(Paused): """ A simple paused state that displays some information about the game. It is followed by a Level state (the first level). """ nextState = Level text = ''' In this game you are a banana, trying to survive a course in self-defense against fruit, where the participants will "defend" themselves against you with a 16 ton weight.''' class StartUp(Paused): """ A paused state that displays a splash image and a welcome message. It is followed by an Info state. """ nextState = Info image = config.splash_image text = ''' Welcome to Squish, the game of Fruit Self-Defense''' class LevelCleared(Paused): """ A paused state that informs the user that he or she has cleared a given level. It is followed by the next level state. """ def __init__(self, number): self.number = number self.text = '''Level %i cleared Click to start next level''' % self.number
544 CHAPTER 29 ■ PROJECT 10: DO-IT-YOURSELF ARCADE GAME def nextState(self): return Level(self.number+1) class GameOver(Paused): """ A state that informs the user that he or she has lost the game. It is followed by the first level. """ nextState = Level text = ''' Game Over Click to Restart, Esc to Quit''' class Game: """ A game object that takes care of the main event loop, including changing between the different game states. """ def __init__(self, *args): # Get the directory where the game and the images are located: path = os.path.abspath(args[0]) dir = os.path.split(path)[0] # Move to that directory (so that the images files may be # opened later on): os.chdir(dir) # Start with no state: self.state = None # Move to StartUp in the first event loop iteration: self.nextState = StartUp() def run(self): """ This method sets things in motion. It performs some vital initialization tasks, and enters the main event loop. """ pygame.init() # This is needed to initialize all the pygame modules # Decide whether to display the game in a window or to use the # full screen: flag = 0 # Default (window) mode
- 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 572 and 573: 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
- Page 622 and 623: ■INDEX 591 nesting blocks 88 Netw
CHAPTER 29 ■ PROJECT 10: DO-IT-YOURSELF ARCADE GAME 543<br />
# Display all the changes:<br />
pygame.display.flip()<br />
class Info(Paused):<br />
"""<br />
A simple paused state that displays some information about the<br />
game. It is followed by a Level state (the first level).<br />
"""<br />
nextState = Level<br />
text = '''<br />
In this game you are a banana,<br />
trying <strong>to</strong> survive a course in<br />
self-defense against fruit, where the<br />
participants will "defend" themselves<br />
against you with a 16 <strong>to</strong>n weight.'''<br />
class StartUp(Paused):<br />
"""<br />
A paused state that displays a splash image and a welcome<br />
message. It is followed by an Info state.<br />
"""<br />
nextState = Info<br />
image = config.splash_image<br />
text = '''<br />
Welcome <strong>to</strong> Squish,<br />
the game of Fruit Self-Defense'''<br />
class LevelCleared(Paused):<br />
"""<br />
A paused state that informs the user that he or she has cleared a<br />
given level. It is followed by the next level state.<br />
"""<br />
def __init__(self, number):<br />
self.number = number<br />
self.text = '''Level %i cleared<br />
Click <strong>to</strong> start next level''' % self.number