Chapter 7 - Collision Detection: Asteroids

Slides:



Advertisements
Similar presentations
Breakout in Greenfoot Barb Ericson
Advertisements

Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Chapter 10 - Additional Scenario Ideas Bruce Chittenden.
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 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.
Game Rules Implementation Lecture 5 1. Revisit 2  Lab 4 – Other Input Methods  Touches Basics  Relationship between TouchesBegan, TouchesMoved, and.
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
GAME:IT Junior Bouncing Ball Objectives: Create Sprites Create Sounds Create Objects Create Room Program simple game.
Guide to Programming with Python
Chapter 5 - Making Music: An On-Screen Piano
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Chapter 4 Code Editor Goals and Objectives Program more efficiently? How can you speed up your development process? Do you want to learn useful shortcuts.
SOURCE 2006 Presentation by Luke Arntson Game Programming Optimization.
GREENFOOT CLUB RESOURCES Brian Cullen – Rossett School
GAME:IT Bouncing Ball Objectives: Create Sprites Create Sounds Create Objects Create Room Program simple game.
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.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
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
2015 CSE/EGR Summer Camps 1 Computer Science Concepts 1. What is an algorithm? 2. Binary information coding 3. Programming concepts via Scratch Designed.
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
Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July
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.
Lesson 3: Arrays and Loops. Arrays Arrays are like collections of variables Picture mailboxes all lined up in a row, or storage holes in a shelf – You.
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.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
11 Debugging Programs Session Session Overview  Create and test a method to calculate percentages  Discover how to use Microsoft Visual Studio.
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 9 - Collision Detection: Asteroids Bruce Chittenden (modified by Jeff Goldstein)
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
Chapter 4 - Finishing the Crab Game
Chapter 5 – Making Music: An On-Screen Piano (Part 1 – Using Loops)
Chapter 5 – Making Music: An On-Screen Piano (Part 2 – Using Arrays)
Interlude 2 - The Greeps Competition
Chapter 3 – Improving the Crab Game
Barb Ericson Georgia Institute of Technology June 2007
Games and Simulations O-O Programming in Java The Walker School
Breakout in Greenfoot Barb Ericson
More on Creating Classes
1.7 A Second Example.
Presentation transcript:

Chapter 7 - Collision Detection: Asteroids Bruce Chittenden

7.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 7.1

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

Spacebar is used to fire a bullet Exercise 7.3 Spacebar is used to fire a bullet

Creates the Explosion Visual and makes and Explosion Sound Exercise 7.4 Creates the Explosion Visual and makes and Explosion Sound

The visual is Present, But It Does Not Do Anything Exercise 7.5 The visual is Present, But It Does Not Do Anything

7.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.

The Background is Created by These Three Statements Exercise 7.6 The Background is Created by These Three Statements

Code to Create the Background is Commented Out Exercise 7.7 Code to Create the Background is Commented Out

Exercise 7.7

Exercise 7.8 Draw Oval Draw Rectangle Fill Oval

Exercise 7.8

Exercise 7.8

Exercise 7.8

Exercise 7.9

13 Defined Constant Fields Exercise 7.9 13 Defined Constant Fields

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

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

Exercise 7.11 Create 300 Stars

Exercise 7.13 Clean Compile

Exercise 7.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 7.15 for (int i = 0; i < count; i++) { } Initialization Loop-Condition Increment for (int i = 0; i < count; i++) { }

Exercise 7.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 = o; while ( i < count) int x = Greenfoot.getRandomNumber(getWidth()/2); int y = Greenfoot.getRandomNumber(getHeight()/2); addObject(new Asteroid(), x, y); i++; }

Exercise 7.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); }

Generate a random number for x and y and place a star at that location Exercise 7.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 7.18

Generate a random number for color in the range 0 to 255 Exercise 7.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 7.19

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

The method checkKeys handles keyboard input Exercise 7.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 7.21

Exercise 7.21 if (Greenfoot.isKeyDown("left")) setRotation(getRotation() - 5); if (Greenfoot.isKeyDown("right")) setRotation(getRotation() + 5); Left Negative Degrees Right Positive Degrees

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

Exercise 7.21

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

Exercise 7.22

7.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 7.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 7.23 The rocket does not move because our move vector has not been initialized and contains zero

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

The rocket drifts slowly toward the right of the World Exercise 7.24 The rocket drifts slowly toward the right of the World

Exercise 7.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")) setRotation(getRotation() - 5); if (Greenfoot.isKeyDown("right")) setRotation(getRotation() + 5 } Add a call to ignite

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

Exercise 7.27 Pseudo Code 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;

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

Exercise 7.27

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

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

Make a call to checkCollision from the Rocket Act method Exercise 7.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 List getIntersectingObjects (Class cls) Actor getOneIntersectingObject (Class cls) Bounding Box Visible Image

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

Exercise 7.31 Add the code to remove the Rocket from the World and display an Explosion /* * 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(); }

Exercise 7.31

Exercise 7.32 The problem with this code is that we removed the Object and then attempted to use it’s coordinates /* * 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()); }

Exercise 7.32

Exercise 7.33 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 (length: ~5 min, 11.6 MB) Making Explosions, Part II (length: ~18.5 min, 57.9 MB) Making Explosions, Part III (length: ~15 min, 52.2 MB)

7.6 Casting 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.

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

Exercise 7.35 /* * 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 actual Constructor

http://www.jafar.com/java/csel/index.html

Exercise 7.36 /* * Make the 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, 128)); image.fillRect(0, 0, WIDTH, HEIGHT); image.setColor(new Color(255, 0, 0, 128)); image.fillRect(5, 5, WIDTH-10, HEIGHT-10); Font font = image.getFont(); font = font.deriveFont(ITALIC, FONT_SIZE); image.setFont(font); image.setColor(Color.BLUE); image.drawString(title, 60, 100); image.drawString(prefix + score, 60, 200); setImage(image); }

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

Example 7.36

gameOver Method is a stub Exercise 7.37 gameOver Method is a stub

Exercise 7.38 /* * 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); }

Exercise 7.39

To insure that the Score is placed in the of the World, center it at Exercise 7.40 To insure that the Score is placed in the of the World, center it at ½ width and ½ height /* * 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); }

Exercise 7.41 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. Space space = (space) getWorld();

Code 7.1 /* * Check for a collision with an Asteroid */ private void checkCollision() { Actor a = getOneIntersectingObject (Asteroid.class); if (a != null) World world = getWorld(); world.addObject (new Explosion(), getX(), getY()); world.removeObject (this); world.gameOver(); }

Exercise 7.42 /* * Check for a collision with an Asteroid Cast (Space) */ 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(); } Cast (Space)

Exercise 7.43 inconvertible types

7.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 7.44 Does not Move Does not Disappear Does not cause Damage

Exercise 7.45 The Constructor and Two Methods ProtonWave () 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 7.46 /* * 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 7.47 /* * 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 7.47 Pseudo Code 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 the size of this image to be the width of the 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

7.8 Growing the Wave GreenfootImage [ ] 0 1 2 3 4 29

Code 7.2 /* * 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 7.48 /* * 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 7.49 /* * Create a new proton wave. */ public ProtonWave() { initializeImages(); setImage(images [0]); }

Exercise 7.49

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

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

Exercise 7.52 /* * 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 7.53 Pseudo Code if (our index has exceed the number of images) remove the wave from the world; else set the next image in the array and increment the index;

Exercise 7.53 /* * 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 7.54 Right Click on ProtonWave Click on new ProtonWave() Drag into the World Click >Act to watch the wave grow

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

Does Not Need Any Parameters and Does Not Return Anything Exercise7.56 /* * Release a proton wave (if it is loaded). */ private void startProtonWave() { } Does Not Need Any Parameters and Does Not Return Anything

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

Exercise 7.58 /* * 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")) setRotation(getRotation() - 5); if (Greenfoot.isKeyDown("right")) setRotation(getRotation() + 5); }

Exercise 7.58

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

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

Exercise 7.59 /* * 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++; protonDelayCount++; }

Exercise 7.59 /* * 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; }

Exercise 7.59

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

Exercise 7.60 /* * Act for the proton wave is: grow and check whether we hit anything. */ public void act() { checkCollision(); grow(); } * Explode all intersecting asteroids. private void checkCollision()

Exercise 7.61 /* * Act for the proton wave is: grow and check whether we hit anything. */ public void act() { checkCollision(); grow(); } * Explode all intersecting asteroids. private void checkCollision()

Exercise 7.62 /* * Explode all intersecting asteroids. */ private void checkCollision() { int range = getImage().getWidth() / 2; }

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

Exercise 7.64 Parameter is the Level of Damage /* * 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

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

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

7.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 7.67 /* * Keep track of the score. */ public void countScore (int count) { scoreCounter.add(count); }

Exercise 7.67 private void breakUp() { Space space = (Space) getWorld(); 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(); space.countScore(10);

Exercise 7.67

7.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.

7.11 Summary of Programming Techniques