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

2015/4/14

How to fix my game? Help!

unicorn16 unicorn16

2015/4/14

#
Hey guys :) I'm a beginner when it comes to Greenfoot and I really need help with my game. I have to make one for school and it's due to wednesday :/ So here is my problem: The actor (boy) needs to collect lifepoints while not getting caught by the randomly moving rocks. A collision with those leads to him being 'eaten up', so he dissapears and a game over screen appears. However if he gets to collect all lifepoints without getting hit, he wins and gets to enter the second level. I managed to program the boys movements and the randomly moving rocks, however how do I program that the boy dissapears and the game over screen? And how can I make him move from level to the other?
Code for the boy:
public class boy extends Mover
{
    /**
     * Act - do whatever the boy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() {
      
        if (Greenfoot.isKeyDown("left") )
        {
            moveLeft();
        }
        if (Greenfoot.isKeyDown("right") )
        {
            moveRight();
        }
        if (Greenfoot.isKeyDown("up") )
        {
            moveUp();
        }
        if (Greenfoot.isKeyDown("down") )
        {
            moveDown();
        }
    }
       
    }   
 
 
 
Code for the rocks:
public class rock extends Mover
{
    public void act()
    {
        move(4);
        if(Greenfoot.getRandomNumber(100) <10)
        {
            turn(Greenfoot.getRandomNumber(40)-20 );
        }
        if (atWorldEdge())
        {
           turn(12);
        }
    }
    public boolean atWorldEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
        }    
}
 
 
Code for the world:
public class world1 extends World
{
 
    /**
     * Constructor for objects of class world1.
     *
     */
    public world1()
    {   
       
        super(750, 450, 1);
        addObjects();
    }
    public void addObjects(){
        addObject(new rock(), 1, 50);
        addObject(new rock(), 152, 282);
        addObject(new rock(), 75 , 110 );
        addObject(new rock(), 176, 59);
        addObject(new rock(), 311, 170);
        addObject(new rock(), 403, 44);
        addObject(new rock(), 495, 230);
        addObject(new rock(), 496, 362);
        addObject(new rock(), 668, 295);
        addObject(new rock(), 572, 127);
        addObject(new boy(), 34, 406);
        addObject(new lifepoints(), 245, 315);
        addObject(new lifepoints(), 603, 369);
        addObject(new lifepoints(), 682, 44);
        addObject(new lifepoints(), 88, 22);
    }
}
Please help me, it's very urgent! Thank you all already :)
davmac davmac

2015/4/14

#
  • Read through the Greenfoot API documentation and see if you can identify which methods you will need to use. Try putting appropriate calls to these methods in to your code. If you don't understand how to use the method, post here in the discussion forum with a specific question.
  • Read the other documentation, in particular the tutorials. The third tutorial seems to cover a large part of what you want.
  • If you are still stuck (getting errors, it's not working, or there's something specific that you still don't know how to do) then post here in the discussion forum.
  • Never ask for help in such a vague way. Show that you have made some effort. Pinpoint the particular issues that you are having trouble with. Otherwise, people tend to get annoyed.
unicorn16 unicorn16

2015/4/14

#
I wanted to add this
 
public boolean isTouchingboy() {
    Actor aboy = getOneIntersectingObject( boy.class );  
    if (boy.class == null) {  
       return false;  
    } else {  
        return true;  
    }  
to my actor (boy) but it won't work out. After compiling my 'flying rocks' wouldn't move anymore. Besides that I mentioned that the boy has to collect a certain number of lifepoints (4 in the first level) to move on to the next game. I browsed and found this code:
double dx, dy;    
 
public void grab() {
    lifepoints a = (lifepoints)getOneIntersectingObject(lifepoints.class);
 
    if(a.getX() > getX()) dx = (Math.abs(getX() - a.getX())/5);
    else dx = ((Math.abs(a.getX() - getX())*-1)/5);
    if(a.getY() > getY()) dy = (Math.abs(getY() - a.getY())/5);
    else dy = ((a.getY() - getY())/5);       
 
    setLocation(getX() + (int)dx, getY() + (int)dy);
}
I have to add this in the boy class, right? But after doing that and compiling my actor would simply move over the lifepoints with picking them up. I tried looking for other codes on mutiple sides, but others either had a syntax fault (that I couldn't fix without deleting it completly) or wouldn't work alltogether. I really hope someone can help me out :) Thanks!
davmac davmac

2015/4/14

#
It's too confusing to answer two complex questions at once, so I'll just deal with the first issue. If I were describe to you in words what your line 2 does:
    Actor aboy = getOneIntersectingObject( boy.class );
It would be: "see if there is an intersecting instance of boy, and if there is, store a reference to it in a new variable 'aboy' (of type Actor)". The next line:
    if (boy.class == null) { 
"if the boy class (the class itself) is not null ..." There is no connect between the two lines of code. You are checking if a class is null, but a class can never be null. You should instead be checking whether getOneIntersectingObject(...) actually returned an intersecting instance:
    if (aboy != null) { 
See how this line is connected with the previous line?
davmac davmac

2015/4/14

#
Regarding the second:
I have to add this in the boy class, right? But after doing that and compiling my actor would simply move over the lifepoints with picking them up.
Can you say why you used this code? Do you want the player to move over the lifepoints? - if not, why are you calling setLocation(...)? It doesn't remove the lifepoints because it doesn't call any method which would remove an object from the world. Remember what I said earlier:
davmac wrote...
Read through the Greenfoot API documentation and see if you can identify which methods you will need to use. Try putting appropriate calls to these methods in to your code. If you don't understand how to use the method, post here in the discussion forum with a specific question.
unicorn16 unicorn16

2015/4/14

#
Thank you for your quick replies! I changed my boy code (regarding the first issue), however the boy still doesn't get removed from the world once he comes in contact with a rock. I looked through the API again and wondered if by adding the following code it might work out?
public void act() {
    boy = getOneIntersectingObject(boy.class);
    if (boy != null) {
        getWorld().removeObject(boy);
    }
}
Where/How should I add that?
danpost danpost

2015/4/14

#
I can see that the 'boy' object is involved, here. When did you say you wanted to remove the 'boy' object? What is supposed to be "intersecting" the boy? Where do you think the code should go?
unicorn16 unicorn16

2015/4/20

#
Thank you both for taking the time to leave a comment. However I already cleared my problem on a much more helpful forum (not to say that this one isn't!), so it doesn't matter anymore. But yeah anyway, have a nice day.
You need to login to post a reply.