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

2014/5/20

How to show score with number images?

rohwedder rohwedder

2014/5/20

#
Hi all. I have 10 images, all made in gimp, which have respectively the number 0 to 9. My issue is that I know how to use the images when the score is 0 - 9 but when I have a score of 10 or more, I can't use the image, since I need to combine 2 of them suddenly. How can I use my images, to display the number 10, 11 etc. without the need of creating them seperately in gimp, but just make use of my own 0 - 9 number graphics. Hope you can help me with this issue.
danpost danpost

2014/5/20

#
If you had your number images in an array,
1
public static GreenfootImage[] numberImage = new GreenfootImage[10];
then the following method (if I did it correctly) should return the image of any non-negative number:
1
2
3
4
5
6
7
8
9
10
11
12
13
public void getNumberImage(int num)
{
    GreenfootImage built= new GreenfootImage(numberImage[num%10]);
    while ((num = num/10) > 0)
    {
        GreenfootImage additional = new GreenfootImage(numberImage[num%10]);
        GreenfootImage combined = new GreenfootImage(built.getWidth()+additional.getWidth(), built.getHeight());
        combined.drawImage(additional, 0, 0);
        combined.drawImage(built, additional.getWidth(), 0);
        built = new GreenfootImage(combined);
    }
    return built;
}
danpost danpost

2014/5/20

#
If the number appear too close to each other, add a 'gap' amount (maybe somewhere in the range of 2 to 5) to the x parts of lines 7 and 9.
rohwedder rohwedder

2014/5/22

#
Thank you I got it to work with your solution... It's much appreciated :)
You need to login to post a reply.