Statistics
6
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2026-05-28

AuthorTristan Bunn

This beginners book introduces non-programmers to the fundamentals of computer coding within a visual, arts-focused context. Tristan Bunn’s remarkably effective teaching approach is designed to help you visualize core programming concepts while you make cool pictures, animations, and simulations using Python Mode for the open-source Processing development environment. Right from the first chapter, you'll produce and manipulate colorful drawings, shapes and patterns as Bunn walks you through a series of easy-to-follow graphical coding projects that grow increasingly complex. You’ll go from drawing with code to animating a bouncing DVD screensaver and practicing data-visualization techniques. Along the way, you’ll encounter creative-yet-practical skill-building challenges that relate to everything from video games, cars, and coffee, to fine art, amoebas, and Pink Floyd. As you grow more fluent in both Python and programming in general, topics shift toward the mastery of algorithmic thinking, as you explore periodic motion, Lissajous curves, and using classes to create objects. You’ll learn about: • Basic coding theories and concepts, like variables, data types, pixel coordinates, control flow and algorithms • Writing code that produces drawings, patterns, animations, data visualizations, user interfaces, and simulations • Using conditional statements, iteration, randomness, lists and dictionaries • Defining functions, reducing repetition, and making your code more modular • How to write classes, and create objects to structure code more efficiently In addition to giving you a good grounding in general programming, the skills and knowledge you’ll gain in this book are your entry point to coding for an ever-expanding horizon of creative technologies.

Tags
No tags
ISBN: 1718500971
Publisher: No Starch Press
Publish Year: 2021
Language: 英文
Pages: 296
File Format: PDF
File Size: 9.0 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

F U L L COLOR T R I S T A N B U N N L E A R N P Y T H O N V I S U A L L Y C R E A T I V E C O D I N G W I T H P R O C E S S I N G . P Y
LEARN PYTHON VISUALLY
(This page has no text content)
San Francisco L E A R N P Y T H O N V I S U A L LY C R E A T I V E C O D I N G W I T H P R O C E S S I N G . P Y by Tr is tan Bunn
LEARN PYTHON VISUALLY. Copyright © 2021 by Tristan Bunn. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. 25 24 23 22 21 1 2 3 4 5 6 7 8 9 ISBN-13: 978-1-7185-0096-9 (print) ISBN-13: 978-1-7185-0097-6 (ebook) Publisher: William Pollock Executive Editor: Barbara Yien Production Editor: Katrina Taylor Developmental Editors: Annie Choi and Jill Franklin Cover Design: Gina Redman Interior Design: Octopod Studios Technical Reviewer: Paddy Gaunt Copyeditor: Sharon Wilkey Compositor: Craig Woods, Happenstance Type-O-Rama Proofreader: Emelie Battaglia Indexer: BIM Creatives, LLC For information on book distributors or translations, please contact No Starch Press, Inc. directly: No Starch Press, Inc. 245 8th Street, San Francisco, CA 94103 phone: 1-415-863-9900; info@nostarch.com www.nostarch.com Library of Congress Control Number: 2020950273 No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
About the Author Tristan Bunn kicked off his web design career back in the days of PlayStation 1, grunge music, and dial-up modems. Since then, he’s worked on a diverse range of digital projects for varied clients. He’s currently involved in lecturing, research, and work that blends code, interaction, interface design, and creativity. Tristan has years of experience teaching coding for art, games, web, and other creative technologies. About the Tech Reviewer Paddy Gaunt studied engineering at Cambridge University (UK), working in the chemical and gas industries as well as textile manufacturing. Much of the time, he had the responsibility of implementing IT systems as these became a more significant part of management and marketing. Since its launch in 2012, he has been the chief maintainer of the pi3d Python mod- ule for fast 3D graphics on the Raspberry Pi microcomputer.
(This page has no text content)
B R I E F C O N T E N T S Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Chapter 1: Hello, World! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Chapter 2: Drawing More Complicated Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Chapter 3: Introduction to Strings and Working with Text . . . . . . . . . . . . . . . . . . . . . . . . . 53 Chapter 4: Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Chapter 5: Iteration and Randomness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Chapter 6: Motion and Transformation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Chapter 7: Working with Lists and Reading Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Chapter 8: Dictionaries and JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Chapter 9: Functions and Periodic Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Chapter 10: Object-Oriented Programming and PVector . . . . . . . . . . . . . . . . . . . . . . . . . 207 Chapter 11: Mouse and Keyboard Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 Afterword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
(This page has no text content)
C O N T E N T S I N D E T A I L ACKNOWLEDGMENTS XIII INTRODUCTION XV Who Is This Book For? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi What Is Python Mode for Processing? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi What Are Algorithms? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii What Is Creative Coding? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xviii Where Can I Find Help? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Source Code and Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi What’s in This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Let’s Go! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii 1 HELLO, WORLD! 1 Processing Installation and Python Mode Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Your First Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Fills and Strokes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Background Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Color Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2D Primitives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 triangle() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 ellipse() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 quad() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 line() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Challenge #1: Rainbow Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Basic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Modulo Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Arcs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Challenge #2: Disk Usage Analyzer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2 DRAWING MORE COMPLICATED SHAPES 29 Displaying a Grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Drawing Curves Using Catmull-Rom Splines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Curving Lines with curve() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Changing Curves with curveTightness() . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 C O N T E N T S I N D E T A I L CREATIVE CODING WITH PROCESSING.PY III About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .v About the Tech Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .v ACKNOWLEDGMENTS XIII INTRODUCTION XV Who Is This Book For? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi What Is Python Mode for Processing? . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi What Are Algorithms? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii What Is Creative Coding? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xviii Where Can I Find Help? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Source Code and Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi What’s in This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Let’s Go! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii 1 HELLO, WORLD! 1 Processing Installation and Python Mode Setup . . . . . . . . . . . . . . . . . . . . . . 2 Your First Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Fills and Strokes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Background Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Color Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2D Primitives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 triangle() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 ellipse() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 quad() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 line() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 CHALLENGE #1: RAINBOW TASK 18 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Basic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Modulo Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Arcs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 CHALLENGE #2: DISK USAGE ANALYZER 27 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
x   Contents in Detail Drawing Bézier Curves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Using the bezier() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Positioning Anchor and Control Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Drawing Shapes Using Vertices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Bézier Vertices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Using Vector Graphics Software for Generating Shapes . . . . . . . . . . . . . . . . . . . . . . . 50 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 3 INTRODUCTION TO STRINGS AND WORKING WITH TEXT 53 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Creating Strings in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Using Concatenation and String Formatting . . . . . . . . . . . . . . . . . . . . . . . . . 56 Working with String Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 String Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Slice Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Typography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Text Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 4 CONDITIONAL STATEMENTS 69 Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 The Boolean Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 elif Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 else Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Challenge #3: Four-Square Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 5 ITERATION AND RANDOMNESS 85 Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Using Iteration to Draw Concentric Circles . . . . . . . . . . . . . . . . . . . . . . . . . . 86 while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Challenge #4: Create Line Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 break and continue Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Randomness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 random() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Random Seed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Truchet Tiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Contents in Detail   xi 6 MOTION AND TRANSFORMATION 105 Perceiving Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Adding Motion to Processing Sketches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 The draw() and setup() Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Saving Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Challenge #5: DVD Screensaver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Processing Transformation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 translate() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 rotate() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 scale() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 shearX() and shareY() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 pushMatrix() and popMatrix() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Challenge #6: Analog Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 7 WORKING WITH LISTS AND READING DATA 133 Introducing Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Creating and Accessing Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Modifying Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Combining Loops and Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 Drawing Shapes by Using a List of Color Values . . . . . . . . . . . . . . . . . . . . . 140 Looping with enumerate() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Creating Lists of Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Challenge #7: Breakout Level. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Reading Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 File Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 CSV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Challenge #8: Games Sales Chart. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 8 DICTIONARIES AND JSON 159 Introducing Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 Accessing Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Modifying Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Nesting Dictionaries and Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Combining Loops and Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Iterating Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Iterating Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Iterating Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Working with JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Understanding JSON Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Using Web APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Reading in JSON Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 Challenge #9: Coffee Chart. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
xii    9 FUNCTIONS AND PERIODIC MOTION 175 Defining Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Creating a Simple Speech Bubble Function . . . . . . . . . . . . . . . . . . . . . . . . 176 Drawing Compound Shapes Using a Function . . . . . . . . . . . . . . . . . . . . . . 179 Adding Arguments and Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Using Keyword Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Setting Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Mixing Positional and Keyword Arguments . . . . . . . . . . . . . . . . . . . . . . . . 186 Returning Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 Defining Functions for Periodic Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 An Introduction to Trigonometric Functions . . . . . . . . . . . . . . . . . . . . . . . . . 190 Circular and Elliptical Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 Sine Waves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 Lissajous Curves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 Creating Screensaver-Like Patterns with Lissajous Curves . . . . . . . . . . . . . . . 203 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 10 OBJECT-ORIENTED PROGRAMMING AND PVECTOR 207 Working with Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Defining a New Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Creating an Instance from a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 Adding Attributes to a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 Adding Methods to a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 Splitting Your Python Code into Multiple Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 Programming Movement with Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 The PVector Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 Moving an Amoeba with PVector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 Adding Many Amoebas to the Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Challenge #10: Collision Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 11 MOUSE AND KEYBOARD INTERACTION 239 Mouse Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 Mouse Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 Mouse Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 Creating a Paint App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 Keyboard Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 Adding Keyboard Shortcuts to the Paint App . . . . . . . . . . . . . . . . . . . . . . . 251 Challenge #11: Adding Paint App Features . . . . . . . . . . . . . . . . . . . . . . 252 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 AFTERWORD 255 INDEX 259
A C K N O W L E D G M E N T S I’ve been a fan of No Starch Press books for years, and I’m thrilled to have them publish my first book. I’d like to thank everybody there, in particular my editors, Jill Franklin and Annie Choi, for their invaluable feedback and direction. Thanks to Paddy Gaunt, the technical reviewer, for checking over all my code and offering some excellent suggestions to improve it. Additionally, I’d like to thank the creators, maintainers, and community surrounding Processing and its related projects, and also the developers of the Python programming language. You’ve inspired my students and me, and it’s a privilege to share your hard work with everybody who reads this book.
(This page has no text content)
When I first encountered programming code, I stared, amazed and bewildered, at a screen of obscure commands and symbols and wondered how anybody could understand it, let alone write it. I’d hit the F5 key, and the program would magically spawn a cityscape in which two play- ers, depicted as gorillas, could hurl explosive bananas at each other. I tried changing a few lines to see what would happen, and on occasion, it was something pre- dictable or cool. More often than not, the game would simply fail to run. In a futile attempt to be helpful, the computer would diag- nose my errors, rambling on about syntax and “illegal” operations of varying description. For some years thereafter, I was content to avoid learning to program. That began to change when I became interested in making my creative work more interactive. You may already have encountered a few of the same I N T R O D U C T I O N
xvi   Introduction barriers that frustrated me. Maybe you were getting by just fine with visual tools but then hit a wall. Or to your disappointment (and horror?), you dis- covered that what you sought to accomplish required delving into code. Software applications, with all of their graphical widgets, make us feel like we’re in control. The illusion, however, soon fades when you discover that the tool you desire is missing. Through learning to program, you gain a true mastery of your computer. Who Is This Book For? This book assumes no prior programming experience. It strives to make the process of learning to program as visual and entertaining as possible. The content is based on my extensive experience teaching first-time coders, designers, and interactive media students. The skills and knowledge you’ll gain are fundamental to programming for an ever-expanding horizon of creative technologies, such as games, the web, augmented/virtual reality, and even visual effects for films. If you’re an artist, student, designer, researcher, or just somebody keen on picking up coding skills, Python Mode for Processing is excellent for learning to program in a visual context. For anybody with prior programming experience, this book would be useful for learning Python, Python Mode for Processing, or creative coding techniques. You may have experience with another visual programming language— something like Scratch, where you connect together graphical elements like boxes, icons, and arrows. Python is not such a language—rather, it is a tex- tual programming language that requires you to type code. To make learn- ing visual, though, you’ll focus on writing code that produces drawings, patterns, animations, data visualizations, user interfaces, and simulations. This approach not only makes for cool-looking graphics, but also helps you visualize the underlying concepts of programming. What Is Python Mode for Processing? Python Mode for Processing combines the Python programming language and Processing, a development environment for interactive and graphics program- ming. You’ll also see Python Mode for Processing referred to as Processing.py. The project started as a command line tool named Processing.py, but its developer decided to label it Python Mode when it was made available for the Processing development environment. In this book, you can consider the terms largely interchangeable. Python is one of the most popular programming languages in use today. There are many good reasons for this, but here’s why you should care. First, Python is a beginner-friendly language. It’s more approachable than languages like Java or C++, so you’ll find it easier to read, write, and
Introduction   xvii understand. Second, it’s a general-purpose language, suitable for program- ming artificial intelligence (AI), games, simulations, web applications, and just about everything in between. Processing, which has been around since the early 2000s, is composed of a programming language and an editor for writing and compiling code. It provides a collection of special commands that allow you to draw, animate, and handle user input by using code. The creators, Casey Reas and Ben Fry, developed Processing to make programming more accessible for designers and artists, although its thriving user base has grown to include researchers, hobbyists, and educators. Java is the basis for the original Processing programming language, but other variants have since appeared, including JavaScript (p5.js) and Ruby (JRubyArt) versions. In 2010, Jonathan Feinberg created Processing.py, which you can think of as a sort of extension for Processing that allows you to write Python instead of Java-esque code. Both Python and Processing are open source and won’t cost you a cent. What’s more, you can use them on just about any platform, including Linux, macOS, and Microsoft Windows. What Are Algorithms? You’ll encounter the term algorithm frequently in the domain of pro- gramming. You can think of an algorithm as a set of rules a computer or machine must follow to achieve a particular goal. As an example, an algo- rithm for making a cup of instant coffee would read as follows: 1. Place one teaspoon of coffee granules in a mug. 2. Fill the kettle with water. 3. Switch on the kettle. 4. Once the water has boiled, add 240 ml boiling water to the mug. 5. Add one level teaspoon of sugar to the same mug. 6. Stir the contents. 7. Serve. However, this set of steps is insufficient for programming a real-life coffee-making robot. Should the sizes of the mugs vary, smaller ones would overflow. Furthermore, the robot would ignore any requests for milk or extra sugar. Computers cannot make any assumptions, and require explicit and unambiguous direction, communicated in a language that machines understand—like Python. Learning the Python language may be the hur- dle you face initially, but as you grow more fluent, the challenge will shift toward the mastery of algorithmic thinking.
xviii   Introduction What Is Creative Coding? Creative coding is computer programming for creative output. This broad term encompasses, but is not limited to, computer-generated audio and visual art, interactive installations, experimental games, and data visualizations. Take, for example, Frederic Brodbeck’s Cinemetrics project. Using Python, Brodbeck developed a program that analyzes DVD movie data to generate visual fingerprints of films. The fingerprint is an open ring formed from many segments; a single segment represents a span of 10 shots, and the concentric bands show the color breakdown for each of those segments. The diagonal length of each segment indicates the amount of motion. Figure 1 is a Cinemetrics fingerprint for the film Quantum of Solace (2008). amount of motion chapter color palette 1 segment = 10 shots beginning end Figure 1: Quantum of Solace fingerprint, created by Frederic Brodbeck. Screenshot from http://cinemetrics .site/. The fingerprints can also be animated, in which case motion is instead visualized using pulsating segments. An interactive interface provides a selection of presets and filters so that you can arrange fingerprints along- side one another and make comparisons—for example, between originals and remakes, different genres, the works of a single director, and so forth. Figure 2 compares (from left to right) 2001: A Space Odyssey (1968), The Simpsons Movie (2007), and a soccer match.
Introduction   xix Figure 2: Fingerprints comparing (left to right) 2001: A Space Odyssey, The Simpsons Movie, and a soccer match. Screenshots from http://cinemetrics .site/. Many creative coding projects employ a similar underlying approach, in which data is fed into a program to influence how it controls output. Music visualizations with real-time audio synchronization—like those featured in popular media player software—are a good example. However, you can experiment with plenty of other data sources, such as web feeds, fitness track- ers, environmental sensors, and a plethora of public datasets. In some instances, it’s useful to opt for randomized data values. Consider procedurally generated game content. As opposed to construct- ing levels manually, you can program games to generate dungeon layouts, terrain, narrative elements, and enemy spawn locations automatically. Of course, such games should include sensible constraints; for instance, a cap on the total number of enemies that can appear at once, and algorithms for ensuring that stage layouts are not impossible to traverse. Game characters may be composed using a random selection of modu- lar components, or generated entirely from shapes and formulas. As an example, I’ve written a Processing Python program that generates the randomized microbial beasties displayed in Figure 3. The code—an adap- tation of Lieven Menschaert’s NodeBox script Aquatics!—spawns a creature with a random fill color, shape (defined by something named the superfor- mula), and no fewer than three eyes. There’s a 70 percent chance that hair will grow along the creature’s edges, which can be swayed by the force of a randomly directed current.