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

2018/1/7

i need help with atleast one of these 3 things

nick_crompton nick_crompton

2018/1/7

#
i want three things, the first being that i want trump to shoot left when i press left on my keyboard and then space to shoot and same thing for shooting to the right. second i want the score to go up 1 with every mexican i hit but it wont work for some reason, even though i did everything exacly as the youtubers do and their game works. the third is the gameover screen, i want it to appear when trump is either dead or i have reached score=20, i only need help with trump dying and the game stopping but my actor has multiple lives so im stuck. if anyone can help with atleast one of these i would be greatful
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Trump here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Trump extends Actor
{int vSpeed;
    int acceleration=1;
    private int health = 3;
    Rocket rocket = new Rocket();
    private boolean spaceDown;
    
    
    /**
     * Act - do whatever the Trump wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
         bestuur();
        checkFall();
      spring();
     fire();
      Shot();
 
    }
    public void Shot()
{
    
    Actor Kogel = getOneObjectAtOffset(0, 0, Kogel.class);
   
    if(Kogel != null)
    {
        getWorld().removeObject(Kogel);
        health--;
        if (health == 0) getWorld().removeObject(this);
       
        return;
}
Actor Bom = getOneObjectAtOffset(0, 0, Bom.class);

     if (Bom != null)
    {
        getWorld().removeObject(Bom);
        health--;
        if (health == 0) getWorld().removeObject(this);
        return;
}
}


    public void spring()
    {
    if(Greenfoot.isKeyDown("up") && onGround())
        {
            vSpeed=-15;
            setLocation (getX(), getY() + vSpeed);
        }
          Object above = getOneObjectAtOffset(0, -getImage().getHeight()/2 - 2, Steen.class);
          
        if (Greenfoot.isKeyDown("space") && above!=null) // Deze code zorgt ervoor dat je niet van onder door de bricks kan.
        {
            vSpeed=0;
        }
    }
    
    
    public boolean onGround()
    {
        Object under = getOneObjectAtOffset(0, getImage().getHeight()/2 + 2, Steen.class);
        return under != null;
    }

    public void checkFall()
    {
        if (onGround()) {
            vSpeed = 0;
        }
        else {
            fall();
        }
    }

    public void fall()
    {
        setLocation (getX(), getY() + vSpeed);
        if(vSpeed<10)
        {
            vSpeed += acceleration;
        }
    }

    public void bestuur()

    {
        if (Greenfoot.isKeyDown("left"))
        {
            move(-3);
            
        }
        if (Greenfoot.isKeyDown("right"))
        {
            move(3);
        }
    }
 
      public void fire()
    {

  if (!spaceDown && Greenfoot.isKeyDown("space"))
        {
         
            for (int i = 0; i<2;i++)
            {
                   Rocket rocket = new Rocket();
                 getWorld().addObject(rocket,getX(), getY());
               if(Greenfoot.isKeyDown("right" )){  
                  rocket.setRotation(0*i);
                 rocket.move(10);
            }
           
               else if(Greenfoot.isKeyDown("left" )){
                rocket.setRotation(180*i);
                rocket.move(-10); 
            }
            
            } 
            spaceDown=true;
            }
if (spaceDown && !Greenfoot.isKeyDown("space"))
{
    spaceDown = false;
}
}


    }
    

   
    
    

danpost danpost

2018/1/7

#
The condition on line 65 looks quite questionable as the "space" key is used for firing a shot which has nothing to do with springing. The 'for' loop starting at line 117 looks highly suspicious. I do not think you really want to create two rockets at a time. You just want to create one and have it go a specific (left or right) direction. Apparently, you only want to be able to have Trump shoot when he is moving (because "left" and "right" control his movement). The condition on line 114 can be have the additional constraint that only the "left" or "right" key is being pressed:
Greenfoot.isKeyDown("left") != Greenfoot.isKeyDown("right")
Then, without the loop, you can create one Rocket object and, depending on which one is determined down (adding an 'if' clause inside the block), apply the proper rotation it. You can remove line 13.
nick_crompton nick_crompton

2018/1/7

#
thanks it works, if you can look into the other issues, i would appreciate it :)
danpost danpost

2018/1/7

#
nick_crompton wrote...
thanks it works, if you can look into the other issues, i would appreciate it :)
The scoring would probably be in the Rocket classs, which is not provided. The game over would be in your World subclass, which is not provided. (tthe World subclass would probably also be needed for the scoring as well)
nick_crompton nick_crompton

2018/1/7

#
i want the score to go up when i hit a mexican.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Rocket here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Rocket extends Actor
{
    /**
     * Act - do whatever the Rocket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
        atWorldEdge();
                remove();
    }    
    public void move()
    {
        move(10);
       
    }
      public boolean atWorldEdge()
{
 if (getY() <= 5 || getY() >= getWorld() . getHeight() -5) 
        return true;
      if(getX() > getWorld().getWidth()-getImage().getWidth()) {
        return true;
    }
    // check for left edge
    if(getX() < getImage().getWidth()) {
        return true;
    }

    else {
        return false;
    }   
        
}// bron https://www.greenfoot.org/topics/1124
public void remove()
{
Actor bom = getOneIntersectingObject(Bom.class);
Actor isis = getOneIntersectingObject(Isis.class);
Actor noordkorea = getOneIntersectingObject(NoordKorea.class);
Actor kogel = getOneIntersectingObject(Kogel.class);
if(bom !=null)
{
World myWorld=getWorld();
myWorld.removeObject(bom);
myWorld.removeObject(this);

}

 else if(isis !=null)
{
World myWorld=getWorld();

myWorld.removeObject(this);

}

 else if(kogel !=null)
{
World myWorld=getWorld();
myWorld.removeObject(kogel);
myWorld.removeObject(this);

}

 else if(noordkorea !=null)
{
World myWorld=getWorld();

myWorld.removeObject(this);

}
else if(atWorldEdge())
{
    getWorld().removeObject(this);  
}

  

}

}
^^rocket class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Border here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Border extends World
{
    Counter counter = new Counter();
public int getRandomNumber(int start,int end)
{
       int normal = Greenfoot.getRandomNumber(end-start+1);
       return normal+start;
}// bron https://www.greenfoot.org/topics/1986
    /**
     * Constructor for objects of class Border.
     * 
     */
    public Border()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
       
    }
  
    
    public void act()
    {
    add();
    }
     public Counter getCounter()
    {
    return counter;
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    public void add()
    {
        
     if (Greenfoot.getRandomNumber(70)<1){
           addObject(new Mexicaan(), getRandomNumber(150,500), 10);
       }
    }// bron https://www.greenfoot.org/topics/1986

    private void prepare()
    {
        Steen steen = new Steen();
        addObject(steen,23,397);
        Steen steen2 = new Steen();
        addObject(steen2,17,399);
        Steen steen3 = new Steen();
        addObject(steen3,18,399);
        Steen steen4 = new Steen();
        addObject(steen4,56,398);
        Steen steen5 = new Steen();
        addObject(steen5,50,398);
        Steen steen6 = new Steen();
        addObject(steen6,89,399);
        Steen steen7 = new Steen();
        addObject(steen7,81,398);
        Steen steen8 = new Steen();
        addObject(steen8,120,398);
        Steen steen9 = new Steen();
        addObject(steen9,114,398);
        Steen steen10 = new Steen();
        addObject(steen10,150,398);
        Steen steen11 = new Steen();
        addObject(steen11,173,398);
        Steen steen12 = new Steen();
        addObject(steen12,200,398);
        Steen steen13 = new Steen();
        addObject(steen13,231,398);
        Steen steen14 = new Steen();
        addObject(steen14,222,399);
        Steen steen15 = new Steen();
        addObject(steen15,253,399);
        Steen steen16 = new Steen();
        Steen steen17 = new Steen();
        addObject(steen17,285,399);
        Steen steen18 = new Steen();
        addObject(steen18,320,399);
        Steen steen19 = new Steen();
        addObject(steen19,363,398);
        Steen steen20 = new Steen();
        addObject(steen20,398,396);
        Steen steen21 = new Steen();
        addObject(steen21,444,397);
        removeObject(steen20);
        Steen steen22 = new Steen();
        addObject(steen22,345,399);
        Steen steen23 = new Steen();
        addObject(steen23,310,398);
        Steen steen24 = new Steen();
        addObject(steen24,277,398);
        Steen steen25 = new Steen();
        addObject(steen25,394,399);
        Steen steen26 = new Steen();
        addObject(steen26,387,398);
        Steen steen27 = new Steen();
        addObject(steen27,425,398);
        Steen steen28 = new Steen();
        addObject(steen28,419,398);
        Steen steen29 = new Steen();
        addObject(steen29,475,398);
        Steen steen30 = new Steen();
        addObject(steen30,469,397);
        Steen steen31 = new Steen();
        addObject(steen31,504,398);
        Steen steen32 = new Steen();
        addObject(steen32,528,396);
        Steen steen33 = new Steen();
        addObject(steen33,561,397);
        Steen steen34 = new Steen();
        addObject(steen34,557,398);
        Steen steen35 = new Steen();
        addObject(steen35,594,396);
        Steen steen36 = new Steen();
        addObject(steen36,583,398);
        Steen steen37 = new Steen();
        Steen steen38 = new Steen();
        Steen steen39 = new Steen();
        addObject(steen39,12,394);
        Trump trump = new Trump();
        addObject(trump,311,337);
        Isis isis = new Isis();
        addObject(isis,587,353);
        NoordKorea noordkorea = new NoordKorea();
        addObject(noordkorea,31,350);
        noordkorea.setLocation(14,349);

       
        Counter counter = new Counter();
        addObject(counter,60,29);
    }
}
^^world
danpost danpost

2018/1/7

#
Remove line 138 in the Border class (you already created a counter at line 11). Also 'counter' in line 138 is a different variable than 'counter' in line 11 as the one on line 138 is a local variable while the one at 11 is an instance field. You can access the counter from the rocket class with:
Counter counter = ((Border)getWorld()).getCounter();
This can be done when scoring and before the rocket is removed from the world.
danpost danpost

2018/1/7

#
In Border class act method, you can ask if a Trump object is in the world and stop the game there.
nick_crompton nick_crompton

2018/1/7

#
yes, i have been looking for that code but everything i try is not working,
 List objectslookingfor = getObjects(ObjectLookingFor.class);
i got this from one of your other threads but greenfoot gives an error at "list" and "ObjectLookingFor"
nick_crompton nick_crompton

2018/1/7

#
and i think you are misunderstanding my principle with this game, the rocket is used to defend trump, not score points. the points are gathered when a mexican is touched by trump.
nick_crompton nick_crompton

2018/1/7

#
no need to look into the score i already fixed it, i switched up 2 codes from different classes + the mistake you noticed
You need to login to post a reply.