Creating a grid of dice representing an image

Hello,

Try your most recent version of code with:

img.resize(80, 80);
and
tilesX = 80;

Take a close look at this and simplify it:

      int px = int(x);
      int py = int(y);
      color c = img.get(px,py);

Above works as is but can be simplified to one line.

Comment out all the show related code and try this with a suitable fill():

rect(x*tileW, y*tileH, tileW, tileH);

You really must go through every line of code and scrutinize it until you understand it.

Add a comment to each important line on your code to explain what it is doing.

Use println() where necessary to see the program variables. You can comment them out later!

And voila!

Once you have the above working and you understand it try replacing the rectangle with:

  • text; this can be 1 to 6 for a die for example.
  • other shapes that are the correct size.
  • scaling shapes such as a larger die as necessary to the correct size.

Code that I provide is not always intended to be plug and play and inserted in to your code… it is for insight to help you along.

Write and understand simple examples first and then consider how best to integrate them in to your code or not use them.

Try to write code to scale the die first and then integrate it into your code.

Keep at it!

:)

1 Like