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

2016/12/14

Collision

1
2
3
Nosson1459 Nosson1459

2016/12/14

#
What is the best, smallest, and most accurate (<-- most important) way of detecting collision for the Pengu class? (Please give some example code that could work here.) (The collision has to detect everything and in every way that it is below, just shorter and (more) accurate.) This is the Mover class which Pengu extends from:
import greenfoot.*;

/**
 * The class Mover provides some basic movement methods. Use this as a superclass
 * for other actors that should be able to move left and right, jump up and fall 
 * down.
 */
public abstract class Mover extends Actor
{
    private static final int acceleration = 2;      // down (gravity)
    private static final int speed = 7;             // running speed (sideways)
    public int vSpeed = 0;                         // current vertical speed
    private boolean hasHit=false;
    public void moveRight()
    {
        setLocation ( getX() + speed, getY() );
    }

    public void moveLeft()
    {
        setLocation ( getX() - speed, getY() );
    }

    public boolean onTop()
    {
        return onTop1B()||onTop2B()||onTop3B();
    }

    public boolean onTop2()
    {
        return onTop1Bc()||onTop2Bc()||onTop3Bc();
    }

    private boolean onTop1B()
    {
        Box box = (Box)getOneObjectAtOffset(-getImage().getWidth()/2+8, -getImage().getHeight()/2 , Box.class);
        return box != null&&vSpeed<0;
    }

    private boolean onTop2B()
    {
        Box box = (Box)getOneObjectAtOffset(0, -getImage().getHeight()/2 , Box.class);
        return box != null&&vSpeed<0;
    }

    private boolean onTop3B()
    {
        Box box = (Box)getOneObjectAtOffset(getImage().getWidth()/2-8, -getImage().getHeight()/2 , Box.class);
        return box != null&&vSpeed<0;
    }

    private boolean onTop1Bc()
    {
        Block block=(Block) getOneObjectAtOffset(-getImage().getWidth()/2+8, -getImage().getHeight()/2 , Block.class);
        return block != null&&block.isThere==true;
    }

    private boolean onTop2Bc()
    {
        Block block=(Block)  getOneObjectAtOffset(0, -getImage().getHeight()/2 , Block.class);
        return block != null&&block.isThere==true;
    }

    private boolean onTop3Bc()
    {
        Block block=(Block) getOneObjectAtOffset(getImage().getWidth()/2-8, -getImage().getHeight()/2 , Block.class);
        return block != null&&block.isThere==true;
    }

    private boolean onGround1B()
    {
        Box box = (Box)getOneObjectAtOffset(0, getImage().getHeight()/2 , Box.class);
        return box != null&&box.isThere==true;
    }

    private boolean onGround2B()
    {
        Box box = (Box)getOneObjectAtOffset(getImage().getWidth()/2-10, getImage().getHeight()/2 , Box.class);
        return box != null&&box.isThere==true;
    }

    private boolean onGround3B()
    {
        Box box  = (Box)getOneObjectAtOffset(-getImage().getWidth()/2+10, getImage().getHeight()/2 , Box.class);
        return box != null&&box.isThere==true;
    }

    private boolean onGroundB()
    {
        return onGround1B()||onGround2B()||onGround3B();
    }

    private boolean onGround1Bc()
    {
        Block block =(Block) getOneObjectAtOffset(0, getImage().getHeight()/2 , Block.class);
        return block != null&&block.isThere==true;
    }

    private boolean onGround2Bc()
    {
        Block block =(Block) getOneObjectAtOffset(getImage().getWidth()/2-10, getImage().getHeight()/2 , Block.class);
        return block != null&&block.isThere==true;
    }

    private boolean onGround3Bc()
    {
        Block block =(Block) getOneObjectAtOffset(-getImage().getWidth()/2+10, getImage().getHeight()/2 , Block.class);
        return block != null&&block.isThere==true;
    }

    private boolean onGroundBc()
    {
        return onGround1Bc()||onGround2Bc()||onGround3Bc();
    }

    private boolean onGround1CA()
    {
        Object under = getOneObjectAtOffset(0, getImage().getHeight()/2 , Cliff.class);
        return under != null;
    }

    private boolean onGround2CA()
    {
        Object under = getOneObjectAtOffset(getImage().getWidth()/2-10, getImage().getHeight()/2 , Cliff.class);
        return under != null;
    }

    private boolean onGround3CA()
    {
        Object under = getOneObjectAtOffset(-getImage().getWidth()/2+10, getImage().getHeight()/2 , Cliff.class);
        return under != null;
    }

    private boolean onGroundCA()
    {
        return onGround1CA()||onGround2CA()||onGround3CA();
    }

    private boolean onGround1P()
    {
        Object under = getOneObjectAtOffset(0, getImage().getHeight()/2 , Platform.class);
        return under != null;
    }

    private boolean onGround2P()
    {
        Object under = getOneObjectAtOffset(getImage().getWidth()/2-10, getImage().getHeight()/2 , Platform.class);
        return under != null;
    }

    private boolean onGround3P()
    {
        Object under = getOneObjectAtOffset(-getImage().getWidth()/2+10, getImage().getHeight()/2 , Platform.class);
        return under != null;
    }

    private boolean onGroundP()
    {
        return onGround1P()||onGround2P()||onGround3P();
    }

    public boolean onGround()
    {
        return onGroundBc()||onGroundB()||onGroundCA()||onGroundP();
    }

    private boolean touchingLeftSide1B()
    {
        Box box=(Box)getOneObjectAtOffset(-getImage().getWidth()/2,getImage().getHeight()/2-10,Box.class);
        return box!=null&&box.isThere==true;
    }

    private boolean touchingLeftSide2B()
    {
        Box box=(Box)getOneObjectAtOffset(-getImage().getWidth()/2,-getImage().getHeight()/2+1,Box.class);
        return box!=null&&box.isThere==true;
    }

    private boolean touchingLeftSide3B()
    {
        Box box=(Box)getOneObjectAtOffset(-getImage().getWidth()/2,0,Box.class);
        return box!=null&&box.isThere==true;
    }

    private boolean touchingLeftSideB()
    {
        return touchingLeftSide1B()||touchingLeftSide2B()||touchingLeftSide3B();
    }

    private boolean touchingLeftSide1Bc()
    {
        Block block=(Block)getOneObjectAtOffset(-getImage().getWidth()/2,getImage().getHeight()/2-10,Block.class);
        return block != null&&block.isThere==true;
    }

    private boolean touchingLeftSide2Bc()
    {
        Block block=(Block)getOneObjectAtOffset(-getImage().getWidth()/2,-getImage().getHeight()/2+1,Block.class);
        return block != null&&block.isThere==true;
    }

    private boolean touchingLeftSide3Bc()
    {
        Block block=(Block)getOneObjectAtOffset(-getImage().getWidth()/2,0,Block.class);
        return block != null&&block.isThere==true;
    }

    private boolean touchingLeftSideBc()
    {
        return touchingLeftSide1Bc()||touchingLeftSide2Bc()||touchingLeftSide3Bc();
    }

    private boolean touchingLeftSide1CA()
    {
        Object side=getOneObjectAtOffset(-getImage().getWidth()/2,getImage().getHeight()/2-10,Cliff.class);
        return side!=null;
    }

    private boolean touchingLeftSide2CA()
    {
        Object side=getOneObjectAtOffset(-getImage().getWidth()/2,-getImage().getHeight()/2+1,Cliff.class);
        return side!=null;
    }

    private boolean touchingLeftSide3CA()
    {
        Object side=getOneObjectAtOffset(-getImage().getWidth()/2,0,Cliff.class);
        return side!=null;
    }

    private boolean touchingLeftSideCA()
    {
        return touchingLeftSide1CA()||touchingLeftSide2CA()||touchingLeftSide3CA();
    }

    public boolean touchingLeftSide()
    {
        return touchingLeftSideB()||touchingLeftSideCA()||touchingLeftSideBc();
    }

    private boolean touchingRightSide1B()
    {
        Box box=(Box)getOneObjectAtOffset(getImage().getWidth()/2,-getImage().getHeight()/2+1,Box.class);
        return box!=null&&box.isThere==true;
    }

    private boolean touchingRightSide2B()
    {
        Box box=(Box)getOneObjectAtOffset(getImage().getWidth()/2,getImage().getHeight()/2-10,Box.class);
        return box!=null&&box.isThere==true;
    }

    private boolean touchingRightSide3B()
    {
        Box box=(Box)getOneObjectAtOffset(getImage().getWidth()/2,0,Box.class);
        return box!=null&&box.isThere==true;
    }

    public boolean touchingRightSideB()
    {
        return touchingRightSide1B()||touchingRightSide2B()||touchingRightSide3B(); 
    }

    private boolean touchingRightSide1Bc()
    {
        Block block=(Block)getOneObjectAtOffset(getImage().getWidth()/2,-getImage().getHeight()/2+1,Block.class);
        return block != null&&block.isThere==true;
    }

    private boolean touchingRightSide2Bc()
    {
        Block block=(Block)getOneObjectAtOffset(getImage().getWidth()/2,getImage().getHeight()/2-10,Block.class);
        return block != null&&block.isThere==true;
    }

    private boolean touchingRightSide3Bc()
    {
        Block block=(Block)getOneObjectAtOffset(getImage().getWidth()/2,0,Block.class);
        return block != null&&block.isThere==true;
    }

    public boolean touchingRightSideBc()
    {
        return touchingRightSide1Bc()||touchingRightSide2Bc()||touchingRightSide3Bc(); 
    }

    private boolean touchingRightSide1CA()
    {
        Object side=getOneObjectAtOffset(getImage().getWidth()/2,-getImage().getHeight()/2+1,Cliff.class);
        return side!=null;
    }

    private boolean touchingRightSide2CA()
    {
        Object side=getOneObjectAtOffset(getImage().getWidth()/2,getImage().getHeight()/2-20,Cliff.class);
        return side!=null;
    }

    private boolean touchingRightSide3CA()
    {
        Object side=getOneObjectAtOffset(getImage().getWidth()/2,0,Cliff.class);
        return side!=null;
    }

    public boolean touchingRightSideCA()
    {
        return touchingRightSide1CA()||touchingRightSide2CA()||touchingRightSide3CA(); 
    }

    public boolean touchingRightSide()
    {
        return touchingRightSideB()||touchingRightSideCA()||touchingRightSideBc(); 
    }

    public void setVSpeed(int speed)
    {
        vSpeed = speed;
    }

    public void fall()
    {
        bump();
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
        if ( atBottom() )
            gameEnd();
    }

    private boolean atBottom()
    {
        return getY() >= getWorld().getHeight() - 2;
    }

    public void gameEnd()
    {
        Scene scene=(Scene)getWorld();
        scene.lives--;
        scene.reset(true);
        scene.levelUp();
    }

    public void bump()
    {
        if (onTop()||onTop2())
        {
            if(!hasHit)
            {
                vSpeed=-vSpeed;
            }
            hasHit=true;
        }
        if(onGround())
        {
            hasHit=false;
        }
    }
}
This is the Pengu class (colliding actor):
import greenfoot.*;

/**
 * A little penguin that wants to get to the other side.
 */
public class Pengu extends Mover
{
    private static final int jumpStrength = 23;
    private int timer=0;
    private int delay=0;
    private static final int gunReload=5;
    private GreenfootImage right=new GreenfootImage("Pengu/pengu-right2.png");
    private GreenfootImage left=new GreenfootImage("Pengu/pengu-left2.png");
    private GreenfootImage rightShooting=new GreenfootImage("Pengu/pengu-right2Shooting.png");
    private GreenfootImage leftShooting=new GreenfootImage("Pengu/pengu-left2Shooting.png");
    public Pengu()
    {
        setImage(right);
    }

    public void act() 
    {
        Scene scene=(Scene)getWorld();
        if(!scene.pause)
        {
            if(scene.shooting==true)delay++;
            shoot();
            shooting();
            coins();
            checkKeys();       
            checkFall();
        }
    }

    private void checkKeys()
    {
        Scene scene=(Scene)getWorld();
        if (Greenfoot.isKeyDown("left")&&getX()>=20 )
        {
            scene.direction=false; 
            if(!touchingLeftSide())
                moveLeft();
        }
        if (Greenfoot.isKeyDown("left"))
        {
            if(!scene.shooting)
            {
                setImage(left);
            }
            else if(scene.shooting=true)
            {
                setImage(leftShooting);
            }
        }
        if (Greenfoot.isKeyDown("right")&&scene.move==false)
        {
            scene.direction=true;
            if(!touchingRightSide())
                moveRight();
        }
        if (Greenfoot.isKeyDown("right"))
        {
            if(!scene.shooting)
            {
                setImage(right);
            }
            else if(scene.shooting=true)
            {
                setImage(rightShooting);
            }
        }
        if (Greenfoot.isKeyDown("up") )
        {
            if (onGround())
                jump();
        }
    }    

    private void jump()
    {
        Scene scene=(Scene)getWorld();
        setVSpeed(-jumpStrength);
        fall();
    }

    private void coins()
    {
        Scene scene=(Scene)getWorld();
        Coin coin=(Coin)getOneIntersectingObject(Coin.class);
        if(coin!=null)
        {
            scene.removeObject(coin);
            scene.score(100);
            scene.coins=scene.coins+1;
        }
    }

    private void shoot()
    {
        Scene scene=(Scene)getWorld();
        if(Greenfoot.isKeyDown("space")&&delay>=gunReload&&scene.shooting==true)
        {
            if(scene.direction==false)
            {
                scene.addObject(new Bullet(-8),getX()-26,getY());
            }
            if(scene.direction==true)
            {
                scene.addObject(new Bullet(8),getX()+26,getY());
            }
            delay=0;
        }
    }

    private void shooting()
    {
        Scene scene=(Scene)getWorld();
        if(isTouching(Shooting.class))
        {
            scene.shooting=true;
            scene.score(250);
            removeTouching(Shooting.class);
        }
        if(scene.shooting)
        {
            if(scene.direction==false)
            {
                setImage(leftShooting);
            }
            if(scene.direction==true)
            {
                setImage(rightShooting);
            }
        }
        else if(!scene.shooting)
        {
            if(scene.direction==false)
            {
                setImage(left);
            }
            if(scene.direction==true)
            {
                setImage(right);
            }
        }
    }

    public void checkFall()
    {
        if (onGround()) {
            setVSpeed(0);
            Actor under = getOneObjectAtOffset(0, getImage().getHeight()/2 , null);
            if(!checkDoor())
            {
                if(under!=null)
                {
                    int h=under.getImage().getHeight()/2+getImage().getHeight()/2-5;
                    int y=under.getY();
                    setLocation(getX(),y-h);
                }
            }
        }
        else {
            fall();
        }
    }

    private boolean checkDoor()
    {
        return isTouching(Door.class)||isTouching(DoorPost.class);
    }
}
This is the Box class (collision object, has a variable in it (boolean isThere) that needs to be checked for collision):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Box here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Box extends Boxes
{
    private GreenfootImage image1=new GreenfootImage("mystery.png");
    private GreenfootImage image2=new GreenfootImage("MysteryG.png");
    private GreenfootImage image3=new GreenfootImage("Block.png");
    private GreenfootImage image4=new GreenfootImage("transparentBox.png");
    private GreenfootImage image5=new GreenfootImage("Bricks2.png");
    private int timer=0;
    private boolean hit=false;
    private int counter=0;
    private boolean empty=false;
    private boolean coin=false;
    private boolean shooting=false;
    private boolean invisible=false;
    private boolean extraLife=false;
    private boolean brick=false;
    public boolean isThere=true;
    public Box(int type)
    {
        if(type==1)
        {
            invisible=false;
            coin=true;
            shooting=false;
            extraLife=false;
        }
        if(type==2)
        {
            invisible=false;
            shooting=true;
            coin=false;
            extraLife=false;
        }
        if(type==3)
        {
            invisible=false;
            shooting=false;
            coin=false;
            extraLife=true;
        }
        if(type==4)
        {
            invisible=true;
            coin=true;
            shooting=false;
            extraLife=false;
            isThere=false;
        }
        if(type==5)
        {
            invisible=true;
            coin=false;
            shooting=true;
            extraLife=false;
            isThere=false;
        }
        if(type==6)
        {
            invisible=true;
            shooting=false;
            coin=false;
            extraLife=true;
            isThere=false;
        }
        if(invisible==false)
            setImage(image1);
        if(invisible==true)
            setImage(image4);
    }

    /**
     * Act - do whatever the Box wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Scene scene=(Scene)getWorld();
        if(!scene.pause)
        {
            if(!empty)
            {
                checkPengu();
                hit();
            }
            remove();
        }
    }  

    private boolean checkPengu1()
    {
        Pengu pengu=(Pengu)getOneObjectAtOffset(0,getImage().getHeight()/2+1,Pengu.class);
        return pengu!=null&&pengu.vSpeed<0;
    }

    private boolean checkPengu2()
    {
        Pengu pengu=(Pengu)getOneObjectAtOffset(getImage().getWidth()/2-1,getImage().getHeight()/2,Pengu.class);
        return pengu!=null&&pengu.vSpeed<0;
    }

    private boolean checkPengu3()
    {
        Pengu pengu=(Pengu)getOneObjectAtOffset(-getImage().getWidth()/2+1,getImage().getHeight()/2,Pengu.class);
        return pengu!=null&&pengu.vSpeed<0;
    }

    private void checkPengu()
    {
        if(checkPengu1()||checkPengu2()||checkPengu3())
        {
            hit=true;
        }
    }

    private void hit()
    {
        Scene scene=(Scene)getWorld();
        if(hit==true)
        {
            timer++;
            if(counter<6)
            {
                if(timer==2&&(getImage()==image1||getImage()==image4||getImage()==image5))
                {
                    setImage(image2);
                    timer=0;
                    counter++;
                }
                if(timer==2&&getImage()==image2)
                {
                    setImage(image1);
                    timer=0;
                    counter++;
                }
            }

        }
        if(counter==5)
        {
            empty=true;
            if(shooting==true)
            {
                scene.addObject(new Shooting(),getX(),getY());
            }
            else if(coin==true)
            {
                scene.coins++;
            }
            else if(extraLife==true)
            {
                scene.addObject(new ExtraLife(),getX(),getY());
            }
            scene.score(100);
            counter=0;
            isThere=true;
            hit=false;
            setImage(image3);
        }
    }
}
This is the Block class (collision object, has a variable in it (boolean isThere) that needs to be checked for collision):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Block here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Block extends Boxes
{
    private GreenfootImage image1=new GreenfootImage("Block.png");
    private GreenfootImage image2=new GreenfootImage("Bricks2.png");
    private GreenfootImage image3=new GreenfootImage("EndBricks.png");
    private GreenfootImage image4=new GreenfootImage("Tunnel.png");
    public boolean isThere=true;
    private boolean addObjects=false;
    public Block(int type)
    {
        if(type==1)
        {
            setImage(image1);
        }
        if(type==2)
        {
            setImage(image2);
        }
        if(type==3)
        {
            setImage(image3);
        }
        if(type==4)
        {
            setImage(image4);
            isThere=false;
        }
        if(type==5)
        {
            setImage(image1);
            addObjects=true;
        }
    }

    /**
     * Act - do whatever the Block wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Scene scene=(Scene)getWorld();
        if(!scene.pause)
        {
            addObjects();
            remove();
        }
    }

    private void addObjects()
    {
        Scene scene=(Scene)getWorld();
        if(addObjects)
        {
            scene.addObjects=getX();
        }
    }
}
I didn't post the Cliff class because the only thing that it has to do with the collision is the picture.
danpost danpost

2016/12/14

#
It would be very difficult to come up with something that would work here. There is zero documentation and the world class code is missing. The documentation is needed to decipher what you are doing with all the 'scene' fields. I get the feeling that those fields should be in this class (and they probably are not needed at all; but, again, hard to say without any documentation).
Nosson1459 Nosson1459

2016/12/15

#
I'll do them one at a Time. Here is the World class:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * This is the whole scene. It creates and contains the objects that are in it.
 */
public class Scene extends World
{
    public int lives=2; // the number of lives, when 0 - game over
    public int score; // the total score of the game
    public int coins=0; // the amount of collected coins
    public int level=1; // the game level
    public boolean move=false; // only side-scroll when true
    public boolean direction=true; // the direction the penguin is facing
    public boolean way=true; // direction for  side-scrolling on a platform
    public boolean shooting=false; // whether penguin has shooting
    public boolean pause=false; // whather game is paused
    public boolean isDoor=false; // when a door is added (for stopping side-scrolling)
    public boolean onCloud=false; // when on a cloud - true
    public boolean isThere=false; // true when the paused actor is there
    private Pengu pengu; // penguin actor
    private Score addScore; // Score actor/object
    /**
     * The Scene constructor
     */
    public Scene()
    {  
        super(750,650,1,false);
        setBackground("clouds.jpg");
        setPaintOrder(HelpBox.class,Help.class,Coins.class,Level.class,Life.class,Score.class,Pause.class,Bar.class,Door.class,
            Pengu.class,Bullet.class,Cliff.class,DoorPost.class,Paused.class,Box.class,Shooting.class,Snake.class,SnakeD.class,Platform.class);
        addObject(new Bar(),375,18);
        addObject(new Coins("Coins: "),645,18);
        addObject(new Life("Lives: "),85,18);
        addScore=new Score("Score: ");
        addObject(addScore,470,18);
        addObject(new Level("Level: "),270,18);
        addObject(new HelpBox(),730,18);
        addObject(new Pause(),18,18);
        levelUp();
    }

    /**
     * The act method for Scene
     */public void act()
    {
        if(!pause)
        {
            score=addScore.value;
            if(lives<0)
            {
                Greenfoot.setWorld(new End(false,score));
            }
            if(coins==100)
            {
                score(1000);
                lives++;
                coins=0;
            }
            sideScrolling();
        }
    }

    /**
     * Adds the total score to the EndScore class
     */
    public void score(int score)
    {
        addScore.add(score);
    }

    /**
     * Resets the level for restarting or for placing the next level
     */
    public void reset(boolean shoot)
    {
        removeObjects(getObjects(SideScrollers1.class));
        removeObjects(getObjects(Pengu.class));
        isDoor=false;
        move=false;
        onCloud=false;
        if(shoot)shooting=false;
    }

    /**
     * Calls each level method when the previous one is passed 
     */
    public void levelUp()
    {
        if(level==1)
            level1();
        if(level==2)
            level2();
        if(level==3)
            level3();
        if(level==4)
        {
            Greenfoot.setWorld(new End(true,score)); //for the GameOver/Win screen, and score
        }
    }

    /**
     * The level 1 constructor
     */
    public void level1()
    {
        pengu=new Pengu();
        addObject(pengu,192,470);
        for(int j=20;j<481;j+=40)
        {
            for(int i=510;i<631;i+=40)
            {
                addObject(new Block(2),j,i);
            }
        }
        addObject(new Creature(),440,390);
        for(int i=240;i<401;i+=40)
        {
            addObject(new Box(1),i,390);
        }
        addObject ( new Cliff(3), 658, 555);
        addObject(new SnakeD(),658,450);
        addObject(new Box(6),658,350);
        addObject(new Cliff(2),908,525);
        addObject(new Coin(),908,310);
        addObject(new Coin(),873,310);
        addObject(new Coin(),943,310);
        addObject(new Cliff(2),1243,525);
        addObject(new Platform(4),1076,450);
        for(int j=1362;j<1483;j+=40)
        {
            for(int i=510;i<631;i+=40)
            {
                addObject(new Block(2),j,i);
            }
        }
        for(int i=1362;i<1443;i+=40)
        {
            addObject(new Coin(),i,390);
        }
        addObject(new Box(2),1482,390);
        addObject(new Block(1),1679,439);
        addObject(new Cliff(3),1788,555);
        addObject(new Cliff(2),1942,525);
        addObject(new Snake(),1942,361);
        for (int j=2105; j<2346; j+=40)
        {
            for (int i=486; i<647; i+=40)
            {
                addObject(new Block(3),j,i);
            }
        }
        addObject(new Door(),2292,433);
        addObject(new DoorPost(),2292,433);
    }

    /**
     * The level 2 constructor
     */
    private void level2()
    {
        reset(false);
        pengu=new Pengu();
        addObject(pengu,60,470);
        int a=509;
        for(int j=-20;j<661;j+=40)
        {
            for(int i=630;i>a;i-=40)
            {
                if(j<301||j>381)
                {
                    addObject(new Block(2),j,i);
                }
            }
            if(j>381&&j<541)
            {
                a+=40;
            }
            else if(j>141&&j<261)
            {
                a-=40;
            }
        }
        for (int i=60;i<141;i+=40)
        {
            addObject(new Box(1),i,382);
        }
        for (int i=60;i<141;i+=40)
        {
            addObject(new Coin(),i,272);
        }
        addObject(new SnakeD(),660,520);
        addObject(new Platform(2),800,540);
        addObject(new Snake(),300,359);
        for(int j=1062;j<1183;j+=40)
        {
            for(int i=630;i>470;i-=40)
            {
                addObject(new Block(2),j,i);
            }
        }
        addObject(new SnakeD(),1142,479);
        for(int i=1062;i<1183;i+=40)
        {
            addObject(new Coin(),i,340);
        }
        addObject(new Block(1),1294,461);
        addObject(new Block(1),1387,412);
        addObject(new Block(1),1487,501);
        addObject(new Block(1),1599,575);
        addObject(new Block(1),1681,440);
        addObject(new Cliff(4),1961,555);
        for(int i=2034;i>1988;i-=45)
        {
            addObject(new Snake(-2),i,447);
        }
        for(int i=1807;i<2116;i+=154)
        {
            addObject(new Box(4),i,354);
        }
        for(int i=1807;i<1913;i+=35)
        {
            addObject(new Coin(),i,239);
        }
        addObject(new Box(2),1961,239);
        for(int i=2010;i<2116;i+=35)
        {
            addObject(new Coin(),i,239);
        }
        int b=470;
        for(int j=2245;j<2726;j+=40)
        {
            for(int i=630;i>b;i-=40)
            {
                addObject(new Block(2),j,i);
            }
            if(j==2365)
                b-=120;
            else if(j==2405)
                b+=120;
            else if(j==2725)
                b-=120;
        }
        addObject(new Creature(),2400,440);
        int c=509;
        for(int j=2805;j<2966;j+=40)
        {
            for(int i=510;i>c;i-=40)
            {
                addObject(new Block(2),j,i);
            }
            if(j>2805)
                c=389;
        }
        int g=590;
        int h=509;
        for(int j=2765;j<3206;j+=40)
        {
            for(int i=g;i>h;i-=40)
            {
                addObject(new Block(4),j,i);
            }
            if(j==2765)
                h+=40;
            if(j>2925)  
                h-=40;
            if(j>2965)
                g-=40;
        }
        int d=629;
        for(int j=2765;j<3206;j+=40)
        {
            for(int i=630;i>d;i-=40)
            {
                addObject(new Block(2),j,i);
            }
            if(j>2965)
                d-=40;
        }
        int e=470;
        int f=389;
        for(int j=3005;j<3206;j+=40)
        {
            for(int i=e;i>f;i-=40)
            {
                addObject(new Block(2),j,i);
            }
            e-=40;
            if(j>3005)
                f-=40;
        }
        addObject(new SnakeD(),3100,360);
        for(int i=2765;i<2966;i+=40)
        {
            addObject(new Coin(),i,595);
        }
        for(int i=2885;i<3086;i+=40)
        {
            addObject(new Coin(),i,275);
        }
        for (int j=3305; j<3546; j+=40)
        {
            for (int i=486; i<647; i+=40)
            {
                addObject(new Block(3),j,i);
            }
        }
        addObject(new Door(),3492,433);
        addObject(new DoorPost(),3492,433);
    }

    /**
     * The level 3 constructor
     */
    private void level3()
    {
        reset(false);
        pengu=new Pengu();
        addObject(pengu,60,470);
        for(int j=-20;j<181;j+=40)
        {
            for(int i=630;i>509;i-=40)
            {
                addObject(new Block(2),j,i);
            }
        }
        for(int i=260;i<500;i+=40)
        {
            addObject(new Coin(),i,390);
        }
        addObject(new Platform(2),300,520);
        int a=489;
        for(int j=500;j<781;j+=40)
        {
            for(int i=630;i>a;i-=40)
            {
                addObject(new Block(2),j,i);
            }
            a-=40;
        }
        for(int i=860;i<1021;i+=40)
        {
            addObject(new Coin(),i,390);
        }
        addObject(new Snake(),780,197);
        addObject(new Platform(2),910,520);
        addObject(new Cliff(1),1140,555);
        addObject(new SnakeD(),1140,447);
        int b=449;
        for(int j=1345;j<1826;j+=40)
        {
            for(int i=630;i>b;i-=40)
            {
                addObject(new Block(2),j,i);
            }
            if(j>1344&&j<1425)
                b-=40;
            if(j>1424&&j<1505)
                b+=40;
            if(j>1504&&j<1585)
                b-=40;
            if(j>1584&&j<1665)
                b+=40;
            if(j>1664&&j<1745)
                b-=40;
            if(j>1744)
                b+=40;
        }
        for (int i=1425;i<1784;i+=160)
        {
            addObject(new Box(1),i,250);
        }
        addObject(new Creature(),1585,370);
        for(int j=1965;j<2286;j+=40)
        {
            for(int i=630;i>549;i-=40)
            {
                addObject(new Block(2),j,i);
            }
        }
        addObject(new Cliff(4),2126,435);
        addObject(new SnakeD(),1966,327);
        addObject(new SnakeD(),2286,327);
        for(int i=2102;i<2223;i+=40)
        {
            addObject(new Box(1),i,220);
        }
        int c=2660;
        addObject(new Cliff(4),c+185,589);
        addObject(new Cliff(2),c,525);
        addObject(new Cliff(2),c+316,525);
        addObject(new Box(4),c+75,384);
        addObject(new Box(2),c+115,384);
        addObject(new Box(3),c+155,384);
        addObject(new Snake(),c+90,523);
        addObject(new Snake(),c+150,523);
        addObject(new Platform(4),2560,400);
        int d=589;
        for(int j=3105;j<3346;j+=40)
        {
            for(int i=630;i>d;i-=40)
            {
                addObject(new Block(2),j,i);
            }
            if(j>3145&&j<3225)d=349;
            if(j>3264)d=469;
        }
        addObject(new Box(4),3145,450);
        addObject(new Coin(),3225,315);
        addObject(new Coin(),3265,315);
        addObject(new Block(1),3885,495);
        for (int j=4385; j<4626; j+=40)
        {
            for (int i=486; i<647; i+=40)
            {
                addObject(new Block(3),j,i);
            }
        }
        addObject(new Door(),4572,433);
        addObject(new DoorPost(),4572,433);
        addObject(new Platform(4),3635,495);
        addObject(new Platform(4),4135,495);
    }

    /**
     * This method is the method that moves all the side scrolling objects
     */
    private void sideScrolling()
    {
        move = pengu.getX() >= 342 && ! onCloud && ! pengu.touchingRightSide();
        if(isDoor==true||pengu.getX()<342)
        {
            move=false;
        }
        if(Greenfoot.isKeyDown("right")&&move==true)
        {
            for (SideScrollers1 obj: getObjects(SideScrollers1.class))
            {
                Actor actor=(Actor) obj;
                actor.setLocation(actor.getX()-7, actor.getY());
            }
        }
    }

    /**
     * This method moves all side scrolling objects back and forth when on a platform (onCloud)
     */
    public void platform()
    {
        for (SideScrollers obj: getObjects(SideScrollers.class))
        {
            Actor actor=(Actor) obj;
            if(way==true)
            {
                actor.setLocation(actor.getX()-4,actor.getY());
            }
            if(way==false)
            {
                actor.setLocation(actor.getX()+4,actor.getY());
            }
        }
    }

    /**
     * this method is called when the help button is pressed (and the Help isn't there)
     */
    public void help()
    {
        addObject(new Help(),375,325);
    }

    /**
     * This method is called when the help button is pressed (and the Help is there)
     */
    public void help2()
    {
        removeObjects(getObjects(Help.class));
    }
}
The While loop with isTouching(Block.class) won't work because It's standing on blocks and looking for blocks AHEAD of him (there is a variable in block/box that has to be retrieved).
Nosson1459 Nosson1459

2016/12/15

#
This is the Mover class:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * The class Mover provides some basic movement methods. Use this as a superclass
 * for other actors that should be able to move left and right, jump up and fall 
 * down.
 */
public abstract class Mover extends Actor
{
    private static final int acceleration = 2;      // down (gravity)
    private static final int speed = 7;             // running speed (sideways)
    public int vSpeed = 0;                          // current vertical speed
    private boolean hasHit=false;                   // boolean for hitting something onTop
    /**
     * Called in Pengu when right arrow key is pressed
     */
    public void moveRight()
    {
        setLocation ( getX() + speed, getY() );
    }

    /**
     * Called in Pengu when left arrow key is pressed
     */
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY() );
    }

    /**
     * The next 8 methods are for checking when the Penguin hits something with his head
     * 
     * The first 2 are for to make the ifs that onTop are used in smaller
     * The next 3 are for chicking if the penguin hit a (mystery) box the vSpeed<0 are to make sure you hit it on the way up (for invisible box)
     * The last 2 are for checking if the penguin hits a block, the isThere is to make sure it's not [invisible]/[just backgroung color]
     * 
     * The reason there are multiple of each type of method is for accuracy,
     * if there's only one method then it's only checking the middle of the penguin so the rest of it's head can just go through
     */
    public boolean onTop()
    {
        return onTop1B()||onTop2B()||onTop3B();
    }

    public boolean onTop2()
    {
        return onTop1Bc()||onTop2Bc()||onTop3Bc();
    }

    private boolean onTop1B()
    {
        Box box = (Box)getOneObjectAtOffset(-getImage().getWidth()/2+8, -getImage().getHeight()/2 , Box.class);
        return box != null&&vSpeed<0;
    }

    private boolean onTop2B()
    {
        Box box = (Box)getOneObjectAtOffset(0, -getImage().getHeight()/2 , Box.class);
        return box != null&&vSpeed<0;
    }

    private boolean onTop3B()
    {
        Box box = (Box)getOneObjectAtOffset(getImage().getWidth()/2-8, -getImage().getHeight()/2 , Box.class);
        return box != null&&vSpeed<0;
    }

    private boolean onTop1Bc()
    {
        Block block=(Block) getOneObjectAtOffset(-getImage().getWidth()/2+8, -getImage().getHeight()/2 , Block.class);
        return block != null&&block.isThere==true;
    }

    private boolean onTop2Bc()
    {
        Block block=(Block)  getOneObjectAtOffset(0, -getImage().getHeight()/2 , Block.class);
        return block != null&&block.isThere==true;
    }

    private boolean onTop3Bc()
    {
        Block block=(Block) getOneObjectAtOffset(getImage().getWidth()/2-8, -getImage().getHeight()/2 , Block.class);
        return block != null&&block.isThere==true;
    }

    /**
     * The next 12 methods are for checking if the Pengu is standing on solid ground
     * 
     * The first 4 are for checking if the Pengu is on solid (isThere==true) Box, with the last for shorter ifs
     * The next 4 are for checking if the Pengu is on solid (isthere==true) Block, with the last for shorter ifs
     * the Box and Block can't be together even though both extend Boxes because of isThere
     * The next/last 4 are for checking if the Pengu is on a Cliff, with the last for shorter ifs
     * 
     * The reason there are multiple of each type of method is for accuracy,
     * if there's only one method then it's only checking the middle of the penguin so the rest of it's body can just go through
     */
    private boolean onGround1B()
    {
        Box box = (Box)getOneObjectAtOffset(0, getImage().getHeight()/2 , Box.class);
        return box != null&&box.isThere==true;
    }

    private boolean onGround2B()
    {
        Box box = (Box)getOneObjectAtOffset(getImage().getWidth()/2-10, getImage().getHeight()/2 , Box.class);
        return box != null&&box.isThere==true;
    }

    private boolean onGround3B()
    {
        Box box  = (Box)getOneObjectAtOffset(-getImage().getWidth()/2+10, getImage().getHeight()/2 , Box.class);
        return box != null&&box.isThere==true;
    }

    private boolean onGroundB()
    {
        return onGround1B()||onGround2B()||onGround3B();
    }

    private boolean onGround1Bc()
    {
        Block block =(Block) getOneObjectAtOffset(0, getImage().getHeight()/2 , Block.class);
        return block != null&&block.isThere==true;
    }

    private boolean onGround2Bc()
    {
        Block block =(Block) getOneObjectAtOffset(getImage().getWidth()/2-10, getImage().getHeight()/2 , Block.class);
        return block != null&&block.isThere==true;
    }

    private boolean onGround3Bc()
    {
        Block block =(Block) getOneObjectAtOffset(-getImage().getWidth()/2+10, getImage().getHeight()/2 , Block.class);
        return block != null&&block.isThere==true;
    }

    private boolean onGroundBc()
    {
        return onGround1Bc()||onGround2Bc()||onGround3Bc();
    }

    private boolean onGround1CA()
    {
        Object under = getOneObjectAtOffset(0, getImage().getHeight()/2 , Cliff.class);
        return under != null;
    }

    private boolean onGround2CA()
    {
        Object under = getOneObjectAtOffset(getImage().getWidth()/2-10, getImage().getHeight()/2 , Cliff.class);
        return under != null;
    }

    private boolean onGround3CA()
    {
        Object under = getOneObjectAtOffset(-getImage().getWidth()/2+10, getImage().getHeight()/2 , Cliff.class);
        return under != null;
    }

    private boolean onGroundCA()
    {
        return onGround1CA()||onGround2CA()||onGround3CA();
    }

    /**
     * The next 4 methods are for checking if the Pengu is standing on a Platform with the last for shorter ifs 
     * 
     * The reason there are multiple of each type of method is for accuracy,
     * if there's only one method then it's only checking the middle of the penguin so the rest of it's body can just go through
     */
    private boolean onGround1P()
    {
        Object under = getOneObjectAtOffset(0, getImage().getHeight()/2 , Platform.class);
        return under != null;
    }

    private boolean onGround2P()
    {
        Object under = getOneObjectAtOffset(getImage().getWidth()/2-10, getImage().getHeight()/2 , Platform.class);
        return under != null;
    }

    private boolean onGround3P()
    {
        Object under = getOneObjectAtOffset(-getImage().getWidth()/2+10, getImage().getHeight()/2 , Platform.class);
        return under != null;
    }

    private boolean onGroundP()
    {
        return onGround1P()||onGround2P()||onGround3P();
    }

    public boolean onGround()
    {
        return onGroundBc()||onGroundB()||onGroundCA()||onGroundP();
    }

    /**
     * The next 13 methods are for checking if the Pengu hits something on his left side
     * 
     * The first 4 are for checking if the Pengu hits solid (isThere==true) Box, with the last for shorter ifs
     * The next 4 are for checking if the Pengu hits solid (isThere==true) Block, with the last for shorter ifs
     * The next 4 are for checking if the Pengu hits a Cliff, with the last for shorter ifs
     * The last of the 13 methods are to make the ifs even shorter
     * 
     * The reason there are multiple of each type of method is for accuracy,
     * if there's only one method then it's only checking the middle of the penguin so the rest of it's body can just go through
     */
    private boolean touchingLeftSide1B()
    {
        Box box=(Box)getOneObjectAtOffset(-getImage().getWidth()/2,getImage().getHeight()/2-10,Box.class);
        return box!=null&&box.isThere==true;
    }

    private boolean touchingLeftSide2B()
    {
        Box box=(Box)getOneObjectAtOffset(-getImage().getWidth()/2,-getImage().getHeight()/2+1,Box.class);
        return box!=null&&box.isThere==true;
    }

    private boolean touchingLeftSide3B()
    {
        Box box=(Box)getOneObjectAtOffset(-getImage().getWidth()/2,0,Box.class);
        return box!=null&&box.isThere==true;
    }

    private boolean touchingLeftSideB()
    {
        return touchingLeftSide1B()||touchingLeftSide2B()||touchingLeftSide3B();
    }

    private boolean touchingLeftSide1Bc()
    {
        Block block=(Block)getOneObjectAtOffset(-getImage().getWidth()/2,getImage().getHeight()/2-10,Block.class);
        return block != null&&block.isThere==true;
    }

    private boolean touchingLeftSide2Bc()
    {
        Block block=(Block)getOneObjectAtOffset(-getImage().getWidth()/2,-getImage().getHeight()/2+1,Block.class);
        return block != null&&block.isThere==true;
    }

    private boolean touchingLeftSide3Bc()
    {
        Block block=(Block)getOneObjectAtOffset(-getImage().getWidth()/2,0,Block.class);
        return block != null&&block.isThere==true;
    }

    private boolean touchingLeftSideBc()
    {
        return touchingLeftSide1Bc()||touchingLeftSide2Bc()||touchingLeftSide3Bc();
    }

    private boolean touchingLeftSide1CA()
    {
        Object side=getOneObjectAtOffset(-getImage().getWidth()/2,getImage().getHeight()/2-10,Cliff.class);
        return side!=null;
    }

    private boolean touchingLeftSide2CA()
    {
        Object side=getOneObjectAtOffset(-getImage().getWidth()/2,-getImage().getHeight()/2+1,Cliff.class);
        return side!=null;
    }

    private boolean touchingLeftSide3CA()
    {
        Object side=getOneObjectAtOffset(-getImage().getWidth()/2,0,Cliff.class);
        return side!=null;
    }

    private boolean touchingLeftSideCA()
    {
        return touchingLeftSide1CA()||touchingLeftSide2CA()||touchingLeftSide3CA();
    }

    public boolean touchingLeftSide()
    {
        return touchingLeftSideB()||touchingLeftSideCA()||touchingLeftSideBc();
    }

    /**
     * The next 13 methods are for checking if the Pengu hits something on his right side
     * 
     * The first 4 are for checking if the Pengu hits solid (isThere==true) Box, with the last for shorter ifs
     * The next 4 are for checking if the Pengu hits solid (isThere==true) Block, with the last for shorter ifs
     * The next 4 are for checking if the Pengu hits a Cliff, with the last for shorter ifs
     * The last of the 13 methods are to make the ifs even shorter
     * 
     * The reason there are multiple of each type of method is for accuracy,
     * if there's only one method then it's only checking the middle of the penguin so the rest of it's body can just go through
     */
    private boolean touchingRightSide1B()
    {
        Box box=(Box)getOneObjectAtOffset(getImage().getWidth()/2,-getImage().getHeight()/2+1,Box.class);
        return box!=null&&box.isThere==true;
    }

    private boolean touchingRightSide2B()
    {
        Box box=(Box)getOneObjectAtOffset(getImage().getWidth()/2,getImage().getHeight()/2-10,Box.class);
        return box!=null&&box.isThere==true;
    }

    private boolean touchingRightSide3B()
    {
        Box box=(Box)getOneObjectAtOffset(getImage().getWidth()/2,0,Box.class);
        return box!=null&&box.isThere==true;
    }

    public boolean touchingRightSideB()
    {
        return touchingRightSide1B()||touchingRightSide2B()||touchingRightSide3B(); 
    }

    private boolean touchingRightSide1Bc()
    {
        Block block=(Block)getOneObjectAtOffset(getImage().getWidth()/2,-getImage().getHeight()/2+1,Block.class);
        return block != null&&block.isThere==true;
    }

    private boolean touchingRightSide2Bc()
    {
        Block block=(Block)getOneObjectAtOffset(getImage().getWidth()/2,getImage().getHeight()/2-10,Block.class);
        return block != null&&block.isThere==true;
    }

    private boolean touchingRightSide3Bc()
    {
        Block block=(Block)getOneObjectAtOffset(getImage().getWidth()/2,0,Block.class);
        return block != null&&block.isThere==true;
    }

    public boolean touchingRightSideBc()
    {
        return touchingRightSide1Bc()||touchingRightSide2Bc()||touchingRightSide3Bc(); 
    }

    private boolean touchingRightSide1CA()
    {
        Object side=getOneObjectAtOffset(getImage().getWidth()/2,-getImage().getHeight()/2+1,Cliff.class);
        return side!=null;
    }

    private boolean touchingRightSide2CA()
    {
        Object side=getOneObjectAtOffset(getImage().getWidth()/2,getImage().getHeight()/2-20,Cliff.class);
        return side!=null;
    }

    private boolean touchingRightSide3CA()
    {
        Object side=getOneObjectAtOffset(getImage().getWidth()/2,0,Cliff.class);
        return side!=null;
    }

    public boolean touchingRightSideCA()
    {
        return touchingRightSide1CA()||touchingRightSide2CA()||touchingRightSide3CA(); 
    }

    public boolean touchingRightSide()
    {
        return touchingRightSideB()||touchingRightSideCA()||touchingRightSideBc(); 
    }

    /**
     * This method sets the vertical speed
     */
    public void setVSpeed(int speed)
    {
        vSpeed = speed;
    }

    /**
     * This method is used for jumping and to make the Pengu land onGround if not onGround or if it hit something with his head
     */
    public void fall()
    {
        bump();
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
        if ( atBottom() )
            gameEnd();
    }

    /**
     * This is a boolean if the penguin is at the bottom of the screen (for removing it)
     */
    private boolean atBottom()
    {
        return getY() >= getWorld().getHeight() - 2;
    }

    /**
     * This method is called when the penguin is atBottom it subtracts a life and resets the level
     */
    public void gameEnd()
    {
        Scene scene=(Scene)getWorld();
        scene.lives--;
        scene.reset(true);
        scene.levelUp();
    }

    /**
     * This method is for checking if the penguin hit his head on something
     */
    public void bump()
    {
        if (onTop()||onTop2())
        {
            if(!hasHit)
            {
                vSpeed=-vSpeed;
            }
            hasHit=true;
        }
        if(onGround())
        {
            hasHit=false;
        }
    }
}
Nosson1459 Nosson1459

2016/12/15

#
This is the Pengu class:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * A little penguin that wants to get to the other side.
 */
public class Pengu extends Mover
{
    private static final int jumpStrength = 23; // this is for the height the Pengu jumps
    private int delay=0; // this is so that the bullets don't continuously come out, rather there is a space in between 
    private GreenfootImage right=new GreenfootImage("Pengu/pengu-right2.png"); //image of Pengu facing right
    private GreenfootImage left=new GreenfootImage("Pengu/pengu-left2.png"); // image of Pengu facing left
    private GreenfootImage rightShooting=new GreenfootImage("Pengu/pengu-right2Shooting.png"); // image of pengu facing right holding fireballs
    private GreenfootImage leftShooting=new GreenfootImage("Pengu/pengu-left2Shooting.png"); // image of Pengu facing left holding fireballs
    /**
     * Pengu class constructor
     */
    public Pengu()
    {
        setImage(right);
    }

    /**
     * Act - do whatever the Pengu wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Scene scene=(Scene)getWorld();
        if(!scene.pause) //makes sure the game is not paused
        {
            if(scene.shooting==true)delay++;
            shoot();
            shooting();
            coins();
            checkKeys();       
            checkFall();
        }
    }

    /**
     * The movement and images for the Pengu
     */
    private void checkKeys()
    {
        Scene scene=(Scene)getWorld();
        if (Greenfoot.isKeyDown("left")&&getX()>=20 )
        {
            scene.direction=false; 
            if(!touchingLeftSide())
                moveLeft();
        }
        if (Greenfoot.isKeyDown("left"))
        {
            if(!scene.shooting)
            {
                setImage(left);
            }
            else if(scene.shooting=true)
            {
                setImage(leftShooting);
            }
        }
        if (Greenfoot.isKeyDown("right")&&scene.move==false)
        {
            scene.direction=true;
            if(!touchingRightSide())
                moveRight();
        }
        if (Greenfoot.isKeyDown("right"))
        {
            if(!scene.shooting)
            {
                setImage(right);
            }
            else if(scene.shooting=true)
            {
                setImage(rightShooting);
            }
        }
        if (Greenfoot.isKeyDown("up") )
        {
            if (onGround())
                jump();
        }
    }    

    /**
     * the jumping for the Pengu
     */
    private void jump()
    {
        Scene scene=(Scene)getWorld();
        setVSpeed(-jumpStrength);
        fall();
    }

    /**
     * This method checks for retrieving coins
     */
    private void coins()
    {
        Scene scene=(Scene)getWorld();
        Coin coin=(Coin)getOneIntersectingObject(Coin.class);
        if(coin!=null)
        {
            scene.removeObject(coin);
            scene.score(100);
            scene.coins=scene.coins+1;
        }
    }

    /**
     * This method is the method that shoots the fireballs
     */
    private void shoot()
    {
        Scene scene=(Scene)getWorld();
        if(Greenfoot.isKeyDown("space")&&delay>=5&&scene.shooting==true)
        {
            if(scene.direction==false)
            {
                scene.addObject(new Bullet(-8),getX()-26,getY());
            }
            if(scene.direction==true)
            {
                scene.addObject(new Bullet(8),getX()+26,getY());
            }
            delay=0;
        }
    }

    /**
     * This method gets the shooting power-up and sets the image for the Pengu
     */
    private void shooting()
    {
        Scene scene=(Scene)getWorld();
        if(isTouching(Shooting.class))
        {
            scene.shooting=true;
            scene.score(250);
            removeTouching(Shooting.class);
        }
        if(scene.shooting)
        {
            if(scene.direction==false)
            {
                setImage(leftShooting);
            }
            if(scene.direction==true)
            {
                setImage(rightShooting);
            }
        }
        else if(!scene.shooting)
        {
            if(scene.direction==false)
            {
                setImage(left);
            }
            if(scene.direction==true)
            {
                setImage(right);
            }
        }
    }

    /**
     * This method makes sure the Pengu is standing on top of the object that it's touching (but not the door or door post)
     */
    public void checkFall()
    {
        if (onGround()) {
            setVSpeed(0);
            Actor under = getOneObjectAtOffset(0, getImage().getHeight()/2 , null);
            if(!checkDoor())
            {
                if(under!=null)
                {
                    int h=under.getImage().getHeight()/2+getImage().getHeight()/2-5;
                    int y=under.getY();
                    setLocation(getX(),y-h);
                }
            }
        }
        else {
            fall();
        }
    }

    /**
     * This method is used to see if the Pengu is touching the door or door post
     */
    private boolean checkDoor()
    {
        return isTouching(Door.class)||isTouching(DoorPost.class);
    }
}
Nosson1459 Nosson1459

2016/12/15

#
This is the Box class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * A mystery box with good things inside for the Pengu
 * 
 * @author Nosson1459 
 * @version (a version number or a date)
 */
public class Box extends Boxes
{
    private GreenfootImage image1=new GreenfootImage("mystery.png"); // image of box with '?'
    private GreenfootImage image2=new GreenfootImage("MysteryG.png"); // image of gold box with '?'
    private GreenfootImage image3=new GreenfootImage("Block.png"); // image of plain block
    private GreenfootImage image4=new GreenfootImage("transparentBox.png"); //same size as boxes/block but transparent
    private int timer=0; // timer so that there is a space between the changing (flash) image from regular to gold box
    private boolean hit=false; // boolean whether this box has been hit or not
    private int counter=0; // counts the image changes so the mystery box isn't flashing the rest of the level
    private boolean empty=false; // boolean whether this box is empty or not
    private boolean coin=false; // boolean for box containing coin
    private boolean shooting=false; // boolean for box containing shooting power-up
    private boolean invisible=false; // boolean whether box is invivible
    private boolean extraLife=false; // boolean for box containing an extra life
    public boolean isThere=true; // boolean whether the box is solid (yet)
    /**
     * Box class constructor
     * For setting the image and invisibility
     */
    public Box(int type)
    {
        if(type==1)
        {
            invisible=false;
            coin=true;
            shooting=false;
            extraLife=false;
        }
        if(type==2)
        {
            invisible=false;
            shooting=true;
            coin=false;
            extraLife=false;
        }
        if(type==3)
        {
            invisible=false;
            shooting=false;
            coin=false;
            extraLife=true;
        }
        if(type==4)
        {
            invisible=true;
            coin=true;
            shooting=false;
            extraLife=false;
            isThere=false;
        }
        if(type==5)
        {
            invisible=true;
            coin=false;
            shooting=true;
            extraLife=false;
            isThere=false;
        }
        if(type==6)
        {
            invisible=true;
            shooting=false;
            coin=false;
            extraLife=true;
            isThere=false;
        }
        if(invisible==false)
            setImage(image1);
        if(invisible==true)
            setImage(image4);
    }

    /**
     * Act - do whatever the Box wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Scene scene=(Scene)getWorld();
        if(!scene.pause)
        {
            if(!empty)
            {
                checkPengu();
                hit();
            }
            remove(); // removes the box when it goes off the left side of the screen
        }
    }  

    /**
     * The following 4 methods are to see if the box has been hit by the Pengu
     * the last one makes hit be true
     */
    private boolean checkPengu1()
    {
        Pengu pengu=(Pengu)getOneObjectAtOffset(0,getImage().getHeight()/2+1,Pengu.class);
        return pengu!=null&&pengu.vSpeed<0;
    }

    private boolean checkPengu2()
    {
        Pengu pengu=(Pengu)getOneObjectAtOffset(getImage().getWidth()/2-1,getImage().getHeight()/2,Pengu.class);
        return pengu!=null&&pengu.vSpeed<0;
    }

    private boolean checkPengu3()
    {
        Pengu pengu=(Pengu)getOneObjectAtOffset(-getImage().getWidth()/2+1,getImage().getHeight()/2,Pengu.class);
        return pengu!=null&&pengu.vSpeed<0;
    }

    private void checkPengu()
    {
        if(checkPengu1()||checkPengu2()||checkPengu3())
        {
            hit=true;
        }
    }

    /**
     * When hit==true then the Box flashes, the object from inside the box pops up and the points get added to the score
     */
    private void hit()
    {
        Scene scene=(Scene)getWorld();
        if(hit==true)
        {
            timer++;
            if(counter<6)
            {
                if(timer==2&&(getImage()==image1||getImage()==image4))
                {
                    setImage(image2);
                    timer=0;
                    counter++;
                }
                if(timer==2&&getImage()==image2)
                {
                    setImage(image1);
                    timer=0;
                    counter++;
                }
            }

        }
        if(counter==5)
        {
            empty=true;
            if(shooting==true)
            {
                scene.addObject(new Shooting(),getX(),getY());
            }
            else if(coin==true)
            {
                scene.coins++;
            }
            else if(extraLife==true)
            {
                scene.addObject(new ExtraLife(),getX(),getY());
            }
            scene.score(100);
            counter=0;
            isThere=true;
            hit=false;
            setImage(image3);
        }
    }
}
Nosson1459 Nosson1459

2016/12/15

#
This is the Block class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Block here.
 * 
 * @author Nosson1459 
 * @version (a version number or a date)
 */
public class Block extends Boxes
{
    private GreenfootImage image1=new GreenfootImage("Block.png"); // image of plain block
    private GreenfootImage image2=new GreenfootImage("Bricks2.png"); // image of bricks
    private GreenfootImage image3=new GreenfootImage("EndBricks.png"); // image of different color bricks
    private GreenfootImage image4=new GreenfootImage("Tunnel.png"); // plain green box (whenever used - isThere=false)
    public boolean isThere=true;
    private boolean addObjects=false;
    /**
     * Block class constructor
     * For setting the image and invisibility
     */
    public Block(int type)
    {
        if(type==1)
        {
            setImage(image1);
        }
        if(type==2)
        {
            setImage(image2);
        }
        if(type==3)
        {
            setImage(image3);
        }
        if(type==4)
        {
            setImage(image4);
            isThere=false;
        }
        if(type==5)
        {
            setImage(image1);
            addObjects=true;
        }
    }

    /**
     * Act - do whatever the Block wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Scene scene=(Scene)getWorld();
        if(!scene.pause)
        {
            remove(); // removes the block when it goes off the left side of the screen
        }
    }
}
That should be all the requested classes. You said there is zero documentation and I wasn't sure what that meant so I did what you see above. If that's not what you meant by documentation then explain what you mean.
Nosson1459 Nosson1459

2016/12/16

#
Is anybody around to answer the question (First post, use the last posts for the classes)?
danpost danpost

2016/12/19

#
I will answer here (since you obviously do not want me to respond to your newer thread). There is no easy way. The code is too bulky to easily work with. The Mover class is just ridiculous (if you ask me) and only tends to complicate matters.
Nosson1459 Nosson1459

2016/12/20

#
danpost wrote...
The code is too bulky to easily work with. The Mover class is just ridiculous (if you ask me) and only tends to complicate matters.
So if someone would start this from the beginning (without my bulky Mover class) what would you tell them to do (let's say I would delete the bulk in Mover)?
Nosson1459 Nosson1459

2016/12/20

#
Should I start making different subclasses, instead of just a different setImages in Box and Block for the images that I don't want to collide with, so that I don't need the isThere variable (then for the invisible Box I'll have to put removeObject(invisibleBox) when I hit the bottom of it and add a different object(solidBlock) for the empty mystery box)? And should I make different subclasses so that your 'while (isTouching(Block.class))' loop will work (not standing on the same thing that we're colliding with? (This will give me a lot of subclasses.)
danpost danpost

2016/12/20

#
Nosson1459 wrote...
Should I start making different subclasses
No. In fact, I would just have one class for all the boxes (with no subclasses). It would have an int field for identification. A non-zero value would mean something was in it and its value would determine what exactly was in it. A zero value would mean it is just a box (not invisible with nothing in it). I would use a switch statement to set the initial image. I would have nothing more in the class except methods to acquire information such as what it contains (the value of the field) and the emptying of them. Any collision detection and actions taken when detected can be done from the Pengu class. At least, that is how I would start.
Nosson1459 Nosson1459

2016/12/20

#
danpost wrote...
Any collision detection and actions taken when detected can be done from the Pengu class.
That's for the mystery boxes. How would you do the collision detection so that you are only hitting the bottom of the box - while going up, and how will you check the int variable from a different class? (Right now the collision for the bottom of the mystery box which is what opens it, is in the Box class, and uses "getOneObjectAtOffset" which is the bulk in Mover for the other collision detections.)
Nosson1459 wrote...
So if someone would start this from the beginning (without my bulky Mover class) what would you tell them to do (let's say I would delete the bulk in Mover)?
The above question is not only referring to the mystery boxes, it's referring to all collision detecting that I have in my Mover class (basically back to the original question).
danpost danpost

2016/12/20

#
Nosson1459 wrote...
How would you do the collision detection so that you are only hitting the bottom of the box - while going up, and how will you check the int variable from a different class? (Right now the collision for the bottom of the mystery box which is what opens it, is in the Box class, and uses "getOneObjectAtOffset" which is the bulk in Mover for the other collision detections.)
Imagine something like this: * move as directed horizontally and stop at obstacles * move vertically as below
public void moveVertically()
{
    vSpeed += acceleration; // pull due to gravity
    setLocation(getX(), getY()+vSpeed); // fall (or rise)
    if (vSpeed < 0) for (Object box : getIntersectingObjects(Box.class)) ((Box)box).activate();
    int dir = (int)Math.signum(vSpeed); // direction of movement
    boolean onSurface = false; // state of standing on something
    while (! getIntersectingObjects(Actor.class).isEmpty())
    {
        setLocation(getX(), getY()-dir); // move off of object
        if (vSpeed > 0) onSurface = true; // landing on surface
        vSpeed = 0; // kill speed
    }
    if (onSurface && Greenfoot.isKeyDown("up")) vSpeed = -jumpStrength; // jumping
}
Line 5 is sufficient to call a method in the Box class for each box hit from the underside to activate them. The method called in the Box class can simply be this:
public void activate()
{
    hit = true;
}
This is totally without the Mover class.
Nosson1459 Nosson1459

2016/12/22

#
danpost wrote...
move as directed horizontally and stop at obstacles
How was I directed, which obstacles do I stop at (I only want to stop at the boxes or blocks when isThere==true)?
danpost wrote...
move vertically as below <Code Omitted> Line 5 is sufficient to call a method in the Box class for each box hit from the underside to activate them. The method called in the Box class can simply be this: <Code Omitted> This is totally without the Mover class.
The Mover class was used for seeing which actors and when to hit/collide, the above (your) code is for hitting mystery boxes (the code for hitting mystery boxes (by me) is in the Box class not Mover). What about the invisible mystery boxes, I want that the Pengu should go through them until they become visible (change from image4 to image3 and isThere becomes true)?
There are more replies on the next page.
1
2
3