Squeak Learn Programming with Robots STEPHANE DUCASSE Squeak: Learn Programming with Robots Copyright (c) 2005 by Stephane Ducasse Lead Editor: Jonathan Hassell Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis, Jason Gilmore, Jonathan Hassell, Chris Mills, Dominic Shakeshaft, Jim Sumser Assistant Publisher: Grace Wong Project Manager: Kylie Johnston Copy Manager: Nicole LeClerc Copy Editor: David Kramer Production Manager: Kari Brooks-Copony Production Editor: Kelly Winquist Compositor: Diana Van Winkle, Van Winkle Design Group Proofreader: Elizabeth Berry Indexer: Valerie Perry Artist: Diana Van Winkle, Van Winkle Design Group Cover Designer: Kurt Krames Manufacturing Manager: Tom Debolski Library of Congress Cataloging-in-Publication Data Ducasse, Stephane. Squeak : learn programming with robots / Stephane Ducasse. p. cm. ISBN 1-59059-491-6 1. Robots--Programming. I. Title. TJ211.45.D83 2005 629.8'925117--dc22 2005013248 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. Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Distributed to the book trade in the United States by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013, and outside the United States by Springer-Verlag GmbH & Co. KG, Tiergartenstr. 17, 69112 Heidelberg, Germany. In the United States: phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders@springer-ny.com, or visit http://www.springer-ny.com. Outside the United States: fax +49 6221 345229, e-mail orders@springer.de, or visit http://www.springer.de. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http://www.apress.com. The information in this book is distributed on an as is basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress 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 this work. The source code for this book is available to readers at http://www.apress.com in the Downloads section. iv Contents at a Glance Foreword... xvii About the Author... xxiii Acknowledgments ... xxv Preface ... xxvii PART 1 ■ ■ ■ Getting Started ■CHAPTER 1 Installation and Creating a Robot... 3 ■CHAPTER 2 A First Script and Its Implications ... 13 ■CHAPTER 3 Of Robots and Men ... 29 ■CHAPTER 4 Directions and Angles... 37 ■CHAPTER 5 Pica s Environment ... 51 ■CHAPTER 6 Fun with Robots ... 61 PART 2 ■ ■ ■ Elementary Programming Concepts ■CHAPTER 7 Looping ... 77 ■CHAPTER 8 Variables ... 87 ■CHAPTER 9 Digging Deeper into Variables ... 101 ■CHAPTER 10 Loops and Variables ... 109 ■CHAPTER 11 Composing Messages ... 119 PART 3 ■ ■ ■ Bringing Abstraction into Play ■CHAPTER 12 Methods: Named Message Sequences ... 135 ■CHAPTER 13 Combining Methods ... 149 ■CHAPTER 14 Parameters and Arguments ... 155 ■CHAPTER 15 Errors and Debugging ... 167 ■CHAPTER 16 Decomposing to Recompose ... 183 ■CHAPTER 17 Strings, and Tools for Understanding Programs ... 197 CONTENTS AT A GLANCE v PART 4 ■ ■ ■ Conditionals ■CHAPTER 18 Conditions... 209 ■CHAPTER 19 Conditional Loops ... 221 ■CHAPTER 20 Boolean and Boolean Expressions... 233 ■CHAPTER 21 Coordinates, Points, and Absolute Moves ... 243 ■CHAPTER 22 Advanced Robot Behavior ... 261 ■CHAPTER 23 Simulating Animal Behavior ... 269 PART 5 ■ ■ ■ Other Squeak Worlds ■CHAPTER 24 A Tour of eToy ... 289 ■CHAPTER 25 A Tour of Alice ... 315 ■INDEX ... 337