# Present random shape, then fixed order (assignment help)

**URL:** https://discourse.processing.org/t/present-random-shape-then-fixed-order-assignment-help/2773
**Category:** Processing
**Tags:** homework
**Created:** [August 19, 2018, 8:35am UTC](https://discourse.processing.org/t/present-random-shape-then-fixed-order-assignment-help/2773 "2018-08-19T08:35:54Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![FrankieZ](https://avatars.discourse-cdn.com/v4/letter/f/ea666f/32.png) [@FrankieZ](https://discourse.processing.org/u/FrankieZ)
#### Post date: [August 19, 2018, 8:35am UTC](https://discourse.processing.org/t/present-random-shape-then-fixed-order-assignment-help/2773/1 "2018-08-19T08:35:54Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [August 19, 2018, 8:42am UTC](https://discourse.processing.org/t/present-random-shape-then-fixed-order-assignment-help/2773/2 "2018-08-19T08:42:54Z")

</div>

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…

> [@Guidelines—Asking Questions](https://discourse.processing.org/t/guidelines-tips-on-asking-questions/2147):
>
> Summary (TL;DR) Ask complete questions to get better answers! Be specific. For example: I want to load an image. I tried using createImg() , and I expected the image to be on canvas, but what happened instead was the image showing up below the canvas. Isolate your problem and work in small steps. Share only the code directly related to your problem if it is part of a bigger project, and if something isn’t working, try the smallest code that could do the thing you want. Can we run your code to …

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [August 19, 2018, 8:53am UTC](https://discourse.processing.org/t/present-random-shape-then-fixed-order-assignment-help/2773/3 "2018-08-19T08:53:41Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![FrankieZ](https://avatars.discourse-cdn.com/v4/letter/f/ea666f/32.png) [@FrankieZ](https://discourse.processing.org/u/FrankieZ)
#### Post date: [August 19, 2018, 9:10am UTC](https://discourse.processing.org/t/present-random-shape-then-fixed-order-assignment-help/2773/4 "2018-08-19T09:10:46Z")

</div>

```auto
int x = 800;

void setup() {
  size (800, 200);
  background (214, 210, 196);
  noStroke();
  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`

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [August 19, 2018, 9:22am UTC](https://discourse.processing.org/t/present-random-shape-then-fixed-order-assignment-help/2773/5 "2018-08-19T09:22:53Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![FrankieZ](https://avatars.discourse-cdn.com/v4/letter/f/ea666f/32.png) [@FrankieZ](https://discourse.processing.org/u/FrankieZ)
#### Post date: [August 19, 2018, 9:48am UTC](https://discourse.processing.org/t/present-random-shape-then-fixed-order-assignment-help/2773/6 "2018-08-19T09:48:07Z")

</div>

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

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [August 19, 2018, 9:52am UTC](https://discourse.processing.org/t/present-random-shape-then-fixed-order-assignment-help/2773/7 "2018-08-19T09:52:55Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![FrankieZ](https://avatars.discourse-cdn.com/v4/letter/f/ea666f/32.png) [@FrankieZ](https://discourse.processing.org/u/FrankieZ)
#### Post date: [August 19, 2018, 10:19am UTC](https://discourse.processing.org/t/present-random-shape-then-fixed-order-assignment-help/2773/8 "2018-08-19T10:19:44Z")

</div>

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

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [August 19, 2018, 10:24am UTC](https://discourse.processing.org/t/present-random-shape-then-fixed-order-assignment-help/2773/9 "2018-08-19T10:24:49Z")

</div>

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

---

<div class="post-metadata">

### Author: ![FrankieZ](https://avatars.discourse-cdn.com/v4/letter/f/ea666f/32.png) [@FrankieZ](https://discourse.processing.org/u/FrankieZ)
#### Post date: [August 19, 2018, 10:28am UTC](https://discourse.processing.org/t/present-random-shape-then-fixed-order-assignment-help/2773/10 "2018-08-19T10:28:28Z")

</div>

```auto
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);
  }
}

```

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [August 19, 2018, 11:01am UTC](https://discourse.processing.org/t/present-random-shape-then-fixed-order-assignment-help/2773/11 "2018-08-19T11:01:49Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [August 19, 2018, 11:04am UTC](https://discourse.processing.org/t/present-random-shape-then-fixed-order-assignment-help/2773/12 "2018-08-19T11:04:26Z")

</div>

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

```auto
int w;

void setup(){
  size(400,400);
  w = 0;
}

void draw(){
  if( w == 0 ){
    background(255,0,0);
  }
  if( w == 1 ){
    background(0,0,255);
  }
}

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

```

---

<div class="post-metadata">

### Author: ![FrankieZ](https://avatars.discourse-cdn.com/v4/letter/f/ea666f/32.png) [@FrankieZ](https://discourse.processing.org/u/FrankieZ)
#### Post date: [August 19, 2018, 11:31am UTC](https://discourse.processing.org/t/present-random-shape-then-fixed-order-assignment-help/2773/13 "2018-08-19T11:31:52Z")

</div>

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

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [August 19, 2018, 11:32am UTC](https://discourse.processing.org/t/present-random-shape-then-fixed-order-assignment-help/2773/14 "2018-08-19T11:32:42Z")

</div>

Instead of drawing different colored backgrounds, draw different shapes!

---

<div class="post-metadata">

### Author: ![FrankieZ](https://avatars.discourse-cdn.com/v4/letter/f/ea666f/32.png) [@FrankieZ](https://discourse.processing.org/u/FrankieZ)
#### Post date: [August 20, 2018, 6:04am UTC](https://discourse.processing.org/t/present-random-shape-then-fixed-order-assignment-help/2773/15 "2018-08-20T06:04:51Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![FrankieZ](https://avatars.discourse-cdn.com/v4/letter/f/ea666f/32.png) [@FrankieZ](https://discourse.processing.org/u/FrankieZ)
#### Post date: [August 20, 2018, 11:32am UTC](https://discourse.processing.org/t/present-random-shape-then-fixed-order-assignment-help/2773/16 "2018-08-20T11:32:24Z")

</div>

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

---

<div class="post-metadata">

### Author: ![dan850](https://avatars.discourse-cdn.com/v4/letter/d/e9c0ed/32.png) [@dan850](https://discourse.processing.org/u/dan850)
#### Post date: [August 24, 2018, 5:02am UTC](https://discourse.processing.org/t/present-random-shape-then-fixed-order-assignment-help/2773/17 "2018-08-24T05:02:25Z")

</div>

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 😄

Good luck on the rest of your assignments!
