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

2022/1/21

Help Creating Level Progression/Second Level Locking

Monk_08 Monk_08

2022/1/21

#
I am Showing the player all 10 Levels so he can choose any level but he first should have to win the previous level to unlock it how would I achieve this :) Code Of The Start_Menu_Screen
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Start_Menu_Screen here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Start_Menu_Screen extends World
{
    /**
     * Constructor for objects of class Start_Menu_Screen.
     * 
     */

    public Start_Menu_Screen()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(700, 500, 1);
        setBackground("StartScreen.png");
        buttonAdd();
    }

    public void buttonAdd()
    {
        addObject(Level1, 110, 125);
        Level1.setImage("1.png");

        addObject(Level2, 185, 125);
        Level2.setImage("2.png");

        addObject(Level3, 260, 125);
        Level3.setImage("3.png");

        addObject(Level4, 335, 125);
        Level4.setImage("4.png");

        addObject(Level5, 410, 125);
        Level5.setImage("5.png");

        addObject(Level6, 485, 125);
        Level6.setImage("6.png");

        addObject(Level7, 560, 125);
        Level7.setImage("7.png");

        addObject(Level8, 110, 200);
        Level8.setImage("8.png");

        addObject(Level9, 185, 200);
        Level9.setImage("9.png");

        addObject(Level10, 260, 200);
        Level10.setImage("10.png");
    }

    boolean mouseDown;

    Button Level1 = new Button("load.png"); 
    Button Level2 = new Button("load.png");
    Button Level3 = new Button("load.png");
    Button Level4 = new Button("load.png");
    Button Level5 = new Button("load.png");
    Button Level6 = new Button("load.png");
    Button Level7 = new Button("load.png");
    Button Level8 = new Button("load.png");
    Button Level9 = new Button("load.png");
    Button Level10 = new Button("load.png");

    public void act()
    {
        knopfdruck();
    }

    private void knopfdruck()
    {
        //Level 1 Button/Animation
        if (Greenfoot.mouseClicked(Level1)) {
            mouseDown = false;
            Level1.setLocation(110, 125);
            Greenfoot.delay(10);
            Greenfoot.setWorld(new TLevel1());
        }
        if (Greenfoot.mouseMoved(Level1))
        {
            Level1.setLocation(110, 115);
        }
        else
        {
            if (Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(Level1)){
                Level1.setLocation(110, 125);
            }
        }
        
        
        //Level 2 Button/Animation
        if (Greenfoot.mouseClicked(Level2)) {
            mouseDown = false;
            Level2.setLocation(185, 125);
            Greenfoot.delay(10);
            Greenfoot.setWorld(new TLevel2());
        }
        if (Greenfoot.mouseMoved(Level2))
        {
            Level2.setLocation(185, 115);
        }
        else
        {
            if (Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(Level2)){
                Level2.setLocation(185, 125);
            }
        }
        
        
        //Level 3 Button/Animation
        if (Greenfoot.mouseClicked(Level3)) {
            mouseDown = false;
            Level3.setLocation(260, 125);
            Greenfoot.delay(10);
            Greenfoot.setWorld(new TLevel3());
        }
        if (Greenfoot.mouseMoved(Level3))
        {
            Level3.setLocation(260, 115);
        }
        else
        {
            if (Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(Level3)){
                Level3.setLocation(260, 125);
            }
        }
        
        
        //Level 4 Button/Animation
        if (Greenfoot.mouseClicked(Level4)) {
            mouseDown = false;
            Level4.setLocation(335, 125);
            Greenfoot.delay(10);
            Greenfoot.setWorld(new TLevel4());
        }
        if (Greenfoot.mouseMoved(Level4))
        {
            Level4.setLocation(335, 115);
        }
        else
        {
            if (Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(Level4)){
                Level4.setLocation(335, 125);
            }
        }
        
        
        //Level 5 Button/Animation
        if (Greenfoot.mouseClicked(Level5)) {
            mouseDown = false;
            Level5.setLocation(410, 125);
            Greenfoot.delay(10);
            Greenfoot.setWorld(new Level5());
        }
        if (Greenfoot.mouseMoved(Level5))
        {
            Level5.setLocation(410, 115);
        }
        else
        {
            if (Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(Level5)){
                Level5.setLocation(410, 125);
            }
        }
        
        
        //Level 6 Button/Animation
        if (Greenfoot.mouseClicked(Level6)) {
            mouseDown = false;
            Level6.setLocation(485, 125);
            Greenfoot.delay(10);
            Greenfoot.setWorld(new Level6());
        }
        if (Greenfoot.mouseMoved(Level6))
        {
            Level6.setLocation(485, 115);
        }
        else
        {
            if (Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(Level6)){
                Level6.setLocation(485, 125);
            }
        }
        
        
        //Level 7 Button/Animation
        if (Greenfoot.mouseClicked(Level7)) {
            mouseDown = false;
            Level7.setLocation(560, 125);
            Greenfoot.delay(10);
            Greenfoot.setWorld(new TLevel1());
        }
        if (Greenfoot.mouseMoved(Level7))
        {
            Level7.setLocation(560, 115);
        }
        else
        {
            if (Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(Level7)){
                Level7.setLocation(560, 125);
            }
        }
        
        
        //Level 8 Button/Animation
        if (Greenfoot.mouseClicked(Level8)) {
            mouseDown = false;
            Level8.setLocation(110, 190);
            Greenfoot.delay(10);
            Greenfoot.setWorld(new TLevel1());
        }
        if (Greenfoot.mouseMoved(Level8))
        {
            Level8.setLocation(110, 190);
        }
        else
        {
            if (Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(Level8)){
                Level8.setLocation(110, 200);
            }
        }
        
        
        //Level 9 Button/Animation
        if (Greenfoot.mouseClicked(Level9)) {
            mouseDown = false;
            Level9.setLocation(185, 190);
            Greenfoot.delay(10);
            Greenfoot.setWorld(new TLevel1());
        }
        if (Greenfoot.mouseMoved(Level9))
        {
            Level9.setLocation(185, 190);
        }
        else
        {
            if (Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(Level9)){
                Level9.setLocation(185, 200);
            }
        }
        
        
        //Level 10 Button/Animation
        if (Greenfoot.mouseClicked(Level10)) {
            mouseDown = false;
            Level10.setLocation(260, 190);
            Greenfoot.delay(10);
            Greenfoot.setWorld(new TLevel1());
        }
        if (Greenfoot.mouseMoved(Level10))
        {
            Level10.setLocation(260, 190);
        }
        else
        {
            if (Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(Level10)){
                Level10.setLocation(260, 200);
            }
        }
    }
}
Code of my First and second level
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Die Klasse PushyWorld
 * 
 * @author Rudolf Silberberg 
 * @version 2018-09-06
 */
public class TLevel1 extends MyWorld
{
    /**
     * Constructor for objects of class PushyWorld.
     * 
     */
    public TLevel1()
    {    
        super(20, 15, 30); //600 x 450 px
        level1();
    }
    
    public void act()
    {
        
    }
    
    private void level1() {
        randDerWelten0Bis2();
        Pushy pushy = new Pushy();
        addObject(pushy, 6, 5);
        Ausgang ausgang = new Ausgang();
        addObject(ausgang,13,5);
    }

    private void randDerWelten0Bis2() {
        Wand wand = new Wand();
        addObject(wand, 4, 11);
        Wand wand2 = new Wand();
        addObject(wand2, 4, 10);
        Wand wand3 = new Wand();
        addObject(wand3, 4, 9);
        Wand wand4 = new Wand();
        addObject(wand4, 4, 8);
        Wand wand5 = new Wand();
        addObject(wand5, 4, 7);
        Wand wand6 = new Wand();
        addObject(wand6, 4, 6);
        Wand wand7 = new Wand();
        addObject(wand7, 4, 5);
        Wand wand8 = new Wand();
        addObject(wand8, 4, 4);
        Wand wand9 = new Wand();
        addObject(wand9, 4, 3);
        Wand wand10 = new Wand();
        addObject(wand10, 5, 3);
        Wand wand11 = new Wand();
        addObject(wand11, 6, 3);
        Wand wand12 = new Wand();
        addObject(wand12, 7, 3);
        Wand wand13 = new Wand();
        addObject(wand13, 8, 3);
        Wand wand14 = new Wand();
        addObject(wand14, 8, 4);
        Wand wand15 = new Wand();
        addObject(wand15, 8, 5);
        Wand wand16 = new Wand();
        addObject(wand16, 8, 6);
        Wand wand17 = new Wand();
        addObject(wand17, 8, 7);
        Wand wand18 = new Wand();
        addObject(wand18, 8, 8);
        Wand wand19 = new Wand();
        addObject(wand19, 9, 8);
        Wand wand20 = new Wand();
        addObject(wand20, 10, 8);
        Wand wand21 = new Wand();
        addObject(wand21, 11, 8);
        Wand wand22 = new Wand();
        addObject(wand22, 11, 7);
        Wand wand23 = new Wand();
        addObject(wand23, 11, 6);
        Wand wand24 = new Wand();
        addObject(wand24, 11, 5);
        Wand wand25 = new Wand();
        addObject(wand25, 11, 4);
        Wand wand26 = new Wand();
        addObject(wand26, 11, 3);
        Wand wand27 = new Wand();
        addObject(wand27, 12, 3);
        Wand wand28 = new Wand();
        addObject(wand28, 13, 3);
        Wand wand29 = new Wand();
        addObject(wand29, 14, 3);
        Wand wand30 = new Wand();
        addObject(wand30, 15, 3);
        Wand wand31 = new Wand();
        addObject(wand31, 15, 4);
        Wand wand32 = new Wand();
        addObject(wand32, 15, 5);
        Wand wand33 = new Wand();
        addObject(wand33, 15, 6);
        Wand wand34 = new Wand();
        addObject(wand34, 15, 7);
        Wand wand35 = new Wand();
        addObject(wand35, 15, 8);
        Wand wand36 = new Wand();
        addObject(wand36, 15, 9);
        Wand wand37 = new Wand();
        addObject(wand37, 15, 10);
        Wand wand38 = new Wand();
        addObject(wand38, 15, 11);
        Wand wand39 = new Wand();
        addObject(wand39, 14, 11);
        Wand wand40 = new Wand();
        addObject(wand40, 13, 11);
        Wand wand41 = new Wand();
        addObject(wand41, 12, 11);
        Wand wand42 = new Wand();
        addObject(wand42, 11, 11);
        Wand wand43 = new Wand();
        addObject(wand43, 10, 11);
        Wand wand44 = new Wand();
        addObject(wand44, 9, 11);
        Wand wand45 = new Wand();
        addObject(wand45, 8, 11);
        Wand wand46 = new Wand();
        addObject(wand46, 7, 11);
        Wand wand47 = new Wand();
        addObject(wand47, 6, 11);
        Wand wand48 = new Wand();
        addObject(wand48, 5, 11);
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Die Klasse PushyWorld
 * 
 * @author Rudolf Silberberg 
 * @version 2018-09-06
 */
public class TLevel2 extends MyWorld
{
    /**
     * Constructor for objects of class PushyWorld.
     * 
     */
    public TLevel2()
    {    
        super(20, 15, 30);
        level2();
    }
    
    private void level2() {
        randDerWelten0Bis2();
        Ausgang ausgang = new Ausgang();
        addObject(ausgang, 13, 5);
        Pushy pushy = new Pushy();
        addObject(pushy, 6, 5);
        Diamant diamant = new Diamant();
        addObject(diamant,6,9);
    }

    private void randDerWelten0Bis2() {
        Wand wand = new Wand();
        addObject(wand, 4, 11);
        Wand wand2 = new Wand();
        addObject(wand2, 4, 10);
        Wand wand3 = new Wand();
        addObject(wand3, 4, 9);
        Wand wand4 = new Wand();
        addObject(wand4, 4, 8);
        Wand wand5 = new Wand();
        addObject(wand5, 4, 7);
        Wand wand6 = new Wand();
        addObject(wand6, 4, 6);
        Wand wand7 = new Wand();
        addObject(wand7, 4, 5);
        Wand wand8 = new Wand();
        addObject(wand8, 4, 4);
        Wand wand9 = new Wand();
        addObject(wand9, 4, 3);
        Wand wand10 = new Wand();
        addObject(wand10, 5, 3);
        Wand wand11 = new Wand();
        addObject(wand11, 6, 3);
        Wand wand12 = new Wand();
        addObject(wand12, 7, 3);
        Wand wand13 = new Wand();
        addObject(wand13, 8, 3);
        Wand wand14 = new Wand();
        addObject(wand14, 8, 4);
        Wand wand15 = new Wand();
        addObject(wand15, 8, 5);
        Wand wand16 = new Wand();
        addObject(wand16, 8, 6);
        Wand wand17 = new Wand();
        addObject(wand17, 8, 7);
        Wand wand18 = new Wand();
        addObject(wand18, 8, 8);
        Wand wand19 = new Wand();
        addObject(wand19, 9, 8);
        Wand wand20 = new Wand();
        addObject(wand20, 10, 8);
        Wand wand21 = new Wand();
        addObject(wand21, 11, 8);
        Wand wand22 = new Wand();
        addObject(wand22, 11, 7);
        Wand wand23 = new Wand();
        addObject(wand23, 11, 6);
        Wand wand24 = new Wand();
        addObject(wand24, 11, 5);
        Wand wand25 = new Wand();
        addObject(wand25, 11, 4);
        Wand wand26 = new Wand();
        addObject(wand26, 11, 3);
        Wand wand27 = new Wand();
        addObject(wand27, 12, 3);
        Wand wand28 = new Wand();
        addObject(wand28, 13, 3);
        Wand wand29 = new Wand();
        addObject(wand29, 14, 3);
        Wand wand30 = new Wand();
        addObject(wand30, 15, 3);
        Wand wand31 = new Wand();
        addObject(wand31, 15, 4);
        Wand wand32 = new Wand();
        addObject(wand32, 15, 5);
        Wand wand33 = new Wand();
        addObject(wand33, 15, 6);
        Wand wand34 = new Wand();
        addObject(wand34, 15, 7);
        Wand wand35 = new Wand();
        addObject(wand35, 15, 8);
        Wand wand36 = new Wand();
        addObject(wand36, 15, 9);
        Wand wand37 = new Wand();
        addObject(wand37, 15, 10);
        Wand wand38 = new Wand();
        addObject(wand38, 15, 11);
        Wand wand39 = new Wand();
        addObject(wand39, 14, 11);
        Wand wand40 = new Wand();
        addObject(wand40, 13, 11);
        Wand wand41 = new Wand();
        addObject(wand41, 12, 11);
        Wand wand42 = new Wand();
        addObject(wand42, 11, 11);
        Wand wand43 = new Wand();
        addObject(wand43, 10, 11);
        Wand wand44 = new Wand();
        addObject(wand44, 9, 11);
        Wand wand45 = new Wand();
        addObject(wand45, 8, 11);
        Wand wand46 = new Wand();
        addObject(wand46, 7, 11);
        Wand wand47 = new Wand();
        addObject(wand47, 6, 11);
        Wand wand48 = new Wand();
        addObject(wand48, 5, 11);
    }
}
And The Code of Pushy the player
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Die Klasse Pushy
 * 
 * @author Rudolf Silberberg
 * @version 2018-09-06
 */
public class Pushy extends Movable
{
    /**
     * Act - do whatever the Pushy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
        pickUp();
        gameEnd();
    }    

    /**
     * Es wird überprüft, ob eine der Pfeiltasten 
     * gedrückt wurde und der Pushy falls möglich
     * in die entsprechende Richtung bewegt.
     */
    private void move(){
        if(Greenfoot.isKeyDown("w"))
        {
            Kiste k = (Kiste)getOneObjectAtOffset(0, -1, Kiste.class);
            if(k != null)
            {
                k.move('w');
            }
            move('w');
        }
        else if(Greenfoot.isKeyDown("a")){
            Kiste k = (Kiste)getOneObjectAtOffset(-1, 0, Kiste.class);
            if(k != null)
            {
                k.move('a');
            }
            move('a');
        }
        else if(Greenfoot.isKeyDown("s")){
            Kiste k = (Kiste)getOneObjectAtOffset(0, 1, Kiste.class);
            if(k != null)
            {
                k.move('s');
            }
            move('s');
        }
        else if(Greenfoot.isKeyDown("d")){
            Kiste k = (Kiste)getOneObjectAtOffset(1, 0, Kiste.class);
            if(k != null)
            {
                k.move('d');
            }
            move('d');
        }
    }

    /**
     * Wenn Pushy auf einen Diamanten tritt,
     * hebt es diesen auf.
     */
    private void pickUp()
    { Actor Diamant = getOneObjectAtOffset(0, 0, Diamant.class);
        if(Diamant != null)
        {
            getWorld().removeObject(Diamant);
        }
    }

    /**
     * Pushy verlä die Welt, wenn es auf einen
     * Ausgang tritt. In der act-Methode der Klasse 
     * PushyWorld wird dies erkannt und das Spiel beendet.
     */
    private void gameEnd()
    {
        Actor Ausgang = getOneObjectAtOffset(0, 0, Ausgang.class);
        Schlussbild s = new Schlussbild();
        if(Ausgang != null) {
            if(getWorld().getObjects(Diamant.class).size() <1) //Ich Hätte natürlich auch die count Methode aus der welt benutzen können habe ich aber übersehen 
            {
                getWorld().addObject(s ,10 ,7);
                s.setImage("gewonnen.png");
                MyWorld.levelCheck = true;
                getWorld().removeObject(this);
                Greenfoot.delay(5);
                Greenfoot.setWorld(new Start_Menu_Screen());
            }

            else 
            {
                getWorld().addObject(s ,10 ,7);
                s.setImage("verloren.png");
                getWorld().removeObject(this);
            }
        }
    }
}
Monk_08 Monk_08

2022/1/21

#
The Image of my Start Screen and classes https://prnt.sc/26gutjy
danpost danpost

2022/1/21

#
Monk_08 wrote...
I am Showing the player all 10 Levels so he can choose any level but he first should have to win the previous level to unlock it how would I achieve this :)
You could use a static field (possibly in your MyWorld class) to track the number of levels completed and use it to regulate which buttons are active and can be used to proceed to its level. When highest accessible level is completed, increment the field to unlock the next level. A non-static field could be used in your MyWorld class to indicate the currently active level number. Set its value appropriately in each of the level worlds constructors. This would make things easier to test highest level completion.
Monk_08 Monk_08

2022/1/23

#
How would I set A non-static field in each of the levels worlds constructors Thanks :)
danpost danpost

2022/1/23

#
Monk_08 wrote...
How would I set A non-static field in each of the levels worlds constructors Thanks :)
In MyWorld class, add field:
protected int currentLevel;
with a method to get its value:
public int getCurrentLevel()
{
    return currentLevel;
}
In, for example, Level1 world (which extends MyWorld), set its value appropriately:
public Level1()
{
    super(20, 15, 30);
    currentLevel = 1;
    // etc.
}
Monk_08 Monk_08

2022/1/23

#
i get an error incompatible types: int cannot be converted to MyWorld
danpost danpost

2022/1/23

#
Monk_08 wrote...
i get an error incompatible types: int cannot be converted to MyWorld
Show what you tried. Need to see everything related to that particular code.
Monk_08 Monk_08

2022/1/23

#
ohh sorry dint see this
protected int currentLevel;

public int getCurrentLevel()
{
    return currentLevel;
}
You need to login to post a reply.