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 535 You modify a rectangle (such as self.rect in this case) by assigning to its attributes (top, bottom, left, right, topleft, topright, bottomleft, bottomright, size, width, height, center, centerx, centery, midleft, midright, midtop, midbottom) or calling methods such as inflate or move. (These are all described in the Pygame documentation at http://pygame.org/docs/ref/ Rect.html). Now that the Pygame technicalities are in place, it’s time to extend and refactor your game logic a bit. Second Implementation In this section, instead of walking you through the design and implementation step by step, I have added copious comments and docstrings to the source code (shown in Listings 29-2 through 29-4 at the end of this section). You can examine the source (“use the source,” remember?) to see how it works, but here is a short rundown of the essentials (and some not-quite-intuitive particulars): • The game consists of five files: config.py, which contains various configuration variables; objects.py, which contains the implementations of the game objects; squish.py, which contains the main Game class and the various game state classes; and weight.png and banana.png, the two images used in the game. • The rectangle method clamp ensures that a rectangle is placed within another rectangle, moving it if necessary. This is used to ensure that the banana doesn’t move off-screen. • The rectangle method inflate resizes (inflates) a rectangle by a given number of pixels in the horizontal and vertical direction. This is used to shrink the banana boundary, to allow some overlap between the banana and the weight before a hit (or “squish”) is registered. • The game itself consists of a game object and various game states. The game object only has one state at a time, and the state is responsible for handling events and displaying itself on the screen. The states may also tell the game to switch to another state. (A Level state may, for example, tell the game to switch to a GameOver state.) That’s it. You may run the game by executing the squish.py file, as follows: $ python squish.py You should make sure that the other files are in the same directory. In Windows, you can simply double-click the squish.py file. ■Tip If you rename squish.py to squish.pyw, double-clicking it in Windows won’t pop up a gratuitous terminal window. If you want to put the game on your desktop (or somewhere else) without moving all the modules and image files along with it, simply create a shortcut to the squish.pyw file. See also Chapter 18 for details on packaging your game.
536 CHAPTER 29 ■ PROJECT 10: DO-IT-YOURSELF ARCADE GAME Some screenshots of the game are shown in Figures 29-3 through 29-6. Figure 29-3. The Squish opening screen Figure 29-4. A banana about to be squished Figure 29-5. The “level cleared” screen Figure 29-6. The “game over” screen Listing 29-2. The Squish Configuration File (config.py) # Configuration file for Squish # ----------------------------- # Feel free to modify the configuration variables below to taste. # If the game is too fast or too slow, try to modify the speed # variables. # Change these to use other images in the game: banana_image = 'banana.png' weight_image = 'weight.png' splash_image = 'weight.png'
- Page 515 and 516: 484 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 517 and 518: 486 CHAPTER 26 ■ PROJECT 7: YOUR
- 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 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 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
CHAPTER 29 ■ PROJECT 10: DO-IT-YOURSELF ARCADE GAME 535<br />
You modify a rectangle (such as self.rect in this case) by assigning <strong>to</strong> its attributes (<strong>to</strong>p,<br />
bot<strong>to</strong>m, left, right, <strong>to</strong>pleft, <strong>to</strong>pright, bot<strong>to</strong>mleft, bot<strong>to</strong>mright, size, width, height, center,<br />
centerx, centery, midleft, midright, mid<strong>to</strong>p, midbot<strong>to</strong>m) or calling methods such as inflate or<br />
move. (These are all described in the Pygame documentation at http://pygame.org/docs/ref/<br />
Rect.html).<br />
Now that the Pygame technicalities are in place, it’s time <strong>to</strong> extend and refac<strong>to</strong>r your game<br />
logic a bit.<br />
Second Implementation<br />
In this section, instead of walking you through the design and implementation step by step, I<br />
have added copious comments and docstrings <strong>to</strong> the source code (shown in Listings 29-2<br />
through 29-4 at the end of this section). You can examine the source (“use the source,” remember?)<br />
<strong>to</strong> see how it works, but here is a short rundown of the essentials (and some not-quite-intuitive<br />
particulars):<br />
• The game consists of five files: config.py, which contains various configuration variables;<br />
objects.py, which contains the implementations of the game objects; squish.py,<br />
which contains the main Game class and the various game state classes; and weight.png<br />
and banana.png, the two images used in the game.<br />
• The rectangle method clamp ensures that a rectangle is placed within another rectangle,<br />
moving it if necessary. This is used <strong>to</strong> ensure that the banana doesn’t move off-screen.<br />
• The rectangle method inflate resizes (inflates) a rectangle by a given number of pixels<br />
in the horizontal and vertical direction. This is used <strong>to</strong> shrink the banana boundary, <strong>to</strong><br />
allow some overlap between the banana and the weight before a hit (or “squish”) is<br />
registered.<br />
• The game itself consists of a game object and various game states. The game object only<br />
has one state at a time, and the state is responsible for handling events and displaying<br />
itself on the screen. The states may also tell the game <strong>to</strong> switch <strong>to</strong> another state. (A Level<br />
state may, for example, tell the game <strong>to</strong> switch <strong>to</strong> a GameOver state.)<br />
That’s it. You may run the game by executing the squish.py file, as follows:<br />
$ python squish.py<br />
You should make sure that the other files are in the same direc<strong>to</strong>ry. In Windows, you can<br />
simply double-click the squish.py file.<br />
■Tip If you rename squish.py <strong>to</strong> squish.pyw, double-clicking it in Windows won’t pop up a gratui<strong>to</strong>us<br />
terminal window. If you want <strong>to</strong> put the game on your desk<strong>to</strong>p (or somewhere else) without moving all the<br />
modules and image files along with it, simply create a shortcut <strong>to</strong> the squish.pyw file. See also Chapter 18<br />
for details on packaging your game.