You are previewing More Python® Programming for the Absolute Beginner.
O'Reilly logo
More Python® Programming for the Absolute Beginner

Book Description

What better way is there to learn a programming language than with a game-oriented approach? If you ask the many readers that have made this book's prequel, PYTHON PROGRAMMING FOR THE ABSOLUTE BEGINNER, a bestseller, they'll tell you - there isn't one. MORE PYTHON PROGRAMMING FOR THE ABSOLUTE BEGINNER offers readers more practice, more exercises, and slightly more advanced instruction in Python programming, all while using the game-focused examples and projects that have proven to be both effective and fun. It picks up where its prequel leaves off, addressing data structures, file handling, exceptions, object oriented programming, GUI programming, multimedia programming, name spaces, and program planning. Following a deliberate, logical progression of topics that cover increasingly complex subject matter, this is a powerful resource that will arm readers with an in-depth knowledge of the Python language.

Table of Contents

  1. Copyright
    1. Dedication
  2. Acknowledgments
  3. About the Author
  4. Introduction
    1. Chapters
      1. 1. Python Has Class
      2. 2. Getting Started with Pygame: The Pie Game
      3. 3. File I/O, Data, and Fonts: The Trivia Game
      4. 4. User Input: The Bomb Catcher Game
      5. 5. Math and Graphics: The Analog Clock Demo
      6. 6. Bitmap Graphics: The Orbiting Spaceship Demo
      7. 7. Animation with Sprites: The Escape the Dragon Game
      8. 8. Sprite Collision Detection: The Zombie Mob Game
      9. 9. Arrays, Lists, and Tuples: The Block Breaker Game
      10. 10. Timing and Sound: The Oil Spill Game
      11. 11. Program Logic: The Snake Game
      12. 12. Trigonometry: The Tank Battle Game
      13. 13. Random Terrain: The Artillery Gunner Game
      14. 14. More of Everything: The Dungeon Role–Playing Game
      15. Appendix A: Installing Python and Pygame
      16. Appendix B: Pygame Key Codes
    2. Book Resources
    3. Styles
  5. 1. Python Has Class
    1. Examining the Geometry Program
    2. Getting Started with Python
      1. Python Tools
        1. Module Docs (Pydoc)
        2. Python Manuals (Pyhelp)
        3. Python (command line)
        4. IDLE (Python GUI)
      2. Python Language
    3. Objects in Python
      1. What Came Before?
        1. Structured Programming
        2. Sequential Programming
        3. Mnemonic Programming
      2. What’s Coming Next?
      3. OOP: The Python Way
        1. Polymorphism
        2. Data Hiding (Encapsulation)
        3. Inheritance
      4. Single Inheritance
      5. Multiple Inheritance
    4. Summary
  6. 2. Getting Started with Pygame: The Pie Game
    1. Examining the Pie Game
    2. Using Pygame
      1. Printing Text
      2. Looping
      3. Drawing Circles
      4. Drawing Rectangles
      5. Drawing Lines
      6. Drawing Arcs
    3. The Pie Game
    4. Summary
  7. 3. File I/O, Data, and Fonts: The Trivia Game
    1. Examining the Trivia Game
    2. Python Data Types
      1. More Printing
      2. Getting User Input
      3. Handling Exceptions
      4. The Mad Lib Game
    3. File Input/Output
      1. Working with Text Files
        1. Writing to a Text File
        2. Reading from a Text File
      2. Working with Binary Files
        1. Writing to a Binary File
        2. Reading from a Binary File
    4. The Trivia Game
      1. Printing Text with Pygame
      2. The Trivia Class
      3. Loading the Trivia Data
      4. Displaying the Question and Answers
      5. Responding to User Input
      6. Going to the Next Question
      7. Main Code
    5. Summary
  8. 4. User Input: The Bomb Catcher Game
    1. Examining the Bomb Catcher Game
    2. Pygame Events
      1. Real-Time Event Loop
      2. Keyboard Events
      3. Mouse Events
    3. Device Polling
      1. Polling the Keyboard
      2. Polling the Mouse
    4. The Bomb Catcher Game
    5. Summary
  9. 5. Math and Graphics: The Analog Clock Demo
    1. Examining the Analog Clock Demo
    2. Basic Trigonometry
      1. Circle Theory
      2. Traversing a Circle’s Perimeter
        1. Calculating X
        2. Calculating Y
      3. Circle Demo
    3. The Analog Clock Demo
      1. Getting the Time
      2. Drawing the Clock
        1. Numbers
        2. Hours
        3. Minutes
        4. Seconds
        5. Finished Code Listing
    4. Summary
  10. 6. Bitmap Graphics: The Orbiting Spaceship Demo
    1. Examining the Orbiting Spaceship Demo
    2. Using Bitmaps
      1. Loading a Bitmap
      2. Drawing the Background
      3. Drawing the Planet
      4. Drawing the Spaceship
    3. Orbiting the Planet
      1. Orbiting
      2. Rotating
    4. Summary
  11. 7. Animation with Sprites: The Escape the Dragon Game
    1. Examining the Escape The Dragon Game
    2. Using Pygame Sprites
      1. Custom Animation
      2. Loading a Sprite Sheet
      3. Changing the Frame
      4. Drawing One Frame
      5. Sprite Groups
      6. MySprite Class
      7. Sprite Animation to the Test
    3. The Escape the Dragon Game
      1. Jumping
      2. Colliding
      3. Source Code
    4. Summary
  12. 8. Sprite Collision Detection: The Zombie Mob Game
    1. Examining the Zombie Mob Game
    2. Collision Detection Techniques
      1. Rectangle Collision Between Two Sprites
      2. Circle Collision Between Two Sprites
      3. Pixel-Perfect Masked Collision Between Two Sprites
      4. Rectangle Collision Between a Sprite and a Group
      5. Rectangle Collision Between Two Groups
    3. The Zombie Mob Game
      1. Creating Your Own Module
      2. Advanced Directional Animation
      3. Colliding with Zombies
      4. Getting Health
      5. Game Source Code
    4. Summary
  13. 9. Arrays, Lists, and Tuples: The Block Breaker Game
    1. Examining the Block Breaker Game
    2. Arrays and Lists
      1. Lists with One Dimension
        1. Changing One Element
        2. Adding One Element
        3. Counting Elements
        4. Searching for Elements
        5. Removing Elements
        6. Reversing a List
        7. Sorting a List
      2. Creating a Stack-like List
      3. Creating a Queue-like List
      4. Lists with More Dimensions
        1. Changing One Element
        2. Changing Many Elements
    3. Tuples
      1. Packing a Tuple
      2. Unpacking a Tuple
      3. Searching for Elements
      4. Counting Elements
      5. Tuples as Constant Arrays
    4. The Block Breaker Game
      1. Block Breaker Levels
        1. Level 1
        2. Level 2
        3. Level 3
      2. Loading and Changing Levels
      3. Initializing the Game
      4. Moving the Paddle
      5. Moving the Ball
      6. Hitting the Paddle
      7. Hitting the Blocks
      8. Main Code
      9. MySprite Update
    5. Summary
  14. 10. Timing and Sound: The Oil Spill Game
    1. Examining the Oil Spill Game
    2. Sound
      1. Loading an Audio File
      2. Playing an Audio Clip
    3. Building The Oil Spill Game
      1. Gameplay
        1. Timing
        2. Oil Mess
        3. Cleaning the Oil
        4. Washing the Background
      2. Source Code
    4. Summary
  15. 11. Program Logic: The Snake Game
    1. Examining The Snake Game
    2. Building The Snake Game
      1. Hatching a Snake—the SnakeSegment Class
      2. Raising a Snake—the Snake Class
      3. Feeding the Snake—the Food Class
      4. Initializing the Game
      5. Program Main
      6. Growth by Eating Food
      7. Biting One’s Self Is Not Advisable
      8. Falling off the World
    3. Teaching the Snake to Move Itself
      1. Moving Automatically
      2. Getting the Current Direction
      3. Moving Toward the Food
      4. Other Code Changes
    4. Summary
  16. 12. Trigonometry: The Tank Battle Game
    1. Examining the Tank Battle Game
    2. Angular Velocity
      1. Calculating Angular Velocity
      2. Pygame’s Goofy Rotation
      3. Moving Forward and Backward at Any Angle
      4. Improved Angle Wrapping
    3. Building The Tank Battle Game
      1. The Tanks
        1. The Tank Constructor
        2. The Tank Update Function
        3. The Tank Draw Function
        4. The String Override
        5. The EnemyTank Class
      2. The Bullets
      3. Main Code
        1. The Header Code
        2. Game Initialization
        3. The Audio Functions
        4. Gameplay Code
    4. Summary
  17. 13. Random Terrain: The Artillery Gunner Game
    1. Examining the Artillery Gunner Game
    2. Creating the Terrain
      1. Defining the Height Map
        1. Terrain Class, First Edition
        2. Drawing the Terrain
        3. Connecting the Dots
      2. Smoothing the Terrain
      3. Locating Grid Points
    3. Artillery Cannons
      1. Placing the Cannons
      2. Drawing the Turrets
      3. Firing the Cannons
      4. Shots Are A’Flyin
      5. Computer Firing
      6. Scoring a Hit
    4. The Complete Game
    5. Summary
  18. 14. More of Everything: The Dungeon Role-Playing Game
    1. Examining the Dungeon Game
    2. Review of Classic Dungeon RPGs
      1. Rogue
      2. NetHack
      3. AngBand
      4. Sword of Fargoal
      5. Kingdom of Kroz
      6. ZZT
    3. Creating a Dungeon Level
      1. Understanding ASCII Characters
        1. ASCII Character Set
        2. Printing ASCII Characters
        3. The ASCII Table Program
        4. An Improved ASCII Table Program
      2. Simulating a Text Console Display
        1. Keeping Track of Tiles
        2. ASCII Table as a Sprite
        3. Drawing the Dungeon Level
      3. Generating Random Rooms
        1. Creating the Dungeon Class
        2. Generating the Northern Rooms
        3. Generating the Southern Rooms
      4. Generating Random Hallways
        1. Horizontal Hallways
        2. Vertical Hallways
        3. Handling Range Errors
    4. Populating the Dungeon
      1. Adding the Entrance and Exit Portals
      2. Adding Gold
      3. Adding Weapons, Armor, and Health Potions
      4. Adding Monsters
      5. Complete Dungeon Class
      6. Adding the Player’s Character
        1. The Player Class
        2. Moving the Player Character
    5. Advanced Gameplay
      1. Picking Up Items
      2. Fighting Monsters
        1. Calculating Defense
        2. Calculating Attack
        3. Calculating Damage
        4. Combat Rounds
        5. Hitting a Monster
      3. Moving Monsters
      4. Visibility Range
      5. Exiting the Level
      6. Wrapping Up the Gameplay
        1. Printing Game Stats
        2. Common Messages
        3. Remaining Code
    6. Summary
  19. A. Installing Python and Pygame
    1. Installing Python
    2. Installing Pygame
  20. B. Pygame Key Codes