How to have my image move horizontally?

I’m working on my game that’s based on the chrome dino run game that plays whenever you’re offline.
However, I’m facing a problem, I can’t get my image to move the way the dino does in the game.
I’m sorry for bothering you, but help or links to a useful resourceful would be nice.
My Code:

float bunHeight = 250, bunPosX = 40;
float speed = 4;
PImage bun;
PImage bg;
boolean jump=false;
int score = 0;
int highscore = 0;

void setup() {
  size(500, 500);
  //protag
  bun = loadImage("Bun.png");
  //bg
  bg = loadImage("background.png");
  println(" use UP ARROW key ");
}
void draw() {
  ///background?
  background(bg);
  fill(255);
  rect(250, 245, 50, 50);
  //draw the bun/protag
  image(bun, bunPosX, 10+bunHeight);
  //High Score Text
  fill(255, 0, 0);
  textSize(20);
  text("High Score: " + highscore, 70, 20); //display high score in 
  //enables jump
  if (jump) {
    bunHeight -= speed;
    if (bunHeight  < 150) speed = -speed;
    if (bunHeight > height/2) {
      speed = -speed;
      jump = false;
    }
  }
}
//Enables Jump by pressing up
void keyReleased() { 
  if ( keyCode == UP) 
    jump = true;
}

Bun
Here’s bun image

1 Like

The dinosaur doesn’t move horizontally! Instead, obstacles move horizontally towards the dinosaur!

Keep the dino in place, and change the X position of the obstacles you are drawing…

1 Like

Oh.
OH!
OHHH!
I understand a bit
Can you explain a bit more, please?

Example:

class Foe {
   float x, y, w, h;
   Foe(){
     x = width;
     y = 300;
     w = 40;
     h = 40;
   }
   void draw(){
     x-=10;
     if( x < -w ){
       x = width;
     }
     fill(255,0,0);
     noStroke();
     rect(x,y-h,w, h); 
   }
}
     

float x=100, y=300, dy=0, ddy=0.32, w=40, h=100;

Foe foe;

void setup(){
  size(800,400);
  foe = new Foe();
}

void draw(){
  background(0);
  dy+=ddy;
  y+=dy;
  if( y >= 300 ){
    dy = 0;
    y = 300;
  }
  fill(0,200,0);
  noStroke();
  rect(x-w/2, y-h, w, h);
  stroke(255);
  line(0,300,width,300);
  foe.draw();
}

void mousePressed(){
  if( y == 300 ){
    dy = -10;
  }
}

This might be too much help.