1.13k likes | 1.51k Views
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.
E N D
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.2 Controls for the Rocket Collision Logic Explosion Logic ScoreBoard Logic Implement ProtonWave
Exercise 7.3 Spacebar is used to fire a bullet
Exercise 7.4 Creates the Explosion Visual and makes and Explosion Sound
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.
Exercise 7.6 The Background is Created by These Three Statements
Exercise 7.7 Code to Create the Background is Commented Out
Exercise 7.8 Draw Oval Draw Rectangle Fill Oval
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 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); } }
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.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
7.3 Turning We want to make the rocket turn left or right using the left and right arrow keys.
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 if (Greenfoot.isKeyDown("left")) setRotation(getRotation() - 5); if (Greenfoot.isKeyDown("right")) setRotation(getRotation() + 5); Left Negative Degrees Right Positive 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.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
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
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.
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
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;
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); } }
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); }
Exercise 7.28 Define a stub method for checkCollision /* * Check for a collision with an Asteroid */ private void checkCollision() { }
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