Help with coding game (catch falling images)

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.

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

which line are you adding?

My guess

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.

1 Like

this one for example too

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.

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

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

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.

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

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

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

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

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

1 Like

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 .

2 Likes

Hello @anamw88,

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

Post Only Your Own Stuff

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.

:)