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

2017/4/25

Increasing the speed of enemies over time

surgikar surgikar

2017/4/25

#
So basically I have made a Space Invaders Game, and I want to make the aliens increase in movement speed over time. I have already typed a code to make it increase every time there is a new wave, but as well as that I want them to increase in speed over time. However, i don't want it to turn out that when the next wave comes the speed is super fast because the Aliens were killed slowly and that their speed was already fast, making the aliens Ultra fast. Please Help . :D In addition, do you know how to make a 0.2 explosion every time an alien is killed, provided i already have the image. Once again, PLEASE HELP. ty.
TheCoolCoder321 TheCoolCoder321

2017/4/25

#
It would be helpful to see the code you are talking about.
surgikar surgikar

2017/4/25

#
In the world code i have this:
        firing_interval = firing_interval - 10;
        movement_speed = movement_speed + 1;
        Aliens.displacement = Aliens.displacement + 5;
and this
 public void act()
    {
        actCounter++;
        if (actCounter > firing_interval) {
            List aliens = getObjects(Alien.class);
            List aliens2 = getObjects(Alien2.class);
            List aliens3 = getObjects(Alien3.class);
            if (!aliens.isEmpty()) {
                int number_of_aliens = aliens.size();
                int i = Greenfoot.getRandomNumber(number_of_aliens);
                Alien randomAlien = (Alien)aliens.get(i);
                randomAlien.shoot();
                Greenfoot.playSound("fastinvader1.wav");
            }
            if (!aliens2.isEmpty()) {
                int number_of_aliens2 = aliens2.size();
                int i = Greenfoot.getRandomNumber(number_of_aliens2);
                Alien2 randomAlien2 = (Alien2)aliens2.get(i);
                randomAlien2.shoot();
                Greenfoot.playSound("fastinvader2.wav");
            }
            if (!aliens3.isEmpty()) {
                int number_of_aliens3 = aliens3.size();
                int i = Greenfoot.getRandomNumber(number_of_aliens3);
                Alien3 randomAlien3 = (Alien3)aliens3.get(i);
                randomAlien3.shoot();
                Greenfoot.playSound("fastinvader3.wav");
            }
            actCounter = 0;
        }
       
    } 
and in the aliens code i have this
 public int moving_speed = Space.movement_speed;
    public static int displacement = 15;
    /**
     * Act - do whatever the Aliens wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        returnAtRightEdge();
        returnAtLeftEdge();

    }  

    public String hitTheRightEdge()
    {
        move(moving_speed);
        int x = getX();
        if(x == 1500)
        {
            return "right";
        } else {
            return null;
        }
    }

    public void returnAtRightEdge()
    {
        String edge = hitTheRightEdge();
        if(edge == "right")
        {
            moving_speed = moving_speed * -1;
            setLocation(getX(), getY()+displacement);
        }
    }

    public String hitTheLeftEdge()
    {
        int x = getX();
        if(x == 100)
        {
            return "left";
        } else {
            return null;
        }
    }

    public void returnAtLeftEdge()
    {
        String edge = hitTheLeftEdge();
        if(edge == "left")
        {
            moving_speed = moving_speed * -  1;
            setLocation(getX(), getY()+displacement);
        }
    }
surgikar surgikar

2017/4/25

#
I'm new to coding so forgive me if its bad :/
Super_Hippo Super_Hippo

2017/4/25

#
Where exactly do you have this?
firing_interval = firing_interval - 10;
movement_speed = movement_speed + 1;
Aliens.displacement = Aliens.displacement + 5;
To make the enemy's speed increase over time, have an int variable in your world acting as a timer. So increase it in the act method and whenever it reaches a certain value, increase the movement speed value of the enemies (=moving_speed, not movement_speed). (Make 'moving_speed' in Aliens static in order to make every Alien to share the same speed if you want. If not, you have to change the speed of every object.) The 'movement_speed' in Space would be the starting speed from a wave then. So it would be this: (both in world, removed the _ and assumed that the speed variable in Alien will be static)
//when starting a wave
movementSpeed++; //new wave, higher speed
Alien.movingSpeed = movementSpeed;
//add aliens
//to increase the Alien speed inside a wave

private int speedTimer = 0;

public void act()
{
    if (++speedTime == 200) //higher number = slower increase in speed
    {
        Alien.movingSpeed++;
    }
}
surgikar surgikar

2017/4/25

#
In answer to your question of: Where exactly do you have this? it's in the Space code
surgikar surgikar

2017/4/25

#
I just basically repeat the prepare code
public void prepare()
    {
        firing_interval = firing_interval - 10;
        movement_speed = movement_speed + 1;
        Aliens.displacement = Aliens.displacement + 5;
        for(int i=0; i<11; i++) {   
            addObject(new Alien(), 100+i*row_length/10,50);
            addObject(new Alien2(), 100+i*row_length/10,150);
            addObject(new Alien2(), 100+i*row_length/10,250);
            addObject(new Alien3(), 100+i*row_length/10,350);
        }
You need to login to post a reply.