Table Of ContentIntroduction to Programming with Greenfoot
Companion Website
Additional material and resources for this book can be found at
http://www.greenfoot.org/book/
For students:
The Greenfoot software
(cid:2)
The scenarios discussed in this book
(cid:2)
The Greenfoot Gallery—a scenario showcase
(cid:2)
Tutorial videos
(cid:2)
A discussion forum
(cid:2)
Technical support
(cid:2)
For teachers:
A teacher discussion forum
(cid:2)
Additional exercises related to the book
(cid:2)
The “Green Room” containing worksheets and other teaching
(cid:2)
resources
This page intentionally left blank
Introduction to
Programming with
Greenfoot
Object-Oriented Programming in Java™
with Games and Simulations
Michael Kölling
Prentice Hall
Upper Saddle River•Boston•Columbus•San Francisco•New York
Indianapolis•London•Toronto•Sydney•Singapore•Tokyo•Montreal•Dubai
Madrid•Hong Kong•Mexico City•Munich•Paris•Amsterdam•Cape Town
Vice President and Editorial Director,ECS:Marcia J. Horton
Editor in Chief,CS:Michael Hirsch
Executive Editor:Tracy Dunkelberger
Assistant Editor:Melinda Haggerty
Editorial Assistant:Allison Michael
Director of Team-Based Project Management:Vince O’Brien
Senior Managing Editor:Scott Disanno
Production Liaison:Irwin Zucker
Production Editor:Shiny Rajesh, Integra
Senior Operations Specialist:Alan Fischer
Operation Specialist:Lisa McDowell
Marketing Manager:Erin Davis
Marketing Coordinator:Kathryn Ferranti
Art Director:Jayne Conte
Cover Designer:Bruce Kenselaar
Art Editor:Greg Dulles
Media Editor:Daniel Sandin
Composition/Full-Service Project Management:Integra
Copyright © 2010 by Pearson Higher Education.Upper Saddle River,New Jersey,07458.All rights
reserved. Manufactured 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, photocopying, recording, or
likewise. To obtain permission(s) to use materials from this work, please submit a written request to Pearson
Higher Education, Permissions Department, 1 Lake Street, Upper Saddle River, NJ 07458.
The author and publisher of this book have used their best efforts in preparing this book. These efforts
include the development, research, and testing of the theories and programs to determine their effectiveness.
The author and publisher make no warranty of any kind, expressed or implied, with regard to these programs
or the documentation contained in this book. The author and publisher shall not be liable in any event for
incidental or consequential damages in connection with, or arising out of, the furnishing, performance,
oruse of these programs.
Library of Congress Cataloging-in-Publication Data on File
10 9 8 7 6 5 4 3 2 1
ISBN-13: 978-0-13-603753-8
ISBN-10: 0-13-603753-4
To Krümel and Cracker—may their imagination never fade.
—mk
Education is not the filling of a pail, but the lighting of a fire.
—William ButlerYeats
This page intentionally left blank
Contents
List of scenarios discussed in this book xi
Acknowledgments xv
Introduction 1
Chapter 1 Getting to know Greenfoot 3
1.1 Getting started 3
1.2 Objects and classes 4
1.3 Interacting with objects 6
1.4 Return types 7
1.5 Parameters 8
1.6 Greenfoot execution 9
1.7 A second example 10
1.8 Understanding the class diagram 10
1.9 Playing with Asteroids 12
1.10 Source code 13
1.11 Summary 15
Chapter 2 The first program: Little Crab 16
2.1 The Little Crab scenario 16
2.2 Making the crab move 18
2.3 Turning 19
2.4 Dealing with screen edges 21
2.5 Summary of programming techniques 25
Chapter 3 Improving the Crab—more sophisticated programming 27
3.1 Adding random behavior 27
3.2 Adding worms 30
3.3 Eating worms 32
|
viii Contents
3.4 Creating new methods 33
3.5 Adding a Lobster 36
3.6 Keyboard control 36
3.7 Ending the game 38
3.8 Adding sound 40
3.9 Summary of programming techniques 42
Chapter 4 Finishing the crab game 43
4.1 Adding objects automatically 43
4.2 Creating new objects 45
4.3 Animating images 46
4.4 Greenfoot images 47
4.5 Instance variables (fields) 48
4.6 Assignment 49
4.7 Using actor constructors 50
4.8 Alternating the images 52
4.9 The if/else statement 53
4.10 Counting worms 53
4.11 More ideas 55
4.12 Summary of programming techniques 56
Interlude 1 Sharing your scenarios 57
I1.1 Exporting your scenario 57
I1.2 Export to application 57
I1.3 Export to a web page 58
I1.4 Publishing on the Greenfoot Gallery 59
Chapter 5 Making music: An on-screen piano 61
5.1 Animating the key 62
5.2 Producing the sound 65
5.3 Abstraction:Creating multiple keys 66
5.4 Building the piano 68
5.5 Using loops:The while loop 69
5.6 Using arrays 72
5.7 Summary of programming techniques 76
Chapter 6 Interacting objects: Newton’s Lab 78
6.1 The starting point:Newton’s Lab 79
6.2 Helper classes:SmoothMover and Vector 80
|
Contents ix
6.3 The existing Body class 83
6.4 First extension:Creating movement 85
6.5 Using Java library classes 86
6.6 Adding gravitational force 87
6.7 The List type 90
6.8 The for-each loop 91
6.9 Applying gravity 93
6.10 Trying it out 95
6.11 Gravity and music 97
6.12 Summary of programming techniques 99
Chapter 7 Collision detection: Asteroids 101
7.1 Investigation:What is there? 102
7.2 Painting stars 103
7.3 Turning 106
7.4 Flying forward 107
7.5 Colliding with asteroids 109
7.6 Casting 112
7.7 Adding fire power:The proton wave 115
7.8 Growing the wave 115
7.9 Interacting with objects in range 119
7.10 Further development 121
7.11 Summary of programming techniques 122
Interlude 2 The Greeps competition 123
I2.1 How to get started 124
I2.2 Programming your Greeps 125
I2.3 Running the competition 126
I2.4 Technicalities 126
Chapter 8 Creating images and sound 127
8.1 Preparation 127
8.2 Working with sound 129
8.3 Sound recording and editing 130
8.4 Sound file formats and file sizes 131
8.5 Working with images 133
8.6 Image files and file formats 133
8.7 Drawing images 135
8.8 Combining images files and dynamic drawing 137
8.9 Summary 139