chapter 7 - collision detection: asteroids bruce chittenden
TRANSCRIPT
![Page 1: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/1.jpg)
Chapter 7 - Collision Detection: Asteroids
Bruce Chittenden
![Page 2: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/2.jpg)
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.
![Page 3: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/3.jpg)
Exercise 7.1
![Page 4: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/4.jpg)
Exercise 7.2
Controls for the RocketCollision LogicExplosion LogicScoreBoard LogicImplement ProtonWave
![Page 5: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/5.jpg)
Exercise 7.3
Spacebar is used to fire a bullet
![Page 6: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/6.jpg)
Exercise 7.4
Creates the Explosion Visual and makes and Explosion Sound
![Page 7: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/7.jpg)
Exercise 7.5
The visual is Present, But It Does Not Do Anything
![Page 8: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/8.jpg)
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.
![Page 9: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/9.jpg)
Exercise 7.6
The Background is Created by These Three Statements
![Page 10: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/10.jpg)
Exercise 7.7
Code to Create the Background is Commented Out
![Page 11: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/11.jpg)
Exercise 7.7
![Page 12: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/12.jpg)
Exercise 7.8
Draw Rectangle
Draw Oval
Fill Oval
![Page 13: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/13.jpg)
Exercise 7.8
![Page 14: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/14.jpg)
Exercise 7.8
![Page 15: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/15.jpg)
Exercise 7.8
![Page 16: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/16.jpg)
Exercise 7.9
![Page 17: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/17.jpg)
Exercise 7.9
13 Defined Constant Fields
![Page 18: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/18.jpg)
Exercise 7.10
/* * Method to create stars. The integer number is how many. */private void createStars(int number) { // Need to add code here}
![Page 19: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/19.jpg)
Exercise 7.11
/* * Method to create stars. The integer number is how many. */
![Page 20: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/20.jpg)
Create 300 Stars
Exercise 7.11
![Page 21: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/21.jpg)
Exercise 7.13
Clean Compile
![Page 22: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/22.jpg)
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
![Page 23: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/23.jpg)
Exercise 7.15
for (int i = 0; i < count; i++) {
}
Initialization
Loop-Condition
Increment
![Page 24: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/24.jpg)
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++; }}
![Page 25: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/25.jpg)
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); }}
![Page 26: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/26.jpg)
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
![Page 27: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/27.jpg)
Exercise 7.18
![Page 28: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/28.jpg)
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
![Page 29: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/29.jpg)
Exercise 7.19
![Page 30: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/30.jpg)
7.3 Turning
We want to make the rocket turn left or right using the left and right arrow keys.
![Page 31: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/31.jpg)
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
![Page 32: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/32.jpg)
Exercise 7.21
![Page 33: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/33.jpg)
Exercise 7.21
Left Negative Degrees
Right Positive Degrees
if (Greenfoot.isKeyDown("left")) setRotation(getRotation() - 5);
if (Greenfoot.isKeyDown("right")) setRotation(getRotation() + 5);
![Page 34: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/34.jpg)
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
![Page 35: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/35.jpg)
Exercise 7.21
![Page 36: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/36.jpg)
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
![Page 37: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/37.jpg)
Exercise 7.22
![Page 38: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/38.jpg)
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
![Page 39: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/39.jpg)
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
![Page 40: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/40.jpg)
Exercise 7.23
The rocket does not move because our move vector has not been initialized and contains zero
![Page 41: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/41.jpg)
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.
![Page 42: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/42.jpg)
Exercise 7.24
The rocket drifts slowly toward the right of the World
![Page 43: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/43.jpg)
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
![Page 44: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/44.jpg)
Exercise 7.26
/* * Go with thrust on */private void ignite (boolean boosterOn){
}
Define a stub method for ignite
![Page 45: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/45.jpg)
Exercise 7.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
![Page 46: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/46.jpg)
Exercise 7.27
/* * Go with thrust on */private void ignite (boolean boosterOn){ if (boosterOn) { setImage (rocketWithThrust); addForce (new Vector (getRotation(), 0.3)); } else { setImage (rocket); }}
The ignite method complete
![Page 47: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/47.jpg)
Exercise 7.27
![Page 48: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/48.jpg)
7.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
![Page 49: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/49.jpg)
Exercise 7.28
/* * Check for a collision with an Asteroid */private void checkCollision(){
}
Define a stub method for checkCollision
![Page 50: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/50.jpg)
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
![Page 51: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/51.jpg)
Intersecting Objects
Bounding Box
Visible Image
List getIntersectingObjects (Class cls)Actor getOneIntersectingObject (Class cls)
![Page 52: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/52.jpg)
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
![Page 53: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/53.jpg)
Exercise 7.31
/* * 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(); }}
Add the code to remove the Rocket from the World and
display an Explosion
![Page 54: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/54.jpg)
Exercise 7.31
![Page 55: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/55.jpg)
Exercise 7.32
/* * 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 it’s coordinates
![Page 56: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/56.jpg)
Exercise 7.32
![Page 57: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/57.jpg)
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)
![Page 58: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/58.jpg)
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.
![Page 59: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/59.jpg)
Exercise 7.34Right Click on ScoreBoard then click on
new ScoreBoard and drag it into the World
![Page 60: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/60.jpg)
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
![Page 61: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/61.jpg)
http://www.jafar.com/java/csel/index.html
![Page 62: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/62.jpg)
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);}
![Page 63: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/63.jpg)
Exercise 7.36
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;
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
![Page 64: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/64.jpg)
Example 7.36
![Page 65: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/65.jpg)
Exercise 7.37
gameOver Method is a stub
![Page 66: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/66.jpg)
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);}
![Page 67: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/67.jpg)
Exercise 7.39
![Page 68: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/68.jpg)
Exercise 7.40
/* * 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
![Page 69: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/69.jpg)
Exercise 7.41The 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();
![Page 70: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/70.jpg)
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(); }}
![Page 71: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/71.jpg)
Exercise 7.42
/* * 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(); }}
Cast (Space)
![Page 72: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/72.jpg)
Exercise 7.43
inconvertible types
![Page 73: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/73.jpg)
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.
![Page 74: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/74.jpg)
Exercise 7.44
Does not MoveDoes not DisappearDoes not cause Damage
![Page 75: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/75.jpg)
Exercise 7.45public 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 MethodsProtonWave ()initializeImages ()act ()
![Page 76: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/76.jpg)
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() { }
![Page 77: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/77.jpg)
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++; } }}
![Page 78: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/78.jpg)
Exercise 7.47
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
Pseudo Code
![Page 79: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/79.jpg)
7.8 Growing the Wave
GreenfootImage [ ]
0 1 2 3 4 29
![Page 80: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/80.jpg)
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++; } }}
![Page 81: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/81.jpg)
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
![Page 82: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/82.jpg)
Exercise 7.49
/* * Create a new proton wave. */public ProtonWave() { initializeImages(); setImage(images [0]);}
![Page 83: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/83.jpg)
Exercise 7.49
![Page 84: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/84.jpg)
Exercise 7.50
/* * Current size of the wave. */private int imageCount = 0;
![Page 85: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/85.jpg)
Exercise 7.51
/* * Grow the wave. If we get to full size remove it. */private void grow (){}
![Page 86: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/86.jpg)
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 (){}
![Page 87: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/87.jpg)
Exercise 7.53
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;
Pseudo Code
![Page 88: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/88.jpg)
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++]);}
![Page 89: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/89.jpg)
Exercise 7.54
Right Click on ProtonWaveClick on new ProtonWave()
Drag into the World
Click >Act to watch the wave grow
![Page 90: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/90.jpg)
Exercise 7.55
/* * Create a new proton wave. */public ProtonWave() { initializeImages(); setImage(images [0]); Greenfoot.playSound ("proton.wav");}
Add Sound
![Page 91: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/91.jpg)
Exercise7.56
/* * Release a proton wave (if it is loaded). */private void startProtonWave(){}
Does Not Need Any Parameters and Does Not
Return Anything
![Page 92: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/92.jpg)
Exercise 7.57
/* * Release a proton wave (if it is loaded). */private void startProtonWave(){ ProtonWave wave = new ProtonWave(); getWorld().addObject (wave, getX(), getY());}
![Page 93: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/93.jpg)
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);}
![Page 94: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/94.jpg)
Exercise 7.58
![Page 95: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/95.jpg)
Exercise 7.59
Proton Wave Can Be Released Too Fast by Holding Down the z Key
![Page 96: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/96.jpg)
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
![Page 97: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/97.jpg)
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++;}
![Page 98: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/98.jpg)
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; }}
![Page 99: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/99.jpg)
Exercise 7.59
![Page 100: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/100.jpg)
7.9 Interacting with Objects in Range
List getObjectsInRange (int radius, Class cls)
![Page 101: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/101.jpg)
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(){}
![Page 102: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/102.jpg)
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(){}
![Page 103: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/103.jpg)
Exercise 7.62
/* * Explode all intersecting asteroids. */private void checkCollision(){ int range = getImage().getWidth() / 2;}
![Page 104: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/104.jpg)
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);}
![Page 105: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/105.jpg)
Exercise 7.64
/* * 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
![Page 106: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/106.jpg)
Exercise 7.65
/* The damage this wave will deal */ private static final int DAMAGE = 30;
![Page 107: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/107.jpg)
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);}
![Page 108: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/108.jpg)
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.
![Page 109: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/109.jpg)
Exercise 7.67
/* * Keep track of the score. */public void countScore (int count){ scoreCounter.add(count); }
![Page 110: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/110.jpg)
Exercise 7.67private 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(); getWorld().removeObject(this); space.countScore(10); }}
![Page 111: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/111.jpg)
Exercise 7.67
![Page 112: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/112.jpg)
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.
![Page 113: Chapter 7 - Collision Detection: Asteroids Bruce Chittenden](https://reader038.vdocuments.us/reader038/viewer/2022102506/56649ce15503460f949aca41/html5/thumbnails/113.jpg)
7.11 Summary of Programming Techniques