Page 2
A visual index of all the effects
Page 3
See this article in english 
Page 4
Cartoon-Style layered terrains

Grass, soil and sand

Alexander Hristov

For a  4K platformer game (yes, that's 4096 bytes) I needed a typical set of grass and sand textures, but due to restrictions on the size of the code, I could not use Perlin noise (nor any other kind of smooth noise, for that matter) nor other more sophisticated and beautiful approaches. So I came up with this little snippet:



    int TILE_SIZE = 32;
    float h = 120/360f;
    float s = 186/255f;
    float b = 128/255f;
    Random rnd = new Random();
    BufferedImage tile = new BufferedImage(TILE_SIZE,TILE_SIZE, BufferedImage.TYPE_INT_RGB);
    Graphics2D gr = tile.createGraphics();
     for (int y = 0; y < TILE_SIZE; y++) {
      for (int x = 0; x< TILE_SIZE; x++) {
        float hd = (rnd.nextInt(10)-5)/360f;
        float sd = (rnd.nextInt(3)-1)/255f;
        float bd = (rnd.nextInt(...)-2*...)/255f;
        gr.setColor(new Color(Color.HSBtoRGB(h+hd,s+sd,b+bd)));


Here are some results that you can obtain with this snippet (by varying the ... in the above code):

Note that this "tile" doesn't actually tile seamlessly. The code is designed to fill all the available area, so if you have an area of grass that is 320x320, simply set TILE_SIZE = 320.

You might be asking yourself why I use drawLine() to draw point by point instead of setRGB. In Java 1.5, most graphics images are managed and accelerated, unless you start tweaking with their pixels on a low level using setRGB or rasters. So if setRGB is used, the image will lose all acceleration .

You can use a short Grass & Sand java application (launchable via webstart) I wrote in order to adjust the parameters visually. Once you have tuned the values, the application can generate a code snippet for you.





Add a Comment

Name (optional)
EMail (optional, will not be displayed)