Mario game help! Move background screen when image is moved?

No! You can certainly have a background image that is larger than your sketch! I took a bit of time to code you a working example:

float background_x_offset;
float mario_x_position;
float mario_y_position;
PImage bg;

void setup(){
  size(200,200);
  spoofBackgroundImage();
background_x_offset = 0;
mario_x_position = width/2;
}

void draw(){
  background(0);
  if( kd[0] ) mario_x_position--;
  if( kd[1] ) mario_x_position++;
  
  if( mario_x_position > 3*width/4 && background_x_offset < 600 ){
    mario_x_position--;
    background_x_offset++;
  }
  mario_x_position = constrain(mario_x_position,10,190);
  image(bg, -background_x_offset,0 );
  fill(255,0,0);
  ellipse(mario_x_position,170,20,20);
  fill(255);
  ellipse(mario_x_position,170,10,10);
  
}
  
void keyPressed(){
  k(true);
}

void keyReleased(){
  k(false);
}
 
int[] ks = {LEFT, RIGHT};
boolean[] kd = {false,false};
 
void k(boolean b){
 for( int i = 0; i < ks.length; i++){
   if( ks[i] == keyCode ) kd[i] = b;
 }
}
  
void spoofBackgroundImage(){
  PGraphics pg = createGraphics(800,200);
  pg.beginDraw();
  pg.background(100,100,200);
  pg.noStroke();
  pg.fill(255);
  for( int i = 0; i < 20; i++){
    pg.ellipse(random(800), random(200),30,20);
  }
  pg.stroke(0);
  for( int i = 0; i < 40; i++){
    pg.fill(255,5*i,100);
    pg.rect(20*i,180,20,20);
  }
  pg.endDraw();
  bg = pg.get();
}
2 Likes