ebook img

Learn to Program with Small Basic: An Introduction to Programming with Games, Art, Science, and Math PDF

344 Pages·12.204 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Learn to Program with Small Basic: An Introduction to Programming with Games, Art, Science, and Math

ages 10 and up LLeeaarrnn ttoo ““WWeellccoommee ttoo tthhee mMaaggiiccaall wwoorrlldd ooff pprrooggrraammmmiinngg.. Wwhhaatt ddoo yyoouu wwaanntt ttoo ccrreeaattee ttooddaayy??”” LL PPrrooggrraamm wwiitthh ee ——VViijjaayyee RRaajjii,, cCrreeaattoorr ooff SSmmaallll BBaassiicc aa rr SSmmaallll BBaassiicc Small Basic is a free, beginner-friendly pro- You’ll also learn to command a turtle nn gramming language created by Microsoft. to draw shapes, create magical moving text, Inspired by BASIC, which introduced program- solve math problems quickly, help a knight tt ming to millions of first-time PC owners in slay a dragon, and more! oo An Introduction to Programming the 1970s and 1980s, Small Basic is a modern Each chapter ends with creative coding PP with Games, Art, Science, and Math language that makes coding simple and fun. challenges so you can take your skills to rr Learn to Program with Small Basic the next level. Learn to Program with Small oo introduces you to the empowering world of Basic is the perfect place to start your com- gg programming. You’ll master the basics with puter science journey. m a j e d m a r j i a n d e d p r i c e simple activities like displaying messages rr and drawing colorful pictures, and then work ABOUT THE AUTHOrs aa your way up to programming games! Learn Majed Marji is a senior development engi- mm how to: neer at General Motors and an adjunct faculty member at Wayne State University ww  Program your computer to greet you in Michigan. He is also the author of Learn ii by name tt to Program with Scratch (No Starch Press).  Make a game of rock-paper-scissors using hh Ed Price is a senior program manager in If/Else statements engineering at Microsoft. He holds an MBA in SS  Create an interactive treasure map using technology management and has been a pro- mm arrays fessor at Bellevue College. He runs customer aa  Draw intricate geometric patterns with feedback programs for Azure Development, ll just a few lines of code Visual Studio, and Small Basic. ll  Simplify complex programs by breaking BB Covers Small Basic them into bite-sized subroutines Version 1.x. Windows 7 aa or higher required. ss ii cc $34.95 ($40.95 CDN) SMALL BASICSHELVE IN: PROGRAM PriceandMarji M GEwEwKT wHEN.En ToFEIsRNtTaEArScITNh IM.NcEoNmT™ ING LANGUAGES/ Learn to Program with SmaLL BaSic L e a r n t o P r o g r a m w i t h S m aL L B aSi c an introduction to Programming with games, art, Science, and math by Majed Marji and Ed Price San Francisco Learn to Program with SmaLL BaSic. Copyright © 2016 by Majed Marji and Ed Price. 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. 20 19 18 17 16 1 2 3 4 5 6 7 8 9 ISBN-10: 1-59327-702-4 ISBN-13: 978-1-59327-702-4 Publisher: William Pollock Production Editor: Laurel Chun Cover Illustration: Josh Ellingson Developmental Editors: Hayley Baker and Tyler Ortman Technical Reviewer: Vijaye Raji Copyeditor: Anne Marie Walker Compositors: Laurel Chun and Kathleen Miller Proofreader: Lisa Devoto Farrell Indexer: BIM Indexing & Proofreading Services For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly: No Starch Press, Inc. 245 8th Street, San Francisco, CA 94103 phone: 415.863.9900; [email protected]; www.nostarch.com Library of Congress Cataloging-in-Publication Data Names: Marji, Majed, author. | Price, Ed, 1978- author. Title: Learn to program with small basic : an introduction to programming with games, art, science, and math / by Majed Marji and Ed Price. Description: San Francisco : No Starch Press, [2016] | Includes index. Identifiers: LCCN 2015039128| ISBN 9781593277024 | ISBN 1593277024 Subjects: LCSH: BASIC (Computer program language) Classification: LCC QA76.73.B3 M3743 2016 | DDC 005.1/3--dc23 LC record available at http://lccn.loc.gov/2015039128 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 authors 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. Brief c ontentS Foreword by Vijaye Raji . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxi Chapter 1: Introducing Small Basic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Chapter 2: Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Chapter 3: Drawing Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27 Chapter 4: Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43 Chapter 5: Drawing Shapes with Turtle Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55 Chapter 6: Getting User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Chapter 7: Empowering Programs with Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Chapter 8: Making Decisions with If Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .97 Chapter 9: Using Decisions to Make Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .111 Chapter 10: Solving Problems with Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129 Chatper 11: Event-Driven Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Chatper 12: Building Graphical User Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .165 Chapter 13: Repeating For Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .181 Chapter 14: Creating Conditional While Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .195 Chapter 15: Grouping Data in One-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . .207 Chapter 16: Storing Data with Associative Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 Chapter 17: Expanding to Higher-Dimension Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 Chapter 18: Advanced Text Magic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 Chapter 19: Receiving File Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .287 Where to Go from Here . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 contentS in De tai L Foreword by Vijaye raji xvii acknowLedgmentS xix introduction xxi Who Should Read This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii What’s in This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii A Note to the Reader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiv 1 introducing SmaLL BaSic 1 What Is a Computer? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 What Is a Computer Program? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 What Is Small Basic? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 The Vision of Small Basic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 The Basics of Small Basic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 The Small Basic Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 The Small Basic Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 The Small Basic Development Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Installing Small Basic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 The Small Basic IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Opening and Saving Your Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Sharing Your Work and Importing Games . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Copy and Paste; Undo and Redo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Running Your Program and Graduating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Writing and Running Your First Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Objects and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Naming Your Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Files Generated by Small Basic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Helping Hands: IntelliSense and Syntax Coloring . . . . . . . . . . . . . . . . . . . . . . 9 Drawing with Small Basic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Try It Out 1-1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Programming Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2 getting Started 13 The Parts of a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Comments and Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Characters and Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Arguments and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Try It Out 2-1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Exploring Other Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Case Sensitivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Sequential Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Displaying Numbers and Doing Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Joining Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Try It Out 2-2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Object Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Setting and Changing Property Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Working with Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Try It Out 2-3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Try It Out 2-4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Programming Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Syntax Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Logic Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Runtime Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Programming Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 3 drawing BaSicS 27 The Graphics Coordinate System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Drawing Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Drawing Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Triangles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Try It Out 3-1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Rectangles and Squares . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Try It Out 3-2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Ellipses and Circles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Pen Size and Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Pen Width and Shape Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Try It Out 3-3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Drawing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Inserting Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Programming Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4 uSing VariaBLeS 43 What’s a Variable? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 The Basics of Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Assigning Expressions to Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Passing Variables to Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Changing the Value of a Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Using Spaces for Readability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Try It Out 4-1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Rules for Naming Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Say What You Mean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Find the Right Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 viii Contents in Detail Stick with Your Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Let IntelliSense Work for You . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Avoid Naming Variables After Methods and Objects . . . . . . . . . . . . . . . . . . 49 Try It Out 4-2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Simplifying Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Try It Out 4-3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Using Variables to Solve Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Try It Out 4-4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Two Kinds of Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Try It Out 4-5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Programming Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 5 drawing ShaPeS with turtLe graPhicS 55 Meet the Turtle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Moving the Turtle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Absolute Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Relative Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Coloring Your Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Controlling Your Speed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Try It Out 5-1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Introducing the For Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Try It Out 5-2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Drawing Regular Polygons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 A Star Is Born . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Try It Out 5-3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Creating Polygon Art Using Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Try It Out 5-4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Endless Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Try It Out 5-5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Programming Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 6 getting uSer inPut 73 Talking to the Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Your Number, Please? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Introducing Yourself to Your Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Writing Prompts for Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 A Moment of Silence, Please (Pause) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Working with User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Converting Fahrenheit to Celsius . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Try It Out 6-1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Averaging Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Reading Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Try It Out 6-2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Programming Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Contents in Detail ix

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.