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

2020/2/16

Help With Creating Several Levels and Creating A counter that continues in Different Worlds

Happycrusher764 Happycrusher764

2020/2/16

#
Hi. How can I create several different levels and create a counter that continues to count in several different worlds. Also i am brand new to coding and greenfoot so I would appreciate it if you told me where I have to add it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public StartScreen()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600 , 1);
        prepare();
    }
 
    public void act()
    {
        if(Greenfoot.isKeyDown("enter"))
            Greenfoot.setWorld(new MyWorld());
 
    }
 
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        TitleScreen titleScreen = new TitleScreen();
        addObject(titleScreen,377,313);
    }
that is my title screen.
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
Counter counter = new Counter();
    //1.5.5 brings cleaner movement coding for the Turtle and a Score Counter that will function in next update.
    //This new update features radical changes to the whole code such as GameOver and new class.
    /**
     * Constructor for objects of class MyWorld.
     *
     */
    public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1);
        prepare();
    }
       
    public Counter getCounter()
    {
           return counter;
    }
     
     
     
     
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        addObject(counter, 100, 40);
        Strawberry strawberry = new Strawberry();
        addObject(strawberry,263,204);
        Strawberry strawberry2 = new Strawberry();
        addObject(strawberry2,441,153);
        Strawberry strawberry3 = new Strawberry();
        addObject(strawberry3,451,321);
        Strawberry strawberry4 = new Strawberry();
        addObject(strawberry4,197,306);
        Strawberry strawberry5 = new Strawberry();
        addObject(strawberry5,87,139);
        Strawberry strawberry6 = new Strawberry();
        addObject(strawberry6,275,78);
        Bee bee = new Bee();
        addObject(bee,331,92);
        Bee bee2 = new Bee();
        addObject(bee2,273,311);
        Bee bee3 = new Bee();
        addObject(bee3,57,52);
        Turtle turtle = new Turtle();
        addObject(turtle,561,41);
        Strawberry strawberry7 = new Strawberry();
        addObject(strawberry7,451,470);
        Strawberry strawberry8 = new Strawberry();
        addObject(strawberry8,669,481);
        Strawberry strawberry9 = new Strawberry();
        addObject(strawberry9,667,302);
        Strawberry strawberry10 = new Strawberry();
        addObject(strawberry10,125,510);
        Strawberry strawberry11 = new Strawberry();
        addObject(strawberry11,304,508);
        Bee bee4 = new Bee();
        addObject(bee4,566,478);
         
    }
that is level 1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int score = 0
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        setImage(new GreenfootImage("Score: " + score, 24, Color.GREEN, Color.BLACK));
    }
     
    public void addScore()
    {
         score++;
    }
That is my counter code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void act()
    {
             
        move(Greenfoot.getRandomNumber(10));
        if (isAtEdge())
    {
        if (Greenfoot.getRandomNumber(100) <100)
    {
        turn(180);
    }
}
          if (Greenfoot.getRandomNumber(100) <20)
    {
        turn(Greenfoot.getRandomNumber(20));
    }
    }
That is my bee code (the enemy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public void act()
    {
    HitEnemy();
            if (Greenfoot.isKeyDown ("a"))
    {
        turn (-3);
    }
     if (Greenfoot.isKeyDown ("d"))
    {
        turn (3);
    }
     if (Greenfoot.isKeyDown ("s"))
    {
        move (-3);
    
     if (Greenfoot.isKeyDown ("w"))
    {
        move (3);
    }
     
    }
That is my turtle (the character the player controls)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void act()
    {
     HitEnemy();
    }  
    public void HitEnemy()
    {
         if (isTouching (Turtle.class))
         {
             
             World myWorld = getWorld();
             MyWorld space = (MyWorld)myWorld;
             Counter counter = space.getCounter();
             counter.addScore();
             getWorld().removeObject (this);
 
            }
    }
That is my strawberry code (the thing the counter is counting and what the turtle eats.)
1
2
3
4
public void act()
    {
        // Add your action code here.
    }
My code for Title Screen Text (theres nothing there.) If you have any advice for the game in general that would be much appreciated. Im also trying to add a You Win pop up on a level (Ive only made 1 level so far but on like level 5 I hope to make a You Win and stuff.
Happycrusher764 Happycrusher764

2020/2/16

#
All my code works so far by the way. I just need some help taking it to the next level
Happycrusher764 Happycrusher764

2020/2/16

#
If you want to play my game to see how it works, I have uploaded the game to Green foot. The Scenario is called Yummy Strawberries v.2.0.0. You can also download the scenario there (Open in Greenfoot.)
Happycrusher764 Happycrusher764

2020/2/16

#
can someone pls reply. i need help
danpost danpost

2020/2/16

#
Happycrusher764 wrote...
All my code works so far by the way. I just need some help taking it to the next level
By replacing lines 8 thru 13 in your MyWorld class with the following and using the (new) newLevel method, your counter should continue between levels:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void newLevel()
{
    Greenfoot.setWorld(new MyWorld(counter));
}
 
public MyWorld(Counter counter)
{
    super(800, 600, 1);
    this.counter = counter;
    prepare();
}
 
public MyWorld()
{
    this(counter);
}
Happycrusher764 Happycrusher764

2020/2/16

#
u mean this code?
1
2
3
4
5
public MyWorld()
  {   
      // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
      super(800, 600, 1);
      prepare();
Happycrusher764 Happycrusher764

2020/2/16

#
@danpost wouldnt that change the size of the scenario? Im confused...
danpost danpost

2020/2/16

#
Happycrusher764 wrote...
@danpost wouldnt that change the size of the scenario? Im confused...
Nope.
Happycrusher764 Happycrusher764

2020/2/17

#
@danpost it didnt work. Came with error message "cannot reference counter before supertype constructor has been called". What does this mean?
danpost danpost

2020/2/17

#
Happycrusher764 wrote...
@danpost it didnt work. Came with error message "cannot reference counter before supertype constructor has been called". What does this mean?
Alright -- try this for my line 15:
1
this(new Counter());
You need to login to post a reply.