This site requires JavaScript, please enable it in your browser!
Greenfoot back
Okiele1
Okiele1 wrote ...

2017/3/13

Making enemies appear at random places while playing

Okiele1 Okiele1

2017/3/13

#
I have this top-down shooter game, where i want more enemies to appear, as soon as you have shot some. I would like the game to get harder and harder, and i'm later on addding a highscore counter, so you can compete. So how do i make enemies appear at random places in my world, so lets say, everytime you kill a zombie, two more spawn at a random place in the world? Thanks in advance!
Super_Hippo Super_Hippo

2017/3/13

#
If you double the number of zombies all the time, it will get harder pretty quickly... Well, this could be done like that:
//when killing a Zombie
((MyWorld) getWorld()).spawnZombie(2);
//in MyWorld - your world, rename if needed
public void spawnZombie(int numZombies)
{
    for (int i=0; i<numZombies; i++)
    {
        Zombie z = new Zombie();
        addObject(z, Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
        if (z.tooNear())
        {
            i--;
            removeObject(z);
        }
    }
}
//in Zombie
public boolean tooNear()
{
    return !getObjectsInRange(100, Player.class).isEmpty(); //100 is the range around the Player where no zombie should be placed. adjust as needed
}
Okiele1 Okiele1

2017/3/14

#
Wow thank you! That worked like a charme! And thanks for the boolean, i had completely forgottten to implement anything like that.. I see what you mean with, that it quickly gets impossible to handle.. Have you got another better idea, for making it harder and harder?
Super_Hippo Super_Hippo

2017/3/14

#
You could add new zombies randomly (without the condition that one died) and then change the random condition over time to make it more likely that new zombies appear at later stages of the game.
Okiele1 Okiele1

2017/3/14

#
I see, that sounds like a better idea, how would you propose doing that? I am new to programming, so i am still learning.
danpost danpost

2017/3/14

#
Probably the best way is to limit the number of zombies allowed in your world. The limit can increase over time using a timer, the score or the number of kills. That way you are guaranteed to not be overridden with zombies too quickly. Something like this might work for you:
if (getObjects(Zombie.class).size() < 20+kills/3) spawnZombie(1);
which says to start with 20 zombies and every time 3 are killed, the number of zombies allowed in the world will increase one. However, tracking the number of kills in the world may not be quite so simple. By adding a field for the limiting value of the number of zombies in the world and increasing it for every, say, 3 kills, it should be manageable:
// instance fields
private int zombieLimit = 20;
private int kills;

// in action code
if (getObjects(Zombie.class).size() < zombieLimit)
{
    kills++;
    if (kills%3 == 0) // every 3 kills
    {
        zombieLimit++; // increase limit on number of zombies allowed in world
        spawnZombie(1); // spawn an extra one due to increase in number allowed
    }
    spawnZombie(1); // spawn one for the kill
}
This is provided that the initial 'zombieLimit' number of zombies are spawned by the world constructor using 'spawnZombie(20);'.
Okiele1 Okiele1

2017/3/14

#
I dont have have a variable named kill, what could be instead of? Greenfoot says that variable is wrong? I talked to one of my friends, he said that i could implement a score counter together with that, but i have no idea how.. EDIT: Didn't see you had editet your answer EDIT EDIT: Can't seem to get it to work.. if i put it in, and i shoot one zombie, it creates 20 more i think, and keeps going?
Nosson1459 Nosson1459

2017/3/14

#
You have to make sure that spawnZombie(20) is only in the constructor, and in the act you only have calls to the method with a 1 as the parameter. If you can't get it to work post this World code.
Okiele1 Okiele1

2017/3/14

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    HealthBar healthbar = new HealthBar();
    
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1300, 800, 1); 
        addObject(new GoodGuy(), 650,400);
        addObject(healthbar, 300, 100); 
       
        
        spawnBadGuys(20);
        
        
        
    }
    
    private int BadGuysLimit = 20;
    private int kills;
    
    public void act()
    {
        kills();
    } 
    
    public void kills()
    {
        if (getObjects(BadGuys.class).size() < BadGuysLimit)
		{
    		kills++;
    		if (kills%3 == 0) // every 3 kills
    		{
        		BadGuysLimit++; // increase limit on number of zombies allowed in world
        		spawnBadGuys(1); // spawn an extra one due to increase in number allowed
    		}
    		spawnBadGuys(1); // spawn one for the kill
		}
    }
    
    public HealthBar getHealthBar()
    {
        return healthbar;
        
    }
    
    public void spawnBadGuys(int numBadGuys)
    {   
        for (int i=0; i<numBadGuys; i++)
        	{
        		BadGuys z = new BadGuys();
        		addObject(z, Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
        			if (z.tooNear())
        			{
        			i--;
            		removeObject(z);
        		}
    		}
	}
	
	
}
This is all the code from MyWorld my zombies are called BadGuys, because i originally had another idea planned.. I am really not good at this yet, so it might not look pretty or neat in any way..
Nosson1459 Nosson1459

2017/3/14

#
Show the code for shooting the Zombie (it could be that I'm asking you for too much and all this isn't necessary but as of now I don't see why you should get more than 1 or 2 zombies for each shot).
Okiele1 Okiele1

2017/3/15

#
  public void die()
        {
            //Det her afsnit viser hvordan bullet rammer zombien så den dør.
            Actor bullet = getOneIntersectingObject (Bullet.class);
            
            if(bullet!=null)
            {
                //Får 2 nye til at spawne når en dør
                ((MyWorld) getWorld()).spawnBadGuys(2);
				getWorld().removeObject(bullet);
                getWorld().removeObject(this);
				
        }
        

        
    }
I'm danish, hence, the comments in another language. The line spawnBadGuys are the line that makes two zombies spawn each time i kill one.
danpost danpost

2017/3/15

#
Okiele1 wrote...
The line spawnBadGuys are the line that makes two zombies spawn each time i kill one.
The world will take care of adding more zombies into the world -- remove lines 8 and 9 (and line 3) in the 'die' method given above.
Okiele1 Okiele1

2017/3/15

#
Thanks! I got it to work, and made a score counter. Now i want to make a Scoreboard to be shown at the end when you have been killed by the zombies. How do i get it to show the top 10 best highscores? Should i create a new thread for this, or is it all right that i ask it here? thanks in advance!
danpost danpost

2017/3/15

#
Okiele1 wrote...
Should i create a new thread for this, or is it all right that i ask it here?
That would be best -- so that the title of the thread fits what is in it.
You need to login to post a reply.