So I have a school assignment where we have to make a little program that makes random abstract art out of drawn rectangles.

This is what is should look like:

enter image description here

It's the top right element of the canvas.

Now the function has to consist of this: When you press 'backspace' the program should generate a new abstract 'painting' within the boarders and it should consist of 15 new random sized and random colored rectangles.

We work in a custom game engine that our teacher made for us. It uses C# as base.

I basically got everything to work. When I press backspace 15 random rectangles are being drawn on the canvas with random colors. The only problem is that when I press backspace again, only the colors change but not the rectangles.

Here is a pastebin link to the code I made so far: http://pastebin.com/YCrjb2T1

So basically for the colors I made a list (CreateRandomColorList) of int arrays. The int arrays are being filled with 3 random numbers between 0 and 255 (RGB colors). These 3 numbers can be given as parameters to set the color. So everytime you press backspace the rectangles are given new random colors.

I wanted to do exactly the same for the rectangles themselves but that did not seem to work.

There are 3 parameters you can give to a rectangle: The x position on the canvas, the y position on the canvas, the width and the height.

Any insights/tips on what could be wrong would be welcome, I hope my explanation is good enough.


1 Answer 1


All of the code you need seems to be there. You call CreateRandomColorList() when backspace is pressed, but you don't modify the rectangles at all.

So you want to add some code inside the backspace if-statement that also regenerates the rectangles in Rectangle_Array which seems to be the array you use to render the rectangles.

if (GAME_ENGINE.GetKeyUp(Key.Back))
    Rectangle = true;

    for (int i = 0; i < Rectangle_Array.Length; i++)
        Rectangle_array[i] = CreateRandomRectangle();

Here CreateRandomRectangle() is defined as

private Rectanglef CreateRandomRectangle()
    return new Rectanglef(Kunst.Next(310, 410), Kunst.Next(60, 300), Kunst.Next(0, 100), Kunst.Next(0, 100))

My C# is a little rusty so there might be a few issues but the basic principles should be there.

In any case, your code that happens inside the backspace checkup should be quite similar to what you do on initialization (assuming that a set of rectangles is displayed on init, otherwise you don't need to do a whole lot other than initialize Rectangle_Array to some placeholder values).

  • \$\begingroup\$ You sir, are my saving angel. All I needed was that: private Rectanglef CreateRandomRectangle() function. I didn't even need that whole rectangle list. Thanks man! \$\endgroup\$
    – S. Neut
    Oct 30, 2016 at 14:03
  • \$\begingroup\$ No problem! Just remember to accept my answer so that the question gets marked as answered and so that others can more easily find help that they might need in the future. \$\endgroup\$
    – user35344
    Oct 30, 2016 at 14:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .