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

2011/12/30

fixing my score board

1
2
3
programmer22 programmer22

2011/12/30

#
i have a score board but i want it to say GAME OVER SCORE(***) TRY AGAIN? it just says game over score 100 atm without the try again and i cant get it to apear at the end of the game heres the code maybe someone can tell me what is wrong and what needs to be fixed import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Color; import java.awt.Font; import java.util.Calendar; /** * The ScoreBoard is used to display results on the screen. It can display some * text and several numbers. * * @author M Kolling * @version 1.0 */ public class ScoreBoard extends Actor { public static final float FONT_SIZE = 48.0f; public static final int WIDTH = 400; public static final int HEIGHT = 300; /** * Create a score board with dummy result for testing. */ public ScoreBoard() { this(100); } /** * Create a score board for the final result. */ public ScoreBoard(int score) { makeImage("Game Over", "Score: ", score); } /** * Create a score board for any message. */ public ScoreBoard(String txt) { makeTxt(txt); } /** * Write anything on the board */ private void makeTxt(String txt) { GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT); image.setColor(new Color(0, 0, 0, 160)); image.fillRect(0, 0, WIDTH, HEIGHT); image.setColor(new Color(255, 255, 255, 100)); image.fillRect(5, 5, WIDTH-10, HEIGHT-10); Font font = image.getFont(); font = font.deriveFont(FONT_SIZE); image.setFont(font); image.setColor(Color.WHITE); image.drawString(txt, 60, 100); setImage(image); } /** * Make the score board image. */ private void makeImage(String title, String prefix, int score) { GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT); image.setColor(new Color(0, 0, 0, 160)); image.fillRect(0, 0, WIDTH, HEIGHT); image.setColor(new Color(255, 255, 255, 100)); image.fillRect(5, 5, WIDTH-10, HEIGHT-10); Font font = image.getFont(); font = font.deriveFont(FONT_SIZE); image.setFont(font); image.setColor(Color.WHITE); image.drawString(title, 60, 100); image.drawString(prefix + score, 60, 200); setImage(image); } }
darkmist255 darkmist255

2011/12/30

#
I'm not too familiar with color or font, but in your code I don't see the word "TRY AGAIN?" anywhere, could that be it?
programmer22 programmer22

2011/12/30

#
well, (this may sound noob) im not sure where to put try again at >.>
darkmist255 darkmist255

2011/12/30

#
I think that you should try this:
private void makeImage(String title, String prefix, int score)
{
GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT);
image.setColor(new Color(0, 0, 0, 160));
image.fillRect(0, 0, WIDTH, HEIGHT);
image.setColor(new Color(255, 255, 255, 100));
image.fillRect(5, 5, WIDTH-10, HEIGHT-10);
Font font = image.getFont();
font = font.deriveFont(FONT_SIZE);
image.setFont(font);
image.setColor(Color.WHITE);
image.drawString(title, 60, 100);
image.drawString(prefix + score, 60, 200); 
image.drawString(TRY AGAIN?, 60, 300);  // THIS IS THE NEW CODE
setImage(image);
}
programmer22 programmer22

2011/12/30

#
i added the new line of code in and it said it expected a ) after try
darkmist255 darkmist255

2011/12/30

#
Maybe try this: earlier in the code declare a variable String tryAgain = "TRY AGAIN?"; Then change line 14 to image.drawString(tryAgain, 60, 300);
darkmist255 darkmist255

2011/12/30

#
Or just try image.drawString("TRY AGAIN?", 60, 300);
programmer22 programmer22

2011/12/30

#
im sorry still new to green foot but how would i declare it a varible can u give an example ?
danpost danpost

2011/12/30

#
I was about to post the same fix, but also changing y-offsets in darmist255's lines 12 through 14 to 75, 150, and 225 (instead of 100, 200, and 300: since the height of the image is 300).
darkmist255 darkmist255

2011/12/30

#
That's okay :D! It's a good thing to learn, to declare the variable, make this your code
    private void makeImage(String title, String prefix, int score)  
    {  
String tryAgain = "TRY AGAIN?"; // <-- This is the variable
    GreenfootImage image = new  GreenfootImage(WIDTH, HEIGHT);  // <-- This was also declaring a variable
    image.setColor(new Color(0, 0, 0, 160));  
    image.fillRect(0, 0, WIDTH, HEIGHT);  
    image.setColor(new Color(255, 255, 255, 100));  
    image.fillRect(5, 5, WIDTH-10, HEIGHT-10);  
    Font font = image.getFont();  
    font = font.deriveFont(FONT_SIZE);  
    image.setFont(font);  
    image.setColor(Color.WHITE);  
    image.drawString(title, 60, 100);  
    image.drawString(prefix + score, 60, 200);   
    image.drawString(TRY AGAIN?, 60, 300);  // THIS IS THE NEW CODE  
    setImage(image);  
    }  
programmer22 programmer22

2011/12/30

#
so variables apply to all games also same error heres what i haveimport greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Color; import java.awt.Font; import java.util.Calendar; /** * The ScoreBoard is used to display results on the screen. It can display some * text and several numbers. * * @author M Kolling * @version 1.0 */ public class ScoreBoard extends Actor { public static final float FONT_SIZE = 48.0f; public static final int WIDTH = 400; public static final int HEIGHT = 300; /** * Create a score board with dummy result for testing. */ public ScoreBoard() { this(100); } /** * Create a score board for the final result. */ public ScoreBoard(int score) { makeImage("Game Over", "Score: ", score); } /** * Create a score board for any message. */ public ScoreBoard(String txt) { makeTxt(txt); } /** * Write anything on the board */ private void makeTxt(String txt) { GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT); image.setColor(new Color(0, 0, 0, 160)); image.fillRect(0, 0, WIDTH, HEIGHT); image.setColor(new Color(255, 255, 255, 100)); image.fillRect(5, 5, WIDTH-10, HEIGHT-10); Font font = image.getFont(); font = font.deriveFont(FONT_SIZE); image.setFont(font); image.setColor(Color.WHITE); image.drawString(txt, 60, 100); setImage(image); } /** * Make the score board image. */ private void makeImage(String title, String prefix, int score) { String tryAgain = "TRY AGAIN?"; GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT); image.setColor(new Color(0, 0, 0, 160)); image.fillRect(0, 0, WIDTH, HEIGHT); image.setColor(new Color(255, 255, 255, 100)); image.fillRect(5, 5, WIDTH-10, HEIGHT-10); Font font = image.getFont(); font = font.deriveFont(FONT_SIZE); image.setFont(font); image.setColor(Color.WHITE); image.drawString(title, 60, 75); image.drawString(prefix + score, 60, 150); image.drawString(TRY AGAIN?, 60, 225); // THIS IS THE NEW CODE setImage(image); } } says ')' expected after try
darkmist255 darkmist255

2011/12/30

#
Also, here's an actual explanation not just an example: To declare a variable: before the code that uses the variable (so usually the start of your class file) you declare them. Standard declaration: (variable type) (variable name) = (value); ex. int health = 10; int is the type (integer), health is name, 10 is how much health. more on variable types here
programmer22 programmer22

2011/12/30

#
oh ok thanks for the explanation
programmer22 programmer22

2011/12/30

#
also it still has the error it says ')' expected after TRY
darkmist255 darkmist255

2011/12/30

#
You'll get used to it, it's very helpful in Java. Ask me anything more if you don't understand, I can give you much more detail and more examples :D!
There are more replies on the next page.
1
2
3