100% found this document useful (6 votes)
40 views53 pages

Buy Ebook Python, Pygame, and Raspberry Pi Game Development 2nd Edition Sloan Kelly Cheap Price

Development

Uploaded by

bumbacwieck
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (6 votes)
40 views53 pages

Buy Ebook Python, Pygame, and Raspberry Pi Game Development 2nd Edition Sloan Kelly Cheap Price

Development

Uploaded by

bumbacwieck
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

Experience Seamless Full Ebook Downloads for Every Genre at textbookfull.

com

Python, Pygame, and Raspberry Pi Game Development


2nd Edition Sloan Kelly

https://textbookfull.com/product/python-pygame-and-
raspberry-pi-game-development-2nd-edition-sloan-kelly/

OR CLICK BUTTON

DOWNLOAD NOW

Explore and download more ebook at https://textbookfull.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Biota Grow 2C gather 2C cook Loucas

https://textbookfull.com/product/biota-grow-2c-gather-2c-cook-loucas/

textboxfull.com

Beginning Robotics with Raspberry Pi and Arduino: Using


Python and OpenCV 2nd Edition Jeff Cicolani

https://textbookfull.com/product/beginning-robotics-with-raspberry-pi-
and-arduino-using-python-and-opencv-2nd-edition-jeff-cicolani/

textboxfull.com

Beginning Robotics with Raspberry Pi and Arduino: Using


Python and OpenCV 2nd Edition Jeff Cicolani

https://textbookfull.com/product/beginning-robotics-with-raspberry-pi-
and-arduino-using-python-and-opencv-2nd-edition-jeff-cicolani-2/

textboxfull.com

Raspberry Pi for Python Programmers Cookbook Second


Edition Tim Cox

https://textbookfull.com/product/raspberry-pi-for-python-programmers-
cookbook-second-edition-tim-cox/

textboxfull.com
Raspberry Pi 3 Cookbook for Python Programmers: Unleash
the potential of Raspberry Pi 3 with over 100 recipes Tim
Cox
https://textbookfull.com/product/raspberry-pi-3-cookbook-for-python-
programmers-unleash-the-potential-of-raspberry-pi-3-with-
over-100-recipes-tim-cox/
textboxfull.com

Beginning Robotics with Raspberry Pi and Arduino: Using


Python and OpenCV Jeff Cicolani

https://textbookfull.com/product/beginning-robotics-with-raspberry-pi-
and-arduino-using-python-and-opencv-jeff-cicolani/

textboxfull.com

Hello Raspberry Pi Python programming for kids and other


beginners 1st Edition Ryan Heitz

https://textbookfull.com/product/hello-raspberry-pi-python-
programming-for-kids-and-other-beginners-1st-edition-ryan-heitz/

textboxfull.com

Build Your Own Robot: Using Python, CRICKIT, and Raspberry


PI 1st Edition Marwan Alsabbagh

https://textbookfull.com/product/build-your-own-robot-using-python-
crickit-and-raspberry-pi-1st-edition-marwan-alsabbagh/

textboxfull.com

Beginning Sensor Networks with XBee, Raspberry Pi, and


Arduino: Sensing the World with Python and MicroPython 2nd
Edition Charles Bell
https://textbookfull.com/product/beginning-sensor-networks-with-xbee-
raspberry-pi-and-arduino-sensing-the-world-with-python-and-
micropython-2nd-edition-charles-bell/
textboxfull.com
Python, PyGame, and
Raspberry Pi Game
Development
Second Edition

Sloan Kelly
Python, PyGame, and
Raspberry Pi Game
Development
Second Edition

Sloan Kelly
Python, PyGame, and Raspberry Pi Game Development
Sloan Kelly
Niagara Falls, ON, Canada

ISBN-13 (pbk): 978-1-4842-4532-3 ISBN-13 (electronic): 978-1-4842-4533-0


https://doi.org/10.1007/978-1-4842-4533-0

Copyright © 2019 by Sloan Kelly


This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or
part of the material is concerned, specifically the rights of translation, reprinting, reuse of
illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way,
and transmission or information storage and retrieval, electronic adaptation, computer software,
or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark
symbol with every occurrence of a trademarked name, logo, or image we use the names, logos,
and images only in an editorial fashion and to the benefit of the trademark owner, with no
intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if
they are not identified as such, is not to be taken as an expression of opinion as to whether or not
they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of
publication, neither the authors nor the editors nor the publisher can accept any legal
responsibility for any errors or omissions that may be made. The publisher makes no warranty,
express or implied, with respect to the material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Spandana Chatterjee
Development Editor: James Markham
Coordinating Editor: Divya Modi
Cover designed by eStudioCalamar
Cover image designed by Freepik (www.freepik.com)
Distributed to the book trade worldwide by Springer Science+Business Media New York,
233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505,
e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a
California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc
(SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected], or visit http://www.apress.com/
rights-permissions.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook
versions and licenses are also available for most titles. For more information, reference our Print
and eBook Bulk Sales web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available
to readers on GitHub via the book’s product page, located at www.apress.com/978-1-4842-4532-3.
For more detailed information, please visit http://www.apress.com/source-code.
Printed on acid-free paper
For Annamarie
Table of Contents
About the Author������������������������������������������������������������������������������xvii
About the Technical Reviewer�����������������������������������������������������������xix
Acknowledgments�����������������������������������������������������������������������������xxi
Introduction�������������������������������������������������������������������������������������xxiii

Chapter 1: What Is a Programming Language?������������������������������������1


What Does a Computer Program Do?�������������������������������������������������������������������2
Conclusion������������������������������������������������������������������������������������������������������������3

Chapter 2: What Is Python?������������������������������������������������������������������5


Programming Styles����������������������������������������������������������������������������������������������5
Object-Oriented�����������������������������������������������������������������������������������������������������7
Functional�������������������������������������������������������������������������������������������������������������8
What Is Pygame?��������������������������������������������������������������������������������������������������8
Conclusion������������������������������������������������������������������������������������������������������������9

Chapter 3: Introducing Python������������������������������������������������������������11


The Terminal Window������������������������������������������������������������������������������������������11
Running the Python Interpreter���������������������������������������������������������������������������12
Python Is Interpreted�������������������������������������������������������������������������������������������13
Python As a Calculator����������������������������������������������������������������������������������������13
Keywords������������������������������������������������������������������������������������������������������������16
Printing���������������������������������������������������������������������������������������������������������������17
String Formatting������������������������������������������������������������������������������������������19

v
Table of Contents

Variables�������������������������������������������������������������������������������������������������������������22
Naming Variables�������������������������������������������������������������������������������������������22
Python As a Calculator, Part II�����������������������������������������������������������������������������23
Arithmetic Operators�������������������������������������������������������������������������������������26
Data Types�����������������������������������������������������������������������������������������������������27
Numeric Types�����������������������������������������������������������������������������������������������27
String Formatting Again��������������������������������������������������������������������������������29
Conclusion����������������������������������������������������������������������������������������������������������31

Chapter 4: Breaking Free from the Interpreter�����������������������������������33


What Is IDLE?������������������������������������������������������������������������������������������������������33
Starting IDLE�������������������������������������������������������������������������������������������������������33
Starting a New File���������������������������������������������������������������������������������������������34
Hello, World!��������������������������������������������������������������������������������������������������������36
Running from the Command Line������������������������������������������������������������������36
Running from Inside IDLE������������������������������������������������������������������������������38
Conclusion����������������������������������������������������������������������������������������������������������39

Chapter 5: Making Decisions��������������������������������������������������������������41


A Note About Blocks��������������������������������������������������������������������������������������������44
Testing for Equality���������������������������������������������������������������������������������������������45
Using Boolean Logic��������������������������������������������������������������������������������������������50
And����������������������������������������������������������������������������������������������������������������50
Or�������������������������������������������������������������������������������������������������������������������51
Not�����������������������������������������������������������������������������������������������������������������52
Nesting Ifs�����������������������������������������������������������������������������������������������������������52
A Note on Switch������������������������������������������������������������������������������������������������54
Conclusion����������������������������������������������������������������������������������������������������������54

vi
Table of Contents

Chapter 6: Making the Raspberry Pi Repeat Itself�����������������������������55


The for Loop��������������������������������������������������������������������������������������������������������55
The range() Function�������������������������������������������������������������������������������������57
While Loops���������������������������������������������������������������������������������������������������������58
Counting��������������������������������������������������������������������������������������������������������58
Sentinel���������������������������������������������������������������������������������������������������������60
Conditional�����������������������������������������������������������������������������������������������������62
Conclusion����������������������������������������������������������������������������������������������������������62

Chapter 7: Containers�������������������������������������������������������������������������63
Container Nomenclature�������������������������������������������������������������������������������������63
Tuples������������������������������������������������������������������������������������������������������������������64
Removing Elements from a Tuple������������������������������������������������������������������64
Changing Element Values������������������������������������������������������������������������������65
Tuples in Printing�������������������������������������������������������������������������������������������66
Deconstructing Tuples�����������������������������������������������������������������������������������67
Lists��������������������������������������������������������������������������������������������������������������������67
List Creation��������������������������������������������������������������������������������������������������68
Adding Values to the List�������������������������������������������������������������������������������68
Removing Values from a List�������������������������������������������������������������������������69
Doctor’s Waiting Room Program��������������������������������������������������������������������70
Dictionaries���������������������������������������������������������������������������������������������������������73
Iterating Through Dictionaries�����������������������������������������������������������������������74
Adding New Items to Dictionaries�����������������������������������������������������������������74
Removing Entries from a Dictionary��������������������������������������������������������������74
Conclusion����������������������������������������������������������������������������������������������������������75

vii
Table of Contents

Chapter 8: Putting It Together: Tic-Tac-Toe����������������������������������������77


The Rules������������������������������������������������������������������������������������������������������������77
Program Layout���������������������������������������������������������������������������������������������������79
Variables�������������������������������������������������������������������������������������������������������������79
The Game������������������������������������������������������������������������������������������������������������80
Save and Run������������������������������������������������������������������������������������������������������85
Conclusion����������������������������������������������������������������������������������������������������������85

Chapter 9: Basic Introduction to PyGame�������������������������������������������87


Importing the PyGame Framework���������������������������������������������������������������������87
Initializing PyGame����������������������������������������������������������������������������������������������88
The Main Loop����������������������������������������������������������������������������������������������������89
Images and Surfaces������������������������������������������������������������������������������������������92
Creating Images��������������������������������������������������������������������������������������������������93
Loading Images���������������������������������������������������������������������������������������������������93
Drawing Images��������������������������������������������������������������������������������������������������94
Screen Coordinates and Resolution��������������������������������������������������������������������94
Sprite Sheets������������������������������������������������������������������������������������������������������95
Full Listing����������������������������������������������������������������������������������������������������������97
Conclusion����������������������������������������������������������������������������������������������������������97

Chapter 10: Designing Your Game������������������������������������������������������99


Initial Concept�����������������������������������������������������������������������������������������������������99
Prototyping��������������������������������������������������������������������������������������������������100
Functional Specification�����������������������������������������������������������������������������������101
Weapon Firing���������������������������������������������������������������������������������������������101
Program Design������������������������������������������������������������������������������������������������101
Coding���������������������������������������������������������������������������������������������������������������102

viii
Table of Contents

Testing��������������������������������������������������������������������������������������������������������������104
Iteration�������������������������������������������������������������������������������������������������������������105
Conclusion��������������������������������������������������������������������������������������������������������105

Chapter 11: Game Project: Bricks����������������������������������������������������107


The Main Framework����������������������������������������������������������������������������������������108
Images��������������������������������������������������������������������������������������������������������������109
Moving the Bat��������������������������������������������������������������������������������������������������110
Bat Initialization�������������������������������������������������������������������������������������������111
Drawing the Bat�������������������������������������������������������������������������������������������112
Moving the Bat��������������������������������������������������������������������������������������������112
Moving the Ball�������������������������������������������������������������������������������������������������114
Ball Initialization������������������������������������������������������������������������������������������114
Ball Movement���������������������������������������������������������������������������������������������115
Bat and Ball Collision�����������������������������������������������������������������������������������118
Serving the Ball�������������������������������������������������������������������������������������������120
Brick Wall����������������������������������������������������������������������������������������������������������121
Brick and Ball Collision��������������������������������������������������������������������������������122
Out of Bounds����������������������������������������������������������������������������������������������124
Conclusion��������������������������������������������������������������������������������������������������������125

Chapter 12: User-Defined Functions�������������������������������������������������127


What Is a Function?������������������������������������������������������������������������������������������127
Format of a Function�����������������������������������������������������������������������������������������127
Functions as a Menial Task/Mnemonic Device�������������������������������������������������128
Sending Parameters�����������������������������������������������������������������������������������������129
Default Argument Values�����������������������������������������������������������������������������������131
Named Parameters��������������������������������������������������������������������������������������132

ix
Table of Contents

Returning Values�����������������������������������������������������������������������������������������������133
Returning Tuples������������������������������������������������������������������������������������������134
Accessing Global Variables�������������������������������������������������������������������������������135
Real-World Example of a Function��������������������������������������������������������������������136
Conclusion��������������������������������������������������������������������������������������������������������139

Chapter 13: File Input and Output�����������������������������������������������������141


Reading a File from Disk�����������������������������������������������������������������������������������141
Writing Data to a File����������������������������������������������������������������������������������������143
Reading and Writing Containers to a File����������������������������������������������������������144
Writing Your Own Serializer�������������������������������������������������������������������������145
Writing Your Own Deserializer���������������������������������������������������������������������147
JSON�����������������������������������������������������������������������������������������������������������������148
JSON Serialization���������������������������������������������������������������������������������������148
JSON Deserializer����������������������������������������������������������������������������������������149
Handling Errors�������������������������������������������������������������������������������������������������150
Conclusion��������������������������������������������������������������������������������������������������������151

Chapter 14: Introducing Object-Oriented Programming�������������������153


Classes and Objects������������������������������������������������������������������������������������������154
Encapsulation���������������������������������������������������������������������������������������������������154
Abstraction��������������������������������������������������������������������������������������������������������155
Inheritance��������������������������������������������������������������������������������������������������������155
Polymorphism���������������������������������������������������������������������������������������������������155
Why Should You Use OOP?��������������������������������������������������������������������������������156
Data Hiding��������������������������������������������������������������������������������������������������156
Reusable������������������������������������������������������������������������������������������������������156
Easier to Code and Test Separately�������������������������������������������������������������156

x
Table of Contents

The Ball Class���������������������������������������������������������������������������������������������������157


Creating an Instance of the Class���������������������������������������������������������������������160
The Ball update( ) Method����������������������������������������������������������������������������161
Constructors������������������������������������������������������������������������������������������������162
SOLID����������������������������������������������������������������������������������������������������������������164
Single Responsibility�����������������������������������������������������������������������������������165
Open-Closed Principle���������������������������������������������������������������������������������165
Liskov Substitution��������������������������������������������������������������������������������������166
Interface Segregation����������������������������������������������������������������������������������166
Dependency Inversion���������������������������������������������������������������������������������167
Conclusion��������������������������������������������������������������������������������������������������������170

Chapter 15: Inheritance, Composition, and Aggregation������������������171


Inheritance��������������������������������������������������������������������������������������������������������172
Base and Child Classes�������������������������������������������������������������������������������������173
Programming to the Interface���������������������������������������������������������������������175
A Note About Constructors and Base Classes���������������������������������������������175
Composition������������������������������������������������������������������������������������������������������177
Aggregation�������������������������������������������������������������������������������������������������������179
Conclusion��������������������������������������������������������������������������������������������������������180

Chapter 16: Game Project: Snake�����������������������������������������������������181


Functions����������������������������������������������������������������������������������������������������������182
Snake Framework���������������������������������������������������������������������������������������������183
Images��������������������������������������������������������������������������������������������������������������190
Loading the Images�������������������������������������������������������������������������������������191
The Game Map��������������������������������������������������������������������������������������������������192
Drawing the ‘Game Over’ Screen����������������������������������������������������������������������193

xi
Table of Contents

Drawing the Game��������������������������������������������������������������������������������������������195


Drawing the Walls���������������������������������������������������������������������������������������������196
Drawing the Player Data�����������������������������������������������������������������������������������198
Drawing the Snake��������������������������������������������������������������������������������������������199
Updating the Game�������������������������������������������������������������������������������������������202
The updateGame( ) Method��������������������������������������������������������������������������203
Snake Movement�����������������������������������������������������������������������������������������205
Touching a Berry������������������������������������������������������������������������������������������206
Collision Detection��������������������������������������������������������������������������������������������208
Helper Functions�����������������������������������������������������������������������������������������208
Conclusion��������������������������������������������������������������������������������������������������������212

Chapter 17: Model View Controller���������������������������������������������������213


Model����������������������������������������������������������������������������������������������������������������214
View������������������������������������������������������������������������������������������������������������������214
Controller����������������������������������������������������������������������������������������������������������214
Why Use MVC?��������������������������������������������������������������������������������������������������215
The Classes�������������������������������������������������������������������������������������������������216
Folder����������������������������������������������������������������������������������������������������������217
The Robot Model�����������������������������������������������������������������������������������������217
The Robot View��������������������������������������������������������������������������������������������219
The Radar View��������������������������������������������������������������������������������������������221
The Robot Controller������������������������������������������������������������������������������������222
The Robot Generator������������������������������������������������������������������������������������225
Ensuring Constant Speed����������������������������������������������������������������������������������227
The Main Robot Program����������������������������������������������������������������������������������228
Conclusion��������������������������������������������������������������������������������������������������������230

xii
Table of Contents

Chapter 18: Audio�����������������������������������������������������������������������������233


Playing a Sound������������������������������������������������������������������������������������������������234
Playing, Pausing, and Changing Volume�����������������������������������������������������������235
Conclusion��������������������������������������������������������������������������������������������������������240

Chapter 19: Finite State Machines���������������������������������������������������241


Game State�������������������������������������������������������������������������������������������������������241
Menu System����������������������������������������������������������������������������������������������������241
Non-player Artificial Intelligence�����������������������������������������������������������������������242
A Finite State Machine Example�����������������������������������������������������������������������243
Finite State Machine Manager��������������������������������������������������������������������244
Conclusion��������������������������������������������������������������������������������������������������������249

Chapter 20: Game Project: Invaders�������������������������������������������������251


The Classes�������������������������������������������������������������������������������������������������������253
The Finite State Machine����������������������������������������������������������������������������������254
MVC and ‘Invaders’�������������������������������������������������������������������������������������������255
The Framework�������������������������������������������������������������������������������������������������255
Bitmap Font�������������������������������������������������������������������������������������������������259
Interstitial Screens��������������������������������������������������������������������������������������263
The Main Menu��������������������������������������������������������������������������������������������264
Player and Bullets���������������������������������������������������������������������������������������������267
The Bullet Classes���������������������������������������������������������������������������������������267
The Player Classes��������������������������������������������������������������������������������������270
Testing Player����������������������������������������������������������������������������������������������273
The Alien Swarm Classes����������������������������������������������������������������������������������275
Collision Detection��������������������������������������������������������������������������������������������282
Explosions���������������������������������������������������������������������������������������������������282
Collision Controller��������������������������������������������������������������������������������������285

xiii
Table of Contents

The Main Program���������������������������������������������������������������������������������������������288


The Main Game State����������������������������������������������������������������������������������������289
Running the Game��������������������������������������������������������������������������������������������292
Conclusion��������������������������������������������������������������������������������������������������������293

Chapter 21: Simple Electronics with the GPIO Pins��������������������������295


Voltage, Current, and Resistance����������������������������������������������������������������������296
What You Will Need�������������������������������������������������������������������������������������������298
Breadboard��������������������������������������������������������������������������������������������������298
Breakout Board��������������������������������������������������������������������������������������������299
Jumper Wires����������������������������������������������������������������������������������������������300
LEDs������������������������������������������������������������������������������������������������������������302
Resistors������������������������������������������������������������������������������������������������������304
Switches������������������������������������������������������������������������������������������������������307
Building a Circuit�����������������������������������������������������������������������������������������������308
Connecting the Breakout Board to the Raspberry Pi�����������������������������������308
Providing Power and Ground�����������������������������������������������������������������������311
Adding the LED��������������������������������������������������������������������������������������������312
Completing the Circuit���������������������������������������������������������������������������������313
Testing the Circuit����������������������������������������������������������������������������������������315
Pin Meanings����������������������������������������������������������������������������������������������������316
The gpiozero Library�����������������������������������������������������������������������������������������317
The Circuit���������������������������������������������������������������������������������������������������318
The Python Program������������������������������������������������������������������������������������319
Getting Button Input������������������������������������������������������������������������������������320
Reading Button Input in Python�������������������������������������������������������������������321
Conclusion��������������������������������������������������������������������������������������������������������322

xiv
Table of Contents

Chapter 22: Game Project: Memory��������������������������������������������������323


Arranging the Breadboard���������������������������������������������������������������������������������324
Placing the LEDs������������������������������������������������������������������������������������������324
Testing the Circuit���������������������������������������������������������������������������������������������326
Placing the Tact Switches����������������������������������������������������������������������������327
Testing the Button Circuit����������������������������������������������������������������������������328
The Memory Game��������������������������������������������������������������������������������������������330
The ButtonLED and ButtonLEDCollection Classes���������������������������������������331
The Main Program���������������������������������������������������������������������������������������334
Full Listing buttonled.py������������������������������������������������������������������������������������336
Full Listing memorygame.py�����������������������������������������������������������������������������338
Conclusion��������������������������������������������������������������������������������������������������������339

Chapter 23: Game Project: Quiz��������������������������������������������������������341


The Electronics�������������������������������������������������������������������������������������������������341
Testing the Buttons��������������������������������������������������������������������������������������342
The Finite State Machine����������������������������������������������������������������������������������345
Making the Game����������������������������������������������������������������������������������������������347
The Questions����������������������������������������������������������������������������������������������348
UI Helper Classes�����������������������������������������������������������������������������������������354
The Game Runner and Base State Class�����������������������������������������������������359
Player Input�������������������������������������������������������������������������������������������������361
The State Classes����������������������������������������������������������������������������������������362
Playing the Game����������������������������������������������������������������������������������������������376
Conclusion��������������������������������������������������������������������������������������������������������377

Chapter 24: Conclusion���������������������������������������������������������������������379

Index�������������������������������������������������������������������������������������������������381

xv
About the Author
Sloan Kelly has worked in the games industry
for nearly 12 years. He has worked on a
number of AAA and indie titles and currently
works for an educational game company. He
lives in Ontario, Canada, with his wife and
children. Sloan is on Twitter @codehoose and
makes YouTube videos in his spare time.

xvii
About the Technical Reviewer
John Watson is a game developer, artist, guitar
player, husband, and father. Among John’s
many software-powered side projects, he’s
building a Raspberry Pi–powered device that
generates interactive music in live modern
dance performances. He’s also developing a
retro-inspired 2D twin-stick arcade shooter
called Gravity Ace. You can follow his progress
on Twitter @yafd or at gravityace.com. Stop by
and say hi!

xix
Acknowledgments
I would like to thank Divya Modi, Spandana Chatterjee, and the entire
team at Apress for giving me the opportunity to write the second edition
of this book. A special thank you to Divya for keeping me in the loop
throughout this process. I would also like to thank John Watson for the
feedback that he gave while reviewing the text.
Thank you also to Eben Upton who gave us the little machine we will
use to make games and explore electronics with and to Pete Shinners
for starting PyGame and the community for keeping it going. Finally, a
huge thank you to Guido van Rossum for designing the excellent Python
language that you, dear reader, are about to learn and enjoy.

xxi
Introduction
This book is intended for anyone who wants to learn how to program
games. It is ideally suited to students who want to learn Python and
PyGame on their Raspberry Pi. While not necessary, this book has been
oriented toward the Raspberry Pi computer.
The Python programming language is ideally suited to beginners and
experts alike. The skills you will learn in this book are easily transferable to
other computer languages too.
If you are unfamiliar with the Raspberry Pi, there are several good
eBook guides on getting started including mine called A Slice of Raspberry
Pi, available from all good eBook retailers.
This book assumes that you are familiar with the Raspberry Pi
computer and that you have the Raspberry Pi Foundation’s recommended
Raspbian operating system installed. Raspbian is a distribution of the
Debian Linux operating system built specifically for the Raspberry Pi. This
distribution contains all the Software Development Kits (SDKs) including
one for Python that includes PyGame. If you don’t have Raspbian installed,
you will have to ensure that you have Python and PyGame installed on
your system.
Don’t have a Raspberry Pi? Not to worry, you can still learn Python and
PyGame. The code in this book will work on other OSs with Python and
PyGame installed; Python is a platform-independent language.
You can obtain more information and download versions of Python from
www.python.org. PyGame can be obtained from www.pygame.org/.
Sprites from Ari Feldman’s SpriteLib have been used for the projects
contained in this book.

xxiii
Introduction

How This Book Is Organized


The book is organized into chapters covering the following:

• Introduction to the Python language


• Containers in Python

• The IDLE IDE

• Introduction to PyGame library

• Designing your game

• User-defined functions

• File input/output

• Object-oriented design and programming

• Model View Controller design pattern

• Finite state machines

• Interfacing with electronics

There are five projects that produce complete games, all the code and
resources for which are on the web site www.sloankelly.net/. The five
games are

• Bricks

• Snake

• Invaders

• Copycat

• Couch quiz

Throughout the book are lines of code that you can type in to tell the
computer to perform actions, or to add text to a file. In the book, these
lines will appear like this:

xxiv
Introduction

print 'hello world'

Python uses white space characters, notably tabs, to denote blocks of


code. Because this is an eBook and it is not possible to know how tabs will
be rendered on your device, white space is very important to Python, so
remember to use the “tab” key to indent the lines exactly as written like so:

name='Sloan'
if (name=='Sloan'):
    print ('Hello', name)

The line that starts with “print” has been indented using the “tab” key.
OS commands that are to be typed into a command window like
Terminal will be preceded with a “$” sign:

$ ls -al

There are screenshots and other graphics throughout the text to


illustrate output, but when it is just text it will appear in a box like this:

This is output from a command.


It can appear on one or more lines.

Finally, anything important will appear in a note formatted like this:

Take note of this message

xxv
CHAPTER 1

What Is a
Programming
Language?
A computer program is a list of statements that a computer must carry
out in order to complete a task, usually a repetitive task that would take
a human a long time to calculate. A computer language describes the
arrangement or syntax of those statements. There are various computer
languages out there, each suitable to one or more tasks.
Each language has its own unique syntax and set of commands, but
they all have constructs that perform roughly the same types of actions:

• Input

• Output

• Branching (making decisions based on data)

• Loops

A command or keyword is a special phrase that is used by the language


to perform an action whether it is to get input from the user or display text
on the screen. These commands are reserved words that cannot be used

© Sloan Kelly 2019 1


S. Kelly, Python, PyGame, and Raspberry Pi Game Development,
https://doi.org/10.1007/978-1-4842-4533-0_1
Chapter 1 What Is a Programming Language?

for any other purpose in your program. We’ll dive deeper into them later in
this book, but examples of keywords in Python are

• for

• if

• pass

What Does a Computer Program Do?


A computer program performs a series of tasks over and over again
manipulating the user’s input and delivering output in a feedback loop.
When you move your mouse (input), the arrow on the screen moves along
with it (output).
The old definition of a computer program was a basic mathematical
formula:

Program = Algorithm + Data

An algorithm is the step-by-step procedure for processing data.


The algorithm solves a problem with the data that it has been supplied.
What kind of problem? It could be anything from calculating the area of a
rectangle or the volume of a room, where to move a player’s avatar based
on the input from a joystick, or deciding how an enemy should react to a
player who just obtained a power up.
Are all computer programs written the same way? Is there a standard
way to approach a given problem? Well, no. Not really. There are many
ways to achieve the same result in computer programming! There is no
correct way of solving a problem. So long as your program does what it is
supposed to, that’s just fine! You may want to ‘tweak’ your code later to
speed it up, but any optimization happens once you have the algorithm
right. Your program must function as expected. This is of paramount
importance.

2
Chapter 1 What Is a Programming Language?

Conclusion
Computer programs are used to perform laborious tasks on a series of
data elements that are input by users. For games, that means updating the
player avatar location and maintaining the game world while displaying it
to the player.
It is not advisable to stick to one language but rather experience as
many languages as you can. This will enable you, the programmer, to
decide which language is best for a given situation. Your first language is
a great choice; Python is a very powerful language that can be used for a
variety of purposes and is perfect for the first-time programmer.

3
CHAPTER 2

What Is Python?
Python is a modern programming language that supports object-oriented,
functional, and imperative programming styles. It is ideal for the beginner
because of its readability and ease of use. The upside to all of this is that
you can write programs in less lines of code than an equivalent C/C++ or
Java program.
What on earth did I just say? Let’s break that last paragraph down and
make it a little more readable.

P
 rogramming Styles
Python is suitable for programming in the following styles:

• Imperative
• Object-oriented

• Functional

Imperative programming was for the longest time the most common
way to write computer code. It describes step by step how to perform
changes to the data in a very linear manner.
For example, we have the following items:

• Tea bag

• Milk

© Sloan Kelly 2019 5


S. Kelly, Python, PyGame, and Raspberry Pi Game Development,
https://doi.org/10.1007/978-1-4842-4533-0_2
Chapter 2 What Is Python?

• Cup

• Spoon

• Kettle

• Water
These are the things we use and manipulate in our ‘program’; this is
our data. We want to change this data to a different state. What state? Well,
we want a cup of milky tea. How do we do that? We prescribe a series of
operations that will transform this data into some other data like so:

• Place tea bag in cup

• Pour water into kettle

• Boil the kettle

• While the kettle is boiling, watch TV

• Pour the water from the kettle to the cup

• Pour milk into the cup

• Stir the tea with the spoon

• Serve

In code (not specifically Python code), this could be written as

addTo(cup, tea_bag)
addTo(kettle, water)
boil(kettle)
while isBoiling(kettle):
    watchTV()
addTo(cup, getWaterFrom(kettle))
addTo(cup, milk)
stir(cup)
serve(cup)

6
Chapter 2 What Is Python?

These are the prescribed steps (process) to change our initial data (our
input) and transform it into our output. See Figure 2-1.

INPUT PROCESS OUTPUT

Figure 2-1. Input, process, output block diagram

O
 bject-Oriented
Imperative programs separate the functionality (the algorithm) from the
data. Object-oriented languages keep the functionality with the data.
Objects contain the data and the instructions used to manipulate that data
in one place.
There is an advantage to this; algorithms stored with it process your
data. Let’s take a pencil as an example. It has certain attributes that
describe it:

• Color
• Hardness

• Nib size

• Length

It also has certain actions or methods that can be applied to it:

• Write

• Erase

• Sharpen

7
Chapter 2 What Is Python?

These methods change the state of the object; remember that state
is determined by the data. For example, when you write using a pencil,
the nib length gets smaller and smaller. When you sharpen the pencil, its
overall length gets shorter, but the nib size is reset to its maximum.

F unctional
Functional programming is not new and was first developed in the
1930s. It has its roots in lambda calculus. Functional programming uses
mathematical functions to perform calculations. No data is changed in
these calculations; instead new values are calculated. This means that
functional programs have no state.
Functional programming tends to be used for recursion (calling the
same function from itself ) and iteration through items.
In Python, Fibonacci numbers can be calculated with the following
one line:

fib = lambda n: n if n < 2 else fib(n-1) + fib(n-2)

This was taken from a discussion on StackOverflow (http://bit.ly/


FibonacciPython).
To calculate a value, the programmer simply passes in an integer value:

fib(5)

What Is Pygame?
Pygame was started by Pete Shinners as a wrapper around the Simple
DirectMedia Library (SDL). It has been maintained by the community
since 2000 and is released under the GNU Lesser General Public License.
Which means you are free to look at the source code if you so choose.

8
Chapter 2 What Is Python?

Pygame was created to allow for the development of games without


resorting to using programming languages like C or C++.
Pygame can be used to write fast-paced 2D games in a retro style,
or modern casual and hyper-casual games. It handles the difficulties of
loading in images, displaying sprites, playing sounds, etc., for you.
For more details about Pygame, please visit their web site:
www.pygame.org/news.

C
 onclusion
Python is a modern, multiparadigm programming language. It can be used
for imperative, object-oriented, and functional programming.
In addition, Pygame is a framework that allows you to create fast-paced
action games in 2D.
So, now that we know what Python is capable of, it’s time we looked at
the language itself.

9
Exploring the Variety of Random
Documents with Different Content
Change “how their pride brought Kawun and Koonbhkurun to such
destruction” to Rawun and Koombhkurun.
“you destroyed the great demon, Kawun: and whenever devils” to
Rawun.
[Chapter II]
“On the hearing this, the Raja Pureechit said to Shookdeo” delete
the first the.
[Chapter III]
“joining its hands, came before him Vishnù said to it” add period
after him.
[Chapter VI]
“the lucky moment, and exact, ray of light, and having
considered” delete the comma after exact.
“on their foreheads, and given them pawn, (betle leaf,) dismissed”
to betel.
“the lines of fate cannot he effaced by one. For this reason” to be.
[Chapter X]
“went and saved the milk; Whilst Krishnù, having broken the
vessels” to whilst.
[Chapter XII]
“of the trees falling, exactly as they had occured” to occurred.
[Chapter XIII]
“Brother! this is some very frighful cave, do not let us go inside” to
frightful.
[Chapter XIV]
“Brothers! this is a nice place, why need We go farther?” to we.
[Chapter XVI]
“Shree Shoodeo said,—O great king! when Shree Krishnù” to
Shookdeo.
“of a kudum tree, placing, his head on the thigh of a companion.”
Delete the second comma.
[Chapter XXIV]
(where the wives were cooking, and said them, “Shree Krishnù
has) to said to them.
[Chapter XXXI]
(began to say,—“Tell me friend! where has Mohun gone) add
comma after me.
[Chapter XXXVII]
(touch other persons.” Krihsnù replied, “I will summon all the) to
Krishnù.
“and the cowherdessess and cowherds, to celebrate with songs”
to cowherdesses.
[Chapter XLVI]
“We will accomplish, whatever you may order us to perform.”
Delete the comma.
“First of all send an invitation to all their brethen” to brethren.
“there is a demon, named Sunkasoor, in the form of a shell” to
Sunkhasoor.
[Chapter XLVII]
“I will give an account now how Shree Kishnù Chund brought” to
Krishnù.
(whether they still continued to love him Nund added, “Give me)
add period after him.
“although other bees buz over it during the whole night” to buzz.
[Chapter L]
“O lord of the earth! when Shree Khrishnù Jee heard these words”
to Krishnù.
[Chapter LI]
“the descendants of Judoo with you, proctect the fort.” to protect.
[Chapter LII]
(be released from it.” Shree Krishnù Jee; said, “Listen,) delete the
semicolon.
[Chapter LIII]
“There is a saying, that it most fitting to contract marriages” add
is after it.
“Raja Rheekmuk was at first very sad, when he heard these” to
Bheekmuk.
“Rookum has given thee to Sissoopal; and now Rookmunee, you
have become a queen.” add comma after now.
“pillars formed of sprouting plaintain were fixed in the earth” to
plantain.
[Chapter LIV]
“Shree Kishnù Chund, having thus inspired the brahmin” to
Krishnù.
“to receive them: and having, met the bridegroom with great
honour” delete the comma.
“for the reception of the bridegroon and his train, entertained” to
bridegroom.
[Chapter LV]
(performing the ceremony “Arta,” showerd down flowers) to
showered.
“where Rajas Oorgursen, Soorsen, Basoodeo and all the grandees”
to Oogursen.
[Chapter LVII]
“conferring happiness and wealth; Worship it continually,” to
worship.
“and suffering great afflction, they began to weep and lament” to
affliction.
(explained all his anxieties to his wife His wife said, “O husband!)
add period after the first wife.
[Chapter LVIII]
“went thence to Akroor, joining his hands bowing his head,” add
comma after hands.
“and came to Shree Krishnù Chund, the extirpater of grief” to
extirpator.
“and there was abundance All the distress and want that had
been” add period after abundance.
[Chapter LX]
“Tell me what answer I am to take back from you to this requst”
to request.
“he ate up all the sweatmeats and other confections” to
sweetmeats.
[Chapter LXI]
“abandoning your worship, medidation, prayers, penance and
guidance” to meditation.
[Chapter LXIII]
(at hearing Banasoor, thus address him, and began to say himself,
“I granted) delete the first comma and add to after say.
“Shree Krishuù, will descend upon the earth in the family of
Judoo” to Krishnù.
(Chitrekha replied, “How shall he escape me?” Chitreka proceeded
to) to Chitrekha.
[Chapter LXIV]
“Banasoor worshipping calling to mind, and meditating upon Shree
Muhadeo Jee” add comma after worshipping.
[Chapter LXV]
“as to reckon the number of cows Raja Narug gave away” to
Nurug.
[Chapter LXVI]
“all the cowherds’ chidren came running out, when they saw the
flag” to children.
“all the materials, necessary for the festival were immediately
produced.” Delete the comma.
[Chapter LXIX]
“and all the Kuoruvùs rose haughtily and went to their homes” to
Kouruvùs.
[Chapter LXXII]
“came Shree Krishnù Chund Jee; Wherever they encamped, a
handsome” to wherever.
[Chapter LXXIII]
(I have still one desire.” Krishnù enquired, “What it is?”) to is it.
“the world has been deceived by your delusive power you behave
towards us as a mortal” add exclamation mark after power.
“the money you have given me to whom shall I go now, and ask
for more?” add comma after me.
“my business is to keep watch in a cemetry, and to receive a tax”
to cemetery.
“and died from excess of the hunger het hen endured, and
afterwards” to he then.
[Chapter LXXV]
“Do not use your amrs against him, stand here and look on” to
arms.
[Chapter LXXVI]
“clearing away what food was left, and the treachers, formed of
leaves” to trenchers.
“on the contrary, their character was enchanced” to enhanced.
[Chapter LXXVII]
“when Shree Krishnù Chund and Bulram Jee were at hustinapoor”
to Hustinapoor.
“he constantly rose up and eat a handful of sand; and performed
difficult penance” to ate.
[Chapter LXXVIII]
“this man is without good qualites, avaricious and proud” to
qualities.
[Chapter LXXIX]
“O great king! when Sounuk and all the saints, and sages having
received Bulram Jee’s order” delete the comma after saints.
[Chapter LXXX]
“and he had nothing to eat, One day Soodama’s wife being”
change the comma to a period.
[Chapter LXXXII]
“and Renooka came stood near her husband’s body, she rolled
upon the ground” to came and stood.
“I have had no controul in these matters, the condition or” to
control.
[Chapter LXXXIII]
“O great king! listen, whilst I gave an account of the
conversations” to give.
[Chapter LXXXV]
(Krishnù enquired, “What it was?” Dewukee Jee replied, “Son!
grief) to What is that.
“when the inhabitatns heard this, there was joy throughout the
city” to inhabitants.
[Chapter LXXXVI]
“explained to Buldeo all the secreets connected with the carrying
off Soobhudra” to secrets.
“having beheld the sincere faith of of those two worshippers”
delete one of.
[Chapter LXXXVII]
“for this reason, that one diety is both without, and with passions”
to deity.
“when fourteen worlds became the semblence of water” to
semblance.
“and that on what sidesoever we look, there you are presented” to
side soever.
[Chapter LXXXIX]
“prepared to be burnt upon it with his bow and arows having
then” to arrows.
“upon the head of the king of the serpent face was an
ornamented throne” to race.
“flowers reaching to his feet, and a pearl of necklace” to necklace
of pearls.
[Chapter XC]
“holy and virtuous men sung, the praises of Huri during the eight
watches” delete the comma.
“surpassed the other, how shall I attempt to describe them.”
change period to question mark.
“Whatever benefit is derived from, religious penance, sacrifice,
gifts, fasting, pilgrimage” delete comma after from.

[End of text]
*** END OF THE PROJECT GUTENBERG EBOOK THE PREM SAGUR
***

Updated editions will replace the previous one—the old editions will
be renamed.

Creating the works from print editions not protected by U.S.


copyright law means that no one owns a United States copyright in
these works, so the Foundation (and you!) can copy and distribute it
in the United States without permission and without paying
copyright royalties. Special rules, set forth in the General Terms of
Use part of this license, apply to copying and distributing Project
Gutenberg™ electronic works to protect the PROJECT GUTENBERG™
concept and trademark. Project Gutenberg is a registered trademark,
and may not be used if you charge for an eBook, except by following
the terms of the trademark license, including paying royalties for use
of the Project Gutenberg trademark. If you do not charge anything
for copies of this eBook, complying with the trademark license is
very easy. You may use this eBook for nearly any purpose such as
creation of derivative works, reports, performances and research.
Project Gutenberg eBooks may be modified and printed and given
away—you may do practically ANYTHING in the United States with
eBooks not protected by U.S. copyright law. Redistribution is subject
to the trademark license, especially commercial redistribution.

START: FULL LICENSE


THE FULL PROJECT GUTENBERG LICENSE
PLEASE READ THIS BEFORE YOU DISTRIBUTE OR USE THIS WORK

To protect the Project Gutenberg™ mission of promoting the free


distribution of electronic works, by using or distributing this work (or
any other work associated in any way with the phrase “Project
Gutenberg”), you agree to comply with all the terms of the Full
Project Gutenberg™ License available with this file or online at
www.gutenberg.org/license.

Section 1. General Terms of Use and


Redistributing Project Gutenberg™
electronic works
1.A. By reading or using any part of this Project Gutenberg™
electronic work, you indicate that you have read, understand, agree
to and accept all the terms of this license and intellectual property
(trademark/copyright) agreement. If you do not agree to abide by all
the terms of this agreement, you must cease using and return or
destroy all copies of Project Gutenberg™ electronic works in your
possession. If you paid a fee for obtaining a copy of or access to a
Project Gutenberg™ electronic work and you do not agree to be
bound by the terms of this agreement, you may obtain a refund
from the person or entity to whom you paid the fee as set forth in
paragraph 1.E.8.

1.B. “Project Gutenberg” is a registered trademark. It may only be


used on or associated in any way with an electronic work by people
who agree to be bound by the terms of this agreement. There are a
few things that you can do with most Project Gutenberg™ electronic
works even without complying with the full terms of this agreement.
See paragraph 1.C below. There are a lot of things you can do with
Project Gutenberg™ electronic works if you follow the terms of this
agreement and help preserve free future access to Project
Gutenberg™ electronic works. See paragraph 1.E below.
1.C. The Project Gutenberg Literary Archive Foundation (“the
Foundation” or PGLAF), owns a compilation copyright in the
collection of Project Gutenberg™ electronic works. Nearly all the
individual works in the collection are in the public domain in the
United States. If an individual work is unprotected by copyright law
in the United States and you are located in the United States, we do
not claim a right to prevent you from copying, distributing,
performing, displaying or creating derivative works based on the
work as long as all references to Project Gutenberg are removed. Of
course, we hope that you will support the Project Gutenberg™
mission of promoting free access to electronic works by freely
sharing Project Gutenberg™ works in compliance with the terms of
this agreement for keeping the Project Gutenberg™ name associated
with the work. You can easily comply with the terms of this
agreement by keeping this work in the same format with its attached
full Project Gutenberg™ License when you share it without charge
with others.

1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.

1.E. Unless you have removed all references to Project Gutenberg:

1.E.1. The following sentence, with active links to, or other


immediate access to, the full Project Gutenberg™ License must
appear prominently whenever any copy of a Project Gutenberg™
work (any work on which the phrase “Project Gutenberg” appears,
or with which the phrase “Project Gutenberg” is associated) is
accessed, displayed, performed, viewed, copied or distributed:
This eBook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it
away or re-use it under the terms of the Project Gutenberg
License included with this eBook or online at
www.gutenberg.org. If you are not located in the United
States, you will have to check the laws of the country where
you are located before using this eBook.

1.E.2. If an individual Project Gutenberg™ electronic work is derived


from texts not protected by U.S. copyright law (does not contain a
notice indicating that it is posted with permission of the copyright
holder), the work can be copied and distributed to anyone in the
United States without paying any fees or charges. If you are
redistributing or providing access to a work with the phrase “Project
Gutenberg” associated with or appearing on the work, you must
comply either with the requirements of paragraphs 1.E.1 through
1.E.7 or obtain permission for the use of the work and the Project
Gutenberg™ trademark as set forth in paragraphs 1.E.8 or 1.E.9.

1.E.3. If an individual Project Gutenberg™ electronic work is posted


with the permission of the copyright holder, your use and distribution
must comply with both paragraphs 1.E.1 through 1.E.7 and any
additional terms imposed by the copyright holder. Additional terms
will be linked to the Project Gutenberg™ License for all works posted
with the permission of the copyright holder found at the beginning
of this work.

1.E.4. Do not unlink or detach or remove the full Project


Gutenberg™ License terms from this work, or any files containing a
part of this work or any other work associated with Project
Gutenberg™.

1.E.5. Do not copy, display, perform, distribute or redistribute this


electronic work, or any part of this electronic work, without
prominently displaying the sentence set forth in paragraph 1.E.1
with active links or immediate access to the full terms of the Project
Gutenberg™ License.

1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.

1.E.7. Do not charge a fee for access to, viewing, displaying,


performing, copying or distributing any Project Gutenberg™ works
unless you comply with paragraph 1.E.8 or 1.E.9.

1.E.8. You may charge a reasonable fee for copies of or providing


access to or distributing Project Gutenberg™ electronic works
provided that:

• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”

• You provide a full refund of any money paid by a user who


notifies you in writing (or by e-mail) within 30 days of receipt
that s/he does not agree to the terms of the full Project
Gutenberg™ License. You must require such a user to return or
destroy all copies of the works possessed in a physical medium
and discontinue all use of and all access to other copies of
Project Gutenberg™ works.

• You provide, in accordance with paragraph 1.F.3, a full refund of


any money paid for a work or a replacement copy, if a defect in
the electronic work is discovered and reported to you within 90
days of receipt of the work.

• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.

1.E.9. If you wish to charge a fee or distribute a Project Gutenberg™


electronic work or group of works on different terms than are set
forth in this agreement, you must obtain permission in writing from
the Project Gutenberg Literary Archive Foundation, the manager of
the Project Gutenberg™ trademark. Contact the Foundation as set
forth in Section 3 below.

1.F.

1.F.1. Project Gutenberg volunteers and employees expend


considerable effort to identify, do copyright research on, transcribe
and proofread works not protected by U.S. copyright law in creating
the Project Gutenberg™ collection. Despite these efforts, Project
Gutenberg™ electronic works, and the medium on which they may
be stored, may contain “Defects,” such as, but not limited to,
incomplete, inaccurate or corrupt data, transcription errors, a
copyright or other intellectual property infringement, a defective or
damaged disk or other medium, a computer virus, or computer
codes that damage or cannot be read by your equipment.

1.F.2. LIMITED WARRANTY, DISCLAIMER OF DAMAGES - Except for


the “Right of Replacement or Refund” described in paragraph 1.F.3,
the Project Gutenberg Literary Archive Foundation, the owner of the
Project Gutenberg™ trademark, and any other party distributing a
Project Gutenberg™ electronic work under this agreement, disclaim
all liability to you for damages, costs and expenses, including legal
fees. YOU AGREE THAT YOU HAVE NO REMEDIES FOR
NEGLIGENCE, STRICT LIABILITY, BREACH OF WARRANTY OR
BREACH OF CONTRACT EXCEPT THOSE PROVIDED IN PARAGRAPH
1.F.3. YOU AGREE THAT THE FOUNDATION, THE TRADEMARK
OWNER, AND ANY DISTRIBUTOR UNDER THIS AGREEMENT WILL
NOT BE LIABLE TO YOU FOR ACTUAL, DIRECT, INDIRECT,
CONSEQUENTIAL, PUNITIVE OR INCIDENTAL DAMAGES EVEN IF
YOU GIVE NOTICE OF THE POSSIBILITY OF SUCH DAMAGE.

1.F.3. LIMITED RIGHT OF REPLACEMENT OR REFUND - If you


discover a defect in this electronic work within 90 days of receiving
it, you can receive a refund of the money (if any) you paid for it by
sending a written explanation to the person you received the work
from. If you received the work on a physical medium, you must
return the medium with your written explanation. The person or
entity that provided you with the defective work may elect to provide
a replacement copy in lieu of a refund. If you received the work
electronically, the person or entity providing it to you may choose to
give you a second opportunity to receive the work electronically in
lieu of a refund. If the second copy is also defective, you may
demand a refund in writing without further opportunities to fix the
problem.

1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.

1.F.5. Some states do not allow disclaimers of certain implied


warranties or the exclusion or limitation of certain types of damages.
If any disclaimer or limitation set forth in this agreement violates the
law of the state applicable to this agreement, the agreement shall be
interpreted to make the maximum disclaimer or limitation permitted
by the applicable state law. The invalidity or unenforceability of any
provision of this agreement shall not void the remaining provisions.

1.F.6. INDEMNITY - You agree to indemnify and hold the Foundation,


the trademark owner, any agent or employee of the Foundation,
anyone providing copies of Project Gutenberg™ electronic works in
accordance with this agreement, and any volunteers associated with
the production, promotion and distribution of Project Gutenberg™
electronic works, harmless from all liability, costs and expenses,
including legal fees, that arise directly or indirectly from any of the
following which you do or cause to occur: (a) distribution of this or
any Project Gutenberg™ work, (b) alteration, modification, or
additions or deletions to any Project Gutenberg™ work, and (c) any
Defect you cause.

Section 2. Information about the Mission


of Project Gutenberg™
Project Gutenberg™ is synonymous with the free distribution of
electronic works in formats readable by the widest variety of
computers including obsolete, old, middle-aged and new computers.
It exists because of the efforts of hundreds of volunteers and
donations from people in all walks of life.

Volunteers and financial support to provide volunteers with the


assistance they need are critical to reaching Project Gutenberg™’s
goals and ensuring that the Project Gutenberg™ collection will
remain freely available for generations to come. In 2001, the Project
Gutenberg Literary Archive Foundation was created to provide a
secure and permanent future for Project Gutenberg™ and future
generations. To learn more about the Project Gutenberg Literary
Archive Foundation and how your efforts and donations can help,
see Sections 3 and 4 and the Foundation information page at
www.gutenberg.org.

Section 3. Information about the Project


Gutenberg Literary Archive Foundation
The Project Gutenberg Literary Archive Foundation is a non-profit
501(c)(3) educational corporation organized under the laws of the
state of Mississippi and granted tax exempt status by the Internal
Revenue Service. The Foundation’s EIN or federal tax identification
number is 64-6221541. Contributions to the Project Gutenberg
Literary Archive Foundation are tax deductible to the full extent
permitted by U.S. federal laws and your state’s laws.

The Foundation’s business office is located at 809 North 1500 West,


Salt Lake City, UT 84116, (801) 596-1887. Email contact links and up
to date contact information can be found at the Foundation’s website
and official page at www.gutenberg.org/contact

Section 4. Information about Donations to


the Project Gutenberg Literary Archive
Foundation
Project Gutenberg™ depends upon and cannot survive without
widespread public support and donations to carry out its mission of
increasing the number of public domain and licensed works that can
be freely distributed in machine-readable form accessible by the
widest array of equipment including outdated equipment. Many
small donations ($1 to $5,000) are particularly important to
maintaining tax exempt status with the IRS.

The Foundation is committed to complying with the laws regulating


charities and charitable donations in all 50 states of the United
States. Compliance requirements are not uniform and it takes a
considerable effort, much paperwork and many fees to meet and
keep up with these requirements. We do not solicit donations in
locations where we have not received written confirmation of
compliance. To SEND DONATIONS or determine the status of
compliance for any particular state visit www.gutenberg.org/donate.

While we cannot and do not solicit contributions from states where


we have not met the solicitation requirements, we know of no
prohibition against accepting unsolicited donations from donors in
such states who approach us with offers to donate.

International donations are gratefully accepted, but we cannot make


any statements concerning tax treatment of donations received from
outside the United States. U.S. laws alone swamp our small staff.

Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.

Section 5. General Information About


Project Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could be
freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose network of
volunteer support.
Project Gutenberg™ eBooks are often created from several printed
editions, all of which are confirmed as not protected by copyright in
the U.S. unless a copyright notice is included. Thus, we do not
necessarily keep eBooks in compliance with any particular paper
edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg Literary
Archive Foundation, how to help produce our new eBooks, and how
to subscribe to our email newsletter to hear about new eBooks.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

textbookfull.com

You might also like