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

2016/12/3

How to put "You Win!!" and "Game Over!" in my game

1
2
Tryxe Tryxe

2016/12/3

#
Hello! So my game is finished and all that's missing is the "You Win" and "Game Over" part of the game. I want my game to display/show "You Win" when my Hero kills every zombie in all the levels. For example, my Hero kills every zombie in level 1, the "You win" won't show yet until he accomplishes levels 2 and 3. I want my game to display/show "Game Over" when my Hero gets eaten by a zombie. For example, my Hero gets eaten in level 1 then it will show "Game Over" same goes with levels 2 and 3.
danpost danpost

2016/12/3

#
Have your worlds check to see if the Hero object is in them -- if not, show game over and stop the scenario. Have your worlds check to see if any Zombies are in the world -- if not, then if last level, show you win and stop the scenario, else proceed to next level.
Tryxe Tryxe

2016/12/3

#
May I see the codes to accommodate that?
Tryxe Tryxe

2016/12/3

#
Oh and "You Win" and "Game Over" are actor classes right? Can anyone show me the codes on what danpost said?
danpost danpost

2016/12/3

#
Tryxe wrote...
Oh and "You Win" and "Game Over" are actor classes right?
They can be either Actor or World subclasses.
Can anyone show me the codes on what danpost said?
Use the 'isEmpty' method of the List class on the List objects returned by the 'getObjects' method of the World class, specifying the type of actors to list (Hero.class and Zombie.class) for the 'if' conditions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if (getObjects(Hero.class).isEmpty())
{
    showGameOver();
}
if (getObjects(Zombie.class).isEmpty())
{
    if (level == 3)
    {
        showYouWin();
    }
    else
    {
        levelUp();
    }
}
Then you would have to implement the various actions of 'showGameOver', 'showYouWin', and 'levelUp' (create the methods that do those things).
Tryxe Tryxe

2016/12/3

#
I made my "YouWin" and "GameOver" actors. Where do I put the 'isEmpty' in the World class or the actor classes of "YouWin" and "GameOver"
Tryxe Tryxe

2016/12/3

#
And what are the methods for 'showGameOver', 'showYouWin', and 'levelUp'? Sorry for asking too much, I'm still trying to learn this :))
Tryxe Tryxe

2016/12/3

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Level1 here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Level1 extends World
{
 
    /**
     * Constructor for objects of class Background.
     *
     */
    public Level1()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1);
    prepare();
}
/**
 * Prepare the world for the start of the program.
 * That is: create the initial objects and add them to the world.
 */
private void prepare()
{
    NextLevel nextlevel = new NextLevel();
    addObject(nextlevel,745,538);
    Hero hero = new Hero();
    addObject(hero,165,492);
    Zombie zombie = new Zombie();
    addObject(zombie,692,130);
}
public void act()
{
    getObjects();
}
public void getObjects()
{
if (getObjects(Hero.class).isEmpty())
{
showGameOver();
}
if (getObjects(Zombie.class).isEmpty())
{
if (level == 3)
{
showYouWin();
}
else
{
levelUp();
}
}
Tryxe Tryxe

2016/12/3

#
That's my code for the World, I placed the "getObjects" there. My 'YouWin' and 'GameOver' actors are still empty. I don't know the methods for the 'YouWin', 'GameOver', and 'levelUp'
danpost danpost

2016/12/3

#
Tryxe wrote...
I made my "YouWin" and "GameOver" actors. Where do I put the 'isEmpty' in the World class or the actor classes of "YouWin" and "GameOver"
The 'isEmpty' lines of code go in your World subclass(es), either in the 'act' method or a method it calls.
Tryxe wrote...
And what are the methods for 'showGameOver', 'showYouWin', and 'levelUp'? Sorry for asking too much, I'm still trying to learn this :))
Those represent the action you want to perform at those times (when you want to add the GameOver actor, the YouWin actor or create and activate a new level world. You can either replace those method calls with the actions needed or create the methods and put the actions in them.
danpost danpost

2016/12/3

#
Tryxe wrote...
That's my code for the World, I placed the "getObjects" there.
What is the 'NextLevel' actor? It seems like a strange name for an actor.
Tryxe Tryxe

2016/12/3

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Level1 here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Level1 extends World
{
 
    /**
     * Constructor for objects of class Background.
     *
     */
    public Level1()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1);
    prepare();
}
/**
 * Prepare the world for the start of the program.
 * That is: create the initial objects and add them to the world.
 */
private void prepare()
{
    NextLevel nextlevel = new NextLevel();
    addObject(nextlevel,745,538);
    Hero hero = new Hero();
    addObject(hero,165,492);
    Zombie zombie = new Zombie();
    addObject(zombie,692,130);
}
public void act()
{
    getObjects();
}
public void getObjects()
{
if (getObjects(Hero.class).isEmpty())
{
showGameOver();
}
if (getObjects(Zombie.class).isEmpty())
{
if (level == 3)
{
showYouWin();
}
else
{
levelUp();
}
}
}
}
Tryxe Tryxe

2016/12/3

#
It said it cannot find symbol for the methods of levelUp, showYouWin, and showGameOver. It cannot find variable level too in line 47
danpost danpost

2016/12/3

#
Tryxe wrote...
It said it cannot find symbol for the methods of levelUp, showYouWin, and showGameOver. It cannot find variable level too in line 47
You have to create (add) those methods to the class and you can remove lines 47 through 52 and 54 from the given code, since this is in the Level1 world.
Tryxe Tryxe

2016/12/3

#
I made some changes, I removed the 'YouWin' and 'GameOver' actors, now they are World subclasses and here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class YouWin here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class YouWin extends World
{
 
    /**
     * Constructor for objects of class YouWin.
     *
     */
    public YouWin()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
    }
    public void act()
    {
        if (getObjects(Zombie.class).isEmpty())
{
if (level == 3)
{
Greenfoot.setWorld(new YouWin());
}
else
{
levelUp();
    }
}
}
}
There are more replies on the next page.
1
2