# Help with coding game (catch falling images)

**URL:** https://discourse.processing.org/t/help-with-coding-game-catch-falling-images/43325
**Category:** Coding Questions
**Created:** [November 27, 2023, 3:19pm UTC](https://discourse.processing.org/t/help-with-coding-game-catch-falling-images/43325 "2023-11-27T15:19:30Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![anamw88](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@anamw88](https://discourse.processing.org/u/anamw88)
#### Post date: [November 27, 2023, 3:19pm UTC](https://discourse.processing.org/t/help-with-coding-game-catch-falling-images/43325/1 "2023-11-27T15:19:30Z")

</div>

Hello, I’m trying to make a game where random food items will fall from the top of the screen and a mouth on the bottom will catch them. I found an existing game with this concept and edited to add my own food pictures. the only problem is i can only replace the ones in the existing code. When i add another line to add one more food item, it doesn’t work. Can anyone help please. im posting my code below.

```auto
PFont fontA;
PFont fontB;
PImage ship;
PImage ground;
PImage eship;
PImage food;
PImage boss1;
PImage enda;

boolean lost=false;
int sphereDiameter = 25;
int endaDiameter = 25;
boolean shoot = false;
float speed=1;
float stop=0;
int hitcount=0;
int score;
float Espeed=6;

int[] sphereXCoords = {randx(), randx(), randx(), randx(), randx()};
float[] sphereYCoords = {0, 0, 0, 0, 0};
int[] ender= {1, 1, 1, 1, 1};
int[] ender2= {2, 1, 1, 1, 1};

int randx()
{
  return int(random(20, 580));
}

void setup()
{
  size(600, 620);
  fontA = loadFont("font1.vlw");
  fontB = loadFont("font2.vlw");
  textFont(fontA); 

  ship=loadImage("face.png");
  ground=loadImage("Backround.png");
  eship=loadImage("Coke.png");
  food=loadImage("Donuts.png");
  boss1=loadImage("Boss1.png");
  enda=loadImage("EggToast.png");
}

void draw()
{
  background(0);
  image(ground, 0, 0);
  fill(color(255, 0, 0));
  textFont(fontA);
  text("POINTSa: " + score, 5, 610);
  text("What Will You Choose?", 130, 30); 
  text("What Will You Choose?", 130, 30); 
  textFont(fontB);
  text("Highscora:38304", 445, 25);
  text("The Star Masta:JSRa", 435, 40);
  {
    if (keyPressed == true) 
    {  
      background(0);
      textFont(fontA);
      text("PAUSED", 250, 250);
      textFont(fontA);
      text("POINTSa: " + score, 5, 610);
      text("Star Destroya", 214, 30); 
      text("Star Destroya", 214, 30); 
      textFont(fontB); 
      text("The Firsta Clasha", 240, 45);

      stroke(225);
      fill(0, 0, 255);
      for (int i=0; i<5; i++)
      {
        image(ground, sphereXCoords[i]-11, sphereYCoords[i]-=speed, 
        sphereDiameter, sphereDiameter);
        if (keyPressed == true) image(ground, sphereXCoords[i]-11, sphereYCoords[i]+=stop, 
        sphereDiameter, sphereDiameter);
      }
    }
    {
      fill(255);
    }
    rect(-50, -50, 50, 50);
  }

  fill(color(190, 190, 255));
  stroke(color(00, 250, 100));

  fill(color(0, 0, 255));
  stroke(color(100, 250, 255));
  image(ship, mouseX-17, 555);
  fill(color(255, 0, 0));
  stroke(color(255, 0, 0));

  if (shoot==true)
  {
    sphereKiller(mouseX);
    shoot = false;
  }

  sphereDropper();
  gameEnder();
}

void mousePressed()
{
  shoot = true;
  if (lost==true)
  {
    restart();
  }
}

void sphereDropper()
{  

  stroke(225);
  fill(0, 0, 255);
  for (int i=0; i<5; i++)
  {
    if (i==0) 
    {
      image(food, sphereXCoords[i]-11, sphereYCoords[i]+=speed*ender2[i], 
      sphereDiameter, sphereDiameter);
      if (keyPressed == true) image(enda, sphereXCoords[i]-11, sphereYCoords[i]+=stop, 
      sphereDiameter, sphereDiameter);
    }
    else {
      image(enda, sphereXCoords[i]-11, sphereYCoords[i]+=speed*ender2[i], 
      sphereDiameter, sphereDiameter);
      if (keyPressed == true) image(eship, sphereXCoords[i]-11, sphereYCoords[i]+=stop, 
      sphereDiameter, sphereDiameter);
    }
  }
}

void sphereKiller(int shotX)
{
  fill(255, 0, 0);
  boolean hit = false;
  for (int i = 0; i < 5; i++)
  {
    if ((shotX >= (sphereXCoords[i]-sphereDiameter/2)) && 
      (shotX <= (sphereXCoords[i]+sphereDiameter/2)))
    {
      hit = true;
      score = score +152;
      line(mouseX, 565, mouseX, sphereYCoords[i]);
      ellipse(sphereXCoords[i], sphereYCoords[i], 
      sphereDiameter+10, sphereDiameter+10);
      sphereXCoords[i] = randx();
      sphereYCoords[i] = 0;
      hitcount++;
      if (hitcount>4)
      {
        hitcount=0;
        speed=speed*1.02;

        {
          hit = true;
          score = score +152;
          line(mouseX, 565, mouseX, sphereYCoords[i]);
          ellipse(sphereXCoords[i], sphereYCoords[i], 
          sphereDiameter+10, sphereDiameter+10);
          sphereXCoords[i] = randx();
          sphereYCoords[i] = 0;
          hitcount++;
          if (hitcount>7)
          {
            hitcount=0;
            ender=ender2;

            {
              image(enda, sphereXCoords[i]-11, sphereYCoords[i]+=Espeed, 
              sphereDiameter, sphereDiameter);
              if (keyPressed == true) image(enda, sphereXCoords[i]-11, sphereYCoords[i]+=stop, 
              sphereDiameter, sphereDiameter);
            }
          }
        }
      }
    }
  }

  if (hit == false)
  {
    line(mouseX, 565, mouseX, 0);
  }
}

void gameEnder()
{
  for (int i=0; i< 5; i++)
  {
    if (sphereYCoords[i]>600)
    {
      fill(color(255, 255, 0));
      lost=true;
  fill(color(255, 0, 0));
  textFont(fontA);

      text("Click to Restart",width/2-100, height/2);
      noLoop();
    }
  }
}

void restart()
{
  lost=false;
shoot = false;
speed=1;
stop=0;
hitcount=0;
score=0;
Espeed=6;

for (int i=0; i<5; i++)
{
sphereXCoords [i] = randx();
sphereYCoords [i]= 0;
ender [i]= 1;
ender2 [i]= 1;
}
ender2[0]=2;
loop();
delay(1000);
}

```

---

<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: [November 27, 2023, 8:12pm UTC](https://discourse.processing.org/t/help-with-coding-game-catch-falling-images/43325/2 "2023-11-27T20:12:50Z")

</div>

> [@anamw88](#):
>
> When i add another line to add one more food item

which line are you adding?

**My guess**

> [@anamw88](#):
>
> food=loadImage(“Donuts.png”);

when you copy this line and say

`food1=loadImage(“Donuts1.png”); // food1 instead of food`

remember, before setup() you must say `PImage food1;// declare`

best search for “food” and copy all lines where you find it and change “food” to “food1” in the new line.

in the long run you want a array / list of that.

---

<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: [November 27, 2023, 8:13pm UTC](https://discourse.processing.org/t/help-with-coding-game-catch-falling-images/43325/3 "2023-11-27T20:13:41Z")

</div>

> [@anamw88](#):
>
> image(food, sphereXCoords[i]-11, sphereYCoords[i]+=speed_ender2[i],_

this one for example too

---

<div class="post-metadata">

### Author: ![anamw88](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@anamw88](https://discourse.processing.org/u/anamw88)
#### Post date: [November 27, 2023, 9:31pm UTC](https://discourse.processing.org/t/help-with-coding-game-catch-falling-images/43325/4 "2023-11-27T21:31:01Z")

</div>

Hi, thanks a lot for replying.  
so yes, I did try adding a "PImage food1; , PImage food2; lines in the beginning and then coordinated that with food=loadImage(“Donuts.png”);  
food1=loadImage(“Chips.png”);  
But, it is not being detected. The extra lines that i add for another items dont show up when i run the sketch.

---

<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: [November 27, 2023, 9:42pm UTC](https://discourse.processing.org/t/help-with-coding-game-catch-falling-images/43325/5 "2023-11-27T21:42:48Z")

</div>

> [@anamw88](#):
>
> image(food, sphereXCoords[i]-11, sphereYCoords[i]+=speed_ender2[i],_

did you for example add these for each different image?

Did you search the word food and add to each line the equivalent new line?

Please post your entire 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: [November 27, 2023, 9:44pm UTC](https://discourse.processing.org/t/help-with-coding-game-catch-falling-images/43325/6 "2023-11-27T21:44:13Z")

</div>

> [@anamw88](#):
>
> when i run the sketch.

it’s good that the Sketch runs in the first place

---

<div class="post-metadata">

### Author: ![anamw88](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@anamw88](https://discourse.processing.org/u/anamw88)
#### Post date: [November 27, 2023, 10:02pm UTC](https://discourse.processing.org/t/help-with-coding-game-catch-falling-images/43325/7 "2023-11-27T22:02:14Z")

</div>

Yes the sketch runs because as i mentioned above, I got the code from an existing game and only had to replaced their pngs with my own. But i wanted to add some more randomised pngs which are not being detected for some reason.  
This line “image(food, sphereXCoords[i]-11, sphereYCoords[i]+=speed\*ender2[i],” im not sure how to add these for each different image. When i copy this and edit according to my declared variable it gives an error.  
I’ve posted the entire code in my first query above. If you run it you can see the EggToast.png and Donuts.png falling but not the Coke.png falling.  
Thank you for your cooperation.

---

<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: [November 27, 2023, 10:08pm UTC](https://discourse.processing.org/t/help-with-coding-game-catch-falling-images/43325/8 "2023-11-27T22:08:23Z")

</div>

> [@anamw88](#):
>
> void sphereDropper()

this is the function you need to look at

each food type (you didn’t name them food, food1, food2 so I missed that) is represented in the arrays sphereXCoords sphereYCoords ender2 (its position and whatnot)

So you need to make the arrays bigger

**Hint**

This

```auto
else {
image(enda, sphereXCoords[i]-11, sphereYCoords[i]+=speed</em>ender2[i],
sphereDiameter, sphereDiameter);
if (keyPressed == true) image(eship, sphereXCoords[i]-11, sphereYCoords[i]+=stop,
sphereDiameter, sphereDiameter);
}

```

needs to be

```auto
else if (i==1) { // with if (i==1) here 
image(enda, sphereXCoords[i]-11, sphereYCoords[i]+=speed</em>ender2[i],
sphereDiameter, sphereDiameter);
if (keyPressed == true) image(eship, sphereXCoords[i]-11, sphereYCoords[i]+=stop,
sphereDiameter, sphereDiameter);
}

```

and followed by

```auto
else if (i==2) {
image(food2, sphereXCoords[i]-11, sphereYCoords[i]+=speed</em>ender2[i],
sphereDiameter, sphereDiameter);
if (keyPressed == true) image(eship, sphereXCoords[i]-11, sphereYCoords[i]+=stop,
sphereDiameter, sphereDiameter);
}

```

```auto

else if (i==3) {
image(food3, sphereXCoords[i]-11, sphereYCoords[i]+=speed</em>ender2[i],
sphereDiameter, sphereDiameter);
if (keyPressed == true) image(eship, sphereXCoords[i]-11, sphereYCoords[i]+=stop,
sphereDiameter, sphereDiameter);
}

```

(maybe replace `eship` with correct image name)

In this code each code section makes one image

code is very bad, when you can, make a food array

---

<div class="post-metadata">

### Author: ![sterretje](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@sterretje](https://discourse.processing.org/u/sterretje)
#### Post date: [November 28, 2023, 4:44am UTC](https://discourse.processing.org/t/help-with-coding-game-catch-falling-images/43325/9 "2023-11-28T04:44:18Z")

</div>

Not providing an answer but I would like to ask you to post code (in future) using code tags.

To fix your opening post

1. Edit the post
2. Select all code
3. Click the button \</\>
4. Save the post

It makes it easier to read, easier to copy and it tells the forum software not to treat e.g. `[]` as .

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [November 28, 2023, 1:22pm UTC](https://discourse.processing.org/t/help-with-coding-game-catch-falling-images/43325/10 "2023-11-28T13:22:46Z")

</div>

Hello @anamw88,

> [@anamw88](#):
>
> I found an existing game with this concept and edited

> [@anamw88](#):
>
> I got the code from an existing game

Please provide some context to this.  
It is important to respect the author and the original work.

There is a section in the FAQ about this:  
[https://discourse.processing.org/faq](https://discourse.processing.org/faq)

> ## [Post Only Your Own Stuff](https://discourse.processing.org/faq#stealing)
> 
> You may not post anything digital that belongs to someone else without permission. You may not post descriptions of, links to, or methods for stealing someone’s intellectual property (software, video, audio, images), or for breaking any other law.
> 
> Before posting, ask: “Can I share it?” Do you personally have the right to share this code, or does it belong to someone else who should give you permission first, such as your company or your project group? Posts that receive answers will not be taken down, as answers belong to the community. If you don’t want code to be public, do not make it public.

`:)`
