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

2019/5/28

Space Invaders - How to make the aliens shoot ?

1
2
dionzeka99 dionzeka99

2019/5/30

#
danpost wrote...
It actually works, but I have 3 class of aliens and I want that when there is none of the aliens, then it ends completely, because it stops when I only kill one kind of class alien if you can explain me
danpost danpost

2019/5/30

#
You can combine the conditions:
if (getObjects(Alien.class).isEmpty() && getObjects(Alien2.class).isEmpty() && ...)
dionzeka99 dionzeka99

2019/6/1

#
danpost wrote...
Do you know how could I put a simple game over when the party ends ?
danpost danpost

2019/6/1

#
dionzeka99 wrote...
Do you know how could I put a simple game over when the party ends ?
You mean something like this:
Greenfoot.setWorld(new GameOver());
dionzeka99 dionzeka99

2019/6/2

#
danpost wrote...
dionzeka99 wrote...
Do you know how could I put a simple game over when the party ends ?
You mean something like this:
Greenfoot.setWorld(new GameOver());
Should I declare a variable for GameOver(); ? Cause my code is wrong if I only write this (I don't really know how to declare it)
danpost danpost

2019/6/2

#
dionzeka99 wrote...
Should I declare a variable for GameOver(); ? Cause my code is wrong if I only write this (I don't really know how to declare it)
Create a new subclass of World called GameOver.
dionzeka99 dionzeka99

2019/6/2

#
danpost wrote...
dionzeka99 wrote...
Should I declare a variable for GameOver(); ? Cause my code is wrong if I only write this (I don't really know how to declare it)
Create a new subclass of World called GameOver.
It worked thanks ;) In addition, do you have any idea of how I can code the shields like in the official space invaders ?
danpost danpost

2019/6/2

#
dionzeka99 wrote...
do you have any idea of how I can code the shields like in the official space invaders ?
I do not "know" exactly how they were coded. I can only suspect that they were made of multiple parts that each disappear when hit by a bullet. I also suspect they were placed into the world strategically so that they align one for one along the verticals at which the aliens stop. This would allow some overlapping while keeping control of which is removed when a bullet does hit. This would also allow bullets that travel the same path to eventually pass through a barrier that has been hit several times along said path.
dionzeka99 dionzeka99

2019/6/2

#
danpost wrote...
Okey I understand thank for your help ;) Can you tell me how I can use getIntersectingObject for more than one class ? For example, I want that when any of my alien hit my shooter, then I lose.
danpost danpost

2019/6/2

#
dionzeka99 wrote...
Can you tell me how I can use getIntersectingObject for more than one class ? For example, I want that when any of my alien hit my shooter, then I lose.
What is your Actor class structure for you alien classes? What codes do you have for your alien detection?
dionzeka99 dionzeka99

2019/6/2

#
danpost wrote...
Here's my Alien1 code (the same as for my other aliens
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Alien here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Alien extends Actor
{
  private int timer = 2+Greenfoot.getRandomNumber(500);
  public Alien()
  {
      setRotation(0);
  }
    
  /**
     * Act - do whatever the Alien wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       Actor bullet = getOneIntersectingObject(Bullet.class); //variable qui retourne la classe bullet
       if(bullet!= null) {
          World world = getWorld();
          MyWorld myWorld = (MyWorld)world;
          Counter counter = myWorld.getCounter();
          counter.addScore();
          getWorld().removeObject(this);
        }
       if (--timer==0){
         shoot();
         timer = 2+Greenfoot.getRandomNumber(500);
        }
        
       setLocation(getX()+1, getY());
       
    }    
    
  public void shoot()
   {
     getWorld().addObject(new AlienBullet(), getX(), getY());   
   }
}
danpost danpost

2019/6/2

#
danpost wrote...
What codes do you have for your alien detection?
(Ship or Player class?)
dionzeka99 dionzeka99

2019/6/2

#
danpost wrote...
danpost wrote...
What codes do you have for your alien detection?
(Ship or Player class?) Here's my Shooter code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    private int IntervalBetweenShots = Integer.MAX_VALUE; //Donne la valeur maximale de l'intervalle de temps entre les tirs.
    
    public Player()
    {
        setRotation(0);
    }
    
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
      boolean isLeftKeyDown = Greenfoot.isKeyDown("left");
        
      if(isLeftKeyDown) {
            int x = getX();
            int y = getY();
            x = x - 5;
            setLocation(x,y);
        }
        
      boolean isRightKeyDown = Greenfoot.isKeyDown("right");
        
      if(isRightKeyDown) {
            int x = getX();
            int y = getY();
            x = x + 5;
            setLocation(x,y);
        }
        
      boolean isSpaceKeyDown = Greenfoot.isKeyDown("space");
        
      if(isSpaceKeyDown){
            if(IntervalBetweenShots > 30){
            getWorld().addObject(new Bullet(), getX(), getY());
            IntervalBetweenShots = 0;
           }  
            IntervalBetweenShots++;
        }
        
      Actor bullet = getOneIntersectingObject(AlienBullet.class);
      if(bullet!= null) {
        World world = getWorld();
        GameOver gameover = new GameOver(); //GameOver est un type de variable (l'objet game over) et new game over crée un acteur game over à placer sur le jeu 
        getWorld().addObject(gameover, 450, 300);
        getWorld().removeObject(this);
        Greenfoot.stop();
        }
        

        
        
    }    
}
danpost danpost

2019/6/2

#
With the following method (in your Player class):
private boolean alienHitsShooter()
{
    if (getOneIntersectingObject(Alien.class) != null) return true;
    if (getOneIntersectingObject(Alien2.class) != null) return true;
    ...
    return false;
}
you can use:
if (alienHitsShooter())
dionzeka99 dionzeka99

2019/6/14

#
danpost Hello danpost how are you ? Can you tell me how could I create an horizontal and vertical movement of my aliens like in the true space invaders game if you could help me ? [/quote wrote...
You need to login to post a reply.
1
2