CrabWorld with Parameterized and Random Object Placement


/ Published in: Java
Save to your folder(s)

A much better implementation of the parameterized and randomized CrabWorld


Copy this code and paste it in your HTML
  1. import greenfoot.*; // (Actor, World, Greenfoot, GreenfootImage)
  2.  
  3. public class CrabWorld extends World
  4. {
  5. private static final int STARTING_X = 150;
  6. private static final int STARTING_Y = 100;
  7. private static final int NUMBER_OF_WORMS = 5;
  8. private static final int NUMBER_OF_LOBSTERS = 3;
  9. private static final int WORLD_X = 800;
  10. private static final int WORLD_Y = 800;
  11.  
  12. /**
  13.   * Create the default crab world (the beach). Our world has a size
  14.   * of 800x800 cells, where every cell is just 1 pixel, five worms
  15.   * and three lobsters
  16.   */
  17. public CrabWorld()
  18. {
  19. super(WORLD_X, WORLD_Y, 1);
  20. populate( STARTING_X, STARTING_Y, NUMBER_OF_WORMS, NUMBER_OF_LOBSTERS );
  21. }
  22.  
  23. /**
  24.   * Fully parameterized version of the CrabWorld constructor.
  25.   * Specify the width and height of the world, the starting location of the crab,
  26.   * and the number of worms and lobsters.
  27.   *
  28.   * @param width the width of the world in cells (pixels, effectively)
  29.   * @param height the height of the world in cells (pixels, effectively)
  30.   * @param startX the starting x-coordinate of the crab
  31.   * @param startY the starting y-coordinate of the crab
  32.   * @param numberOfWorms the starting number of worms
  33.   * @param numberOfLobsters the starting number of lobsters
  34.   */
  35. public CrabWorld( int width, int height, int startX, int startY, int numberOfWorms, int numberOfLobsters )
  36. {
  37. super( width, height, 1 );
  38. populate( startX, startY, numberOfWorms, numberOfLobsters);
  39. }
  40.  
  41. /**
  42.   * Partially paramterized version of the CrabWorld constructor.
  43.   * Specify the number of worms and lobsters only.
  44.   *
  45.   * @param numberOfWorms the starting number of worms
  46.   * @param numberOfLobsters the starting number of lobsters
  47.   */
  48. public CrabWorld( int numberOfWorms, int numberOfLobsters )
  49. {
  50. super(WORLD_X, WORLD_Y, 1);
  51. populate( STARTING_X, STARTING_Y, numberOfWorms, numberOfLobsters);
  52. }
  53.  
  54. private void populate( int startX, int startY, int worms, int lobsters )
  55. {
  56. addObject( new Crab(), startX, startY );
  57.  
  58. for( int i = 0; i < worms; i++ )
  59. {
  60. addObject( new Worm(), Greenfoot.getRandomNumber( getWidth() ), Greenfoot.getRandomNumber( getHeight() ) );
  61. }
  62.  
  63. for( int i = 0; i < NUMBER_OF_LOBSTERS; i++ )
  64. {
  65. addObject( new Lobster(), Greenfoot.getRandomNumber( getWidth() ), Greenfoot.getRandomNumber( getHeight() ) );
  66. }
  67. }
  68.  
  69.  
  70. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.