Hello, I am trying to make the balls go up continuously when holding a key 1,2,3

Hello, this is my code. I am currently trying to do what is said in this picture.

I basically have 3 holes and want to make balls pop out of hole 1 when pressing 1, etc…
So when you hold 1 on the keyboard, balls will continuously go out of hole number 1. Same for 2 and 3. I would really appreciate if someone would help me.

void setup(){
size(400,500);
line(0,400, 50, 400);
line(75,400, 175, 400);
line(200, 400, 300, 400);
line(325, 400, 400, 400);
}

void draw () {
ellipse(63, 420, 15, 15);
ellipse(188, 420, 15, 15);
ellipse(313, 420, 15, 15);

if (keyPressed) {
if (key == ‘1’ || key == ‘1’) {
ellipse(63,420, 15, 15);;
}
} else {

}

}

You want to have many balls. When you want to have many of one thing, you should immediately consider writing a class for that thing. In this case, that means you will want a Ball class.

You will also want an amount of time to pass between balls spawning at each hole. So a Timer class might be good to have too.

And you will need to track which keys are being held down, because you can’t just rely on which key is currently the last one pressed. What if I were to hold down 1 and then tap 2? The balls from hole #1 should still keep coming.

In short, you’ll need something like this:

class Ball {
  float x, y, dy;
  color c;
  Ball(float ix) {
    x = ix;
    y = 20;
    dy = 1;
    c = color( random(255), random(255), random(255) );
  }
  boolean draw() {
    fill(c);
    y += dy;
    ellipse( x, y, 15, 15 );
    return( y > height + 30 );
  }
}

class Timer {
  int time;
  int duration;
  Timer() {
    duration = 300;
    reset();
  }
  void reset() {
    time = millis() + duration;
  }
  boolean alarm() {
    if ( millis() > time ) {
      time = millis() + duration;
      return true;
    }
    return false;
  }
}

ArrayList<Ball> balls = new ArrayList();
Timer[] timers = new Timer[6];
boolean[] keys = new boolean[6];

void setup() {
  size(400, 500);
  for ( int i = 0; i < timers.length; timers[i++] = new Timer() );
}

void draw () {
  background(255, 0, 0);
  if ( millis() < 8000 ) {
    background(0);
    stroke(255);
    line(0, 20, width, 20);
    noStroke();
    fill(0);
    for ( int i = 0; i < 6; i++) {
      ellipse(i*width/7.0 + width/7.0, 20, 20, 20);
    }
    for ( int i = balls.size()-1; i >= 0; i-- ) if ( balls.get(i).draw() ) balls.remove(i);
    for ( int i = 0; i < keys.length; i++) {
      if ( keys[i] ) {
        if ( timers[i].alarm() ) {
          balls.add( new Ball( i*width/7.0 + width/7.0 ) );
        }
      }
    }
  }
}

void keyPressed() {
  key(true);
}

void keyReleased() {
  key(false);
}

void key( boolean b ) {
  if ( keyCode >= '1' && keyCode <= '6' ) {
    keys[int( keyCode - '1')] = b;
  }
}

Of course, since I don’t want to do all the work for you, this code isn’t exactly what you want. For one, there are 6 holes, which start at the top and the balls fall down. And the whole sketch turns red after 8 seconds. So you can’t just submit this for credit.

You’ll actually have to understand it and make some changes to it.