rose
rose

Reputation: 49

All points won't accelerate P5.js

Only one dot can speed up right now. How do I speed up all the dots?

I'm making a dodge ball game with p5js. The player can move a yellow circle using the arrow keys, while dodging six moving red dots.

If the player reaches the exit, the game should reset and the speed of the red circles should become higher.

I tried to implement this, but only one red dot's increases. I'm curious why that is.

let height = 800;
let width = 800;
let s = 4;
let d = 50;

let gamestate = 'running';

let level = 0;
let x = 25;
let y = 25;

function setup() {
  createCanvas(width, height);
}

let dots = [
  {y: height / 4, x: 0, s: s, d: d, direction: 'h' },
  { y: height / 2, x: 0, s: s, d: d, direction: 'h' },
  { y: height / 9, x: 0, s: s, d: d, direction: 'h' },
  { x: width / 4, y: 0, s: s, d: d, direction: 'v' },
  { x: width / 2, y: 0, s: s, d: d, direction: 'v' },
  { x: width / 9, y: 0, s: s, d: d, direction: 'v' },
]

function draw() {
  switch (gamestate) {
    case 'victory':
      victoryScreen();
      break;
    case 'lose':
      loseScreen();
      break;
    default:
      keyPressed();
      background(220);
      fill(50);
      textSize(30)
      text(level, 770, 30);


      fill('green');
      square(0, 0, d);
      fill('purple');
      square(width - d, height - d, d);
      fill('yellow');
      ellipse(x, y, 40);
       


      dots.forEach((dot) => {
        fill('red')
        ellipse(dot.x, dot.y, dot.d, dot.d);
        if (dot.direction === 'h') {
          dot.x += dot.s

          if (dot.x > width || dot.x < 0) {
            dot.s *= -1;
          }
        } else if (dot.direction === 'v') {
          dot.y += dot.s

          if (dot.y > height || dot.y < 0) {
            dot.s *= -1;
          }
        }

        if (x < (dot.x + d) && (dot.x - d) < x) {
          if (y < (dot.y + d) && (dot.y - d) < y) {
            gamestate = 'lose';
          }
        }
     if (x > width-50 && y >height -50){
     x =25;
     y =25;
     dot.s *= 1.5; //only one of red dots is moving faster,, i want all the red dots' speed to be higher
     level++;
    }
      });
          

      if (x > width - d && y > height - d) {
        gamestate = 'victory'
      }
  }
}


function loseScreen() {
  noStroke();
  fill('green');
  square(0, 0, 800);
}

function victoryScreen(){
  noStroke();
  fill('black');
  square(0, 0, 800);
}

function keyPressed() {
  if (keyCode == LEFT_ARROW && keyIsPressed) {
    x = x - 5;
    if (x < d / 2) {
      x = d / 2;
    }
  } else if (keyCode == RIGHT_ARROW && keyIsPressed) {
    x = x + 5;
    if (x > (width - d / 2)) {
      x = width - d / 2;
    }
  } else if (keyCode == DOWN_ARROW && keyIsPressed) {
    y = y + 5;
    if (y > (height - d / 2)) {
      y = height - d / 2;
    }
  } else if (keyCode == UP_ARROW && keyIsPressed) {
    y = y - 5;
    if (y < d / 2) {
      y = d / 2;
    }
  }
}

Upvotes: 1

Views: 73

Answers (1)

Rabbid76
Rabbid76

Reputation: 210968

Accelerate all dots in the forEach method, but rest the position of the yellow point once after the forEach method. Note that when executed in forEach the condition (x > width-50 || y > height -50) is satisfied only for the first dot in the array, but not for the following dots because x and y has already been changed.

dots.forEach((dot) => {
    // [...]

    if (x > width-50 && y > height -50){
        dot.s *= 1.5;
    }
});
if (x > width-50 && y > height -50) {
    x =25;
    y =25;
    level++;
}

let height = 800;
let width = 800;
let s = 4;
let d = 50;

let gamestate = 'running';

let level = 0;
let x = 25;
let y = 25;

function setup() {
  createCanvas(width, height);
}

let dots = [
  {y: height / 4, x: 0, s: s, d: d, direction: 'h' },
  { y: height / 2, x: 0, s: s, d: d, direction: 'h' },
  { y: height / 9, x: 0, s: s, d: d, direction: 'h' },
  { x: width / 4, y: 0, s: s, d: d, direction: 'v' },
  { x: width / 2, y: 0, s: s, d: d, direction: 'v' },
  { x: width / 9, y: 0, s: s, d: d, direction: 'v' },
]

function draw() {
  switch (gamestate) {
    case 'victory':
      victoryScreen();
      break;
    case 'lose':
      loseScreen();
      break;
    default:
      keyPressed();
      background(220);
      fill(50);
      textSize(30)
      text(level, 770, 30);


      fill('green');
      square(0, 0, d);
      fill('purple');
      square(width - d, height - d, d);
      fill('yellow');
      ellipse(x, y, 40);
       


      dots.forEach((dot) => {
        fill('red')
        ellipse(dot.x, dot.y, dot.d, dot.d);
        if (dot.direction === 'h') {
          dot.x += dot.s

          if (dot.x > width || dot.x < 0) {
            dot.s *= -1;
          }
        } else if (dot.direction === 'v') {
          dot.y += dot.s

          if (dot.y > height || dot.y < 0) {
            dot.s *= -1;
          }
        }

        if (x < (dot.x + d) && (dot.x - d) < x) {
          if (y < (dot.y + d) && (dot.y - d) < y) {
            gamestate = 'lose';
          }
        }
        if (x > width-50 && y > height -50){
            dot.s *= 1.5;
        }
      });
      if (x > width-50 && y > height -50) {
        x =25;
        y =25;
        level++;
      }
          

      if (x > width - d && y > height - d) {
        gamestate = 'victory'
      }
  }
}

function loseScreen() {
  noStroke();
  fill('green');
  square(0, 0, 800);
}

function victoryScreen(){
  noStroke();
  fill('black');
  square(0, 0, 800);
}

function keyPressed() {
  if (keyCode == LEFT_ARROW && keyIsPressed) {
    x = x - 5;
    if (x < d / 2) {
      x = d / 2;
    }
  } else if (keyCode == RIGHT_ARROW && keyIsPressed) {
    x = x + 5;
    if (x > (width - d / 2)) {
      x = width - d / 2;
    }
  } else if (keyCode == DOWN_ARROW && keyIsPressed) {
    y = y + 5;
    if (y > (height - d / 2)) {
      y = height - d / 2;
    }
  } else if (keyCode == UP_ARROW && keyIsPressed) {
    y = y - 5;
    if (y < d / 2) {
      y = d / 2;
    }
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.min.js"></script>

Upvotes: 1

Related Questions