Frogger - Need help with moving obstacles

Hi, I’m currently working on an assignment about creating the game Frogger. So I got my frog and the obstacles that my frog will need to avoid.

Regardings the movement of my obstacles, the requirement is that I need to create an global variable name offset, which control my obstacles postions, starting from 0 to bandHeight and then repeat. This variable offset need to be multiple by a value that will shift my obstacles left/right.

I created 3 types of obstacles, divided in 3 lines and the higher lines will need to be faster than the lower ones. Each line also needs to move in the opposite direction with the line above it.

I tried different method (if; for loop, etc) but could not get my obstacles to move. It would mean a lot to me if anyone can help me with this situation. You don’t need to type out the codes, I just need suggestions that can be made in order for my obstacles to move.

TIA!

This is what I got so far:

int level = 1;
float frogX, frogY, frogSize;
float bandHeight, bandWidth, MIDBAND_Y, LOWERBAND_Y;
float edge, radius,inner; 
float bottomCircles_X, bottomCircles_Y, middleSquares_X, middleSquares_Y, topCircles_X, topCircles_Y, hazardSize;
float offset, speed0, speed1, speed2;

void setup(){
  
  fullScreen();
  
  bandWidth = width;
  bandHeight = height/(level+4);
  
  frogSize = bandHeight/3;
  frogX = width/2;
  frogY = height-bandHeight/2;
  radius = frogSize/100;
  edge = frogSize;
  inner = edge+radius;
  
  hazardSize = (bandHeight*2/3);
  
}

void draw(){
 
  drawWorld();
  drawFrog(frogX, frogY, frogSize);
  
  drawHazards();
  
  displayMessage();
  
}

void drawWorld(){
  
  for (int i=1; i<level+3; i++){
    fill(0);
    noStroke();
    rect(0, MIDBAND_Y, bandWidth, bandHeight);
    MIDBAND_Y = bandHeight*i;
    if ( detectWin() ) {
      level++;
      frogX = width/2;
      frogY = height-bandHeight/2;
    }
  }
  
  fill(232, 211, 185);
  noStroke();
  rect(0, 0, bandWidth, bandHeight);
  rect(0, LOWERBAND_Y, bandWidth, bandHeight);
  LOWERBAND_Y = height-bandHeight;
  
}
  
void drawFrog (float x, float y, float diameter) {
  fill(#0FAA5E);
  stroke(0);
  strokeWeight(3);
  ellipse(x, y, diameter, diameter);
}

void moveFrog (float xChange, float yChange) {
  
  if ( objectInCanvas (frogX, frogY, frogSize) ){
    frogX += xChange;
    frogY += yChange;
    frogX = constrain(frogX, inner, width-inner);
    frogY = constrain(frogY, inner, height-inner);
  }
 
 
}

void keyPressed(){
  
  if (key == 'w' || key == 'W' || key == 'i' || key == 'I')
    moveFrog(0, -bandHeight);
  if (key == 's' || key == 'S' || key == 'h' || key == 'h')
    moveFrog(0, bandHeight);
  if (key == 'a' || key == 'A' || key == 'j' || key == 'J')
    moveFrog(-bandHeight, 0);
  if (key == 'd' || key == 'D' || key == 'l' || key == 'L')
    moveFrog(bandHeight, 0);
  
}

boolean objectInCanvas (float x, float y , float diameter){
  
  boolean insideCanvas = true;
  if ( x < 0 || x > width || y < 0 || y > height || diameter < 0)
    insideCanvas = false; // the frog is not inside the canvas
    return insideCanvas;
  
}
void drawHazards(int type, float x, float y, float size){
  
  if (type == 0){
    fill(176, 102, 96);
    ellipse(x, y, size, size);
  }
  
  if (type == 1){
    fill(217, 168, 143);
    rect(x, y, size, size);
  }
  
  if (type == 2){
    fill(234, 195, 184);
    ellipse(x, y, size, size);
  }
  
}
void drawHazards(){
  
    for (float h=0; h<level+2; h++){
      
      bottomCircles_Y = (height-bandHeight*3/2) - h*(3*bandHeight);
      if (bottomCircles_Y < bandHeight){
        bottomCircles_Y = height-bandHeight*3/2;
       }
      
      
      middleSquares_Y = (height-bandHeight*14/5) -  h*(3*bandHeight);
      if (middleSquares_Y < bandHeight){
        middleSquares_Y =  height-bandHeight*14/5;
      }
      
      topCircles_Y =  (height-bandHeight*7/2) - h*(3*bandHeight);
      if (topCircles_Y < bandHeight){
        topCircles_Y =  height-bandHeight*7/2;
      }

     bottomCircles_X = h*(3*bandHeight);
     middleSquares_X = h*(4*bandHeight) - ((bandHeight-50)/2);
     topCircles_X =  h*(5*bandHeight);
  
     drawHazards(0, bottomCircles_X, bottomCircles_Y, hazardSize);
     drawHazards(1, middleSquares_X, middleSquares_Y, hazardSize);
     drawHazards(2, topCircles_X, topCircles_Y, hazardSize);
    
    }
    
  }


void displayMessage(){
  
  fill(#A22837);
  String w = ("Level " +  str(level));
  textSize(bandHeight/2);
  text("Level " +  str(level), width/2-(textWidth(w)/2), bandHeight/2);
  
}

boolean detectWin(){
  boolean winning = false;
  if (frogY < bandHeight/2){
    winning = true;
  }
  return winning;
}
1 Like

Question:

with all due respect:

when you say

Do you really mean bandHeight?

Do we think that offset corresponds to a y value? so which lane we are on?
I assume the lanes are horizontal and the obstacles are moving left/right?

OR do you mean bandWidth?

  • Then offset would help calc the x pos and not the y pos.

Full Code

here only the last lane moves (not using offset)


int level = 1;
float frogX, frogY, frogSize;
float bandHeight, bandWidth, MIDBAND_Y, LOWERBAND_Y;
float edge, radius, inner; 
float bottomCircles_X, bottomCircles_Y, middleSquares_X, middleSquares_Y, topCircles_X, topCircles_Y, hazardSize;
float offset, speed0, speed1, speed2;

float[] i17 = new float[11]; 

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

void setup() {

  fullScreen();

  bandWidth = width;
  bandHeight = height/(level+4);

  frogSize = bandHeight/3;
  frogX = width/2;
  frogY = height-bandHeight/2;
  radius = frogSize/100;
  edge = frogSize;
  inner = edge+radius;

  hazardSize = (bandHeight*2/3);
}

void draw() {
  drawWorld();
  drawFrog(frogX, frogY, frogSize);

  drawAllHazards();

  offset++; 
  if (offset>width) {  // OR bandHeight
    offset=0;
  }

  displayMessage();
}

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

void drawWorld() {
  for (int i=1; i<level+3; i++) {
    fill(0);
    noStroke();
    rect(0, MIDBAND_Y, bandWidth, bandHeight);
    MIDBAND_Y = bandHeight*i;
    if ( detectWin() ) {
      level++;
      frogX = width/2;
      frogY = height-bandHeight/2;
    }
  }

  fill(232, 211, 185);
  noStroke();
  rect(0, 0, bandWidth, bandHeight);
  rect(0, LOWERBAND_Y, bandWidth, bandHeight);
  LOWERBAND_Y = height-bandHeight;
}

void drawFrog (float x, float y, 
  float diameter) {
  fill(#0FAA5E);
  stroke(0);
  strokeWeight(3);
  ellipse(x, y, diameter, diameter);

  ellipse(x-24, y-24, diameter/3, diameter/3);
  ellipse(x+24, y-24, diameter/3, diameter/3);
}

void moveFrog (float xChange, float yChange) {
  if ( objectInCanvas (frogX, frogY, frogSize) ) {
    frogX += xChange;
    frogY += yChange;
    frogX = constrain(frogX, inner, width-inner);
    frogY = constrain(frogY, inner, height-inner);
  }
}

void keyPressed() {
  if (key == 'w' || key == 'W' || key == 'i' || key == 'I')
    moveFrog(0, -bandHeight);
  if (key == 's' || key == 'S' || key == 'h' || key == 'h')
    moveFrog(0, bandHeight);
  if (key == 'a' || key == 'A' || key == 'j' || key == 'J')
    moveFrog(-bandHeight, 0);
  if (key == 'd' || key == 'D' || key == 'l' || key == 'L')
    moveFrog(bandHeight, 0);
}

boolean objectInCanvas (float x, float y, float diameter) {

  boolean insideCanvas = true;
  if ( x < 0 || x > width || y < 0 || y > height || diameter < 0)
    insideCanvas = false; // the frog is not inside the canvas
  return insideCanvas;
}

void drawOneHazard(int type, 
  float x, float y, 
  float size) {

  if (type == 0) {
    fill(176, 102, 96);
    ellipse(x, y, size, size);
  } else if (type == 1) {
    fill(217, 168, 143);
    rect(x, y, size, size);
  } else if (type == 2) {
    fill(234, 195, 184);
    ellipse(x, y, size, size);
  } else {
    // Error
    println("drawHazards says: severe error: type not defined. With "+type+"."); 
    exit();
  }//else
}//func 

void drawAllHazards() {
  for (int h=0; h<level+2; h++) {

    bottomCircles_Y = (height-bandHeight*3/2) - h*(3*bandHeight);
    if (bottomCircles_Y < bandHeight) {
      bottomCircles_Y = height-bandHeight*3/2;
    }


    middleSquares_Y = (height-bandHeight*14/5) -  h*(3*bandHeight);
    if (middleSquares_Y < bandHeight) {
      middleSquares_Y =  height-bandHeight*14/5;
    }

    topCircles_Y =  (height-bandHeight*7/2) - h*(3*bandHeight);
    if (topCircles_Y < bandHeight) {
      topCircles_Y =  height-bandHeight*7/2;
    }

    float speed1 = 6; 
    bottomCircles_X = (i17[h]+=speed1) + h*(3*bandHeight);
    middleSquares_X = h*(4*bandHeight) - ((bandHeight-50)/2);
    topCircles_X    = h*(5*bandHeight);

    if (i17[h]+ h*(3*bandHeight) > width-hazardSize)
      i17[h]  = (hazardSize - h*(3*bandHeight)) - random(44, 422); 

    drawOneHazard(0, bottomCircles_X, bottomCircles_Y, hazardSize);
    drawOneHazard(1, middleSquares_X, middleSquares_Y, hazardSize);
    drawOneHazard(2, topCircles_X, topCircles_Y, hazardSize);
  }
}

void displayMessage() {

  fill(#A22837);
  String w = ("Level " +  str(level));
  textSize(bandHeight/2);
  text("Level " +  str(level), width/2-(textWidth(w)/2), bandHeight/2);
}

boolean detectWin() {
  boolean winning = false;
  if (frogY < bandHeight/2) {
    winning = true;
  }
  return winning;
}

Remark

this is easy:

offset++; 
if(offset>width) {  // OR bandHeight / bandWidth ??
  offset=0; 
}