beginning game development with python and pygame

342 1.2K 1
beginning game development with python and pygame

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

this print for content only—size & color not accurate spine = 0.802" 344 page count Books for professionals By professionals ® Beginning Game Development with Python and Pygame: From Novice to Professional Dear Reader, Creating computer games is a fascinating combination of an art and a science, and can make a rewarding career or hobby. Do you know you can write cutting-edge games that run on Windows, Linux, and Mac OS X with open source technologies and freely available tools? In this book, I introduce you to Python—a powerful, dynamic language— and explain how to use the Pygame library to quickly develop compelling games. I also explain how to create three-dimensional graphics using OpenGL, the technology behind many commercial games, such as the Quake series. My aim for Beginning Game Development with Python and Pygame is to make game development accessible to as wide an audience as possible. Previous programming experience is useful but not essential, because the first two chapters introduce the Python programming language—or serve as a refresher course if you are already familiar with Python. Further chapters cover how to use Pygame to set up a graphical display, draw to the screen, handle events, and read input devices. I also talk you through essential topics such as creating game characters with artificial intelligence and playing sound (includ- ing music). Four chapters are devoted to the topic of three-dimensional graphics in games, and show you how to manipulate objects in a virtual world and render impressive visuals with lighting and special effects. The math involved in pro- gramming a game with three-dimensional graphics can be intimidating, but I explain it in visual terms rather than the more traditional pure mathematical approach you find in textbooks. I also share with you a few of the tips and tricks I have picked up over the years in my career as a professional game developer. I hope to be playing one of your Pygame creations in the future! Will McGugan US $39.99 Shelve in Python User level: Beginner–Intermediate The eXperT’s Voice ® in open source Beginning Game Development with Python and Pygame From Novice to Professional cyan MaGenTa yelloW Black panTone 123 c Will McGugan THE APRESS ROADMAP Foundations of Python Network Programming Beginning Python: From Novice to Professional The Definitive Guide to Django: Web Development Done Right Beginning Game Development with Python and Pygame www.apress.com SOURCE CODE ONLINE Companion eBook See last page for details on $10 eBook version ISBN-13: 978-1-59059-872-6 ISBN-10: 1-59059-872-5 9 781590 598726 5 3 9 9 9 Create stunning video games using popular open source technologies! McGugan Game Companion eBook Available Beginning Development with Python and Pygame www.it-ebooks.info www.it-ebooks.info Beginning Game Development with Python and Pygame From Novice to Professional ■■■ Will McGugan 8725.book Page i Wednesday, September 26, 2007 8:08 PM www.it-ebooks.info Beginning Game Development with Python and Pygame: From Novice to Professional Copyright © 2007 by Will McGugan All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-59059-872-6 ISBN-10 (pbk): 1-59059-872-5 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Jason Gilmore Technical Reviewer: Richard Jones Editorial Board: Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Jason Gilmore, Kevin Goff, Jonathan Hassell, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Kylie Johnston Copy Editor: Liz Welch Assistant Production Director: Kari Brooks-Copony Production Editor: Kelly Winquist Compositor: Pat Christenson Proofreader: Erin Poe Indexer: Becky Hornyak Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http:// www.apress.com. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com in the Source Code/ Download section. 8725.book Page ii Wednesday, September 26, 2007 8:08 PM www.it-ebooks.info For Maria 8725.book Page iii Wednesday, September 26, 2007 8:08 PM www.it-ebooks.info 8725.book Page iv Wednesday, September 26, 2007 8:08 PM www.it-ebooks.info v Contents at a Glance About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xvii Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi ■CHAPTER 1 Introducing Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 ■CHAPTER 2 Exploring Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 ■CHAPTER 3 Introducing Pygame. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 ■CHAPTER 4 Creating Visuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 ■CHAPTER 5 Making Things Move . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 ■CHAPTER 6 Accepting User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 ■CHAPTER 7 Take Me to Your Leader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 ■CHAPTER 8 Moving into the Third Dimension. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 ■CHAPTER 9 Exploring the Third Dimension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 ■CHAPTER 10 Making Things Go Boom. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 ■CHAPTER 11 Lights, Camera, Action! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 ■CHAPTER 12 Setting the Scene with OpenGL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 ■APPENDIX A Game Object Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 ■APPENDIX B Packaging Your Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 ■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 8725.book Page v Wednesday, September 26, 2007 8:08 PM www.it-ebooks.info 8725.book Page vi Wednesday, September 26, 2007 8:08 PM www.it-ebooks.info vii Contents About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xv About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi ■CHAPTER 1 Introducing Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Your First Look at Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Lists and Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Python in Practice. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 ■CHAPTER 2 Exploring Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Creating Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Working with Logic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Understanding Booleans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Understanding Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Defining Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Introducing Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . 26 Using Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Python in Practice. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Using the Standard Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Introducing import. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Useful Modules for Games. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 ■CHAPTER 3 Introducing Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 History of Pygame. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Installing Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Using Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 8725.book Page vii Wednesday, September 26, 2007 8:08 PM www.it-ebooks.info viii ■CONTENTS Hello World Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Understanding Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Retrieving Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Handling Mouse Motion Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Handling Mouse Button Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Handling Keyboard Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Filtering Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Posting Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Opening a Display. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Full-Screen Displays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Resizable Pygame Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Windows with No Borders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Additional Display Flags. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Using the Font Module. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 When Pygame Goes Wrong. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Pygame in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 ■CHAPTER 4 Creating Visuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Using Pixel Power. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Working with Color. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Representing Color in Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Scaling Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Blending Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Using Images. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Storing Images. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Working with Surface Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Drawing with Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 pygame.draw.rect. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 pygame.draw.polygon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 pygame.draw.circle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 pygame.draw.ellipse. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 pygame.draw.arc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 pygame.draw.line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 pgame.draw.lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 pygame.draw.aaline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 pygame.draw.aalines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 8725.book Page viii Wednesday, September 26, 2007 8:08 PM www.it-ebooks.info [...]... significant undertaking When I discovered Python, it lowered a number of barriers to writing software, because I could work faster and accomplish more with less effort, and when combined with Pygame I could experiment with game ideas and build a complete game from scratch in record time The beauty of Pygame is that it makes the various tasks in creating a game (setting up a display, drawing to the... to be and it turns out that’s not particularly complicated at all! Many one-liners in Pygame would take dozens of lines in C++, the traditional tool of game developers Although Python and Pygame are superb tools for rapid game development, there is little in the way of books or web tutorials for Python game programmers, who often have no choice but to mentally translate from another language to Python. .. N How This Book Is Structured Beginning Game Development with Python and Pygame is divided into 12 chapters, each of which builds on the previous chapter with a few notable exceptions I’ve structured it so that you can get results quickly and see something on screen, which you may appreciate if you are as impatient as I am Virtually all the listings are self-contained, and hopefully entertaining, little... another language to Python when researching a new topic in game development This book was conceived to fill that gap and allow the beginner game programmer to get up to speed with Python and learn the fundamentals of game programming without having to first learn C++, C#, Java, or another language first It was also my opportunity to explain 3D game programming in a way that is accessible to nonmathematicians—something... book, you will need at least version 2.4 of Python and version 1.7.1 of Pygame, which you can download from www .python. org and www .pygame. org, respectively If you want to run the 3D sample code, you will also need PyOpenGL, which you can download from pyopengl.sourceforge.net All are free software, and this book contains instructions on how to install them and get started Downloading the Code The source... enough for you to be able to understand the Python code in the book and write some of your own Occasionally, new syntaxes and language features are introduced in the rest of the book, but I explain them where they are first used If you are proficient in Python, you can skip straight to Chapter 3 Chapter 3 is your first introduction to Pygame and covers its history and capabilities It also explains the... up a graphical display and handling events, skills that are essential for any game You will become intimately familiar with the code introduced in this chapter, as it is used in all the sample code for the rest of the book Chapter 4 dives straight into creating visuals and the various ways in which you can draw to the screen with Pygame Chapter 5 explores the techniques that game programmers use to... simplest of games Even seasoned game developers find the technical requirements of starting a game to be enough of a barrier that they are less likely to work on game ideas that are unproven or potentially not commercial-worthy Game development may have become easier over the years, with simplified programming interfaces and more programmers wishing to share their knowledge, but writing a game is still... was a fan of the UK television series Monty Python Python is popular in game development, but it is also used to create everything from applications to web sites Even NASA and Google rely heavily on Python There are plenty of alternative languages that can be used to create games, but I have chosen Python because it has the tendency to take care of the details and leave you—the programmer—to concentrate... displaying game characters on the screen, making them look great, and having them interact with a virtual environment This chapter is a friendly introduction to Python; it will get you up to speed with the language so that you can read the sample code and start writing code of your own If you are familiar with Python, then feel free to skip the first two chapters Read on if you are completely new to Python . Development with Python and Pygame From Novice to Professional ■■■ Will McGugan 8725.book Page i Wednesday, September 26, 2007 8:08 PM www.it-ebooks.info Beginning Game Development with Python and Pygame: . video games using popular open source technologies! McGugan Game Companion eBook Available Beginning Development with Python and Pygame www.it-ebooks.info www.it-ebooks.info Beginning Game Development. ROADMAP Foundations of Python Network Programming Beginning Python: From Novice to Professional The Definitive Guide to Django: Web Development Done Right Beginning Game Development with Python and Pygame www.apress.com SOURCE CODE ONLINE Companion

Ngày đăng: 03/05/2014, 22:12

Từ khóa liên quan

Mục lục

  • Beginning Game Development with Python and Pygame: From Novice to Professional

  • Contents at a Glance

  • Contents

  • About the Author

  • About the Technical Reviewer

  • Acknowledgments

  • Introduction

    • Who This Book Is For

    • How This Book Is Structured

    • Prerequisites

    • Downloading the Code

    • Contacting the Author

    • Introducing Python

      • Your First Look at Python

        • Numbers

        • Strings

          • Concatenating Strings

          • Parsing Strings

          • Slicing Strings

          • String Methods

          • Lists and Tuples

            • Modifying List Items

            • Removing List Items

            • List Methods

            • Tuples

Tài liệu cùng người dùng

Tài liệu liên quan