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

2014/9/24

Carrying score over to a game over screen

BrownBoii333 BrownBoii333

2014/9/24

#
Hi I need help displaying the final score on a game over screen, after losing all the lives available. what is the simplest way to do that without majorly changing my code so far public Scoreboard Pardeep; private Liveboard pardeep; /** * Constructor for objects of class TheMaze. * */ public TheMaze() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(35, 35, 20); pardeep = new Liveboard(); Pardeep = new Scoreboard(); addObject(Pardeep,5,1); addObject(pardeep,5,0); prepare(); setPaintOrder(Liveboard.class, Block.class) ; } public void collectFood() { updateScore(); } public void collectDeath(){ pardeep.updateLiveboard(); } public void updateScore(){ Pardeep.updateScoreboard(); } Now this is what i have in endworld, and it displays score but it's always 0. how do i make it use the final score? import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class EndWorld here. * * @author (your name) * @version (a version number or a date) */ public class EndWorld extends World { private Scoreboard Pardeep; /** * Constructor for objects of class EndWorld. * */ public EndWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); Pardeep = new Scoreboard(); addObject(Pardeep,300,200); } } this is my scoreboard class import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.*; /** * Write a description of class Scoreboard here. * * @author (your name) * @version (a version number or a date) */ public class Scoreboard extends Actor { GreenfootImage image; public int score = 0; public Scoreboard(){ image = new GreenfootImage(150,50); image.setColor(new Color(20,30,140)); image.drawString("Score: " + score, 5, 45); setImage(image); } public Scoreboard(int a){ image = new GreenfootImage(150,50); image.setColor(new Color(20,30,140)); image.drawString("Score: " + a, 5, 45); score = a; setImage(image); } public void updateScoreboard() { score++; updateImage(); } public int returnScore() { return score; } public void updateImage() { image.clear(); image.setColor(new Color(20,30,140)); image.drawString("Score: " + score, 5, 45); setImage(image); } } this is my lives class import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.*; /** * Write a description of class Liveboard here. * * @author (your name) * @version (a version number or a date) */ public class Liveboard extends Actor { GreenfootImage image; public int life = 3; private Minho minho ; public Liveboard(){ image = new GreenfootImage(150,50); image.setColor(new Color(60,179,113)); image.drawString("Lives:" + life, 5, 45); setImage(image); } public void updateLiveboard(){ life--; updateImage(); } public void updateImage() { image.clear(); image.setColor(new Color(60,179,113)); image.drawString("Lives: " + life, 5, 45); setImage(image); } public void act(){ if(life==0){ Greenfoot.setWorld(new EndWorld()); getWorld().addObject(new Scoreboard(),getWorld().getHeight()/2, getWorld().getWidth()/2); } } } and this is my player (minho) class import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * Write a description of class Minho here. * * @author (your name) * @version (a version number or a date) */ public class Minho extends Actor { private static final int EAST = 0; private static final int SOUTH = 90; private static final int WEST = 180; private static final int NORTH = 270; public int MOVE = 1; private int BurgersEaten=0; int SafeTime = 0; private int life = 3; public Minho(){ GreenfootImage img = new GreenfootImage(20,20); img.setColor(Color.RED); img.fill(); setImage(img); } public void act() { move(); eat(); imSafe(); end(); die(); // deathTown(); } public void end(){ Actor Pardeep = getOneIntersectingObject(TheEnd.class); if(Pardeep!=null&& BurgersEaten==30){ } } public void move(){ move(MOVE); if (Greenfoot.isKeyDown("right")){ setRotation(EAST); } if (Greenfoot.isKeyDown("left")){ setRotation(WEST); } if (Greenfoot.isKeyDown("up")){ setRotation(NORTH); } if (Greenfoot.isKeyDown("down")){ setRotation(SOUTH); } if(facingWall()){ MOVE=0; }else{MOVE=1; } } public void die(){ Actor Pardeep = getOneIntersectingObject(Griever.class); if(Pardeep!=null){ TheMaze tm = (TheMaze)getWorld(); tm.collectDeath(); if(life<=3){ setLocation(34,34); life--; }else if(life==0){ Greenfoot.setWorld(new EndWorld()); getWorld().addObject(new Scoreboard(),getWorld().getHeight()/2, getWorld().getWidth()/2); Greenfoot.stop(); } } } public void deathTown(){ if(life==0){ Greenfoot.setWorld(new EndWorld()); getWorld().addObject(new Scoreboard(),getWorld().getHeight()/2, getWorld().getWidth()/2); } } public boolean facingWall() { int dx =0; int dy = 0; switch(getRotation()) { case EAST:dx=1; break; case WEST: dx= -1; break; case SOUTH: dy =1; break; case NORTH: dy = -1; break; } return getOneObjectAtOffset(dx,dy,BabyMaze.class) !=null; } public void eat(){ Actor Pardeep = getOneIntersectingObject(FOOD.class); if(Pardeep!=null) { TheMaze tm = (TheMaze)getWorld(); fatBoy(); BurgersEaten++; tm.collectFood(); tm.removeObject(Pardeep); } } public void imSafe(){ Actor Pardeep = getOneIntersectingObject(MiniGlade.class); SafeTime++; if(Pardeep==null){ SafeTime=10; } if(Pardeep!=null){ if(SafeTime==10) { getWorld().removeObject(Pardeep); SafeTime=0; } else if ( SafeTime > 10 ) { SafeTime = 0; } } } public void fatBoy(){ if(BurgersEaten==5){ getWorld().addObject(new Griever(),35,0); } if(BurgersEaten==10){ getWorld().addObject(new Griever(),19,14); } if(BurgersEaten==15){ getWorld().addObject(new Griever(),11,32); } if(BurgersEaten==20){ getWorld().addObject(new Griever(),35,35); } if(BurgersEaten==25){ getWorld().addObject(new Griever(),0,0); } if(BurgersEaten==30){ getWorld().addObject(new Griever(),32,31); } } } what can I do?
Super_Hippo Super_Hippo

2014/9/24

#
First, why do you want to create the Scoreboard in both worlds? In the 'act' method of 'Liveboard', you check if the 'life' variable is 0. If it is 0, you set a new world ('Endgame', which creates a 'Scoreboard' object in its constructor) and then, you add a new 'Scoreboard' to the 'old' world. You can remove this line. You also check for dying in the 'Minho' class. You only need this once. (Normally in the class of the actor which is dying.) When setting the world, you can pass the score.
Greenfoot.setWorld(new EndWorld( (TheMaze) getWorld().Pardeep.score );
And then, in the constructor of the EndWorld:
    public EndWorld(int score)
    {    
        super(600, 400, 1); 
        Pardeep =  new Scoreboard(score);
        addObject(Pardeep,300,200);
    }
By the way, what does 'Pardeep' mean and why do you use this word for everything?
You need to login to post a reply.