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

2018/2/26

How to use an IF statement to store multiple levels?

sadiebean00 sadiebean00

2018/2/26

#
Hi, I am creating a game for my college assignment which requires the player to progress through levels. I have managed to create the score collection for my first level, but I am struggling to get the code to go across for my other levels. Is there a way of using an IF statement to see what level the player is on and to store the scores of the levels. Thank you
Super_Hippo Super_Hippo

2018/2/26

#
Do you want a separate score for each level or do you want the sum of all levels? If you only need the sum, the most easiest way will be that you have one world and when creating the next world (same class), you add the same score counter to the world again. (And/Or the same player if the player holds the score or permanent upgrades etc.)
sadiebean00 sadiebean00

2018/2/26

#
A sum of the score for each level ideally. That way the player knows how well they have done.
sadiebean00 sadiebean00

2018/2/26

#
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class spaceman here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class spaceman extends Actor
{
    public int Lives = 3;
    public int Counter = 0;
    public spaceman()
    {
        GreenfootImage image = getImage();
        image.scale(60,60);
        setImage(image);
    }
    /**
     * Act - do whatever the spaceman wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        moveAndTurn();
        Actor plant = getOneIntersectingObject(plant.class);
        if (plant !=null)
        {
            pickUp();
            World myWorld = getWorld();
            getWorld().removeObject(plant);
           
        }
        Actor alien = getOneIntersectingObject(alien.class);
        if (isTouching(alien.class))
        {
            hit();
            World myWorld = getWorld();
            getWorld().removeObject(alien);
        }
    }   
    public void moveAndTurn()
    {
        move(2);
        if (Greenfoot.isKeyDown("Left"))
        {
            turn(-3);
        }
        if (Greenfoot.isKeyDown("Right"))
        {
            turn(3);
        }
    }
    private void pickUp()
    {
        level1 level1World = (level1) getWorld();
        Counter counter = level1World.getCounter();
        counter.bumpCount(5);
    }
    public void hit()
    {
            level1 level1World = (level1) getWorld();
            Lives lives = level1World.getLives();
            lives.bumpCount(1);
            if (Lives == 0)
            {
                getWorld().removeObject(this);
                Greenfoot.playSound("Pacman-death-sound.wav");
                Greenfoot.setWorld(new DeathScreen());
            }
    }
}
this is my code for my spaceman class, which is collecting the plants.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 * Write a description of class counter here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    private int score = 0;
    public Counter()
    {
        setImage(new GreenfootImage("Score: 0",26, Color.WHITE, Color.BLACK));
    }   
    public void bumpCount(int amount)
    {
       score += amount;
       setImage(new GreenfootImage("Score: " + score, 26, Color.WHITE, Color.BLACK));
    }
}
this is my code for my counter class, if this helps.
Super_Hippo Super_Hippo

2018/2/26

#
There are several ways to do it. One would be to use one counter object for all worlds and use an array for the score. It displays and adjusts only the score which represents the current level's score. You can then (later when you need it) get all scores from all levels to display them.
sadiebean00 sadiebean00

2018/2/26

#
Thanks, I'll try that
You need to login to post a reply.