Chapter 9 - Collision Detection: Asteroids Bruce Chittenden (modified by Jeff Goldstein)

Slides:



Advertisements
Similar presentations
Chapter 10 - Additional Scenario Ideas Bruce Chittenden.
Advertisements

Getting to know Greenfoot
Having Fun with Your Feet…better yet, Greenfoot TM Nicolas Romero February 28, 2009 Gaming Academy CTD – SEP Northwestern University.
Chapter 8 - Creating Images and Sound Bruce Chittenden.
Chapter 2 - The First Program: Little Crab
Program: Little Crab Mr Gano.
Chapter 7 - Collision Detection: Asteroids
Chapter 6 - Interacting Objects: Newton’s Lab
Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations
Greenfoot Asteroids Mrs. C. Furman August 16, 2010.
Chapter 1 - Getting to know Greenfoot Bruce Chittenden.
Chapter 1 - Getting to know Greenfoot Acknowledgement: Michael Kolling & Bruce Chittenden.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for the Final Lecture Dec 7, 2011 Aditya Mathur Department of Computer Science Purdue.
Computer Science 1620 Programming & Problem Solving.
Games and Simulations O-O Programming in Java The Walker School
Chapter 5 - Making Music: An On-Screen Piano
VIDEO GAME PROGRAMMING Video Game Programming Junior – DigiPutt INSTRUCTOR TEACHER’S ASSISTANT.
Greenfoot. Getting Started Open the Greenfoot download site: Select Greenfoot
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
Chapter 8 – Mouse Input, Images and Sound. Chapter 8 - Content In contrast to previous chapters, we will not build a complete scenario in this chapter.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
Graphics Concepts CS 2302, Fall /3/20142 Drawing Paths.
Chapter 6 – Interacting Objects: Newton’s Lab. topics: objects interacting with each other, using helper classes, using classes from the Java library.
VB Games: Preparing for Memory Brainstorm controls & events Parallel structures (again), Visibility, LoadPicture, User-defined procedures, Do While/Loop,busy.
Chapter 7 – Collision Detection: Asteroids
CSE 113 Introduction to Computer Programming Lecture slides for Week 12 Monday, November 14 th, 2011 Instructor: Scott Settembre.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
Game Maker Terminology
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Greenfoot Game Programming Club.
Guide to Programming with Python Chapter Twelve Sound, Animation, and Program Development: The Astrocrash Game.
Guide to Programming with Python Week 15 Chapter Twelve Sound, Animation, and Program Development: The Astrocrash Game.
Game Maker Galactic Mail Advanced Group: Complete Galactic Mail, then start developing an independent project.
Advanced Stuff Learning by example: Responding to the mouse.
Chapter 9 – Additional Scenarios. Marbles Collision Detection The Marbles scenario does not use any of the built-in Greenfoot collision detection.
Lesson 2: Reading a program. Remember: from yesterday We learned about… Precise language is needed to program Actors and Classes Methods – step by step.
Scratch for Interactivity Dr. Ben Schafer Department of Computer Science University of Northern Iowa.
Chapter 2 – The Little Crab Program:. Little Crab Scenario Inheritance: The Arrows Denote Hierarchy Crab is an Animal Animal is an Actor Therefore, It.
Computer Science I Animations. Bouncing ball. The if statement. Classwork/homework: bouncing something. Compress and upload work to Moodle.
Newton’s Lab Games and Simulations O-O Programming in Java.
The Stingray Example Program CMT3311. Stingray - an example 2D game May be useful as a simple case study Most 2D games need to solve generic problems.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
Chapter 4 - Finishing the Crab Game
Chapter 11: Scrolling with Greenfooot. What Is Scrolling? Scrolling is an important and often necessary concept in games. It can be used to: a)Convey.
Chapter 10: Creating Images and Sound
Arrays.
Chapter 4 - Finishing the Crab Game
Chapter 5 – Making Music: An On-Screen Piano (Part 1 – Using Loops)
Create a Halloween Computer Game in Scratch
Chapter 5 – Making Music: An On-Screen Piano (Part 2 – Using Arrays)
Scratch for Interactivity
Interlude 2 - The Greeps Competition
Chapter 3 – Improving the Crab Game
Barb Ericson Georgia Institute of Technology June 2007
Scratch for Interactivity
Variables and Arithmetic Operations
UBC 2016 Why Coding?.
Organizing Memory in Java
Chapter 10 Algorithms.
Chapter 10 Algorithms.
Games and Simulations O-O Programming in Java The Walker School
Breakout in Greenfoot Barb Ericson
Suggested self-checks: Section 7.11 #1-11
Data Structures & Algorithms
More on Creating Classes
Chapter 10 Algorithms.
Creating a Simple Game in Scratch
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Chapter 9 - Collision Detection: Asteroids Bruce Chittenden (modified by Jeff Goldstein)

9.1 Investigation: What is There? When experimenting with the current scenario, you will notice that some fundamental functionality is missing.  The rocket does not move. It cannot be turned, nor can it be moved forward.  Nothing happens when an asteroid collides with the rocket. It flies straight through it, instead of damaging the rocket.  As a result of this, you cannot lose. The game never ends, and a final score is never displayed.  The ScoreBoard, Explosion, and ProtonWave classes, which we can see in the class diagram, do not seem to feature in the scenario.

Exercise 9.1

Exercise 9.2 Controls for the Rocket Collision Logic Explosion Logic ScoreBoard Logic Implement ProtonWave

Exercise 9.3 Spacebar is used to fire a bullet

Exercise 9.4 Creates the Explosion Visual and makes and Explosion Sound

Exercise 9.5 The visual is Present, But It only shows the outer ring

9.2 Painting Stars The Asteroid Scenario does not use an image file for the background. A world that does not have a background image assigned will, by default, get an automatically created background image that is filled with plain white.

Exercise 9.6 The Background is Created by These Three Statements

Exercise 9.7 Code to Create the Background is Commented Out

Exercise 9.7

Exercise 9.8 Draw Rectangle Draw Oval Fill Oval

Exercise 9.8

Exercise 9.9

13 Defined Constant Fields

Exercise 9.10 /* * Method to create stars. The integer number is how many. */ private void createStars(int number) { // Need to add code here }

Exercise 9.11 /* * Method to create stars. The integer number is how many. */

Create 300 Stars Exercise 9.11

Exercise 9.13 Clean Compile

Exercise 9.14 /** * Add a given number of asteroids to our world. Asteroids are only added into * the left half of the world. */ private void addAsteroids(int count) { for(int i = 0; i < count; i++) { int x = Greenfoot.getRandomNumber(getWidth()/2); int y = Greenfoot.getRandomNumber(getHeight()/2); addObject(new Asteroid(), x, y); } The addAsteroids creates a count number of asteroids and adds them to the World

Exercise 9.15 for (int i = 0; i < count; i++) { } Initialization Loop-Condition Increment

Exercise 9.16 /* * Add a given number of asteroids to our world. Asteroids are only added into * the left half of the world. */ private void addAsteroids(int count) { int i = 0; while ( i < count) { int x = Greenfoot.getRandomNumber(getWidth()/2); int y = Greenfoot.getRandomNumber(getHeight()/2); addObject(new Asteroid(), x, y); i++; }

Exercise 9.17 /* * Add a given number of asteroids to our world. Asteroids are only added into * the left half of the world. */ private void addAsteroids(int count) { for ( int i = 0; i < count; i++) { int x = Greenfoot.getRandomNumber(getWidth()/2); int y = Greenfoot.getRandomNumber(getHeight()/2); addObject(new Asteroid(), x, y); }

Exercise 9.18 /* * Method to create stars. The integer number is how many. */ private void createStars(int number) { GreenfootImage background = getBackground(); for (int i = 0; i < number; i++) { int x = Greenfoot.getRandomNumber ( getWidth() ); int y = Greenfoot.getRandomNumber ( getHeight() ); background.setColor (new Color(255, 255, 255)); background.fillOval (x, y, 2, 2); } Generate a random number for x and y and place a star at that location

Exercise 9.18

Exercise 9.19 /* * Method to create stars. The integer number is how many. */ private void createStars(int number) { GreenfootImage background = getBackground(); for (int i = 0; i < number; i++) { int x = Greenfoot.getRandomNumber( getWidth() ); int y = Greenfoot.getRandomNumber( getHeight() ); int color = Greenfoot.getRandomNumber (256); background.setColor(new Color(color, color, color)); background.fillOval(x, y, 2, 2); } Generate a random number for color in the range 0 to 255

Exercise 9.19

9.3 Turning We want to make the rocket turn left or right using the left and right arrow keys.

Exercise 9.20 /* * Check whether there are any key pressed and react to them. */ private void checkKeys() { if (Greenfoot.isKeyDown("space")) { fire(); } The method checkKeys handles keyboard input

Exercise 9.21

Left (-) Degrees Right (+) Degrees if (Greenfoot.isKeyDown("left")) turn(-5);// alternate solution: setRotation(getRotation() - 5); if (Greenfoot.isKeyDown("right")) turn(5);// alternate solution: setRotation(getRotation() + 5);

Exercise 9.21 /* * Check whether there are any key pressed and react to them. */ private void checkKeys() { if (Greenfoot.isKeyDown ("space")) { fire(); } if (Greenfoot.isKeyDown ("left")) { turn(- 5); } If left arrow key is down rotate left 5 degrees

Exercise 9.21

Exercise 9.22 /* * Check whether there are any key pressed and react to them. */ private void checkKeys() { if (Greenfoot.isKeyDown("space")) { fire(); } if (Greenfoot.isKeyDown ("left")) { turn(- 5); } if (Greenfoot.isKeyDown ("right")) { turn(5); } If right arrow key is down rotate right 5 degrees

Exercise 9.22

9.4 Flying Forward Our Rocket class is a subclass of the SmoothMover class. This means that it holds a movement vector that determines its movement and that it has a move () method that makes it move according to this vector

Exercise 9.23 /* * Do what a rocket's gotta do. (Which is: mostly flying about, and turning, * accelerating and shooting when the right keys are pressed.) */ public void act() { move (); checkKeys(); reloadDelayCount++; } Add the move () method

Exercise 9.23 The rocket does not move because our move vector has not been initialized and contains zero

Exercise 9.24 /* * Initialize this rocket. */ public Rocket() { reloadDelayCount = 5; addToVelocity ( new Vector (13, 0.3)); //initially slow drifting } Add an initial movement to the rocket constructor.

Exercise 9.24 The rocket drifts slowly toward the right & slightly down

Exercise 9.25 /* * Check whether there are any key pressed and react to them. */ private void checkKeys() { if (Greenfoot.isKeyDown("space")) fire(); ignite (Greenfoot.isKeyDown ("up")); if (Greenfoot.isKeyDown("left")) turn(- 5); if (Greenfoot.isKeyDown("right")) turn(5); } Add a call to ignite

Exercise 9.26 /** * Go with thrust on */ private void ignite (boolean boosterOn) { } Define a stub method for ignite

Exercise 9.27 when “up” arrow key is pressed change image to show engine fire; add movement; when “up” arrow key is released change back to normal image; Pseudo code

Exercise 9.27 /* * Go with thrust on */ private void ignite (boolean boosterOn) { if (boosterOn) { setImage (rocketWithThrust); addToVelocity (new Vector (getRotation(), 0.3)); } else { setImage (rocket); } The ignite method complete

Exercise 9.27

9.5 Colliding with Asteroids If (we have collided with an asteroid) { remove the rocket from the world; place an explosion into the world; show final score (game over); } Pseudo code

Exercise 9.28 /* * Check for a collision with an Asteroid */ private void checkCollision() { } Define a stub method for checkCollision

Exercise 9.29 /* * Do what a rocket's gotta do. (Which is: mostly flying about, and turning, * accelerating and shooting when the right keys are pressed.) */ Public void act() { move (); checkKeys(); checkCollision(); reloadDelayCount++; } Make a call to checkCollision from the Rocket Act method

Intersecting Objects Bounding Box Visible Image List getIntersectingObjects (Class cls) Actor getOneIntersectingObject (Class cls)

Exercise 9.32 /* * Check for a collision with an Asteroid */ private void checkCollision() { Actor a = getOneIntersectingObject (Asteroid.class); if (a != null) { } Check for intersecting with an Asteroid (not null… we hit!)

Exercise 9.33 /* * Check for a collision with an Asteroid */ private void checkCollision() { Actor a = getOneIntersectingObject (Asteroid.class); if (a != null) { World world = getWorld(); world.removeObject (this); world.addObject (new Explosion(), getX(), getY()); } The problem with this code is that we removed the Object and then attempted to use its coordinates

Exercise 9.34

Exercise 9.34 (working) /* * Check for a collision with an Asteroid */ private void checkCollision() { Actor a = getOneIntersectingObject (Asteroid.class); if (a != null) { World world = (Space) getWorld(); world.addObject (new Explosion(), getX(), getY()); world.removeObject (this); } Add the code to display an explosion. Then remove Rocket from the World.

Exercise 9.35 This is a very advanced exercise. A more sophisticated way to show an explosion is introduced in the Greenfoot tutorial videos. Making Explosions, Part I Making Explosions, Part I (length: ~ 5 min) Making Explosions, Part II (length: ~ 10 min) Making Explosions, Part III (length: ~ 8.8 min) Making Explosions, Part II Making Explosions, Part III Making Explosions, Part IVMaking Explosions, Part IV (length: ~ 9.2 min) Making Explosions, Part V (length: ~ 6.3 min) Making Explosions, Part V Please view these explosion videos your author has prepared.

9.6 Game Over So, when should the game end? Now, the question is how to do it??? Suppose we end the game when we (our rocket) is struck by an asteroid.

Exercise 9.36 Right Click on ScoreBoard then click on new ScoreBoard and drag it into the World

Exercise 9.37 /* * Create a score board with dummy result for testing. */ public ScoreBoard() { this(100); } /* * Create a score board for the final result. */ public ScoreBoard(int score) { makeImage("Game Over", "Score: ", score); } The ScoreBoard class has two constructors, a Default Constructor and the one parameter Constructor

Exercise 9.38 public class ScoreBoard extends Actor { public static final float FONT_SIZE = 40.0f; public static final int WIDTH = 400; public static final int HEIGHT = 300; import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Color; import java.awt.Font; import java.util.Calendar; The information about Fonts is in java.awt.Font

Exercise 9.38 /* * Make a more interesting score board image. */ private void makeImage(String title, String prefix, int score) { GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT); image.setColor(new Color(255,255,255, 68)); image.fillRect(0, 0, WIDTH, HEIGHT); image.setColor(new Color(0, 0, 0, 68)); image.fillRect(15, 15, WIDTH-15, HEIGHT-15); Font font = image.getFont(); font = font.deriveFont(ITALIC, FONT_SIZE); image.setFont(font); image.setColor(Color.RED); image.drawString(title, 60, 100); image.drawString(prefix + score, 60, 200); setImage(image); }

Example 9.38 (another example)

Exercise 9.39 gameOver Method is a stub

Exercise 9.40 /* * This method is called when the game is over to display the final score. * (Notice that the scoreboard neatly fits inside the space) */ public void gameOver() { addObject(new ScoreBoard(999), 300, 250); }

Exercise 9.42 /* * This method is called when the game is over to display the final score. */ public void gameOver() { addObject(new ScoreBoard(999), getWidth() / 2, getHeight() / 2); } To insure that the Score is placed in the of the World, center it at ½ width and ½ height

Exercise 9.43 (not working…yet) /* * Check for a collision with an Asteroid */ private void checkCollision() { Actor a = getOneIntersectingObject (Asteroid.class); if (a != null) { World world = (Space) getWorld(); world.addObject (new Explosion(), getX(), getY()); world.removeObject (this); world.gameOver(); } Add the code to display an explosion. Then remove Rocket from the World.

Exercise 9.43 The error comes from the fact that gameOver () is defined in class Space while getWorld returns a type of World. To solve this problem we tell the compiler explicitly that this world we are getting is actually of type Space. Error: but we need Space space = (space) getWorld(); // as a reference

Casting (will solve our problem) In computer science, type conversion, typecasting, and coercion refers to different ways of, implicitly or explicitly, changing an entity of one data type into another. This is done to take advantage of certain features of type hierarchies or type representations. One example would be small integers, which can be stored in a compact format and converted to a larger representation when used in arithmetic computations. In object-oriented programming, type conversion allows programs to treat objects of one type as one of their ancestor types to simplify interacting with them.computer sciencedata typeobject-oriented

Exercise 9.44 (working) /* * Check for a collision with an Asteroid */ private void checkCollision() { Actor a = getOneIntersectingObject (Asteroid.class); if (a != null) { Space space = (Space) getWorld(); space.addObject (new Explosion(), getX(), getY()); space.removeObject (this); space.gameOver(); } gameOver now has a reference to Space class where it is defined.

Exercise 9.44 (display)

Exercise 9.45 inconvertible types

9.7 Adding Fire Power: The Proton Wave The idea is this: Our proton wave, once released, radiates outward from our rocket ship, damaging or destroying every asteroid in its path. Since it works in all directions simultaneously, it is a much more powerful weapon than our bullets.

Exercise 9.46 Does not Move Does not Disappear Does not cause Damage

Exercise 9.47 public ProtonWave() { initializeImages(); } public static void initializeImages() { if(images == null) { GreenfootImage baseImage = new GreenfootImage("wave.png"); images = new GreenfootImage[NUMBER_IMAGES]; int i = 0; while (i < NUMBER_IMAGES) { int size = (i+1) * ( baseImage.getWidth() / NUMBER_IMAGES ); images[i] = new GreenfootImage(baseImage); images[i].scale(size, size); i++; } public void act() { } The Constructor and Two Methods ProtonWave () initializeImages () act ()

Exercise 9.48 /* * Create a new proton wave. */ public ProtonWave() { } /* * Create the images for expanding the wave. */ public static void initializeImages() { } /* * Act for the proton wave is: grow and check whether we hit anything. */ public void act() { }

Exercise 9.49 (explained) if we have not created the images, do so now get the base Image from the file wave.png create an array of NUMBER_IMAGES (30) images while there are still images to initialize calculate size of this image to be the width of image divided by the number of images multiplied by the index of this image set the next element in the array of images to the base image scaled to new size increment the number of images end of the while loop end of if images not created Pseudo code for initializeImages method

Figure 9.3: Growing the Wave GreenfootImage [ ]

Section 9.8: Code 9.3 /* * Create the images for expanding the wave. */ public static void initializeImages() { if (images == null) { GreenfootImage baseImage = new GreenfootImage("wave.png"); images = new GreenfootImage[NUMBER_IMAGES]; int i = 0; while (i < NUMBER_IMAGES) { int size = (i+1) * ( baseImage.getWidth() / NUMBER_IMAGES ); images[i] = new GreenfootImage(baseImage); images[i].scale(size, size); i++; }

Exercise 9.50 /* * Create the images for expanding the wave. */ public static void initializeImages() { if (images == null) { GreenfootImage baseImage = new GreenfootImage("wave.png"); images = new GreenfootImage[NUMBER_IMAGES]; for (int i = 0; i < NUMBER_IMAGES; i++) { int size = (i+1) * ( baseImage.getWidth() / NUMBER_IMAGES ); images[i] = new GreenfootImage(baseImage); images[i].scale(size, size); } for Loop

Exercise 9.51 /* * Create a new proton wave. */ public ProtonWave() { initializeImages(); setImage(images [0]); }

Exercise 9.51 (smallest image)

Exercise 9.52 /* * Current size of the wave. */ private int imageCount = 0;

Exercise 9.53 /* * Grow the wave. If we get to full size remove it. */ private void grow () { }

Exercise 9.54 /* * Act for the proton wave is: grow and check whether we hit anything. */ public void act() { grow(); } /* * Grow the wave. If we get to full size remove it. */ private void grow () { }

Exercise 9.55 if (our index has exceed the number of images) remove the wave from the world else set next image in the array & increment index end of if index is exceeded Pseudo code for grow method:

Exercise 9.55 /* * Grow the wave. If we get to full size remove it. */ private void grow () { if (imageCount >= NUMBER_IMAGES) getWorld().removeObject (this); else setImage(images[imageCount++]); }

Exercise 9.56 (testing) Right Click on ProtonWave Click on new ProtonWave() Drag into the World Click >Act to watch the wave grow

Exercise 9.57 /* * Create a new proton wave. */ public ProtonWave() { initializeImages(); setImage(images [0]); Greenfoot.playSound ("proton.wav"); } Add Sound

Exercise 9.58 (in the Rocket class) /* * Release a proton wave (if it is loaded). */ private void startProtonWave() { } Does Not Need Any Parameters and Does Not Return Anything

Exercise 9.59 /* * Release a proton wave (if it is loaded). */ private void startProtonWave() { ProtonWave wave = new ProtonWave(); getWorld().addObject (wave, getX(), getY()); }

Exercise 9.60 /* * Check whether there are any key pressed and react to them. */ private void checkKeys() { if (Greenfoot.isKeyDown("space")) fire(); if (Greenfoot.isKeyDown("z")) startProtonWave(); ignite (Greenfoot.isKeyDown ("up")); if (Greenfoot.isKeyDown("left")) turn(- 5); if (Greenfoot.isKeyDown("right")) turn(5); }

Exercise 9.60 (testing it)

Exercise 9.61 Proton Wave Can Be Released Too Fast by Holding Down the z Key

Exercise 9.61 private static final int gunReloadTime = 5; // The minimum delay between firing the gun. private static final int protonReloadTime = 200; // The minimum delay between proton wave bursts. private int reloadDelayCount; // How long ago we fired the gun the last time. private int reloadProtonDelayCount; // How long ago we fired proton wave the last time. private GreenfootImage rocket = new GreenfootImage("rocket.png"); private GreenfootImage rocketWithThrust = new GreenfootImage("rocketWithThrust.png"); A Delay Count of 200 Seems More Reasonable Than 500

Exercise 9.61 /* * Release a proton wave (if it is loaded). */ private void startProtonWave() { if (protonDelayCount >= protonReloadTime) { ProtonWave wave = new ProtonWave(); getWorld().addObject (wave, getX(), getY()); protonDelayCount = 0; } Improved version works if enough counts have elapsed

Exercise 9.61 (much better)

9.9 Interacting with Objects in Range List getObjectsInRange (int radius, Class cls)

Exercise 9.62 & 9.63 /* * act for the proton wave: checks asteroid collisions & grows the wave * (to avoid exceptions the order here is very important, too!) */ public void act() { checkCollision(); grow(); } /* * should a protonWave touch an asteroid, * explode all intersecting asteroids. */ private void checkCollision() { }

Exercise 9.64 /* * should a protonWave touch an asteroid, * explode all intersecting asteroids. */ private void checkCollision() { int range = getImage().getWidth() / 2; }

Exercise 9.65 import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.util.List; // important to add this import /* * Explode all intersecting asteroids. */ private void checkCollision() { int range = getImage().getWidth() / 2; List asteroids = getObjectsInRange (range, Asteroid.class); }

Exercise 9.66 /* * Hit this asteroid dealing the given amount of damage. */ public void hit(int damage) { stability = stability - damage; if(stability <= 0) breakUp (); } Before We Make Changes to the Method checkCollision (), Lets Look at the Hit Method Parameter is the Level of Damage

Exercise 9.67 /* The damage this wave will deal */ private static final int DAMAGE = 30;

Exercise 9.68 /* * Explode all intersecting asteroids. */ private void checkCollision() { int range = getImage().getWidth() / 2; List asteroids = getObjectsInRange (range, Asteroid.class); for (Asteroid a : asteroids) a.hit (DAMAGE); }

9.10 Further Development The following are some suggestions for future work, in the form of exercises. Many of them are independent of each other – they do not need to be done in this particular order. Pick those first that interest you most, and come up with some extension of your own.

Exercise 9.69 /* * Keep track of the score. */ public void countScore (int count) { scoreCounter.add(count); }

Exercise 9.69 private void breakUp() { Space space = (Space) getWorld(); // space is now a reference to Space class public methods Greenfoot.playSound("Explosion.wav"); if(size <= 16) // Removing an Asteroid is worth 25 points { getWorld().removeObject(this); space.countScore(25); } else // Splitting an Asteroid is worth 10 points { int r = getMovement().getDirection() + Greenfoot.getRandomNumber(45); double l = getMovement().getLength(); Vector speed1 = new Vector(r + 60, l * 1.2); Vector speed2 = new Vector(r - 60, l * 1.2); Asteroid a1 = new Asteroid(size/2, speed1); Asteroid a2 = new Asteroid(size/2, speed2); getWorld().addObject(a1, getX(), getY()); getWorld().addObject(a2, getX(), getY()); a1.move(); a2.move(); getWorld().removeObject(this); space.countScore(10); }

Exercise 9.69

9.11 Summary of Programming Techniques Understanding lists and loops is initially quite difficult, but very important in programming, so you should carefully review these aspects of your code if you are not yet comfortable in using them. The more practice you get, the easier it becomes. After using them for a while, you will be surprised that you found them so difficult at first.

9.11 Summary of Programming Techniques