32,39 €
With a growing interest in learning to program, game development is an appealing topic for getting started with coding. From geometry to basic Artificial Intelligence algorithms, there are plenty of concepts that can be applied in almost every game. Python is a widely used general-purpose, high-level programming language. It provides constructs intended to enable clear programs on both a small and large scale. It is the third most popular language whose grammatical syntax is not predominantly based on C. Python is also very easy to code and is also highly flexible, which is exactly what is required for game development. The user-friendliness of this language allows beginners to code games without too much effort or training. Python also works with very little code and in most cases uses the “use cases” approach, reserving lengthy explicit coding for outliers and exceptions, making game development an achievable feat.
Python Game Programming by Example enables readers to develop cool and popular games in Python without having in-depth programming knowledge of Python. The book includes seven hands-on projects developed with several well-known Python packages, as well as a comprehensive explanation about the theory and design of each game.
It will teach readers about the techniques of game design and coding of some popular games like Pong and tower defense. Thereafter, it will allow readers to add levels of complexities to make the games more fun and realistic using 3D.
At the end of the book, you will have added several GUI libraries like Chimpunk2D, cocos2d, and Tkinter in your tool belt, as well as a handful of recipes and algorithms for developing games with Python.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 251
Veröffentlichungsjahr: 2015
Copyright © 2015 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: September 2015
Production reference: 1230915
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78528-153-2
www.packtpub.com
Authors
Alejandro Rodas de Paz
Joseph Howse
Reviewers
Benjamin Johnson
Dennis O'Brien
Acquisition Editors
Owen Roberts
Sonali Vernekar
Content Development Editor
Dharmesh Parmar
Technical Editor
Ryan Kochery
Copy Editor
Vikrant Phadke
Project Coordinator
Harshal Ved
Proofreader
Safis Editing
Indexer
Rekha Nair
Graphics
Jason Monteiro
Production Coordinator
Manu Joseph
Cover Work
Manu Joseph
Alejandro Rodas de Paz is a computer engineer and game developer from Seville, Spain.
He came across Python back in 2009, while he was studying at the University of Seville. Alejandro developed several academic projects with Python, from web crawlers to artificial intelligence algorithms. In his spare time, he started building his own games in Python. He did a minor in game design at Hogeschool van Amsterdam, where he created a small 3D game engine based on the ideas he learned during this minor.
He has also developed some open source projects, such as a Python API for the Philips Hue personal lighting system. You can find these projects in his GitHub account at https://github.com/aleroddepaz.
Prior to this publication, Alejandro collaborated with Packt Publishing as a technical reviewer on the book Tkinter GUI Application Development Hotshot.
I would like to thank my parents, Feliciano and María Teresa, for their absolute trust and support. They have been an inspiration to me and an example of hard work.
I would also like to thank my girlfriend, Lucía, for her love and for putting up with me while I worked on this book.
Joseph Howse is a writer, software developer, and business owner from Halifax, Nova Scotia, Canada. Computer games and code are imbibed in his earliest memories, as he learned to read and type by playing text adventures with his older brother, Sam, and watching him write graphics demos in BASIC.
Joseph's other books include OpenCV for Secret Agents, OpenCV Blueprints, Android Application Programming with OpenCV 3, and Learning OpenCV 3 Computer Vision with Python. He works with his cats to make computer vision systems for humans, felines, and other users. Visit http://nummist.com to read about some of his latest projects done at Nummist Media Corporation Limited.
I dedicate my work to Sam, Jan, Bob, Bunny, and my cats, who have been my lifelong guides and companions.
I congratulate my coauthor for producing an excellent compendium of classic examples of game development. I am grateful for the opportunity to add my chapter on checkers (draughts) and computer vision.
I am also indebted to the many editors and technical reviewers who have contributed to planning, polishing, and marketing this book. I have come to expect an outstanding team when working with Packt Publishing, and once again, all of them have guided me with their experience and saved me from sundry errors and omissions. Please meet the technical reviewers by reading their biographies here.
Finally, I want to thank my readers and everybody in the open source community. We are united in our efforts to build and share all kinds of projects and knowledge, paving the way for books such as this to succeed.
Benjamin Johnson is an experienced Python programmer with a passion for game programming, software development, and web design. He is currently studying computer science at The University of Texas at Austin and plans to specialize in software engineering. His most popular Python projects include an adventure game engine and a particle simulator, both developed using Pygame. You can check out Benjamin's latest Pygame projects and articles on his website at www.learnpygame.com.
I would like to thank Packt Publishing for giving me the opportunity to read and review this excellent book!
Dennis O'Brien is the director of data science at Game Show Network Games. He studied physics at the University of Chicago as an undergraduate and completed his graduate studies in computer science from the University of Illinois, Chicago. He was the principal software engineer at Electronic Arts, a senior software engineer at Leapfrog Enterprises, and a lead game developer at Jellyvision Games.
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.
Welcome to Python Game Programming By Example. As hobbyist programmers or professional developers, we may build a wide variety of applications, from large enterprise systems to web applications made with state-of-the-art frameworks. However, game development has always been an appealing topic, maybe simply for creating casual games and not just for high-budget AAA titles.
If you want to explore the different ways of developing games in Python, a language with clear and simple syntax, then this is the book for you. In each chapter, we will build a new game from scratch, using several popular libraries and utilities. By the end of this book, you will be able to quickly create your own 2D and 3D games, and have a handful of Python libraries in your tool belt to choose from.
Chapter 1, Hello, Pong!, details the required software, its installation, and the basic syntax of Python: data structures, control flow statements, object orientation, and so on. It also includes the first game of the book, the classic "Hello, world" game.
Chapter 2, Cocos Invaders, introduces the cocos2d game engine and explains how to build a game similar to Space Invaders to put this knowledge into practice. Here, you learn the basics of collisions, input handling, and scene setup.
Chapter 3, Building a Tower Defense Game, is where you learn to develop a full-fledged game with cocos2d. This game includes some interesting components, such as a HUD and a main menu.
Chapter 4, Steering Behaviors, covers seemingly intelligent movements for autonomous characters. You will be adding these strategies gradually, in different levels of a basic game built with particle systems.
Chapter 5, Pygame and 3D, presents the foundations of 3D and guides you through the basic structure of an OpenGL program.
Chapter 6, PyPlatformer, is where you develop a 3D platformer game with all the techniques learned in the previous chapter.
Chapter 7, Augmenting a Board Game with Computer Vision, introduces the topic of computer vision, which allows software to learn about the real world via a camera. In this chapter, you build a system to analyze a game of checkers (draughts) in real time as players move pieces on a physical board.
The projects covered in this book assume that you have installed Python 3.4 on a computer with Windows, Mac OS X, or Linux. We also assume that you have included pip during the installation process, since it will be the package manager used to install the required third-party packages.
If you have ever wanted to create casual games in Python and you wish to explore the various GUI technologies that this language offers, then this is the book for you. This title is intended for beginners in Python with little or no knowledge of game development, and it covers step by step how to build seven different games, from the well-known Space Invaders to a classical 3D platformer.
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Additionally, up-to-date example code for Chapter 7, Augmenting a Board Game with Computer Vision, is posted at http://nummist.com/opencv.
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/B04505_Graphics.pdf.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Additionally, any errata for Chapter 7, Augmenting a Board Game with Computer Vision, will be posted at http://nummist.com/opencv.
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.
You can also contact the authors directly. Alejandra Rodas de Paz, author of Chapters 1 to 6, can be reached at <[email protected]>. Joseph Howse, author of Chapter 7, can be reached at <[email protected]>, and answers to common questions can be found on his website, http://nummist.com/opencv.
The Breakout game starts with a paddle and a ball at the bottom of the screen and some rows of bricks at the top. The player must eliminate all the bricks by hitting them with the ball, which rebounds against the borders of the screen, the bricks, and the bottom paddle. As in Pong, the player controls the horizontal movement of the paddle.
The player starts the game with three lives, and if they miss the ball's rebound and it reaches the bottom border of the screen, one life is lost. The game is over when all the bricks are destroyed, or when the player loses all their lives.
This is a screenshot of the final version of our game:
So far, we have the window set up and now we can start drawing items on the canvas. The Canvas widget is two-dimensional and uses the Cartesian coordinate system. The origin—the (0, 0) ordered pair—is placed in the top-left corner, and the axis can be represented as shown in the following screenshot:
Keeping this layout in mind, we can use two methods of the Canvas widget to draw the paddle, the bricks, and the ball:
Each of these calls returns an integer, which identifies the item handle. This reference will be used later to manipulate the position of the item and its options. The **options syntax represents a key/value pair of additional arguments that can be passed to the method call. In our case, we will use the fill and the tags option.
The x0 and y0 coordinates indicate the top-left corner of the previous screenshot, and x1 and y1 are indicated in the bottom-right corner.
For instance, we can call canvas.create_rectangle(250, 300, 330, 320, fill='blue', tags='paddle') to create a player's paddle, where:
We will invoke other Canvas methods to manipulate the items and retrieve widget information. This table gives the references to the Canvas widget that will be used in this chapter:
Method
Description
canvas.coords(item)
Returns the coordinates of the bounding box of an item.
canvas.move(item, x, y)
Moves an item by a horizontal and a vertical offset.
canvas.delete(item)
Deletes an item from the canvas.
canvas.winfo_width()
Retrieves the canvas width.
canvas.itemconfig(item, **options)
Changes the options of an item, such as the fill color or its tags.
canvas.bind(event, callback)
Binds an input event with the execution of a function. The callback handler receives one parameter of the type Tkinter event.
canvas.unbind(event)
Unbinds the input event so that there is no callback function executed when the event occurs.
canvas.create_text(*position, **opts)
Draws text on the canvas. The position and the options arguments are similar to the ones passed in canvas.create_rectangle and canvas.create_oval.
canvas.find_withtag(tag)
Returns the items with a specific tag.
canvas.find_overlapping(*position)
Returns the items that overlap or are completely enclosed by a given rectangle.
You can check out a complete reference of the event syntax as well as some practical examples at http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm#events.