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

2020/3/23

need help with my scoreboard

Ac951 Ac951

2020/3/23

#
i cant et my scoreboard to set the highscore of the game. Also i build shelters in the game too and i can only place one in the world when im supposed to get 3 per game. when the lobster hits the bottom of the screen the score goes up and when i shoot the bombs its supposed t kills the lobsters and the amount of bombs goes down. PLEASE HELPPPP lobsters public class Lobstrosity extends Actor { private int touching; private int count=1; private ScoreBoard score = new ScoreBoard(); /** * Act - do whatever the Lostrosity wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { setRotation(90); move(1); if (isAtEdge()) { getWorld().addObject(new Lobstrosity(),Greenfoot.getRandomNumber(800)+1, Greenfoot.getRandomNumber(240)+40); setLocation(Greenfoot.getRandomNumber(800)+1, Greenfoot.getRandomNumber(240)+40); ((MyWorld)getWorld()).getScoreBoard().incScore(1); //((MyWorld)getWorld()).getScoreBoard().getHighScore(); if(score.getScore() > score.getHighScore()) { ((MyWorld)getWorld()).getScoreBoard(); score.setHighScore(score.getScore()); score.resetForNewGame(); } } if (isTouching(Shelter.class)==true) { ((Shelter)getOneIntersectingObject(Shelter.class)).reduceStrength(); getWorld().removeObject(this); } } Player public class Player extends Actor { private Shelter placeShelt= new Shelter(10); int Ammo = 10; int sheltersCount = 3; int initialStrength=10; private ScoreBoard score = new ScoreBoard(); /** * Act - do whatever the Player wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (Greenfoot.isKeyDown("left")) { move(-1); } if (Greenfoot.isKeyDown("right")) { move(1); } if(isTouching(Lobstrosity.class) || getWorld().getObjects(Lobstrosity.class).isEmpty()) { getWorld().removeObjects(getWorld().getObjects(null)); if(score.getScore() > score.getHighScore()) { ((MyWorld)getWorld()).getScoreBoard(); score.setHighScore(score.getScore()); score.resetForNewGame(); } } if (Greenfoot.isKeyDown("space") && placeShelt.getWorld() == null && placeShelt.hasHealth()) { placeShelt(); } /** * fire the PoisonBomb */ if ("b".equals(Greenfoot.getKey()) && getWorld().getObjects(PoisonBomb.class).isEmpty()) { if(Ammo>0) { PoisonBomb poisonbomb = new PoisonBomb(); getWorld().addObject(poisonbomb, getX(), getY()); Ammo--; ((MyWorld)getWorld()).getScoreBoard().decBombCount(); } else { } } } public void placeShelt() { if(((MyWorld)getWorld()).getScoreBoard().getShelterCount()>0) { getWorld().addObject(placeShelt, getX(), getY()-50); ((MyWorld)getWorld()).getScoreBoard().decShelterCount(); sheltersCount--; } else { } } } myWorld public class MyWorld extends World { // YOU MAY WISH/NEED TO ADD INSTANCE VARIABLE(S) HERE!!! ScoreBoard scoreboard = new ScoreBoard(); int sheltersLeft = 3; /** * 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(); act(); } private void prepare() { Player player = new Player(); addObject(player, 400, 575); addObject(new Lobstrosity(),400,Greenfoot.getRandomNumber(240)+40); } /** * getScoreBoard gives us the Scoreboard found in the world * NOTE ---> YOU ***MUST*** CODE THIS !!! * * @return the ScoreBoard object seen in the world */ public ScoreBoard getScoreBoard() { addObject(scoreboard,400, 10); return scoreboard; } public void act() { if (Greenfoot.isKeyDown("n") && getObjects(Lobstrosity.class).isEmpty()) { Player player = new Player(); addObject(player, 400, 575); scoreboard = new ScoreBoard(); addObject(scoreboard,400, 10); Lobstrosity lobstrosity = new Lobstrosity(); addObject(lobstrosity,400,Greenfoot.getRandomNumber(240)+40); } } /** * containsShelter tells us whether or not a shelter * is already in the world. * @return true if there is a shelter, false if not */ //public boolean containsShelter() //{ //} /** * containsBomb tells us whether or not a bomb * is already in the world. * @return true if there is a bomb, false if not */ public boolean containsBomb() { // fairly inelegant and inefficient way to code this // but avoids the need for code elsewhere. return (!getObjects(PoisonBomb.class).isEmpty()); } /** * lobstrosityCount tells us how man lobstrosities are * in the World. * @return the number of lobstrosities in the world */ public int lobstrosityCount() { return getObjects(Lobstrosity.class).size(); } } ScoreBoard public class ScoreBoard extends Actor { private int gameScore; // current game score private int highScore; // high score so far private int sheltersLeft; // number of shelters left private int bombsLeft; // number of bombs left // "global" representing the height (bottom to top) of a // ScoreBoard public static final int SCOREBOARD_HEIGHT = 25; /** * Constructor for ScoreBoard to build starting scoreboard. */ public ScoreBoard() { gameScore=0; // no game started yet, so score is 0 highScore=0; // no game started yet, so high score is 0 bombsLeft=10; // no game started yet, so all bombs available sheltersLeft=3; // no game started yet, so all shelters available } // Greenfoot trick to draw image ater object added to world. protected void addedToWorld(World toWorld) { redraw(); } // redraws scoreboard, updating values displayed as we go // we don't want this called from outside the class, so // it is private in this case. private void redraw() { // needs regular access to world, so store in a variable. MyWorld placedIn = (MyWorld) getWorld(); // check to see if image is valid. If not, rebuild it. GreenfootImage img = getImage(); if (img==null || img.getWidth()!= placedIn.getWidth() || img.getHeight()!=SCOREBOARD_HEIGHT) { img = new GreenfootImage(placedIn.getWidth(), SCOREBOARD_HEIGHT); setImage(img); } // fill image with gray background img.setColor(Color.LIGHT_GRAY); img.fill(); // draw scores, etc. in black img.setColor(Color.BLACK); img.drawString("Score:"+ gameScore, 600, SCOREBOARD_HEIGHT-8); img.drawString("High Score:"+ highScore, 400, SCOREBOARD_HEIGHT-8); // draw in shelter and bomb stats img.drawString("Shelters:"+ sheltersLeft, 100, SCOREBOARD_HEIGHT-8); img.drawString("Bombs:"+ bombsLeft, 300, SCOREBOARD_HEIGHT-8); } /** * incScore - adds something to score * @param byVal - value to add to the score. */ public void incScore(int byVal) { gameScore+=byVal; redraw(); } /** * getScore - returns the current score * @return the current game score */ public int getScore() { return gameScore; } /** * decBombCount - decrease the number of bombs by 1, but only * if there are any bombs left * @return true when there was at least 1 bomb left, * false otherwise */ public boolean decBombCount() { // if there are bombs left if (bombsLeft>0) { bombsLeft--; // reduce bomb count redraw(); // redraw image to see updated bomb count return true; // we succeeded! } return false; // not enough bombs to do this! } /** * decShelterCount - decrease the number of shelters by 1, but * only if there are any shelters left * @return true when there was at least 1 shelter left, * false otherwise */ public boolean decShelterCount() { // if there are shelters left if (sheltersLeft>0) { sheltersLeft--; // now there's one less shelter redraw(); // redraw to reflect change return true; // we had a shelter left! } return false; // no shlters left! } /** * getShelterCount returns how mny shelters are available * * @return the current number of shelters */ public int getShelterCount() { return sheltersLeft; } /** * getHighScore returns the current value of high score * * @return the current high score */ public int getHighScore() { return highScore; } /** * setHighScore gives high score a new value * * @param newScore the intended new value of high score */ public void setHighScore(int newScore) { highScore = newScore; // store the new high score redraw(); // redisplay scoreboard } /** * resetForNewGame resets the scoreboard data for a new game * * note that this does NOT reset high score */ public void resetForNewGame() { // games start with 3 shelters, 10 bombs, and a score of 0 sheltersLeft=3; bombsLeft=10; gameScore=0; redraw(); // redisplay the scoreboard. } } shelter public class Shelter extends Actor { private int strengthLeft; // remaining strength private int strength = 10; // dimensions of the shelter object. Note that these are // private as there is no reason to utiize these outside // of this class. private static final int SHELTER_WIDTH = 225; private static final int SHELTER_HEIGHT = 25; public void act() { /** * Constructor for a shelter. * * @param initialStrength is the starting strength of * this shelter */ } public boolean hasHealth() { return strengthLeft > 0; } public Shelter(int initialStrength) { strengthLeft = initialStrength; // remember initial strength redraw(); // redraw the shelter } // redraw simply rebuilds the image for the shelter. // note that methods outside this class should not call // redraw, so it is private. private void redraw() { // (re)build image if necessary GreenfootImage img = getImage(); if (img==null || img.getWidth()!=SHELTER_WIDTH || img.getHeight()!=SHELTER_HEIGHT) { img = new GreenfootImage(SHELTER_WIDTH, SHELTER_HEIGHT); setImage(img); } // set background to light gray img.setColor(Color.LIGHT_GRAY); img.fill(); // add text repressenting the strength left in black img.setColor(Color.BLACK); img.drawString(""+ strengthLeft, SHELTER_WIDTH/2-10, SHELTER_HEIGHT-8); } /** * reduceStrngth reduces this shelter's strength by 1 unit */ public void reduceStrength() { reduceStrength(1); // use the other reduceStrength method } /** * reduceStrength reduces the strength of the shelter by a * requested amount. * @param byVal is the amount to reduce this shelter's strength */ public void reduceStrength(int byVal) { strengthLeft-=byVal; // reduce the strength // if shelter is still strong enough to function ... if (strengthLeft>0) redraw(); // ... redisplay the shelter else // shelter not strong enough to function getWorld().removeObject(this); // remove this shelter } }
Super_Hippo Super_Hippo

2020/3/23

#
Each object creates its own ScoreBoard object. You place a shelter. When the shelter is removed from the world, it has 0 health, so ‘placeShelt.hasHealth()’ returns false and it will never add a new one.
Ac951 Ac951

2020/3/23

#
ok so i got it to do 3 shelters but shelter #2 and #3 both only have a health of 1???
Ac951 Ac951

2020/3/23

#
public class Player extends Actor { public Shelter placeShelt = new Shelter(10); int Ammo = 10; int sheltersCount = 3; int initialStrength = 10; private ScoreBoard score = new ScoreBoard(); /** * Act - do whatever the Player wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (Greenfoot.isKeyDown("left")) { move(-1); } if (Greenfoot.isKeyDown("right")) { move(1); } if(isTouching(Lobstrosity.class) || getWorld().getObjects(Lobstrosity.class).isEmpty()) { getWorld().removeObjects(getWorld().getObjects(null)); if(score.getScore() > score.getHighScore()) { ((MyWorld)getWorld()).getScoreBoard(); score.setHighScore(score.getScore()); score.resetForNewGame(); } } if (Greenfoot.isKeyDown("space") && placeShelt.getWorld() == null ) { placeShelt(); } /** * fire the PoisonBomb */ if ("b".equals(Greenfoot.getKey()) && getWorld().getObjects(PoisonBomb.class).isEmpty()) { if(Ammo>0) { PoisonBomb poisonbomb = new PoisonBomb(); getWorld().addObject(poisonbomb, getX(), getY()); Ammo--; ((MyWorld)getWorld()).getScoreBoard().decBombCount(); } else { } } } public void placeShelt() { if(sheltersCount>0) { ((MyWorld)getWorld()).addObject(placeShelt, getX(), getY()-50); ((MyWorld)getWorld()).getScoreBoard().decShelterCount(); sheltersCount--; } else { } } }
Super_Hippo Super_Hippo

2020/3/23

#
You re-add the same shelter, so it still has 0 health.
Ac951 Ac951

2020/3/23

#
im confused what should i do??
Super_Hippo Super_Hippo

2020/3/23

#
Remove this line:
private Shelter placeShelt= new Shelter(10);
Replace this:
getWorld().addObject(placeShelt, getX(), getY()-50);
with
getWorld().addObject(new Shelter(10), getX(), getY()-50);
Replace this:
if (Greenfoot.isKeyDown("space") && placeShelt.getWorld() == null )
with
if (Greenfoot.isKeyDown("space") && getWorld().getObjects(Shelter.class).isEmpty())
Ac951 Ac951

2020/3/23

#
wow Thank you!!!!! is there any way u could help me with the highscore as well???
Ac951 Ac951

2020/3/23

#
and how can i get my scoreboard to show before my lobster hits the bottom of the screen??
Ac951 Ac951

2020/3/23

#
public class ScoreBoard extends Actor { private int gameScore; // current game score private int highScore; // high score so far private int sheltersLeft; // number of shelters left private int bombsLeft; // number of bombs left // "global" representing the height (bottom to top) of a // ScoreBoard public static final int SCOREBOARD_HEIGHT = 25; /** * Constructor for ScoreBoard to build starting scoreboard. */ public ScoreBoard() { gameScore=0; // no game started yet, so score is 0 highScore=0; // no game started yet, so high score is 0 bombsLeft=10; // no game started yet, so all bombs available sheltersLeft=3; // no game started yet, so all shelters available } // Greenfoot trick to draw image ater object added to world. protected void addedToWorld(World toWorld) { redraw(); } // redraws scoreboard, updating values displayed as we go // we don't want this called from outside the class, so // it is private in this case. private void redraw() { // needs regular access to world, so store in a variable. MyWorld placedIn = (MyWorld) getWorld(); // check to see if image is valid. If not, rebuild it. GreenfootImage img = getImage(); if (img==null || img.getWidth()!= placedIn.getWidth() || img.getHeight()!=SCOREBOARD_HEIGHT) { img = new GreenfootImage(placedIn.getWidth(), SCOREBOARD_HEIGHT); setImage(img); } // fill image with gray background img.setColor(Color.LIGHT_GRAY); img.fill(); // draw scores, etc. in black img.setColor(Color.BLACK); img.drawString("Score:"+ gameScore, 600, SCOREBOARD_HEIGHT-8); img.drawString("High Score:"+ highScore, 400, SCOREBOARD_HEIGHT-8); // draw in shelter and bomb stats img.drawString("Shelters:"+ sheltersLeft, 100, SCOREBOARD_HEIGHT-8); img.drawString("Bombs:"+ bombsLeft, 300, SCOREBOARD_HEIGHT-8); } /** * incScore - adds something to score * @param byVal - value to add to the score. */ public void incScore(int byVal) { gameScore+=byVal; redraw(); } /** * getScore - returns the current score * @return the current game score */ public int getScore() { return gameScore; } /** * decBombCount - decrease the number of bombs by 1, but only * if there are any bombs left * @return true when there was at least 1 bomb left, * false otherwise */ public boolean decBombCount() { // if there are bombs left if (bombsLeft>0) { bombsLeft--; // reduce bomb count redraw(); // redraw image to see updated bomb count return true; // we succeeded! } return false; // not enough bombs to do this! } /** * decShelterCount - decrease the number of shelters by 1, but * only if there are any shelters left * @return true when there was at least 1 shelter left, * false otherwise */ public boolean decShelterCount() { // if there are shelters left if (sheltersLeft>0) { sheltersLeft--; // now there's one less shelter redraw(); // redraw to reflect change return true; // we had a shelter left! } return false; // no shlters left! } /** * getShelterCount returns how mny shelters are available * * @return the current number of shelters */ public int getShelterCount() { return sheltersLeft; } /** * getHighScore returns the current value of high score * * @return the current high score */ public int getHighScore() { return highScore; } /** * setHighScore gives high score a new value * * @param newScore the intended new value of high score */ public void setHighScore(int newScore) { highScore = newScore; // store the new high score redraw(); // redisplay scoreboard } /** * resetForNewGame resets the scoreboard data for a new game * * note that this does NOT reset high score */ public void resetForNewGame() { // games start with 3 shelters, 10 bombs, and a score of 0 sheltersLeft=3; bombsLeft=10; gameScore=0; redraw(); // redisplay the scoreboard. } } heres my scoreboard class right now
Ac951 Ac951

2020/3/23

#
public class Player extends Actor { int Ammo = 10;//number of bombs per game int sheltersCount = 3;//number of shelters per game int initialStrength = 10;//how much heath the shelters have private ScoreBoard score = new ScoreBoard();//the score of the scorebaord in the world /** * Act - do whatever the Player wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (Greenfoot.isKeyDown("left")) { move(-1);//moves player to the left } if (Greenfoot.isKeyDown("right")) { move(1);//moves player to the right } if(isTouching(Lobstrosity.class) )//|| getWorld().getObjects(Lobstrosity.class).isEmpty()) { getWorld().removeObjects(getWorld().getObjects(null));//removes all characters if(score.getScore() > score.getHighScore()) { score.setHighScore(score.getScore());//sets the highscore score.resetForNewGame();//resets the score ((MyWorld)getWorld()).getScoreBoard();//sets the scoreboard } } if (Greenfoot.isKeyDown("space") && getWorld().getObjects(Shelter.class).isEmpty() ) { placeShelt();//adds a shelter only if there is none in the world already } if ("b".equals(Greenfoot.getKey()) && getWorld().getObjects(PoisonBomb.class).isEmpty()) { //shoots the bomb is b is pressed if(Ammo>0) { PoisonBomb poisonbomb = new PoisonBomb();//adds a new bomb getWorld().addObject(poisonbomb, getX(), getY());//adds a new bomb where the b is pressed Ammo--;//subtracts from the ammo ((MyWorld)getWorld()).getScoreBoard().decBombCount();//decrease the number of boms on the scoreboard } else { //nothing because we're out of ammo } } } /** * adds a shelter to the world only if theres enough left */ public void placeShelt() { if (sheltersCount>0) { getWorld().addObject(new Shelter(10), getX(), getY()-50);//adds a new shelter ((MyWorld)getWorld()).getScoreBoard().decShelterCount();//decreases the shelter count in th scoreboard sheltersCount--;//subtracts from the shelter count } else { //nothing because we're out of shelters } } } Here is my player class
Dr.Blythe Dr.Blythe

2020/4/17

#
Little bit late at this point in time, but *ahem*, really?
You need to login to post a reply.