# Struggling with code - New programmer

**URL:** <https://discourse.processing.org/t/struggling-with-code-new-programmer/150>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [May 20, 2018, 6:47am UTC](https://discourse.processing.org/t/struggling-with-code-new-programmer/150 "2018-05-20T06:47:29Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![sabz20005](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@sabz20005](https://discourse.processing.org/u/sabz20005)\
**Post date:** [May 20, 2018, 6:47am UTC](https://discourse.processing.org/t/struggling-with-code-new-programmer/150/1 "2018-05-20T06:47:29Z")

</div>

Hey guys, this is my first post so I hope I hope I’m doing this correctly. I am trying to create a code where each time an object hits the bottom of the screen a new circle is on the screen, starting in the top left screen. Each time the objects hits the ground the next circle is drawn to the right of the previous circle and so on, if it moves out of room it would move to the next line. My only problem is that once the first circle is drawn the previous circle gets deleted and is no longer on the screen. The drawing of the circles is in loop inside a function which is called once the hammer reaches the bottom of the screen.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 20, 2018, 11:58am UTC](https://discourse.processing.org/t/struggling-with-code-new-programmer/150/2 "2018-05-20T11:58:12Z")

</div>

Homework

> only problem is that once the first circle is drawn the previous circle gets deleted and is no longer on the screen

store number of circles / coins you have. For loop i up to that number. Draw the coins insides this loop.

Call this loop not when the hammer falls but in your draw()

---

<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:** [May 20, 2018, 12:37pm UTC](https://discourse.processing.org/t/struggling-with-code-new-programmer/150/3 "2018-05-20T12:37:27Z")

</div>

And please share what code you have already written!

---

<div class="post-metadata">

**Author:** ![sabz20005](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@sabz20005](https://discourse.processing.org/u/sabz20005)\
**Post date:** [May 20, 2018, 12:46pm UTC](https://discourse.processing.org/t/struggling-with-code-new-programmer/150/4 "2018-05-20T12:46:33Z")

</div>

This is everything I have so far relevant to my question (didn’t copy all of it so its easier to read) also to the first person telling me to the for loop, thank you for your advice, the only problem is that i want each circle to appear as the hammer reaches the bottom not every circle to be drawn, thank you for your help though 🙂

```auto
int HammerSide1 = int(random(2, 8)); //HAMMER SIDE TO SIDE
int HammerDown1 = 0; // HAMMER DOWN

int circlesize = 30;
int count = 570;

float x = -560;

int A = -40;
int B = 40;
int circle = 0;

int circledraw = 35;

void setup() {
  size(512, 318); //DISPLAY WINDOW
  frameRate(60); //FRAME RATE

  HammerSide1 = HammerSide1 * 64;
  DoorClosed = (int)random(180, 480);
  DoorOpen = (int)random(180, 480);

}

void draw() {
 
    background(135, 206, 255);
    strokeWeight(1);
    fill(120, 72, 0);
    rect(0, 300, 512, 20);

    //HAMMER FALLING CODE ------>
    fill(0, 255, 0);
    if (HammerDown1 < -25) {
      HammerSide1 = int(random(2, 8));
      HammerSide1 = HammerSide1 * 64;
    }
    if (HammerDown1 < 275); 
    { 
      HammerDown1 = HammerDown1 + 2;
    }
    if (HammerDown1 >= 275) { 
      HammerDown1 = -75;
      A = A + 80;
   //circledraw = circledraw - 1;
  

   }
         circles();

/*if (circledraw == 1){
  circles();
} */
    // HAMMER FALLING CODE <-----

    //HAMMER DESING ---->
    fill(165, 45, 45);
    rect(HammerSide1, HammerDown1, 35, 10);
    rect(HammerSide1-10, HammerDown1, 10, 10);

    fill(169);
    stroke(169);
    rect(HammerSide1, HammerDown1 -10, 10, 10);
    rect(HammerSide1, HammerDown1, 10, 20);
    //HAMMER DESING <----

void circles(){

    while (count > 0) {
      noStroke();
      fill(0);
          ellipse(A, B, circlesize, circlesize);
count = -5;
circlesize = -10;
    }
   count = 570;
   circlesize = 30;
    if (A > 570) {
      A = 40;
      B = B + 80;
    }
    if (A > 570) {
      if (B > 260) {
        A = 40;
        B = 40;
        count = 570;
      }
    }
}

```

@TfGuy44 @Chrisir

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 20, 2018, 10:02pm UTC](https://discourse.processing.org/t/struggling-with-code-new-programmer/150/5 "2018-05-20T22:02:09Z")

</div>

> [@sabz20005](#):
>
> only problem is that i want each circle to appear as the hammer reaches the bottom not every circle to be drawn,

Not sure if you got me there.

When the hammer falls, you increase the number of circles you want to display. 0, then 1,then 2…

Displaying is then in the for loop

---

<div class="post-metadata">

**Author:** ![sabz20005](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@sabz20005](https://discourse.processing.org/u/sabz20005)\
**Post date:** [May 20, 2018, 11:40pm UTC](https://discourse.processing.org/t/struggling-with-code-new-programmer/150/6 "2018-05-20T23:40:05Z")

</div>

Thank you for your reply again @Chrisir  
I understand what your saying now however I’m struggling to implement it, can you tell me what i have done wrong? full code below

```auto
//VARIABLES ---->
int i = 0;

int HammerSide1 = int(random(2, 8)); //HAMMER SIDE TO SIDE
int HammerDown1 = 0; // HAMMER DOWN

int circlesize = 30;
int count = 570;

float x = -560;

int A = 40;
int B = 40;

// VATRIABLES <-------

void setup() {
  size(512, 318); //DISPLAY WINDOW
  frameRate(60); //FRAME RATE

  HammerSide1 = HammerSide1 * 64;
}

void draw() {

  background(135, 206, 255);
  strokeWeight(1);
  fill(120, 72, 0);
  rect(0, 300, 512, 20);

  //HAMMER FALLING CODE ------>
  fill(0, 255, 0);
  if (HammerDown1 < -25) {
    HammerSide1 = int(random(2, 8));
    HammerSide1 = HammerSide1 * 64;
  }
  if (HammerDown1 < 275); 
  { 
    HammerDown1 = HammerDown1 + 2;
  }
  if (HammerDown1 >= 275) { 
    HammerDown1 = -75;
    i = i ++;
    //circledraw = circledraw - 1;
  }

  circles();
  /*if (circledraw == 1){
   circles();
   } */
  // HAMMER FALLING CODE <-----

  //HAMMER DESING ---->
  fill(165, 45, 45);
  rect(HammerSide1, HammerDown1, 35, 10);
  rect(HammerSide1-10, HammerDown1, 10, 10);

  fill(169);
  stroke(169);
  rect(HammerSide1, HammerDown1 -10, 10, 10);
  rect(HammerSide1, HammerDown1, 10, 20);
  //HAMMER DESING <----
}

void circles() {

  for ( i = 0; i > 32; ) {
    noStroke();
    fill(0);
    ellipse(A, B, circlesize, circlesize);
    /*         
     if (A > 570) {
     A = 40;
     B = B + 80;
     }
     if (A > 570) {
     if (B > 260) {
     A = 40;
     B = 40;
     
     }
     }*/
  }
}

//GAME CODE <<<<<<<<

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 21, 2018, 11:17am UTC](https://discourse.processing.org/t/struggling-with-code-new-programmer/150/7 "2018-05-21T11:17:48Z")

</div>

> [@sabz20005](#):
>
> for ( i = 0; i \> 32; ) {

for ( i = 0; i \> numberOfCircles; i++) {

i++ is missing

numberOfCircles: make it a global Variable of type int; increase every time hammer hits

Call circles from draw

---

<div class="post-metadata">

**Author:** ![sabz20005](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@sabz20005](https://discourse.processing.org/u/sabz20005)\
**Post date:** [May 22, 2018, 10:54am UTC](https://discourse.processing.org/t/struggling-with-code-new-programmer/150/8 "2018-05-22T10:54:43Z")

</div>

@Chrisir Thank you so much for your help ill let you know how i progress 🙂

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 22, 2018, 12:25pm UTC](https://discourse.processing.org/t/struggling-with-code-new-programmer/150/9 "2018-05-22T12:25:58Z")

</div>

Ok then, excellent 😉

---

<div class="post-metadata">

**Author:** ![lqdev](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lqdev/32/29_2.png) [@lqdev](https://discourse.processing.org/u/lqdev)\
**Post date:** [May 23, 2018, 5:46am UTC](https://discourse.processing.org/t/struggling-with-code-new-programmer/150/10 "2018-05-23T05:46:26Z")

</div>

Actually it should be

```auto
for (int i = 0; i < numberOfCircles; i++) {

```

otherwise the program will get stuck in an infinite loop

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 27, 2018, 6:09am UTC](https://discourse.processing.org/t/struggling-with-code-new-programmer/150/11 "2018-05-27T06:09:41Z")

</div>

ah, thanks! my bad…
