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

2019/5/16

MAKE THE FINAL SCORE PER LEVEL APPEAR ON THE GAME OVER WORLD/ YOU WIN WORLD

gdrgnxxi gdrgnxxi

2019/5/16

#
Here is the code of Counter Actpr. It is a greenfoot generated Image. I want the finals score of the player to be shown when it is game over or when the player wins. HELP TO JUST DISPLAY THE FINAL SCORE :) 

import greenfoot.*; 
import java.awt.Color;

public class Counter extends Actor
{
    int score = 0;
    private GreenfootSound gain = new GreenfootSound("score.wav");
    

    public void act() 
    {
        setImage(new GreenfootImage("Score : " + score, 30, greenfoot.Color.BLACK, greenfoot.Color.WHITE));
       
    }
        

    public void gainScore(){    
            gain.play();
            score = score+1;  
        }
       
     public void foodScore(){
            gain.play();
            score = score+5;
           
    }
}          
 
gdrgnxxi gdrgnxxi

2019/5/16

#
The player scores everytime it shoots a trash actor btw. it is a shooting game
danpost danpost

2019/5/16

#
You can remove line 4 and change line 14 to:
setImage(new GreenfootImage("Score : " + score, 30, Color.BLACK, Color.WHITE));
For your issue, provide the code for the class where you have the Counter object (where "new Counter()" is located).
gdrgnxxi gdrgnxxi

2019/5/16

#
This is the first Trash class with a gainScore method (i have 3 trash  in total, 1 trash per level)

import greenfoot.*;  

public class Trash extends Actor
{
    public Trash()
    {
        GreenfootImage image = getImage();
        int myNewHeight = (int)image.getHeight() / 20;
        int myNewWidth = (int)image.getWidth() / 20;
        image.scale(myNewWidth, myNewHeight);
    }
    

    public void act() 
    {
        fall();
        getHit();
    }    
     public void fall()
    {
        setLocation(getX(), getY() + 1);
    }
    
     public void getHit()
    {
        Actor knife = getOneIntersectingObject(Knife.class);
        if(knife!= null)
            {
            getWorld().removeObject(knife);

            World realWorld = getWorld(); 
            
            RealGame levelone= (RealGame)realWorld; //accessing the counter every time trash gets hit by knife
            Counter counter = levelone.getCounter(); //get actual counter from the world
            counter.gainScore();
            
            getWorld().removeObject(this);
        }else{
            Actor Trash = getOneObjectAtOffset(0,0, Trash.class);
                if(Trash != null)getWorld();
                   {
                   if(getY() >= 599){
                       World realWorld = getWorld(); 
                        RealGame levelone= (RealGame)realWorld; //getting my realword object to the realgame type world, get everything it do
                        HealthBar healthBar = levelone.gethealthBar();
                        healthBar.loseHealth(1);
                        
                        getWorld().removeObject(this);
                } 
            }
        }
    }
       
    }

gdrgnxxi gdrgnxxi

2019/5/16

#
this is the class with foodScore method. 

import greenfoot.*; 
 
public class Chef extends Actor
{
 
    public Chef()
    {
        //resize chef
        GreenfootImage image = getImage();
        int myNewHeight = (int)image.getHeight() / 2 ;
        int myNewWidth = (int)image.getWidth() / 2;
        image.scale(myNewWidth, myNewHeight);
    }

    /**
     * Act - do whatever the Chef wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        moveChef();
        //level 1 food score methods
        getHotdog();
        getSausage();
        getKetsup();
        getMustard();
         //level 2 food score methods
        getCheese2();
        getPatty2();
        getTomato2();
        getLettuce2();
        getBuun2();
        //level3 food score methods
        getPasta3();
        getSlicedTom3();
        getSlicedsausage3();
        getOnion3();
        getGroundPork3();
        getCheese3();
    }
    
    public void moveChef(){
         if (Greenfoot.isKeyDown("RIGHT"))
        {
            move(5);
        }
        if (Greenfoot.isKeyDown("LEFT"))
        {
            move(-5);
        }
        if ("space".equals(Greenfoot.getKey()))
        {
            fire();
        }
    }
   
    public void fire()
    {
        Knife knife = new Knife();
        getWorld().addObject(knife, getX(), getY());
    }
    
    public void getHotdog()
    {
            Actor Hotdog = getOneIntersectingObject(Hotdog.class);
            if(Hotdog!= null)
            {
            World realWorld = getWorld(); 
            RealGame levelone= (RealGame)realWorld;
            
            Counter counter = levelone.getCounter(); //get actual counter from the world
            counter.foodScore();
            
            getWorld().removeObject(Hotdog);
            
            Sausage sau = new Sausage();
            realWorld.addObject(sau,668,40);
        
            }
        }
        
         public void getSausage()
    {
        Actor Sausage = getOneIntersectingObject(Sausage.class);
        if(Sausage!= null)
            {
           World realWorld = getWorld(); 
           RealGame levelone= (RealGame)realWorld;  
           
           Counter counter = levelone.getCounter(); //get actual counter from the world
           counter.foodScore();
            
           getWorld().removeObject(Sausage);
            
           Ketsup ket = new Ketsup();
           realWorld.addObject(ket,668,40);
        
            }
        }
        public void getKetsup()
        {
        Actor Ketsup = getOneIntersectingObject(Ketsup.class);
        if(Ketsup!= null)
            {
            World realWorld = getWorld(); 
            RealGame levelone= (RealGame)realWorld;
            
            Counter counter = levelone.getCounter(); //get actual counter from the world
            counter.foodScore();
            
            getWorld().removeObject(Ketsup);
            
            Mustard mus = new Mustard();
            realWorld.addObject(mus,668,40);
        
            }
        }
    
        public void getMustard()
        {
        Actor Mustard = getOneIntersectingObject(Mustard.class);
        if(Mustard!= null)
            {
            World realWorld = getWorld(); 
            RealGame levelone= (RealGame)realWorld;
            
            Counter counter = levelone.getCounter(); //get actual counter from the world
            counter.foodScore();
            
            getWorld().removeObject(Mustard);
            
            ((RealGame)getWorld()).level1stop();
            Greenfoot.setWorld(new YouWin1());
            //((YouWin1)getWorld()).youwin1Play();
            }
        }
        
        
        public void getCheese2()
         {
        Actor Cheese = getOneIntersectingObject(Cheese.class);
        if(Cheese!= null)
            {
           World realWorld = getWorld(); 
           RealGame2 leveltwo= (RealGame2)realWorld;     
           
           Counter counter = leveltwo.getCounter(); //get actual counter from the world
           counter.foodScore();
            
           getWorld().removeObject(Cheese);
            
           Patty pat = new Patty();
           realWorld.addObject(pat,456,25);
            }
        }
        
        public void getPatty2()
        {
        Actor Patty = getOneIntersectingObject(Patty.class);
        if(Patty!= null)
           {
           World realWorld = getWorld(); 
           RealGame2 leveltwo= (RealGame2)realWorld;   
           
           Counter counter = leveltwo.getCounter(); //get actual counter from the world
           counter.foodScore();
            
           getWorld().removeObject(Patty);
            
           Tomato tom = new Tomato();
           realWorld.addObject(tom,456,25);
        
        }
        }
        
        public void getTomato2()
        {
        Actor Tomato = getOneIntersectingObject(Tomato.class);
        if(Tomato!= null)
            {
           World realWorld = getWorld(); 
           RealGame2 leveltwo= (RealGame2)realWorld;    
            
            Counter counter = leveltwo.getCounter(); //get actual counter from the world
            counter.foodScore();
            
            getWorld().removeObject(Tomato);
            
            Lettuce let = new Lettuce();
            realWorld.addObject(let,250,55);   
        
            }
        }
        
        public void getLettuce2()
        {
        Actor Lettuce = getOneIntersectingObject(Lettuce.class);
        if(Lettuce!= null)
            {
           World realWorld = getWorld(); 
           RealGame2 leveltwo= (RealGame2)realWorld;    
           
            Counter counter = leveltwo.getCounter(); //get actual counter from the world
            counter.foodScore();
            
            getWorld().removeObject(Lettuce);
            
            Buun bun = new Buun();
            realWorld.addObject(bun,530,55);  
        
            }
        }
        
        public void getBuun2()
        {
            Actor Buun = getOneIntersectingObject(Buun.class);
            if(Buun!= null)
            {
           World realWorld = getWorld(); 
           RealGame2 leveltwo= (RealGame2)realWorld; 
           
            Counter counter = leveltwo.getCounter(); //get actual counter from the world
            counter.foodScore();
            
           getWorld().removeObject(Buun);
           ((RealGame2)getWorld()).level2Stop();
           
           Greenfoot.setWorld(new YouWin2());       
           
            }
        }

        public void getPasta3()
        {
           Actor Pasta = getOneIntersectingObject(Pasta.class);
           if(Pasta!= null)
           {
           World realWorld = getWorld(); 
           RealGame3 levelthree= (RealGame3)realWorld;   
           
            Counter counter = levelthree.getCounter(); //get actual counter from the world
            counter.foodScore();
            
            getWorld().removeObject(Pasta);
            
            SlicedTom sl = new SlicedTom();
            realWorld.addObject(sl,645, 30);     
            }
        }
        
        public void getSlicedTom3()
        {
           Actor SlicedTom = getOneIntersectingObject(SlicedTom.class);
           if(SlicedTom!= null)
            {
           World realWorld = getWorld(); 
           RealGame3 levelthree= (RealGame3)realWorld; 
           
            Counter counter = levelthree.getCounter(); //get actual counter from the world
            counter.foodScore();
            
            getWorld().removeObject(SlicedTom);
            
            Slicedsausage slsau = new Slicedsausage();
            realWorld.addObject(slsau,530,55);     
            }
        }
        
        public void getSlicedsausage3()
        {
           Actor Slicedsausage = getOneIntersectingObject(Slicedsausage.class);
           if(Slicedsausage!= null)
            {
           World realWorld = getWorld(); 
           RealGame3 levelthree= (RealGame3)realWorld;
           
           Counter counter = levelthree.getCounter(); //get actual counter from the world
           counter.foodScore();
            
            getWorld().removeObject(Slicedsausage);
            
            Onion oni = new Onion();
            realWorld.addObject(oni,320, 40);     
            }
        }
        
        public void getOnion3()
        {
           Actor Onion = getOneIntersectingObject(Onion.class);
           if(Onion!= null)
            {
           World realWorld = getWorld(); 
           RealGame3 levelthree= (RealGame3)realWorld;  
           
            Counter counter = levelthree.getCounter(); //get actual counter from the world
            counter.foodScore();
            
           getWorld().removeObject(Onion);
            
           GroundPork pork = new GroundPork();
            realWorld.addObject(pork,668,40);     
            }
        }
        
        public void getGroundPork3()
        {
        Actor GroundPork = getOneIntersectingObject(GroundPork.class);
        if(GroundPork!= null)
            {
           World realWorld = getWorld(); 
           RealGame3 levelthree= (RealGame3)realWorld;  
           
            Counter counter = levelthree.getCounter(); //get actual counter from the world
            counter.foodScore();
            
            getWorld().removeObject(GroundPork);
            
            Cheese3 che = new Cheese3();
            realWorld.addObject(che, 315,16);
            }
        }
        
        public void getCheese3()
        {
        Actor Cheese3 = getOneIntersectingObject(Cheese3.class);
        if(Cheese3!= null)
            {
           World realWorld = getWorld(); 
           RealGame3 levelthree= (RealGame3)realWorld;  
           
            Counter counter = levelthree.getCounter(); //get actual counter from the world
            counter.foodScore();
            
           getWorld().removeObject(Cheese3);
           Greenfoot.setWorld(new End());
           GreenfootSound end= new GreenfootSound("You win.mp3");
           end.play();
            }
        }
    }
gdrgnxxi gdrgnxxi

2019/5/16

#
My score comes from both the gainScore and foodscore method called on diff classes. thank u danpost
danpost danpost

2019/5/16

#
There are two main ways to get the score to the win world. One is to add a parameter to the win world constructors and pass either the Counter object or the int score to it. The other is to split the line that creates the win world and sets the world active up into two lines so that you gain a reference to the new win world and can work with it directly:
/**  A  */

// world constructor:

public YouWin1(Counter counter)
// or
public YouWin1(int score)

// in Chef class
Greenfoot.setWorld(new YouWin1(counter)
// or
Greenfoot.setWorld(new YouWin1(counter.score)

/**  B  */
// in Chef class
World yw1 = new YouWin1();
Greenfoot.setWorld(yw1);
yw1.addObject(counter, yw1.getWidth()/2, 350);
In A, the constructor will need to do something with the value passed to it to show the score. In B, I just transferred the counter into the new world.
You need to login to post a reply.