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/28

#
Hello everyone, I am making the famous Space Invaders game for a school project and I got a problem. Indeed, I am trying to make my alien shoot randomly but I don't know how to make it. Can someone help me please (If you need, I can send my code). Thanks and have a good day.
Super_Hippo Super_Hippo

2019/5/28

#
What do you mean with shoot randomly? With random time between shots and/or in random directions and/or random ammunition?
danpost danpost

2019/5/28

#
Using a timer field with a random value:
private int timer = 60+Greenfoot.getRandomNumber(180);;
the following will cause a random shot:
if (--timer == 0)
{
    shoot();
    timer = 60+Greenfoot.getRandomNumber(180);
}
You can play with the values to suit your needs (the 60 was for a minimum delay between shots).
dionzeka99 dionzeka99

2019/5/29

#
Super_Hippo wrote...
What do you mean with shoot randomly? With random time between shots and/or in random directions and/or random ammunition?
I mean with randon time between shots
dionzeka99 dionzeka99

2019/5/29

#
danpost wrote...
Using a timer field with a random value:
private int timer = 60+Greenfoot.getRandomNumber(180);;
the following will cause a random shot:
if (--timer == 0)
{
    shoot();
    timer = 60+Greenfoot.getRandomNumber(180);
}
You can play with the values to suit your needs (the 60 was for a minimum delay between shots).
Thank you men It may help ;)
dionzeka99 dionzeka99

2019/5/29

#
danpost wrote...
Hello Danpost, I tried your code, but when my alien shoot, the bullet doesn't go down Here's my code for my alien ;
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 = 60+Greenfoot.getRandomNumber(180);
  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(AlienBullet.class); //variable qui retourne la classe bullet
    
       if (--timer==0){
         shoot();
         timer = 30+Greenfoot.getRandomNumber(180);
        }
    }    
    
  public void shoot()
   {
     getWorld().addObject(new AlienBullet(), getX(), getY());   
   }
}
danpost danpost

2019/5/29

#
dionzeka99 wrote...
Hello Danpost, I tried your code, but when my alien shoot, the bullet doesn't go down Here's my code for my alien ; << Code Omitted
What code are you using in your AlienBullet class?
dionzeka99 dionzeka99

2019/5/29

#
danpost wrote...
Here's my code for my AlienBullet ;
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class AlienBullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class AlienBullet extends AllBullets
{
    private static final int DEFAULT_DIRECTION = 0;
    private int direction;
    private int timer = 60+Greenfoot.getRandomNumber(180);
    public AlienBullet()
    {
        this(DEFAULT_DIRECTION);
    }   
    
    public AlienBullet(int direction)
    {
        setRotation(direction);
    }   

     public void shoot()
   {
     getWorld().addObject(new AlienBullet(), getX(), getY());   
   }
   
   public void act()
   {
     if (--timer==0){
       shoot();
       timer = 30+Greenfoot.getRandomNumber(180);
      }   
   
    
   }
}
I tried to do something with what you told me before ^^
Super_Hippo Super_Hippo

2019/5/29

#
The bullet isn't shooting anything. It should simply move.
danpost danpost

2019/5/29

#
Super_Hippo wrote...
The bullet isn't shooting anything. It should simply move.
Yes. The bullets should not shoot more bullets. That is the behavior of the Alien objects (to shoot bullets). And you have not added any movement code to the AlienBullet class.
dionzeka99 dionzeka99

2019/5/29

#
danpost wrote...
Super_Hippo wrote...
Can you help me guys ? Cause I don't really know how to do it if you can. If not, no problem I understand ;))
danpost danpost

2019/5/29

#
I suggest you remove line 13 and lines 24 thru 28; then replace lines 31 thru 33 with a movement command (in AlienBullet class)
dionzeka99 dionzeka99

2019/5/29

#
danpost wrote...
I found a way to make my aliens shoot randomly thank you men ;)
dionzeka99 dionzeka99

2019/5/29

#
danpost wrote...
By the way, do you have any idea of how I could end a game ? For example, I would like that when my aliens are all touched by my bullet, then the game ends
danpost danpost

2019/5/29

#
dionzeka99 wrote...
By the way, do you have any idea of how I could end a game ? For example, I would like that when my aliens are all touched by my bullet, then the game ends
If bullet-touched aliens are removed from the world, then you can use the following starter line in your World subclass act method:
if (getObjects(Alien.class).isEmpty()) {
There are more replies on the next page.
1
2