Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 - Collision Detection: Asteroids

Similar presentations


Presentation on theme: "Chapter 7 - Collision Detection: Asteroids"— Presentation transcript:

1 Chapter 7 - Collision Detection: Asteroids
Bruce Chittenden

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

3 Exercise 7.1

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

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

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

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

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

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

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

11 Exercise 7.7

12 Exercise 7.8 Draw Oval Draw Rectangle Fill Oval

13 Exercise 7.8

14 Exercise 7.8

15 Exercise 7.8

16 Exercise 7.9

17 13 Defined Constant Fields
Exercise 7.9 13 Defined Constant Fields

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

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

20 Exercise 7.11 Create 300 Stars

21 Exercise 7.13 Clean Compile

22 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

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

24 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++; }

25 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); }

26 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

27 Exercise 7.18

28 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

29 Exercise 7.19

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

31 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

32 Exercise 7.21

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

34 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

35 Exercise 7.21

36 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

37 Exercise 7.22

38 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

39 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

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

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

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

43 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

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

45 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;

46 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);

47 Exercise 7.27

48 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); }

49 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() { }

50 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

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

52 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

53 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(); }

54 Exercise 7.31

55 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()); }

56 Exercise 7.32

57 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)

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

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

60 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

61

62 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); }

63 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;

64 Example 7.36

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

66 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); }

67 Exercise 7.39

68 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); }

69 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();

70 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(); }

71 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)

72 Exercise 7.43 inconvertible types

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

74 Exercise 7.44 Does not Move Does not Disappear Does not cause Damage

75 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 ()

76 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()

77 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++; }

78 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

79 7.8 Growing the Wave GreenfootImage [ ]

80 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++; }

81 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

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

83 Exercise 7.49

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

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

86 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 ()

87 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;

88 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++]); }

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

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

91 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

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

93 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); }

94 Exercise 7.58

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

96 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

97 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++; }

98 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; }

99 Exercise 7.59

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

101 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()

102 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()

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

104 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); }

105 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

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

107 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); }

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

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

110 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);

111 Exercise 7.67

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

113 7.11 Summary of Programming Techniques


Download ppt "Chapter 7 - Collision Detection: Asteroids"

Similar presentations


Ads by Google