Hello. I am just a beginner. I want to know how to complete a jump

please format code with </> button * homework policy * asking questions
My current code, when I press another keycode in the middle of a jump, it changes the direction of the jump, instead of waiting for a jump to end before starting a new jump. My code is here. It looks like a jumpFly game

final int CLOCK_LONG=200;
final int CLOCK_SHORT=150;
final int DOTS_SIZE=20;
final int BETWEEN_DOTS=200;
float angleLong=0;
float angleShort=0;
final int FLY_SIZE=30;
int flyX=width/2-BETWEEN_DOTS/2;
int flyY=height/2-BETWEEN_DOTS/2;
int jumpSpeed=10;

void setup() {

size(500, 500);
}

void draw() {
background(#24B41D);
strokeWeight(6);
stroke(#FC8412);

drawHand();
moveHand();
drawDots();
drawFly();
jumping();
}

void drawHand() {

float longX=cos(angleLong)* CLOCK_LONG;
float longY=sin(angleLong)* CLOCK_LONG;
float shortX=cos(angleShort)*CLOCK_SHORT;
float shortY=sin(angleShort)*CLOCK_SHORT;
line(width/2, height/2, width/2+longX, height/2+longY);
line(width/2, height/2, width/2+shortX, height/2+shortY);
}

void drawDots() {
noStroke();
strokeWeight(1);
fill(0, 0, 255);
ellipse(width/2-BETWEEN_DOTS/2, height/2-BETWEEN_DOTS/2, DOTS_SIZE, DOTS_SIZE);
ellipse(width/2+BETWEEN_DOTS/2, height/2-BETWEEN_DOTS/2, DOTS_SIZE, DOTS_SIZE);
ellipse(width/2-BETWEEN_DOTS/2, height/2+BETWEEN_DOTS/2, DOTS_SIZE, DOTS_SIZE);
ellipse(width/2+BETWEEN_DOTS/2, height/2+BETWEEN_DOTS/2, DOTS_SIZE, DOTS_SIZE);
}

void moveHand() {

angleLong=(angleLong+0.02)%TWO_PI;
angleShort=(angleShort+0.01)%TWO_PI;
}

void drawFly() {

fill(255);
circle(flyX,flyY,FLY_SIZE);

}

void jumping() {
if (keyCode==UP&&flyY<=height/2+BETWEEN_DOTS/2) {
flyY=flyY-jumpSpeed;
flyY=max(flyY, height/2-BETWEEN_DOTS/2);
} else
if (keyCode==DOWN&&flyY>=height/2-BETWEEN_DOTS/2) {
flyY=flyY+jumpSpeed;
flyY=min(height/2+BETWEEN_DOTS/2, flyY);
} else
if (keyCode==LEFT&&flyX<=width/2+BETWEEN_DOTS/2) {
flyX=flyX-jumpSpeed;
flyX=max(flyX, width/2-BETWEEN_DOTS/2);
} else
if (keyCode==RIGHT&&flyX>=width/2-BETWEEN_DOTS/2) {
flyX=flyX+jumpSpeed;
flyX=min(flyX, width/2+BETWEEN_DOTS/2);
}
}

Hello and a warm welcome to the forum!

Nice to have you here!

I don’t understand

your sketch shows a clock.

Where do you want a jump? Where you move the ball with cursor?

Do you mean the ball doesn’t fly direct ---- but in a curve? Like /\ ?

Warm regards,

Chrisir

do you know fly o’clock, this game?The fly is the white circle, and there has four dots, I want to jump between them. Now , if I press key-DOWN, the white circle will go down ,but when the white ball is moving down, if I press key-LEFT, the ball won’t go where I need it to get.
When I press-DOWN, no matter I press any other button, the whole program will not run until the white ball completes this movement. After this movement completes, if I press another keycode, it will jump or move again, but during jumping or moving time, no matter I press any other button, the whole program will not run . This is what I want to achieve.

here is an example, where the dot doesn’t go directly from a to b BUT
in a curve (like in a jump).

see https://www.processing.org/reference/curvePoint_.html

I will read your answer in a second…

Warm regards, Chrisir


final int CLOCK_LONG=200;
final int CLOCK_SHORT=150;
final int DOTS_SIZE=20;
final int BETWEEN_DOTS=200;
float angleLong=0;
float angleShort=0;
final int FLY_SIZE=30;
int flyX=width/2-BETWEEN_DOTS/2;
int flyY=height/2-BETWEEN_DOTS/2;
int jumpSpeed=10;

float t;

void setup() {

  size(500, 500);
}

void draw() {
  background(#24B41D);
  strokeWeight(6);
  stroke(#FC8412);

  drawHand();
  moveHand();
  drawDots();
  drawFly();
  jumping();

  fly1 () ;
}

void drawHand() {

  float longX=cos(angleLong)* CLOCK_LONG;
  float longY=sin(angleLong)* CLOCK_LONG;
  float shortX=cos(angleShort)*CLOCK_SHORT;
  float shortY=sin(angleShort)*CLOCK_SHORT;
  line(width/2, height/2, width/2+longX, height/2+longY);
  line(width/2, height/2, width/2+shortX, height/2+shortY);
}

void drawDots() {
  noStroke();
  strokeWeight(1);
  fill(0, 0, 255);
  ellipse(width/2-BETWEEN_DOTS/2, height/2-BETWEEN_DOTS/2, DOTS_SIZE, DOTS_SIZE);
  ellipse(width/2+BETWEEN_DOTS/2, height/2-BETWEEN_DOTS/2, DOTS_SIZE, DOTS_SIZE);
  ellipse(width/2-BETWEEN_DOTS/2, height/2+BETWEEN_DOTS/2, DOTS_SIZE, DOTS_SIZE);
  ellipse(width/2+BETWEEN_DOTS/2, height/2+BETWEEN_DOTS/2, DOTS_SIZE, DOTS_SIZE);
}

void moveHand() {

  angleLong=(angleLong+0.02)%TWO_PI;
  angleShort=(angleShort+0.01)%TWO_PI;
}

void drawFly() {
  fill(255);
  ellipse(flyX, flyY, FLY_SIZE, FLY_SIZE);
}

void jumping() {
  if (keyCode==UP&&flyY<=height/2+BETWEEN_DOTS/2) {
    flyY=flyY-jumpSpeed;
    flyY=max(flyY, height/2-BETWEEN_DOTS/2);
  } else
    if (keyCode==DOWN&&flyY>=height/2-BETWEEN_DOTS/2) {
      flyY=flyY+jumpSpeed;
      flyY=min(height/2+BETWEEN_DOTS/2, flyY);

      //
      fly1();
    } else
      if (keyCode==LEFT&&flyX<=width/2+BETWEEN_DOTS/2) {
        flyX=flyX-jumpSpeed;
        flyX=max(flyX, width/2-BETWEEN_DOTS/2);
      } else
        if (keyCode==RIGHT&&flyX>=width/2-BETWEEN_DOTS/2) {
          flyX=flyX+jumpSpeed;
          flyX=min(flyX, width/2+BETWEEN_DOTS/2);
        }
}

void fly1 () {

  noFill();
  //  curve(5, 26, 5, 26, 73, 24, 73, 61);
  //  curve(5, 26, 73, 24, 73, 61, 15, 65);
  stroke(255, 0, 0); 
  curve(
    width/2+310, height/2-BETWEEN_DOTS/2, 
    width/2-BETWEEN_DOTS/2, height/2-BETWEEN_DOTS/2, 
    width/2-BETWEEN_DOTS/2, height/2+BETWEEN_DOTS/2, 
    width/2+310, height/2+BETWEEN_DOTS/2
    );  

  fill(255);
  ellipseMode(CENTER);
  int steps = 6;
  // for (int i = 0; i <= steps; i++) {
  // float t = i / float(steps);
  float x;// = curvePoint(5, 5, 73, 73, t);
  float y;// = curvePoint(26, 26, 24, 61, t);

  x = curvePoint(  width/2+310, width/2-BETWEEN_DOTS/2, width/2-BETWEEN_DOTS/2, width/2+310, t);
  y = curvePoint( height/2-BETWEEN_DOTS/2, height/2-BETWEEN_DOTS/2, height/2+BETWEEN_DOTS/2, height/2+BETWEEN_DOTS/2, t);
  ellipse(x, y, 5, 5);

  t+=0.01;
  if (t>=1) t=0.0;
}

Sorry, not like this. I want it move straight, not a curve. You can run my code. When you press one keycode, immediately press another keycode, you’ll see that the ball isn’t on the four blue dots.

I don’t know the game.

What you wrote:

Is this what you want to achieve or what happens now?

I understand now.

You can set a variable boolean isMoving to true as long as you are moving.

When the white ball reaches its target say isMoving = false;

When isMoving == false don’t accept any new key input!!!

So before setup() say boolean isMoving = false;

EDIT: Not sure how to do this…

Chrisir

Sorry, I can understand what you are talking about. But I don’t know how to use it. Where should I add this boolean? I am very confused
EDIT:Thank you very much. I will try it . Also, I have one more question, how to add Z coordinate, to make this jump looks like a real jump. The white ball size will get bigger in the first half of jump, and the ball will get smaller in the second half of jump. I have formula but don’t know how to use.
the first half of jump:𝑓𝑙𝑦𝑍 = 2 ∗ (𝑀𝐴𝑋_𝑍/𝑆𝑃𝐴𝐶𝐼𝑁𝐺) ∗ 𝑗𝑢𝑚𝑝𝑂𝑓𝑓𝑠𝑒t
the second half of jump:𝑓𝑙𝑦𝑍 = 2 ∗ 𝑀𝐴𝑋_𝑍 (1 −𝑗𝑢𝑚𝑝𝑂𝑓𝑓𝑠𝑒𝑡/𝑆𝑃𝐴𝐶𝐼𝑁𝐺 )

It’s more complicate than I thought it would be.

I guess you need a separate function where you read the key and one function, where you do the movement. Then you can skip the key function when a movement is still happening:

if(isMoving)
    return;

I already showed you a way with a curved way for the movement using curvePoints.

A example in 3D, where you need another size() command

There is a nice tutorial about P3D


final int CLOCK_LONG=200;
final int CLOCK_SHORT=150;
final int DOTS_SIZE=20;
final int BETWEEN_DOTS=200;
float angleLong=0;
float angleShort=0;
final int FLY_SIZE=30;
int flyX=width/2-BETWEEN_DOTS/2;
int flyY=height/2-BETWEEN_DOTS/2;
int jumpSpeed=10;

float t;

void setup() {

  size(500, 500, P3D);
}

void draw() {
  background(#24B41D);
  strokeWeight(6);
  stroke(#FC8412);
  lights(); 

  drawHand();
  moveHand();
  drawDots();
  drawFly();
  jumping();

  fly1 () ;
}

void drawHand() {

  float longX=cos(angleLong)* CLOCK_LONG;
  float longY=sin(angleLong)* CLOCK_LONG;
  float shortX=cos(angleShort)*CLOCK_SHORT;
  float shortY=sin(angleShort)*CLOCK_SHORT;
  line(width/2, height/2, width/2+longX, height/2+longY);
  line(width/2, height/2, width/2+shortX, height/2+shortY);
}

void drawDots() {
  noStroke();
  strokeWeight(1);
  fill(0, 0, 255);
  ellipse(width/2-BETWEEN_DOTS/2, height/2-BETWEEN_DOTS/2, DOTS_SIZE, DOTS_SIZE);
  ellipse(width/2+BETWEEN_DOTS/2, height/2-BETWEEN_DOTS/2, DOTS_SIZE, DOTS_SIZE);
  ellipse(width/2-BETWEEN_DOTS/2, height/2+BETWEEN_DOTS/2, DOTS_SIZE, DOTS_SIZE);
  ellipse(width/2+BETWEEN_DOTS/2, height/2+BETWEEN_DOTS/2, DOTS_SIZE, DOTS_SIZE);
}

void moveHand() {

  angleLong=(angleLong+0.02)%TWO_PI;
  angleShort=(angleShort+0.01)%TWO_PI;
}

void drawFly() {
  fill(255);
  ellipse(flyX, flyY, FLY_SIZE, FLY_SIZE);
}

void jumping() {
  if (keyCode==UP&&flyY<=height/2+BETWEEN_DOTS/2) {
    flyY=flyY-jumpSpeed;
    flyY=max(flyY, height/2-BETWEEN_DOTS/2);
  } else
    if (keyCode==DOWN&&flyY>=height/2-BETWEEN_DOTS/2) {
      flyY=flyY+jumpSpeed;
      flyY=min(height/2+BETWEEN_DOTS/2, flyY);

      //
      fly1();
    } else
      if (keyCode==LEFT&&flyX<=width/2+BETWEEN_DOTS/2) {
        flyX=flyX-jumpSpeed;
        flyX=max(flyX, width/2-BETWEEN_DOTS/2);
      } else
        if (keyCode==RIGHT&&flyX>=width/2-BETWEEN_DOTS/2) {
          flyX=flyX+jumpSpeed;
          flyX=min(flyX, width/2+BETWEEN_DOTS/2);
        }
}

void fly1 () {

  noFill();
  //  curve(5, 26, 5, 26, 73, 24, 73, 61);
  //  curve(5, 26, 73, 24, 73, 61, 15, 65);
  stroke(255, 0, 0); 
  float height1 = -2220; 
  curve(
    width/2+310, height/2+BETWEEN_DOTS/2, height1, // control 1
    width/2+BETWEEN_DOTS/2, height/2-BETWEEN_DOTS/2, 0, // real point 1
    width/2+BETWEEN_DOTS/2, height/2+BETWEEN_DOTS/2, 0, // real point 2
    width/2+310, height/2+BETWEEN_DOTS/2, height1   // control 2
    );  

  fill(255);
  ellipseMode(CENTER);

  float x;// 
  float y;// 
  float z;

  x = curvePoint(  width/2+310, width/2+BETWEEN_DOTS/2, width/2+BETWEEN_DOTS/2, width/2+310, t);
  y = curvePoint( height/2-BETWEEN_DOTS/2, height/2-BETWEEN_DOTS/2, height/2+BETWEEN_DOTS/2, height/2+BETWEEN_DOTS/2, t);
  z = curvePoint( height1, 0, 0, height1, t);

  pushMatrix(); 
  translate( x, y, z);
  noStroke(); 
  sphere (12); 
  popMatrix();

  t+=0.01;
  if (t>=1) 
    t=0.0;
}

Chrisir

For the isMoving, could you give me some advice on this? I have been puzzled by this question since I asked it before.I have already tried almost 2 days on this.Now I have created a Boolean isMoving=false, but I really don’t know how to use it. Can you write a simple code to explain it? You don’t need to use my code.For example, draw a circle and make it move left and RIGHT. When it is moving left, if I press RIGHT button, it won’t work.

Remark 1

width and height don’t work before the size command in setup

so these

  flyX=width/2-BETWEEN_DOTS/2;
  flyY=height/2-BETWEEN_DOTS/2;

must be in setup after size.

(you had it before setup, so no use. That’s why the code lines didn’t work and the player didn’t appear at the start)

Before setup() only have this:

int flyX;
int flyY;

Remark 2

if (keyCode==RIGHT&&flyX>=width/2-BETWEEN_DOTS/2) {
   flyX=flyX+jumpSpeed;
   flyX=min(flyX, width/2+BETWEEN_DOTS/2);

You are going right here and flyX gets bigger, so this flyX>=width/2-BETWEEN_DOTS/2 is wrong,
we must have flyX<=width/2+BETWEEN_DOTS/2 (so with <= AND with + instead of -)

(It worked only because you had an (otherwise unnecessary) flyX=min(flyX, width/2+BETWEEN_DOTS/2); here.)

This is often the case in the other 3 code blocks!

My example

I repaired it so that the cursor key right and left work and that isMoving is used.

You can go left and right now with the player, but you can’t press DOWN in-between. That’s because of isMoving.

You have to repair DOWN and UP.

Chrisir


// Clock Game 

final int CLOCK_LONG=200;
final int CLOCK_SHORT=150;
final int DOTS_SIZE=20;
final int BETWEEN_DOTS=200;

float angleLong=0;
float angleShort=0;

final int FLY_SIZE=30;
int flyX;
int flyY;
final int jumpSpeed=4;

boolean isMoving=false; // prevent new key from executing during the player moves !!!!!!!!!!!!!!!!!!!!!!!! 
int keyIndicator = -1;  // store the key

// ------------------------------------------------------------------------

void setup() {
  size(500, 500);

  flyX=width/2-BETWEEN_DOTS/2;
  flyY=height/2-BETWEEN_DOTS/2;
}

void draw() {
  background(#24B41D);
  strokeWeight(6);
  stroke(#FC8412);

  drawHand();
  moveHand();
  drawDots();
  drawFly();
  jumping();

  // ????? debug 
  if (key==' ')
    isMoving=false;//reset
}

// ------------------------------------------------------------------------

void drawHand() {
  float longX=cos(angleLong)* CLOCK_LONG;
  float longY=sin(angleLong)* CLOCK_LONG;
  float shortX=cos(angleShort)*CLOCK_SHORT;
  float shortY=sin(angleShort)*CLOCK_SHORT;
  line(width/2, height/2, width/2+longX, height/2+longY);
  line(width/2, height/2, width/2+shortX, height/2+shortY);
}

void drawDots() {
  noStroke();
  strokeWeight(1);
  fill(0, 0, 255);
  ellipse(width/2-BETWEEN_DOTS/2, height/2-BETWEEN_DOTS/2, DOTS_SIZE, DOTS_SIZE);
  ellipse(width/2+BETWEEN_DOTS/2, height/2-BETWEEN_DOTS/2, DOTS_SIZE, DOTS_SIZE);
  ellipse(width/2-BETWEEN_DOTS/2, height/2+BETWEEN_DOTS/2, DOTS_SIZE, DOTS_SIZE);
  ellipse(width/2+BETWEEN_DOTS/2, height/2+BETWEEN_DOTS/2, DOTS_SIZE, DOTS_SIZE);
}

void moveHand() {
  angleLong=(angleLong+0.02)%TWO_PI;
  angleShort=(angleShort+0.01)%TWO_PI;
}

void drawFly() {
  fill(255);
  ellipse(flyX, flyY, 
    FLY_SIZE, FLY_SIZE);
}

void keyPressed() {
  if (isMoving)  // when it's true 
    return; // leave !!!!!!

  // when these lines are excuted, isMoving is false!!!!!

  // when we are NOT moving, check the key
  if (keyCode==UP) 
    keyIndicator = 0; 
  else if  (keyCode==DOWN)
    keyIndicator = 1;
  else if (keyCode==LEFT)
    keyIndicator = 2;
  else if (keyCode==RIGHT) 
    keyIndicator = 3;
  else return; // leave

  // we set it to true
  isMoving=true;
}

void jumping() {
  if (! isMoving)  // when it's false (The ! sign means NOT)  
    return; // leave !!!!!!

  if (keyIndicator==0) { 
    if (flyY<=height/2+BETWEEN_DOTS/2) {
      flyY=flyY-jumpSpeed;
      // flyY=max(flyY, height/2-BETWEEN_DOTS/2);
    } else {
      // movement has ended, so movement is allowed again, set isMoving to false
      isMoving=false;
    }
  } else if (keyIndicator==1) {
    if (flyY>=height/2-BETWEEN_DOTS/2) {
      flyY=flyY+jumpSpeed;
      // flyY=min(height/2+BETWEEN_DOTS/2, flyY);
    } else {
      // movement has ended, so movement is allowed again, set isMoving to false
      isMoving=false;
    }
  } else if (keyIndicator==2) {
    // LEFT 
    if (flyX>=width/2-BETWEEN_DOTS/2) {
      flyX=flyX-jumpSpeed; // decrease 
      //flyX=max(flyX, width/2-BETWEEN_DOTS/2);
    } else {
      // movement has ended, so movement is allowed again, set isMoving to false 
      isMoving=false;
    }
  } else if (keyIndicator==3) {
    // RIGHT 
    if (flyX<=width/2+BETWEEN_DOTS/2) {
      flyX=flyX+jumpSpeed; // increase
      //flyX=min(flyX, width/2+BETWEEN_DOTS/2);
    } else { 
      // movement has ended, so movement is allowed again, set isMoving to false
      isMoving=false;
    }
  }// if
  //
}//func 
//

Thank you very much. This helps me a lot. I will try to finish it. Thanks again.

1 Like

Can I ask you one more question? Now I finish everything, and I want to calculate when I press the keycode, the number of jumping will add one. But I dont know how to add only one if I only press keycode one time.My code is:
void printScore() {
textSize(height/20);
fill(0);
String message = "Number of jumps: " + numJumps;
text(message, (width-textWidth(message))/2, textAscent()+10);
if (keyCode == UP||keyCode == DOWN||keyCode == RIGHT||keyCode == LEFT) {
numJumps++;
}
}

1 Like

I like your function.

But you can also leave this part out of it:

Instead say numJumps++;

somewhere else in your code.

Where’s best?