100% found this document useful (1 vote)
41 views

PDF Python, Pygame, and Raspberry Pi Game Development 2nd Edition Sloan Kelly download

Game

Uploaded by

nyiribuyckz0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
41 views

PDF Python, Pygame, and Raspberry Pi Game Development 2nd Edition Sloan Kelly download

Game

Uploaded by

nyiribuyckz0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Download the Full Version of textbook for Fast Typing 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

Download More textbook Instantly Today - Get Yours Now at 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 orders-ny@springer-sbm.com, 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 rights@apress.com, 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
The Battle of Manila, May 1, 1898.—The American Fleet.

The Battle of Manila, May 1, 1898.—The Spanish Fleet.


Steaming straight for the little promontory of Cavité, under the
protection of the batteries of which the Spanish squadron was lying,
he at once opened fire, as has already been said, and with the result
that, in the course of the morning, he destroyed the Reina Christina,
a cruiser of 3000 tons, mounting nineteen Hontoria guns, from six to
four inches in calibre, two machine guns and five torpedo tubes;
Castilla, 2350 tons, ten Krupp guns and four revolving cannon, with
three torpedo tubes; Don Antonio de Ulloa, 1152 tons, eight Hontoria
guns, one machine gun and two torpedo tubes; Isla de Cuba, 1040
tons, twelve guns and three torpedo tubes; Marques del Duero,
gunboat, 500 tons, three guns and one torpedo tube; El Cano,
gunboat, 525 tons, three guns, three machine guns and one torpedo
tube; Velasco, cruiser, 1139 tons, five guns and two machine guns;
and the Isla de Mindinao, an armed transport of 4195 tons.
There were several other small gun boats, and one which came
into the harbor some days after the battle and was taken. The
batteries at Cavité were silenced at the same time that the force
afloat was destroyed.
Our loss was limited to eight wounded by an explosion on board
the Baltimore, while Commodore Dewey reported the Spanish loss
as not fully known, but certainly one hundred and fifty killed,
including the captain of the Reina Christina. After Cavité was
possessed he reported two hundred and fifty sick and wounded
within his lines.
Before the battle had been long going on the Reina Christina, the
flag-ship of Admiral Montijo was struck by a shell which set her on
fire, forward. As they were soon compelled by the flames to abandon
her, the Admiral transferred his flag to the Isla de Cuba, and soon
after this time the Don Antonio de Ulloa burst into flames. The
Spanish stuck well to their guns, but seemed to lack practice, as
their aim was for the most part bad, and their shot either fell short or
went clean over the American vessels. The guns on shore, at Cavité,
were also badly served, and did no harm to the assailants, although
the Spaniards fought with great courage to the last. When these
were silenced a small force was landed from the Petrel to occupy the
place, and the medical officers landed to assist the Spanish
wounded.
On Monday, the 2d of May, the American squadron went up
opposite Manila and anchored. Of course it was impossible for
Commodore Dewey to occupy a place extending over so much
ground without a land force, but he held the city completely under his
guns.
The news of the action was received with great joy all over our
country, and on the day the news was received the Secretary of the
Navy sent the following message, along with a confidential despatch
to the Commander in Chief of the Asiatic Squadron:

Washington, May 7th.


Dewey, Manila:—
“The President, in the name of the American people, thanks you
and your officers and men for your splendid achievement and
overwhelming victory. In recognition he has appointed you an Acting
Admiral, and will recommend a vote of thanks to you by Congress.”
Long.
The thanks of Congress were promptly voted, with a sword for
Commodore Dewey, and a medal for each officer and man who took
part in the engagement. Congress also increased the number of
Rear Admirals from six to seven, so that the president might appoint
Dewey a Rear Admiral, which was done at once, and the
appointment confirmed by the Senate.
In considering this action it must be remembered that, although
Dewey’s vessels were more powerful than those of the Spanish
Navy, he had the disadvantage of advancing into strange waters,
where shoals existed, and where, for all he knew, torpedoes and
mines were laid. In fact two of the latter were exploded in front of the
squadron, but so hurriedly as to do no harm. He had also the shore
batteries to contend against, which made the opposing weight of
metal more than equal to his. “The Spanish admiral” says a
contemporary journal, “though he must have been aware that the
American squadron was somewhere in the vicinity, could not bring
himself to believe that the American commodore would have the
audacity to steam into a mined harbor in the night time, with forts on
both sides, and the Spanish squadron ready to receive him. But
Dewey took the chances, and his being beforehand was half the
victory. Many men, equally as brave in action, would have delayed to
reconnoitre, and thereby have given time for the enemy to make
additional preparations to receive him.
In consequence of Dewey’s disregard of possible danger, he found
the Spanish ships in a cramped position where they could best be
attacked.
There is also another thing to be noticed about the engagement at
Manila Bay, and that is in regard to torpedo boats. It certainly seems
that they are not the danger they are supposed to be, if used in
daylight. Two of the Spanish torpedo boats from Cavite were
directed against the Olympia, and were seen as soon as they
started. They escaped several large projectiles directed at them by
the great guns of the flag-ship, but they were easily destroyed when
the six-pounder rapid firers got trained upon them. In daylight the
torpedo boat is no longer to be feared. What a night attack will do,
under the glare of the search lights, is more uncertain. For harbor
defense torpedo boats may be very useful, but they are too wearing,
on both officers and men, for any prolonged sea service.
Dewey’s action has taught us several things, for, except the
Japan-China war, there had been no fighting with the new ships, and
Dewey’s victory was a glorious one in its conduct and its results. It
has also been useful in teaching the nations what they did not know
before, and in impressing more strongly what they had some
apprehension of. It showed that originality and dash, after careful
planning and adequate preparation, will generally succeed. With
several fortified positions on shore the advantage should have
certainly been with the Spanish forces, but in spite of the great risk
that every officer and every man must have known was being taken,
“there was not a faint heart in all that squadron, but an enthusiasm
and esprit du corps that could not but win with such a leader.”
As far as a naval action went, that at Manila was a “clean cut”
thing. Nothing can take away from the small, well-drilled, well-
manned and well-officered East Indian Squadron the credit they
have so thoroughly well earned.
Rear Admiral Dewey was born in Montpelier, Vt., in 1838. Dr.
Julius Y. Dewey, the father of the admiral, was a cultured gentleman
of the old school, honored for his stalwart integrity and stern force of
character. Admiral Dewey’s mother was celebrated throughout
Vermont, her native state, for remarkable beauty of person and
grace of manner.
The handsome colonial mansion in Montpelier was long a center
of New England hospitality, and the Deweys were paramount among
the first families of Vermont.
When Dewey was fourteen years old, a desire for a sea-faring life
took possession of him, but his father did not take kindly to the
thought of his son’s becoming a sailor; so a compromise was
effected, and young Dewey left the Montpelier public school and
entered the Norwich University at Northfield, Vt. This being a military
school the youthful enthusiasm of the boy was temporarily appeased
by musket practice and drill; but after two years had passed, Dr.
Dewey decided that if his son must follow the sea, he should do so in
a manner consistent with the Dewey stock.
An appointment to the Naval Academy placed the future Admiral
at Annapolis in 1858. Upon his graduation, in 1858, he cruised for a
few years as midshipman on the steam frigate Swatara in the
Mediterranean and then returned to Montpelier.
On April 19, 1861, Dewey was commissioned Lieutenant, and for
two years served on the steam sloop Mississippi, taking part in the
action of the West Gulf Squadron. The Mississippi grounded, and
was riddled by the shore batteries at Port Hudson. The officers and
men were landed on the opposite side by boats; Captain Smith and
Lieutenant Dewey being the last to leave the ship. In 1863 the young
Lieutenant was in the thick of the fight with the gunboats that met the
Confederates below Donaldsonville, and subsequently served on the
steam gunboat Agawam, Captain Rhind, of the North Atlantic
Squadron, and participated in two attacks on Fort Fisher in 1864-
1865.
Dewey was commissioned Lieutenant Commander March 3, 1865,
and a year later became executive officer of the famous Kearsarge.
He also served on the frigate Colorado, flagship of the European
Squadron.
On returning to the United States in 1868, he was detailed for duty
at Annapolis, remaining for two years.
Dewey received his first command—the Narragansett—in 1870,
and was engaged in special service till 1875, two years of which he
had charge of the Pacific Survey. Meantime he became a
Commander.
Commander Dewey became Light House Inspector in 1876, and
was Secretary of the Light House Board from 1877 to 1882, when he
was assigned to the command of the Juniata, of the Asiatic
Squadron. In 1884 he was promoted to be Captain, and took
command of the Dolphin, one of the first craft of the new Navy, and
afterwards of the Pensacola, flagship of the European Squadron.
In 1888 Captain Dewey served as Chief of the Bureau of
Equipment and Recruiting, with rank of Commodore. He was
commissioned Commodore February 28, 1896. From 1893 to 1895
Commodore Dewey was a member of the Light House Board. In
1896 and 1897, he was President of the Inspection and Survey
Board. On November 30, 1897, he was assigned to the command, of
the Asiatic Squadron, and assumed his duties January 3, 1898.
In recognition of his splendid achievement at Manila, Commodore
Dewey was appointed a Rear Admiral in May, 1898.
Admiral Dewey married the daughter of the celebrated Governor
Goodwin, War Governor of New Hampshire—a fighter of the old
school. Mrs. Dewey did not long survive the birth of her only son,
George Goodwin Dewey.
Admiral Dewey made his home in Washington after his wife’s
death. He was fond of horseback exercise, being never without a
thoroughbred animal, and he treated them with a considerate
kindness that was characteristic of the man.
Admiral Dewey was an early riser, and spent most of his time in
the public service. He was temperate to the degree of
abstemiousness. He was Life President of the Metropolitan Club, of
Washington, member of the University Club, of New York, and for
some time a member of the Somerset Club, of Boston. During his
earlier residence in Washington, he was a member of the Maryland
Hunting Club, but later his active public service prevented him
indulging his taste for gunning.
Admiral Dewey’s son said of him: “Father has always been an
extremely active man. He has been a lifelong student of everything
connected with the sea. He is a constant reader, but in his studies he
seldom goes outside of nautical science, or some collateral branch,
such as Naval History. He made a study of harbors, too, and is a
thorough geographer. I attribute his success at Manila in part to his
knowledge of the harbor. He undoubtedly knew just what he was
doing and where he was going when he made that midnight dash
which seems to be so amazing to people who don’t know him. He
knows how to navigate; he never carried a pilot all the time he was
captain. He did his own navigating.
“Then, too, father had every confidence in his men, and the feeling
was reciprocal, which was another strong element that contributed to
his success. He knew what they could do. The cardinal principle of
my father has been: ‘Whatever is worth doing is worth doing well.’”
This was the keynote of the life of George Dewey, whose name in
a day was placed with those of John Paul Jones, Decatur, Farragut,
and others who have shone in Naval History.
Transcriber’s Notes

Except as described under Changes below, this e-


text uses the language used in the source
document. Inconsistent spelling, capitalisation,
spacing and hyphenation, unusual, obsolete and
archaic language, etc. have been retained, also in
proper, geographical and ships’ names. Accents
have not been added or corrected. Differences in
wording between the Lists of Illustrations and the
captions in the text have not been rectified.
Chapter numbering: in the first book chapters are
numbered I through XV (which has been
corrected to XIV) in the Table of Contents, and I
through XIV in the text. The remainder of the
chapters (including those in the second book) are
not numbered. This oddity has been retained in
this e-text.
Page numbering: this e-text uses prefixes I and II in
the page numbers for the first and second book.
Page I-203: But they ‘(the French)’ having covered
...: as printed in the source document; possibly an
error for But they” (the French) “having covered
....
Page I-237, ... than deserving gentlemen sent out by
the Admiralty.”: either the closing quote mark is
redundant, or the opening quote mark is missing.
Page I-358, “Villeneuve, like others, ...: the closing
quote mark is missing.
Page II-62, “It was also asserted that the Guerrière’s
powder was bad; ...: the closing quote mark is
missing.
Page II-251, “The Richmond waved to me ...: the
closing quote mark is missing.
Page II-397, Abbreviations: the abbreviations
referring to the engines do not occur in the
table(s).
Page II-429, ... “though he must have been aware
...: the closing quote mark is missing.
Changes
Illustrations have been moved out of text
paragraphs.
Some obvious minor typographical and punctuation
errors have been corrected silently.
Soleil Royale has been corrected to Soleil Royal
throughtout the book.
Page I-VI: ... the Carthaginians at Myloe ... has been
changed to ... the Carthaginians at Mylœ ... as in
the text.
Page I-XI: XV. DE GRASSE AND RODNEY. A. D.
1782 has been changed to XIV. DE GRASSE
AND RODNEY. A. D. 1782.
Page I-XV: Disputes over Corea has been changed
to Disputes over Korea as in text.
Page I-XVII: page number 226 has been changed to
195 (Le Soleil Royal); entry 21a Dutch Man-of-
War, 17th Century. inserted.
Page I-72: ... too unwieldly to be brought up again
has been changed to ... too unwieldy to be
brought up again.
Page I-265: ... the Genereux and Timoléon ... has
been changed to ... the Généreux and Timoléon ...
as elsewhere.
Page I-270: a superfluous page reference has been
deleted from caption NELSON WOUNDED AT
TENERIFFE.
Page I-353: closing quote mark has been added
after All is mine, right or wrong.
Page I-355: closing quote mark has been inserted
after ... to continue them there until the business
is decided), ....
Page I-393: Opening quote mark has been inserted
before When once he had explained his system
....
Page I-463: ... but when the inflexible and the
Téméraire ... has been changed to ... but when
the ... Inflexible and the Téméraire ....
Page II-XI, List of illustrations: entries 18 and 19
have been inserted.
Page II-XI, List of Illustrations: items 31 (American
Fleet) and 32 (Manila Bay Map) were
interchanged according to their position in the
book.
Page II-62: closing quote mark has been inserted
after The Guerrière, moreover, was out-
manœuvred.
Page II-227: closing quote mark has been inserted
after ... your thundering bad navigation.
Page II-235: closing quote mark has been inserted
after D. G. F.
Page II-270: opening quote mark inserted before
Completely exhausted, I managed to reach the
shore, ....
Page II-344: The lastest completed battle-ships ...
has been changed to The latest completed battle-
ships ....
Page II-388: closing quote mark has been inserted
after This was altered and other changes made.
Page II-395, table row Foote, column Officers: 44
has been changed to 4.
Page II-393-397: the pages have been re-arranged
to form complete tables; the page numbering as
used in the book has been preserved. Page 394:
decimal commas in the column Speed have been
replaced with decimal points for consistency with
the remainder of the table. The data have been
aligned more consistently for better readability.
*** END OF THE PROJECT GUTENBERG EBOOK NAVAL
BATTLES OF THE WORLD ***

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
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