Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

let image1; let image2; let image3; let image4; let overlayImage; let currentImageIndex…

  1. User

    let image1; let image2; let image3; let image4; let overlayImage; let currentImageIndex = 0; let falling = false; let broken = false; let overlayY = 0; let oriPostionX=0; let oriPostionY=0;

    let px= 0; let py =0;

    function preload() { // 加载图片 image1 = loadImage('loudao.jpg'); //楼道 image2 = loadImage('fangjian4.jpg');//第四个房间 image3 = loadImage('datu4.jpg'); // 放大的窗户 pingzi = loadImage('pingzi.png'); // 瓶子的图片 image4 = loadImage('kaichuang.jpg'); smashSound = loadSound('glass_smash.mp3');

    oriPostionX=windowWidth/2 +200; oriPostionY=windowHeight/2 +100; px=oriPostionX; py=oriPostionY;//这里是原先的位置,<<<<< acc=0; }

    function setup() { createCanvas(windowWidth,windowHeight); }

    function draw() {

    // 根据当前图片索引绘制对应的图像 if (currentImageIndex === 0) { background(0); imageMode(CENTER); image(image1, windowWidth/2, windowHeight/2); imageMode(CORNER);

    } else if (currentImageIndex === 1) { background(10, 24, 37); imageMode(CENTER); image(image2, windowWidth/2, windowHeight/2); imageMode(CORNER); } else if(currentImageIndex == 2){ // 在画面中心绘制第三张图片,并将尺寸扩大1.5倍 background(10, 24, 37); imageMode(CENTER); image(image3, windowWidth/2, windowHeight/2); imageMode(CORNER); }

    if(currentImageIndex ==2 && falling) {
      
      imageMode(CENTER);
      if(broken==false){
        image(pingzi,px,py);
        py+=acc*0.1;
        acc++;    //<<<<acc代表加速度
        if(abs(py-windowHeight*7/8)<20){ //<<<<<<<<<<<<当py的坐标临近窗口高度7/8的时候触发事件
          smashSound.play();
          broken = true;
        }
      }else{
        image(pingzi,px,py);
      }
    
    imageMode(CORNER);
    

    } }

    function keyPressed() { if (key === 'd' || key === 'D') { currentImageIndex = (currentImageIndex + 1) % 3; // 切换图片索引

    if(broken){
      falling=false;
      broken=false;
      px=oriPostionX;
      py=oriPostionY;//这里是原先的位置,<<<<<
      acc=0;
    
    }
    

    } if (key === 'q' || key === 'Q') { falling = true; } }

    function windowResized() { resizeCanvas(windowWidth, windowHeight); } 帮我这段代码里添加一段内容,具体要求如下:能够识别外部声音大小,当音量达到90分贝时,可以从'pingzi.png'出现下一张图片'kaichuang.jpg'

  2. Assistant

    可以通过p5.js中的micLevel()函数获取外部声音的大小。同时,在播放下一张图片时,可以使用setTimeout()函数来延时一段时间后切换图片,以免音量瞬间高峰引起图片频繁切换。

    以下是修改后的代码:

    let image1; let image2; let image3; let image4; let overlayImage; let currentImageIndex = 0; let falling = false; let broken = false; let overlayY = 0; let oriPostionX = 0; let oriPostionY = 0;

    let px = 0; let py = 0; let acc = 0; let loudnessThreshold = 90; let timer;

    function preload() { // 加载图片 image1 = loadImage('loudao.jpg'); //楼道 image2 = loadImage('fangjian4.jpg');//第四个房间 image3 = loadImage('datu4.jpg'); // 放大的窗户 pingzi = loadImage('pingzi.png'); // 瓶子的图片 image4 = loadImage('kaichuang.jpg'); smashSound = loadSound('glass_smash.mp3');

    oriPostionX = windowWidth / 2 + 200; oriPostionY = windowHeight / 2 + 100; px = oriPostionX; py = oriPostionY; //这里是原先的位置,<<<<< acc = 0; }

    function setup() { createCanvas(windowWidth, windowHeight); }

    function draw() {

    // 根据当前图片索引绘制对应的图像 if (currentImageIndex === 0) { background(0); imageMode(CENTER); image(image1, windowWidth / 2, windowHeight / 2); imageMode(CORNER);

    } else if (currentImageIndex === 1) { background(10, 24, 37); imageMode(CENTER); image(image2, windowWidth / 2, windowHeight / 2); imageMode(CORNER); } else if (currentImageIndex == 2) { // 在画面中心绘制第三张图片,并将尺寸扩大1.5倍 background(10, 24, 37); imageMode(CENTER); image(image3, windowWidth / 2, windowHeight / 2); imageMode(CORNER); }

    if (currentImageIndex == 2 && falling) {

    imageMode(CENTER);
    if (broken == false) {
      image(pingzi, px, py);
      py += acc * 0.1;
      acc++; //<<<<acc代表加速度
      if (abs(py - windowHeight * 7 / 8) < 20) { //<<<<<<<<<<<<当py的坐标临近窗口高度7/8的时候触发事件
        smashSound.play();
        broken = true;
        if (micLevel() > loudnessThreshold) {
          timer = setTimeout(function() {
            currentImageIndex = 3;
          }, 2000); //延迟切换图片
        }
      }
    } else {
      image(pingzi, px, py);
    }
    
    imageMode(CORNER);
    

    } }

    function keyPressed() { if (key === 'd' || key === 'D') { currentImageIndex = (currentImageIndex + 1) % 3; // 切换图片索引

    if (broken) {
      falling = false;
      broken = false;
      px = oriPostionX;
      py = oriPostionY; //这里是原先的位置,<<<<<
      acc = 0;
    
    }
    

    } if (key === 'q' || key === 'Q') { falling = true; } }

    function windowResized() { resizeCanvas(windowWidth, windowHeight); }