Present random shape, then fixed order (assignment help)

Hi there! Does anyone know how to write a program that randomly presents a shape out of a collection of 3 (let’s say line out of rectangle,ellipse and line) and when users press a key it will change to anothet shape in a fixed order?

Yes. There are several forum regulars who could easily write such a program. None of them will, however, until you have put some effort into such a sketch yourself, and shown us that effort by posting your code.

Even if you have no code, attempt to write some.

… And this is coming from the forum regular who is most likely to just do it for you…

1 Like

Anyway. Break your problem down into smaller parts.

I’m serious!

If you try to do the whole thing at once it will be a mess.

Start with the big problem:

“write a program that randomly presents a shape out of a collection of 3 (let’s say line out of rectangle,ellipse and line) and when users press a key it will change to another shape in a fixed order”

To me, this breaks down into the following:

  • Write a program.
  • Do something randomly.
  • Present a shape (out of three possible shapes).
  • Detect user input (key presses).
  • Change to another shape.
  • Make sure the shapes appear in a fixed order.
int x = 800;

void setup() {
  size (800, 200);
  background (214, 210, 196);
  fill (166, 25, 46);
  rect (0, 0, 115, 200);

void draw() {
  background (214, 210, 196);
  fill (166, 25, 46);
  rect (0, 0, 115, 200);
  fill (55, 58, 54);
  ellipse (x, 100, 100, 100);
  x = (x - 1);

That’s what I have done for now. What I need to do now is to write a program as I said in the red part to the left. I know “random” and “void keyPressed” would do but do not know how to apply to this case.Preformatted text

Please format your code. You can do that by editing your post (the little pen icon on the top right corner) and then selecting your code and clicking on that icon: </>

Now for your program, start by writing a code that is simply changing the background color when you press a key: from red to green to blue to red again, etc. You will need that kind of structure to change the shapes.

Also there is something that is not clear: should the program change shape randomly when the user press a key or the random selection happen just at the beginning and then when the user press a key it will change it a predefined order?

The random selection only occurs in the beginning and then a predefined order will come into effect when the user presses a key

So definitely try to code a program that do what I described in my previous post.
A hint: you will need to create a global variable, let’s call it step that will keep track on which step of the program you currently are. step = 1 would be red background step = 2 would be green and step = 3 would be blue.
Now the keyPressed() event can change that step variable. And the draw function can use that variable to know wich background to draw.

I tried but it said “cannot convert from int to boolean”

We would need your code to see what you have done in order to help you figure out what is wrong in it.

int step;
void keyPressed() {
  if (step = 1) {
    background (255, 0, 0);
  } else if (step = 2) {
    background (0, 255, 0);
  } else if (step = 3) {
    background (0, 0, 255);

There is a difference between = and ==.

= is an assignment operator. It stores the value on the right in the variable on the left.

== is a test for equality. It returns true if two things are equal.

In the code above, you are not checking if things are equal! You are assigning your step variable new values!

Also, you should do all your drawing in draw. Consider this simple example:

int w;

void setup(){
  w = 0;

void draw(){
  if( w == 0 ){
  if( w == 1 ){

void mousePressed(){
  w = w + 1;
  if( w == 2 ){
    w = 0;
1 Like

So now how can I write the program I intended at first?

Instead of drawing different colored backgrounds, draw different shapes!

I have gone this far. However, there is still something wrong with shapes coming from the right. They keep overlapping with each other when I press ‘j’. Can you please kindly how to solve this?

I have finished my assignment successfully, guys! Thank you very much for all your help!


that’s great news!! Glad that we were able to assist you. Always remember to break your problems down into smaller steps BEFORE coding then try to code smaller executible versions of your larger programs :smile:

Good luck on the rest of your assignments!