ebook img

Starting Out with Alice PDF

401 Pages·2012·13.41 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 Starting Out with Alice

STARTING OUT WITH Alice Third Edition This page intentionally left blank STARTING OUT WITH Alice Third Edition A Visual Introduction to Programming Tony Gaddis Haywood Community College Boston Columbus Indianapolis New York San Francisco Upper Saddle River Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montréal Toronto Delhi Mexico City São Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo Editorial Director, ECS Marcia Horton Executive Editor Matt Goldstein Editorial Assistant Jenah Blitz-Stoehr Director of Marketing Christy Lesko M arketing Manager Yezan Alayan Senior Marketing Coordinator Kathryn Ferranti Director of Production Erin Gregg Managing Editor Jeff Holcomb Senior Production Project Manager Marilyn Lloyd Production/Manufacturing Pat Brown A rt Director Jayne Conte Text Designer J erilyn Bockorick, Alsiha Webber/Nesbitt Graphics, Inc. Cover Designer Joyce Cosentino Wells/Karen Noferi M anager, Rights and Permissions Michael Joyce T ext Permission Coordinator Jenn Kennett/Creative Compliance Cover Image Shutterstock/Yuri Arcurs Media Project Manager Renata Butera Full-Service Project Management Jogender Taneja/Aptara®, Inc. C omposition Aptara®, Inc. Printer/Binder RR Donnelley Willard Cover Printer Lehigh-Phoenix Text Font Sabon LT Std Credits and acknowledgments borrowed from other sources and reproduced, with permission, in this textbook appear on the appropriate page within text. Copyright © 2013, 2011, 2008 Pearson Education, Inc., publishing as A ddison-Wesley. All rights reserved. Printed in the United States of America. This publication is protected by Copyright, and permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopy- ing, recording, or likewise. To obtain permission(s) to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or you may fax your request to 201-236-3290. Many of the designations by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trade- mark claim, the designations have been printed in initial caps or all caps. Library of Congress Cataloging-in-Publication Data Gaddis, Tony. Starting out with Alice : a visual introduction to programming / Tony Gaddis, Haywood Community College.––Third edition. pages cm Includes index. ISBN-13: 978-0-13-312974-8 ISBN-10: 0-13-312974-8 1. Alice (Computer program language) 2. Object-oriented programming (Computer science)––Computer-assisted instruction––Computer programs. 3. Computer games––Programming. 4. Computer animation––Computer programs. I. Title. QA76.76.C672G28 2013 794.8'1526––dc23 2012036332 10 9 8 7 6 5 4 3 2 1 ISBN 10: 0-13-312974-8 ISBN 13: 978-0-13-312974-8 Preface v Brief Contents Preface xi Chapter 1 Introduction to Alice and Objects 1 Chapter 2 Programming in Alice 57 Chapter 3 Variables, Functions, Math, and Strings 113 Chapter 4 Decision Structures 1 49 Chapter 5 Repetition Structures 1 85 Chapter 6 Methods, Functions, and More about Variables 2 13 Chapter 7 Events 2 71 Chapter 8 Lists and Arrays 3 03 Chapter 9 Recursion 345 Appendix A Installing Alice 3 65 Appendix B Answers to Checkpoints 3 69 Index 377 v This page intentionally left blank Contents Preface xi Chapter 1 Introduction to Alice and Objects 1 1.1 What Is a Computer Program? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 Algorithms and Programming Languages . . . . . . . . . . . . . . . . . . . . . . . 2 1.3 Learning to Program with Alice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 TUTORIAL 1-1: Opening and playing an Alice world . . . . . . . . . . . . . . . . . . . . . 6 1.4 Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.5 Classes and the Alice Galleries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 TUTORIAL 1-2: Creating a world and adding objects. . . . . . . . . . . . . . . . . . . . 19 1.6 3D Objects and the Camera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1 TUTORIAL 1-3: Moving the camera in 3D space . . . . . . . . . . . . . . . . . . . . . . . 34 TUTORIAL 1-4: Manipulating objects in 3D space . . . . . . . . . . . . . . . . . . . . . . 40 TUTORIAL 1-5: Manipulating subpart objects . . . . . . . . . . . . . . . . . . . . . . . . . 43 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1 Chapter 2 Programming in Alice 57 2.1 Writing Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 TUTORIAL 2-1: Adding instructions to an Alice world . . . . . . . . . . . . . . . . . . . 62 TUTORIAL 2-2: Exploring additional primitive methods . . . . . . . . . . . . . . . . . 67 2.2 Naming Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 3 2.3 Designing a Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 TUTORIAL 2-3: Using the program design cycle . . . . . . . . . . . . . . . . . . . . . . . 82 2.4 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 8 TUTORIAL 2-4: Inserting comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 2.5 Tips for Setting Up an Initial Scene . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 0 2.6 Executing Instructions Simultaneously . . . . . . . . . . . . . . . . . . . . . . . . . 97 TUTORIAL 2-5: Creating simultaneously executed instructions . . . . . . . . . . . . 98 2.7 Exporting Your Code for Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 04 2.8 Exporting an Alice World to Video . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 06 vii viii Contents Chapter 3 Variables, Functions, Math, and Strings 113 3.1 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 TUTORIAL 3-1: Creating and using a variable . . . . . . . . . . . . . . . . . . . . . . . . 116 T UTORIAL 3-2: Creating a set instruction for a variable . . . . . . . . . . . . . . . . 118 3.2 Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 T UTORIAL 3-3: Calling an ask user function . . . . . . . . . . . . . . . . . . . . . . . 122 TUTORIAL 3-4: Using a proximity function . . . . . . . . . . . . . . . . . . . . . . . . . . 127 3.3 Creating Math Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 30 TUTORIAL 3-5: Using math to avoid collisions . . . . . . . . . . . . . . . . . . . . . . . 132 3.4 Working with Strings and Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 38 TUTORIAL 3-6: Converting a Number variable to a string . . . . . . . . . . . . . . . 141 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 44 Chapter 4 Decision Structures 149 4.1 Boolean Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 49 4.2 The If/Else Decision Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 51 TUTORIAL 4-1: Creating an If/Else instruction . . . . . . . . . . . . . . . . . . . . . 154 TUTORIAL 4-2: Creating a Single-Alternative Decision Structure . . . . . . . . . . 160 4.3 Nested If/Else Decision Structures . . . . . . . . . . . . . . . . . . . . . . . . . 164 TUTORIAL 4-3: Creating Nested If/Else Instructions . . . . . . . . . . . . . . . . . 165 4.4 Relational Comparisons and Logical Operators . . . . . . . . . . . . . . . . . 1 71 T UTORIAL 4-4: Using a relational operator . . . . . . . . . . . . . . . . . . . . . . . . . . 173 TUTORIAL 4-5: Testing an object’s color property . . . . . . . . . . . . . . . . . . . . 177 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 81 Chapter 5 Repetition Structures 185 5.1 The Loop Instruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 TUTORIAL 5-1: Using the Loop instruction . . . . . . . . . . . . . . . . . . . . . . . . . . 187 TUTORIAL 5-2: Creating a User-Controlled Loop . . . . . . . . . . . . . . . . . . . . . 189 5.2 The While Instruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 TUTORIAL 5-3: Using a While instruction to make an object vanish . . . . . . . 198 TUTORIAL 5-4: Using a While instruction to make an object smaller . . . . . . . 202 TUTORIAL 5-5: Using the While instruction to move an object . . . . . . . . . . . 204 5.3 Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 07 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 08 Chapter 6 Methods, Functions, and More about Variables 213 6.1 Writing Custom Class-Level Methods . . . . . . . . . . . . . . . . . . . . . . . . 2 13 TUTORIAL 6-1: Creating a class-level method . . . . . . . . . . . . . . . . . . . . . . . . 214 6.2 Saving an Object to a New Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 TUTORIAL 6-2: Saving an object to a class . . . . . . . . . . . . . . . . . . . . . . . . . . 218 6.3 Stepwise Refinement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 T UTORIAL 6-3: Completing the W orkOut world . . . . . . . . . . . . . . . . . . . . . 225 6.4 Passing Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 27 Contents ix TUTORIAL 6-4: Passing arguments to a method . . . . . . . . . . . . . . . . . . . . . . 229 6.5 Using Class-Level Variables as Properties . . . . . . . . . . . . . . . . . . . . . . 2 33 TUTORIAL 6-5: Adding a property to an object . . . . . . . . . . . . . . . . . . . . . . 235 6.6 Writing Class-Level Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 38 T UTORIAL 6-6: Writing a class-level function . . . . . . . . . . . . . . . . . . . . . . . . 238 T UTORIAL 6-7: Writing a class-level function . . . . . . . . . . . . . . . . . . . . . . . . 243 6.7 World-Level Methods and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 248 6.8 Using Clipboards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 6.9 Tips for Visual Effects and Animation . . . . . . . . . . . . . . . . . . . . . . . . . 252 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 63 Chapter 7 Events 271 7.1 Responding to Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 71 7.2 Handling Key Press and Mouse Events . . . . . . . . . . . . . . . . . . . . . . . . 275 TUTORIAL 7-1: Handling key press events . . . . . . . . . . . . . . . . . . . . . . . . . . 276 TUTORIAL 7-2: Handling the while a key is pressed Event . . . . . . . . . . 280 TUTORIAL 7-3: Handling a mouse click event . . . . . . . . . . . . . . . . . . . . . . . . 283 7.3 Using Events in Simulations and Games. . . . . . . . . . . . . . . . . . . . . . . 286 7.4 Tips for Games and Simulations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 90 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 97 Chapter 8 Lists and Arrays 303 8.1 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 TUTORIAL 8-1: Creating a list and using the F or all in order and For all together instructions . . . . . . . . . . . . . . . . . . . . . . . . . 307 TUTORIAL 8-2: More complex list processing . . . . . . . . . . . . . . . . . . . . . . . . 313 TUTORIAL 8-3: Using the Let the mouse move <objects> event . . . . . . . . 327 8.2 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 30 T UTORIAL 8-4: Creating an array and a loop that steps through it . . . . . . . . 333 TUTORIAL 8-5: Randomly selecting an array element . . . . . . . . . . . . . . . . . . 339 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 41 Chapter 9 Recursion 345 9.1 Introduction to Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 45 TUTORIAL 9-1: Creating a recursive method . . . . . . . . . . . . . . . . . . . . . . . . . 349 9.2 Problem Solving with Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 52 TUTORIAL 9-2: Recursive problem solving in animation . . . . . . . . . . . . . . . . 353 T UTORIAL 9-3: Writing a recursive mathematical function . . . . . . . . . . . . . . 358 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 60 Appendix A Installing Alice 365 Appendix B Answers to Checkpoints 369 Index 377

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.