I just have one more quick question. Currently I spawn eight asteroids in a random location between the right edge of the world and a location that is twice the length of the world. When the program is run, the asteroids move to the left towards the spaceship on the screen, prompting the user to dodge these asteroids. I want the game to be infinite, so what must I do to edit my code so that the FOR loop generating the eight asteroids is repeated once the asteroid with the highest x-value comes into sight (passes left over right edge)? I suspect I will somehow need to find out how to find the asteroid with the highest x-value, then create an IF block that states if the asteroid with the highest x-value (the last asteroid since all moving left) becomes less than the width of the screen, then to do the loop again to repeat the process. I originally thought I would need a timer for this, but since all the asteroids are moving a random speed (between -1 and -20), a static time delay in spawning a group of eight asteroids would not be accurate every time. Can someone please help? Here is a link to what the game looks like currently:
https://drive.google.com/file/d/1nwxdlKqwgwIc97LfT-M7SmcYxBR2_TT1/view
Asteroid Class:
World Class:
This is the last problem with my game I am trying to fix.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Asteroid here. * * @author (your name) * @version (a version number or a date) */ public class Asteroid extends Actor { int aSpeed; GreenfootSound closeCall = new GreenfootSound( "closeCallSound.wav" ); public Asteroid( int speedIn) { aSpeed = speedIn; move(aSpeed); GreenfootImage img = new GreenfootImage( 20 , 20 ); img = getImage(); img.scale( 25 + Greenfoot.getRandomNumber( 200 ), 25 + Greenfoot.getRandomNumber( 200 )); //random size between 25 and 200 (both for width and height, not proportional) setImage(img); setRotation(Greenfoot.getRandomNumber( 60 )- 30 ); //random orientation } protected void addedToWorld(World w) //built-in method, no call needed { while (getX() == 0 || isTouching(Asteroid. class )) //check if any overlap occurs { setLocation(Greenfoot.getRandomNumber(w.getWidth()) + (w.getWidth() + ( this .getImage().getWidth() / 2 )), Greenfoot.getRandomNumber(w.getHeight())); //random location for asteroids } } void removeAsteroids() { //remove asteroids if asteroids are no longer needed to save space if ( this .isAtEdge()) { if ( this .getY() + ( this .getImage().getHeight() / 2 ) < 0 ) { getWorld().removeObject( this ); //System.out.println("surpassed edge"); } else if ( this .getY() - ( this .getImage().getHeight() / 2 ) > getWorld().getHeight()) { getWorld().removeObject( this ); //System.out.println("surpassed edge"); } else if ( this .getX() + ( this .getImage().getHeight() / 2 ) < 0 ) { getWorld().removeObject( this ); //System.out.println("surpassed edge"); } } } void CollisionDetection() { //prevents overlap of asteroids when moving Actor a = getOneIntersectingObject(Asteroid. class ); if (a != null ) { if ( this .getX() < a.getX()) { this .setLocation( this .getX() - 4 , this .getY()); } if ( this .getX() > a.getX()) { this .setLocation( this .getX() + 4 , this .getY()); } if ( this .getY() < a.getY()) { this .setLocation( this .getX(), this .getY() - 4 ); } if ( this .getY() > a.getY()) { this .setLocation( this .getX(), this .getY() + 4 ); } } } /** * Act - do whatever the Asteroid wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { //random movement move( this .aSpeed); CollisionDetection(); removeAsteroids(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.lang.*; /** * Write a description of class MyWorld here. * * @author (your name) * @version (a version number or a date) */ public class MyWorld extends World { private SpaceBackground Image1, Image2; /** * Constructor for objects of class MyWorld. * */ public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 1200 , 800 , 1 , false ); Image1 = new SpaceBackground(); addObject(Image1, getWidth()/ 2 , getHeight()/ 2 ); Image2 = new SpaceBackground(); addObject(Image2, getWidth() + getWidth()/ 2 , getHeight()/ 2 ); Spaceship ship = new Spaceship(); addObject(ship, 100 , getHeight()/ 2 ); spawn(); } void spawn() { for ( int i = 0 ; i <= 7 ;i++) { //spawning only 6 asteroids for testing Asteroid test = new Asteroid((Greenfoot.getRandomNumber( 19 )- 20 )); addObject(test, 0 , 0 ); } } public void act() { Image1.ActivateScroll(); Image2.ActivateScroll(); } } |