HAPPY BOOKSGIVING
Use code BOOKSGIVING during checkout to save 40%-55% on books and eBooks. Shop now.
This eBook includes the following formats, accessible from your Account page after purchase:
EPUB The open industry format known for its reflowable content and usability on supported mobile devices.
PDF The popular standard, used most often with the free Acrobat® Reader® software.
This eBook requires no passwords or activation to read. We customize your eBook by discreetly watermarking it with your name, making it uniquely yours.
Also available in other formats.
Register your product to gain access to bonus material or receive a coupon.
Becoming a coder is all fun and games!
Everyone should learn to code. Much like drawing and sketching, playing an instrument, cooking, or taking pictures and shooting videos, coding is a creative endeavor, which means it's a way to actually create stuff, and creating stuff is incredibly rewarding and satisfying. Sure, it's fun to spend hours on your phone looking at what other people have created; but that's nothing compared to the joy and satisfaction of creating things that other people consume and use. Yep, coding is fun!
And if that weren't enough, when you learn to code you develop all sorts of invaluable skills and traits beyond just coding. These include planning, problem solving, communication, logic, empathy, attention to detail, patience, resilience, persistence, and creativity. And it turns out that these skills (especially creativity and creative problem solving) are some of the most in-demand out there. So, coding will help your future career, too, regardless of what that career may be.
But, where to start?
Captain Code is a welcoming, engaging, and fun introduction to becoming a coder, designed for the young (ages 10-17) and young-at-heart. Experienced educators and coders Ben & Shmuel Forta will guide you using Python, one of the most popular programming languages in the world. You'll learn by creating games, yes, games, from simple projects to retro text-based adventures to complete graphical arcade style games. Captain Code is 400 glossy color pages of goodness packed with welcoming images, useful tips and tidbits, and engaging, readable text that focuses on doing while having fun. All code listings are in full-color and QR codes link to bonus content, downloads, challenge solutions, and more.
Captain Code makes coding exciting and rewarding, as it prepares a new generation to take their next steps forwardin education, careers, or both.
So, are you ready to unleash your coding superpower and become Captain Code?
Visit Ben Forta's website for access to challenge solutions, downloadable game images, code, and more.
Introduction xv
PART I: IT'S ALL FUN AND GAMES 1
Chapter 1 Getting Started 3
Understanding Computer Programming 4
What is a computer? 4
How do we talk to computers? 5
What is Python? 8
Setting Things Up 9
Installing Python 9
Installing and Configuring Visual Studio Code 10
Creating a Work Folder 13
Writing Your First Python Program 15
Selecting Your Work Folder 16
It's Coding Time! 17
Summary 19
Chapter 2 Mad Libs 21
Understanding Functions 22
Using Variables 23
Creating a Variable 24
Using a Variable 24
Some Important Variable Rules 25
Variables, More Variables, and Even More Variables 26
Getting User Input 28
Playing Mad Libs 30
Write Your Story 30
Add Variables 30
Get User Input 32
Summary 33
Chapter 3 Roll the Dice 35
Using Libraries 36
The random Library 36
Generating Random Numbers 37
Choosing a Random Item 38
3 Is Not 3 41
Commenting Your Code 43
One Die, Two Dice 45
Summary 49
Chapter 4 Calculate the Day 51
Working with Dates 52
The datetime Library 52
Using the datetime Class 55
Making Decisions 56
The if Statement 56
What else? 58
if Revisited 59
Testing for Other Options 61
Using in 62
Beating the Mathematician 63
Handling Numeric Inputs 63
Putting It All Together 64
An Alternate Solution 67
Summary 67
Chapter 5 Rock Paper Scissors 69
More Strings 70
Game Time 72
Handling User Input 72
The Game Code 74
One Last Tweak 76
Summary 77
Chapter 6 Secret Codes 79
Lists 80
Creating Lists 80
Accessing List Items 82
Changing List Items 83
Adding and Removing Items 84
Finding Items 85
Sorting 86
Loop-de-Loop 89
Looping Through Items 90
Looping Through Numbers 92
Nested Loops 93
Cracking the Code 95
Encrypting Characters 96
Modulus Math 97
Encryption Code 98
Decryption Code 102
Summary 104
Chapter 7 Guess the Number 105
Conditional Loops 106
Game Time 111
The Basic Game 111
Putting It All Together 116
Summary 120
Chapter 8 Becoming a Coder 121
How Coders Code 122
Have a Plan 122
Think Small 123
Game Components 124
Restricting User Input 125
Storing User Guesses 128
Displaying Lists 129
Masking Characters 131
Summary 136
Chapter 9 Hangman 137
Game Time 138
So How Does It Work? 141
Summary 148
Chapter 10 Keep Going 149
Birthday Countdown 150
Program Requirements 150
Program Flow 150
Some Tips 151
Tip Calculator 152
Program Requirements 152
Program Flow 153
Some Tips (Pun Intended) 153
Password Generator 154
Program Requirements 154
Program Flow 155
Some Tips 155
Summary 159
PART II: ON AN ADVENTURE 161
Chapter 11 Getting Func-ky 163
Functions Revisited 164
Creating a Function 165
Passing Arguments 167
Returning Values 171
Summary 175
Chapter 12 Exploring 177
Game Concept 178
Game Structure 179
Prompting for Options 181
Processing Options 182
Create a Work Folder 183
Game Time 184
Test It 189
Summary 191
Chapter 13 Cleanup Time 193
Optimizing Your Code 194
String Externalization 196
Creating the Strings File 196
Using Externalized Strings 200
Summary 201
Chapter 14 Reduce, Reuse, Recycle, Refactor 203
Understanding Refactoring 204
Identifying Refactoring Opportunities 205
Creating a User Choice Component 207
Designing a Reusable Component 208
Creating the User Options Function 213
Updating Your Code 218
Summary 221
Chapter 15 Carrying (and Using) Stuff 223
Planning the Inventory System 224
Creating a Dictionary 225
Working with Dictionaries 226
Lists of Dictionaries 228
The Inventory System 229
Creating an Inventory 230
Plugging In the Inventory System 232
Using the Inventory System 233
Displaying the Inventory 238
Summary 239
Chapter 16 Keeping It Classy 241
The Player System 242
Creating a Player Class 243
Creating the Class 243
Defining Properties 244
Creating Methods 247
Initializing the Class 250
Using Our New Class 251
Summary 255
Chapter 17 Color Your World 257
Installing Third-Party Libraries 258
Using Colorama 259
Importing and Initializing the Library 259
Coloring Your Output 260
Summary 264
Chapter 18 Keep Going 265
Health and Lives 266
Shopping for Items 271
Random Events 275
Battling Enemies 277
Saving and Restoring 280
Summary 282
PART III: RACING AROUND 283
Chapter 19 Crazy Driver 285
Introducing Pygame 286
Prepping the Game 286
Game Concept 286
Installing Pygame 288
Creating Work Folders 288
Obtaining Images 289
Getting Started 289
Initializing Pygame 290
Displaying Stuff 294
The Game Loop 295
Summary 300
Chapter 20 Image-ine the Possibilities 301
Files and Folders 302
Setting the Background 305
Placing the Cars 310
Summary 317
Chapter 21 We Like to Move It 319
Moving the Enemy 320
Moving the Player 323
Summary 327
Chapter 22 Crash, Bang, Boom 329
You Crashed, Game Over 330
Tracking Score 332
Increasing Difficulty 334
Summary 336
Chapter 23 Finishing Touches 337
Game Over Revisited 338
Pause 341
Varying Enemies 343
Ice Cubes 348
Summary 351
Chapter 24 Keep Going 353
Splash Screen 354
Scores and High Scores 354
Oil Slick 356
Multiple Enemies 357
And Then 358
Summary 359
What Next? 361
There's a Lot More to Python 362
Web Development 362
Mobile App Development 364
Game Development 364
And Then 365
Index 367
Bonus Online Chapter 25 Tinkering, Testing, and Debugging Revisited (Online Only)