jeudi 17 mars 2022

How do I reset a random value after pressing a key in p5.js?

I have to make a "choose your own adventure" type code for one of my classes and for my idea, I decided to make the setting take place in a carnival and have the user choose a game to play. I'm currently trying to recreate the first carnival game I chose, which is balloon pop. For this, I simply have 6 ellipses representing balloons and I want the user to press "t" to throw a dart, and there would be a 50/50 chance for them to score. How I tried doing this is by setting a variable called "outcome" equal to "int(random(1, 10))" and if the generated value is between 1-5, then they pop a balloon but if it's between 6-10, then they miss. The problem is I have the random value in the setup function and it only generates a value once. I want it so that the value would generate a new number every time the user presses "t" to throw a dart, and I'm having trouble figuring out how to do that. Is there a way generate a new value each time?

Here's the balloon pop portion of my code:

let scenes = [];
let currentScene = 0;
let outcome;
let popp = 0;

function setup() {
  createCanvas(windowWidth, windowHeight);
  colorMode(HSB);
  
  outcome = int(random(1, 10));
  
  textFont("Paytone One");
  textSize(width/50);
  
  scenes[0] = {
    text: "Throw darts at the balloons in order to pop them. \nWhat would you like to do? \n(T)hrow Dart",
    keys: ["t"],
    nextPages: [1],
  }
  
  scenes[1] = {
    text: "Throw darts at the balloons in order to pop them. \nWhat would you like to do? \n(T)hrow Dart",
    keys: ["t"],
    nextPages: [0, 1],
  }
  
}

function draw() {
  background(185,100,100);
  noStroke();
  
  highScore();
  
  
  fill("black");
  text(scenes[currentScene].text, 40, 40);
  
  if (key == "t"){
    if(outcome >= 1 && outcome <= 5){
      fill(0,100,100);
      rectMode(CENTER);
      rect(width/2, height/2 + 50, width/1.5, height/2);
      balloons(tL);
      popp = 1;
    }else{
      text("You missed!", width/2.25, height/3.5)
      fill(0,100,100);
      rectMode(CENTER);
      rect(width/2, height/2 + 50, width/1.5, height/2);
      balloons();
    }
  }
  else {
    fill(0,100,100);
    rectMode(CENTER);
    rect(width/2, height/2 + 50, width/1.5, height/2);
    
    balloons();
  }
  console.log(outcome);
}

function keyPressed() {
  for (let i = 0; i < scenes[currentScene].keys.length; i++) {
    if (key == scenes[currentScene].keys[i]) {
      currentScene = scenes[currentScene].nextPages[i];
    }
  }
}

function highScore(points = 0) {
  fill("black");
  text("Points: " + points, width - 100, 20);
}

function balloons(tL = 1, tM = 1, tR = 1, bL = 1, bM = 1, bR = 1){
  push();
  fill(60, 100, 100, tL);
  ellipse(width/3.5, height/2.6+50, 120, 100);
  
  fill(175, 100, 100, tM);
  ellipse(width/2, height/2.6+50, 120, 100);
  
  fill(300, 100, 100, tR);
  ellipse(width/1.4, height/2.6+50, 120, 100);
  
  fill(90, 100, 100, bL);
  ellipse(width/3.5, height/1.6+50, 120, 100);

  fill(45, 100, 100, bM);
  ellipse(width/2, height/1.6+50, 120, 100);
  
  fill(215, 100, 100, bR);
  ellipse(width/1.4, height/1.6+50, 120, 100);
  pop();
}

let tL = 0; let tM = 0;
let tR = 0; let bL = 0;
let bM = 0; let bR = 0;



Aucun commentaire:

Enregistrer un commentaire