1 / 113

Chapter 7 - Collision Detection: Asteroids

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.

Download Presentation

Chapter 7 - Collision Detection: Asteroids

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. Exercise 7.3 Spacebar is used to fire a bullet

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

  7. 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. Exercise 7.6 The Background is Created by These Three Statements

  10. 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. 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 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Exercise 7.28 Define a stub method for checkCollision /* * Check for a collision with an Asteroid */ private void checkCollision() { }

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

More Related