Well, the title might be very complicated if you think of what I'm actually trying to do. Well I have a typical 'the-sky-is-falling' game and as in most of these type of games you have to catch apples, and I maximum allows 3 at a time, and you get + 1 score each time you catch an apple. And now all I need to do is to make it add another apple when one apple dies (I already have a variable to check for if the apples is alive, and when to kill them), so here's my source code:

Main.java:

import java.applet.*;
import java.awt.*;

@SuppressWarnings("serial")
public class Main extends Applet implements Runnable
{
	private Image buffering_image;
	private Graphics buffering_graphics;
	private int max_apples = 3;
	private int score = 0;
	private GameObject player;
	private GameObject[] apple = new GameObject[max_apples];
	private boolean move_left = false;
	private boolean move_right = false;

    public void init()
    {
        load_content();
        setBackground(Color.BLACK);
    }

	public void run()
	{		
		while(true)
		{
			if(move_left)
			{
				player.player_x -= player.movement_speed;
			}
			else if(move_right)
			{
				player.player_x += player.movement_speed;
			}
			
			for(int i = 0; i < max_apples; i++)
			{
			    apple[i].apple_y += apple[i].falling_speed;
			}
			
			repaint();
			prevent();
			
			try
			{
				collision();
			}
			catch(InterruptedException e)
			{
				e.printStackTrace();
			}
			
		    try
		    {
		        Thread.sleep(20);
		    }
		    catch(InterruptedException ie)
		    {
		        System.out.println(ie);
		    }
		}
	}
	
	private void prevent()
	{
		if(player.player_x <= 0)
		{
		    player.player_x = 0;	
		}
		else if(player.player_x >= 925)
		{
		    player.player_x = 925;	
		}
	}
	
	private void load_content()
	{
	    MediaTracker media = new MediaTracker(this);
	    
	    player = new GameObject(getImage(getCodeBase(), "Sprites/player.gif"));
	    media.addImage(player.sprite, 0);
	    
	    for(int i = 0; i < max_apples; i++)
	    {	 	    	
	        apple[i] = new GameObject(getImage(getCodeBase(), "Sprites/apple.jpg"));

            if(!apple[i].alive)
            {
                apple[i] = new GameObject(getImage(getCodeBase(), "Sprites/apple.jpg"));
            }
	    }
	    
	    try
	    {
	        media.waitForAll();	
	    }
	    catch(Exception e)
	    {
	    	System.out.println(e);
	    }
	}
	
	public boolean collision() throws InterruptedException
	{
		for(int i = 0; i < max_apples; i++)
		{
		    Rectangle apple_rect = new Rectangle(apple[i].apple_x, apple[i].apple_y,
                    apple[i].sprite.getWidth(this),
                    apple[i].sprite.getHeight(this));
		    Rectangle player_rect = new Rectangle(player.player_x, player.player_y,
                    player.sprite.getWidth(this),
                    player.sprite.getHeight(this));
		    
		    if(apple_rect.intersects(player_rect))
		    {
		    	if(apple[i].alive)
		    	{
		    	    score++;
		    	    apple[i].alive = false;
		    	}
		    }
		    else if(apple[i].apple_y > 750)
		    {
		    	apple[i].alive = false;
		    }
		}
		
	    return true;
	}

    public void update(Graphics g)
    {
        if(buffering_image == null)
        {
            buffering_image = createImage(getSize().width, getSize().height);
            buffering_graphics = buffering_image.getGraphics();
        }
        
        buffering_graphics.setColor(getBackground());
        buffering_graphics.fillRect(0, 0, getSize().width, getSize().height);
        buffering_graphics.setColor(getForeground());
        paint(buffering_graphics);
        g.drawImage(buffering_image, 0, 0, this);
    }
    
    public boolean keyDown(Event e, int i)
    {
    	i = e.key;

        if(i == 1006)
        {
            move_left = true;
        }
        else if(i == 1007)
        {
        	move_right = true;
        }
    	
		return true;	
    }
    
    public boolean keyUp(Event e, int i)
    {
    	i = e.key;
    	
        if(i == 1006)
        {
            move_left = false;
        }
        else if(i == 1007)
        {
        	move_right = false;
        }   	
    	
        return true;
    }

    public void paint(Graphics g)
    {
        g.drawImage(player.sprite, player.player_x, player.player_y, this);
        
        for(int i = 0; i < max_apples; i++)
        {
        	if(apple[i].alive)
        	{
        		g.drawImage(apple[i].sprite, apple[i].apple_x, apple[i].apple_y, this);
        	}
        }
        
        g.setColor(Color.RED);
        g.drawString("Score: " + score, 450, 100);
    }

    public void start()
    {
        Thread thread = new Thread(this);
        thread.start();
    }
}

And here's my GameObject class:

import java.awt.*;
import java.util.*;

public class GameObject
{
    public Image sprite;
    public Random random = new Random();
    public int player_x;
    public int player_y;
    public int movement_speed = 15;
    public int falling_speed;
    public int apple_x;
    public int apple_y;
    public boolean alive;
    
    public GameObject(Image loaded_image)
    {
    	player_x = 425;
    	player_y = 725;
    	sprite = loaded_image;
    	falling_speed = random.nextInt(20) + 1;
    	apple_x = random.nextInt(920) + 1;
    	apple_y = random.nextInt(100) + 1;
    	alive = true;
    }
}

So please look through my whole code, (you should be able to understand it, unless you're a complete noob ;)) and tell me what's 'wrong' or at least give me a tip on how I can make it create a new apple when one of them dies..

Kind regards,
Benjamin Dahse

If apple_y reaches getSize().height then you need to reset it to the top of the screen. You could randomize apple_x if you wanted to or just reset apple_y to 0.

If apple_y reaches getSize().height then you need to reset it to the top of the screen. You could randomize apple_x if you wanted to or just reset apple_y to 0.

THANKS A LOT DUDE! I got it working.. I just erased the 'alive' boolean and set it to that each time an apple gets out of the screen it'll get a new position, and the same thing, that if the apple's rectangle intersects with the player's rectangle it'll also be assigned to a new position variable.. I'm sorry to waste your time asking such a stupid question, but you know sometimes the solution is just so simple that you don't even think about it....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.