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

2022/11/19

How can I show final Score in another world?

Bob_Rafael Bob_Rafael

2022/11/19

#
I am programming a game and I want to show the final score of the game but in a different world, I am not sure how to do it..
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class MyWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    private static final String bgImageName = "space1.jpg";
    private static final int picHeight = (new GreenfootImage(bgImageName)).getHeight();
   
    private GreenfootImage bgImage, bgBase;
    private int scrollPosition = 0;
     
    HealthBar healthbar = new HealthBar();
     
    int enemyCount = 0;
    int enemy2Count = 0;
    int enemy3Count = 0;
    int healthCount = 0;
    int projectile2Spawn = 0;
    Counter counter = new Counter();
    GreenfootSound bgm = new GreenfootSound("bgm1.mp3");
    /**
     * Constructor for objects of class MyWorld.
     *
     */
     
    public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
         
        setBackground(bgImageName);
        bgImage = new GreenfootImage(getBackground());
        bgBase = new GreenfootImage(getHeight(), picHeight);
 
        prepare();
    }
     
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    public Counter getCounter()
    {
        return counter;
    }
     
    public HealthBar getHealthBar()
    {
        return healthbar;
    }
     
    public void act(){  
        paint(scrollPosition);
         
        if(Greenfoot.mouseClicked(this))
        {
            bgm.stop();
        }
        else if(!bgm.isPlaying())
        {
            bgm.setVolume(15);
            bgm.playLoop();
        }
         
        spawnEnemy1();
        spawnEnemy2();
        spawnEnemy3();
         
        spawnHealth();
        spawnProjectile2();
             
    }
     
    private void paint(int position)
    {
        int scrollAmt = 5;
        GreenfootImage bg = new GreenfootImage(getBackground());
        getBackground().drawImage(bg, 0, scrollAmt); // scroll image down
        getBackground().drawImage(bg, 0, scrollAmt-getHeight());
    }
     
     public void spawnProjectile2()
    {
        projectile2Spawn++;
        if(projectile2Spawn>2000)
        {
            addObject(new Projectile2(), Greenfoot.getRandomNumber(600),0);
            projectile2Spawn = 0;
        }
         
         
    }
     
    public void spawnHealth()
    {
        healthCount++;
        if(healthCount>1000)
        {
            addObject(new HealthUp(), Greenfoot.getRandomNumber(600),0);
            healthCount = 0;
        }
    }
     
     
         
    public void spawnEnemy1(){
        enemyCount++;
        if(enemyCount>80)
        {
            addObject(new Enemy1(), Greenfoot.getRandomNumber(600),0);
            enemyCount = 0;
        }
         
    }
     
    public void spawnEnemy2(){
        if(counter.score >= 50)
        {
            enemy2Count++;
            if(enemy2Count>200)
            {
                addObject(new Enemy2(), Greenfoot.getRandomNumber(600),0);
                enemy2Count = 0;
                 
            }
        }
         
    }
    
    public void spawnEnemy3(){
        if(counter.score >=100)
        {
            enemy3Count++;
            if(enemy3Count>350)
            {
                addObject(new Enemy3(), Greenfoot.getRandomNumber(600),0);
                enemy3Count =0;
            }
        }
         
    }
    //spawn enemy3
     
    private void prepare()
    {
        Player player = new Player();
        addObject(player,241,343);
        player.setLocation(295,372);
 
        addObject(counter,25,25);
        counter.setLocation(506,379);
         
        addObject(healthbar, 88, 378);
    }
}
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Enemy1 here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Enemy1 extends Enemies
{
    /**
     * Act - do whatever the Enemy1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int enemyHealth = 2;
    public Enemy1()
    {
        GreenfootImage image = getImage();
        image.scale(image.getWidth()-2, image.getHeight()-2);
        setImage(image);
        setRotation(0);
    }
    public void act()
    {
        // Add your action code here.
        EnemyDirection();
        hitByProjectile();
    }
     
    public void EnemyDirection()
    {
        setLocation(getX(),getY()+1);
        if(getY()==70)
        {
            enemyFire();
        }
    }
     
    public void removeEnemy(){
        if(getY()==399){
            getWorld().removeObject(this);
        }
    }
     
    public void hitByProjectile()
    {
        Actor playerProjectile = getOneIntersectingObject(playerProjectile.class);
        Actor playerProjectile2 = getOneIntersectingObject(playerProjectile2.class);
        if(playerProjectile != null)
        {
            getWorld().removeObject(playerProjectile);
            enemyHealth--;
             
        }else if(playerProjectile2 != null)
        {
            getWorld().removeObject(playerProjectile2);
            enemyHealth = enemyHealth-2;
        }
        if(enemyHealth == 0)
        {
            getScoreCounter();
            getWorld().removeObject(this);
             
        }
        else if(getY()==399){
            World world  = getWorld();
            MyWorld myWorld = (MyWorld)world;
            HealthBar healthbar = myWorld.getHealthBar();
            healthbar.loseHealth();
             
            getWorld().removeObject(this);
        }
    }
     
    public void getScoreCounter()
    {
        World world = getWorld();
        MyWorld myWorld = (MyWorld)world;
        Counter counter =myWorld.getCounter();
        counter.addScore();
    }
     
    public void enemyFire()
    {
        getWorld().addObject(new enemyProjectile(),getX(),getY()+5);
    }
}
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 { int score = 0; String str = "Score: "; /** * Act - do whatever the Counter wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Counter(){ setImage(new GreenfootImage(str+ score,30,Color.BLUE,null)); } public void act() { // Add your action code here. setImage(new GreenfootImage(str+ score,30,Color.BLUE,null)); } public void addScore() { score++; } }
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class GameOver here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class GameOver extends World
{
 
    /**
     * Constructor for objects of class GameOver.
     *
     */
    public GameOver()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 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()
    {
        loseImg loseImg = new loseImg();
        addObject(loseImg,289,189);
    }
}
Spock47 Spock47

2022/11/20

#
You can pass the score directly to the constructor of GameOver:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// in MyWorld class when the game ends:
Greenfoot.setWorld(GameOver(counter));
 
// GameOver world (only lines 7 and 11 are changed from your version)
public class GameOver extends World
{
    public GameOver(final Counter score)
    {   
        super(600, 400, 1);
        prepare();
        addObject(score, 200, 100);
    }
  
    private void prepare()
    {
        loseImg loseImg = new loseImg();
        addObject(loseImg,289,189);
    }
}
Live long and prosper, Spock47
Bob_Rafael Bob_Rafael

2022/11/20

#
Nice, it works thank you
You need to login to post a reply.