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

2014/6/15

Help with win and lose and level switching

Moho Moho

2014/6/15

#
i made a game and i have 5 levels when you die it switches to a game over screen and when you win and collect the coins ( the point of the game is to collect 6 golden coins each level without dying). my problem is when i beat level % (final level) i want it to switch to a you win screen which i already have. thanks in advance and if i didnt make anything clear just ask
danpost danpost

2014/6/16

#
You made everything perfectly clear. However, there is some information that is needed before any help can be given in the matter. How to proceed to 'you win' screen depends on how your levels are set up and what you have as far as a 'you win' screen. The levels could be different worlds, or not. The 'you win' screen could be a subclass of either Actor or World. The code for the actor that collects the coins will probably need posted (using the 'code' link below the 'Post a reply' box to insert the code into the post). Other things may also come into play as far as how it is to be coded.
Moho Moho

2014/6/16

#
@danpost my "you win" and 'you lose ' are both worlds with images . The way i switch from level to level is i have a counter for the coins and if the counter is equal to 6 it sets the world to level ( insert level number) all my levels are worlds and same goes to my win and lose screens if you need the code in side my player or enemy or coin ill be willing to share
danpost danpost

2014/6/16

#
You should be able to place in your player class, in the code that increments the number of golden coins collected, the following:
// after incrementing coins collected
if (/* coins collected */ == 6)
{
    if (/* is last level */) Greenfoot.setWorld(new /* you win world */());
    else Greenfoot.setWorld(new /* next level world */());
}
This may not help too much, but that is all I can give with what information was supplied. The code only takes what you are asking and puts it in a basic form of what the end result will be. For more specific code, your class names needed in the code above will need to be supplied as well as your level world class code.
danpost danpost

2014/6/16

#
Moho wrote...
The way i switch from level to level is i have a counter for the coins and if the counter is equal to 6 it sets the world to level ( insert level number) all my levels are worlds and same goes to my win and lose screens if you need the code in side my player or enemy or coin ill be willing to share
I might be a bit confused between what 'sets the world to level(insert level number)' and 'all my levels are worlds' each imply. The first part gives the impression that all levels are created from the same class. The second part gives the impression that each level has a separate class.
Moho Moho

2014/6/16

#
@danpost my levels are all under world. this is my code for my player
public static int direction = 0;
    private int verticalSpeed = 0;
    private int acceleration = 2;
    int counter = 0;

    public void act()
    {

        checkKeyPress();
        fire();
        eat();
        if (counter == 6)
        {
            Greenfoot.setWorld(new Level2());
        }
        act++;
        //This should be included in the act() method so that the game is always checking for user input.
        //Methods such as shooting a gun go here.
    }

    /**
     * This is used for responding to keyboard input by the user.
     * This will only happen when the player is far enough to the left or right of the screen for scrolling to be needed.
     */
    public void checkKeyPress()
    {

        if(Greenfoot.isKeyDown("A") )
        {
            move(-3);
        }
        if(Greenfoot.isKeyDown("D"))
        {
            move(3);
        }
        if(Greenfoot.isKeyDown("W") )
        {
            jump();
        }
        if(Greenfoot.isKeyDown("S"))
        {

            setLocation(getX(), getY()+5);

        }
    }

    public void jump()
    {
        setLocation(getX(), getY()-5);

    }
    int act = 0;

    public void fire()
    {
        if(Greenfoot.isKeyDown("space")&& act >= 35)
        {
            getWorld().addObject(new Bullet(), getX(), getY());
            act = 0;
        }

    }

    public void fall()
    {
        setLocation ( getX(), getY() + verticalSpeed);
        verticalSpeed = verticalSpeed + acceleration;
    }

    public void eat()
    {
        if (canSee(Coin.class))
        {
            eat(Coin.class);
            counter++;
        }
    }
Moho Moho

2014/6/16

#
if you can help in another cuz you seem like a very good programmer. im most likely gonna add so much more levels to my game how do i make my enemies shoot after a certain level. for example any level over level3 enemies start to shoot. if you want my enemy code here it is.
public void act() 
    {
        eat();
        move(6);
        worldEdge();
    }

    public void eat()
    {
        if (canSee(Player.class))
        {
            Greenfoot.setWorld(new Lose());
        }
    }

    public void worldEdge()
    {
        if (atWorldEdge())
        {
            turn(Greenfoot.getRandomNumber(99)+1);
        }
    }
danpost danpost

2014/6/16

#
Add a static boolean field to the Enemy class called 'canShoot'. In your initial world class constructor, add 'Enemy.canShoot = false;' and in the constructor of the world that they can first shoot, add 'Enemy.canShoot = true;'. Finally, enclose the shooting code in the Enemy class within an 'if' block whose condition asks 'if (canShoot)'.
Moho Moho

2014/6/17

#
i tried what you said and been working on it but it didnt quite work out the way i wanted it to do. it basically did nothing. can you please right the exact code i should write and where i should add it. thanks in advance. if you need my player or enemy code they are posted above
danpost danpost

2014/6/17

#
First you need to add code so the enemies can shoot at all. After that, if you coded it like in the player class with 'fire();' in the act method, then in the class of the enemy code it like 'if (canShoot) fire();'. Sidenote: I understand that the enemies will be shooting on their own -- that the 'fire' method in this class will be quite different from that which is in the player class.
danpost danpost

2014/6/17

#
Line 14 above of the player code should be replaced with something like this:
World currentWorld = getWorld();
World nextWorld = null;
if (currentWorld instanceof Level1) nextWorld = new Level2();
if (currentWorld instanceof Level2) nextWorld = new Level3();
// continue through to: if instance of last world, next world is you win world
Greenfoot.setWorld(nextWorld);
so you do not keep repeating level2.
You need to login to post a reply.