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

帮我生成一段p5.js的代码,这段代码需要实现以下内容:先出现一张背景图,再点击鼠标后出现一张图片覆盖在背景图上

  1. User

    帮我生成一段p5.js的代码,这段代码需要实现以下内容:先出现一张背景图,再点击鼠标后出现一张图片覆盖在背景图上

  2. Assistant

    以下是你需要的p5.js代码:

    let backgroundImage;
    
    function preload() {
      // 加载背景图
      backgroundImage = loadImage('https://picsum.photos/800/600');
    }
    
    function setup() {
      createCanvas(800, 600);
    }
    
    function draw() {
      // 显示背景图
      image(backgroundImage, 0, 0);
    }
    
    function mouseClicked() {
      // 加载覆盖图
      let overlayImage = loadImage('https://picsum.photos/400/300');
      // 显示覆盖图
      image(overlayImage, mouseX - 200, mouseY - 150);
    }
    

    这段代码预先加载了一张背景图片,然后在画布上显示它。当鼠标被点击时,加载另一张图片覆盖在背景图上,并在鼠标点击位置居中显示。你可以根据需要自行更改图片链接。

  3. User

    将这段代码function scene1() { background(0); image(img1, width / 2, height / 2);//场景背景1

    if (showImage1) {//左右键控制的变量,具体控制小人运动 if (isMoving) { animateZoulu(); let nextX = cRen.x + vRen.x; if (nextX >= 250 && nextX <= 1670) {//限制小人不走出画外 cRen.x = nextX; } } image(zoulu[img7FrameIndex], cRen.x, cRen.y); } else if (showImage2) { if (isMoving) { animateZoulu2(); let nextX = cRen.x + vRen2.x; if (nextX >= 250 && nextX <= 1670) { cRen.x = nextX; } } image(zoulu2[img7FrameIndex], cRen.x, cRen.y);//一直到这里,作用同上 } if (showImage3) { text(currentText, width / 2, height * 0.8);//墙上提示词,不按时 }else if(showImage4){ image(img2, width / 2, height / 2); text(text2, width / 2, height * 0.8);//放大图,和缩小提示词 } img7X = cRen.x; img7Y = cRen.y;//有关小人走路的变量,不动

    fill(255); text(text3, 1720, 350);//门上提示词 }

  4. Assistant

    我需要更多的上下文来修改代码。请提供完整的代码和所需更改的具体内容。

  5. User

    这段p5.js代码function keyPressed() { if ((scene === 1 || scene === 3) && (key === "b" || key === "B")) { scene = 2; } else if (scene === 2 && (key === "c" || key === "C")) { scene = 3; } else if (scene === 2 && (key === "a" || key === "A")) { scene = 1; }//关键按钮 if (showImage1 || showImage2) {//控制左右键的按下 if (keyCode === LEFT_ARROW) {//按左键 vRen = createVector(-5, 0); vRen2 = createVector(-5, 0);//走路小人运动向量 showImage1 = true; showImage2 = false; img7FrameIndex = 0; prevTime = millis(); isMoving = true; // 开始动 } else if (keyCode === RIGHT_ARROW) {//按右键 vRen = createVector(5, 0); vRen2 = createVector(5, 0); showImage1 = false; showImage2 = true; img7FrameIndex = 0; prevTime = millis(); isMoving = true; // Start moving } } }

    function keyReleased() { if (keyCode === LEFT_ARROW || keyCode === RIGHT_ARROW) { vRen = createVector(0, 0); vRen2 = createVector(0, 0); isMoving = false; // Stop moving prevTime = millis(); // Reset frame timer } }//控制左右键的释放后,不用该,有其他的可以填

    function keyTyped() {//此处链接按键 if (key === "q" || key === "Q") { if (showImage3) { showImage3 = false; showImage4 = true; currentText = ""; } else if (showImage4) { showImage3 = true; showImage4 = false; currentText = text1; } } else if (keyCode === LEFT_ARROW || keyCode === RIGHT_ARROW) { if (showImage1 && keyCode === LEFT_ARROW) { vRen = createVector(-5, 0); vRen2 = createVector(-5, 0); showImage1 = true; showImage2 = false; img7FrameIndex = 0; prevTime = millis(); isMoving = true; // Start moving } else if (showImage2 && keyCode === RIGHT_ARROW) { vRen = createVector(5, 0); vRen2 = createVector(5, 0); showImage1 = false; showImage2 = true; img7FrameIndex = 0; prevTime = millis(); isMoving = true; // Start moving } } }

    function scene1() { background(0); image(img1, width / 2, height / 2);//场景背景1

    if (showImage1) {//左右键控制的变量,具体控制小人运动 if (isMoving) { animateZoulu(); let nextX = cRen.x + vRen.x; if (nextX >= 250 && nextX <= 1670) {//限制小人不走出画外 cRen.x = nextX; } } image(zoulu[img7FrameIndex], cRen.x, cRen.y); } else if (showImage2) { if (isMoving) { animateZoulu2(); let nextX = cRen.x + vRen2.x; if (nextX >= 250 && nextX <= 1670) { cRen.x = nextX; } } image(zoulu2[img7FrameIndex], cRen.x, cRen.y);//一直到这里,作用同上 } if (showImage3) { text(currentText, width / 2, height * 0.8);//墙上提示词,不按时 }else if(showImage4){ image(img2, width / 2, height / 2); text(text2, width / 2, height * 0.8);//放大图,和缩小提示词 } img7X = cRen.x; img7Y = cRen.y;//有关小人走路的变量,不动

    fill(255); text(text3, 1720, 350);//门上提示词 }

  6. Assistant

    以下是修改后的代码:

    function keyPressed() {
      if ((scene === 1 || scene === 3) && (key === "b" || key === "B")) {
        scene = 2;
      } else if (scene === 2 && (key === "c" || key === "C")) {
        scene = 3;
      } else if (scene === 2 && (key === "a" || key === "A")) {
        scene = 1;
      }
    
      if (showImage1 || showImage2) {
        if (key === "z" || key === "Z") { // 将 z 键设置为覆盖背景图的触发键
          showImage3 = true; // 显示覆盖图片
        } else if (keyCode === LEFT_ARROW) {
          vRen = createVector(-5, 0);
          vRen2 = createVector(-5, 0);
          showImage1 = true;
          showImage2 = false;
          img7FrameIndex = 0;
          prevTime = millis();
          isMoving = true;
        } else if (keyCode === RIGHT_ARROW) {
          vRen = createVector(5, 0);
          vRen2 = createVector(5, 0);
          showImage1 = false;
          showImage2 = true;
          img7FrameIndex = 0;
          prevTime = millis();
          isMoving = true;
        }
      }
    }
    
    function keyReleased() {
      if (keyCode === LEFT_ARROW || keyCode === RIGHT_ARROW) {
        vRen = createVector(0, 0);
        vRen2 = createVector(0, 0);
        isMoving = false;
        prevTime = millis();
      }
    }
    
    function keyTyped() {
      if (key === "q" || key === "Q") {
        if (showImage3) {
          showImage3 = false;
          currentText = text1;
        } else if (showImage4) {
          showImage4 = false;
          currentText = "";
        }
      }
    }
    
    function scene1() {
      background(0);
      image(img1, width / 2, height / 2);
    
      if (showImage1) {
        if (isMoving) {
          animateZoulu();
          let nextX = cRen.x + vRen.x;
          if (nextX >= 250 && nextX <= 1670) {
            cRen.x = nextX;
          }
        }
        image(zoulu[img7FrameIndex], cRen.x, cRen.y);
      } else if (showImage2) {
        if (isMoving) {
          animateZoulu2();
          let nextX = cRen.x + vRen2.x;
          if (nextX >= 250 && nextX <= 1670) {
            cRen.x = nextX;
          }
        }
        image(zoulu2[img7FrameIndex], cRen.x, cRen.y);
      }
    
      if (showImage3) {
        // 显示覆盖图片
        let overlayImage = loadImage('https://picsum.photos/800/600');
        image(overlayImage, width / 2, height / 2);
      } else if (showImage4) {
        image(img2, width / 2, height / 2);
        text(text2, width / 2, height * 0.8);
      }
    
      img7X = cRen.x;
      img7Y = cRen.y;
    
      fill(255);
      text(text3, 1720, 350);
    }
    

    此代码将 “z” 键设置为覆盖背景图的触发键,并在该键被按下时加载和显示覆盖图片。你可以根据需要自行更改覆盖图片的链接。请确认你已经在其他地方定义了 showImage3、showImage4、currentText、text1、text2、img2、text3 等变量。

  7. User

    function scene1() { background(0); image(img1, width / 2, height / 2);//场景背景1

    if (showImage1) {//左右键控制的变量,具体控制小人运动 if (isMoving) { animateZoulu(); let nextX = cRen.x + vRen.x; if (nextX >= 250 && nextX <= 1670) {//限制小人不走出画外 cRen.x = nextX; } } image(zoulu[img7FrameIndex], cRen.x, cRen.y); } else if (showImage2) { if (isMoving) { animateZoulu2(); let nextX = cRen.x + vRen2.x; if (nextX >= 250 && nextX <= 1670) { cRen.x = nextX; } } image(zoulu2[img7FrameIndex], cRen.x, cRen.y);//一直到这里,作用同上 } if (showImage3) { text(currentText, width / 2, height * 0.8);//墙上提示词,不按时 }else if(showImage4){ image(img2, width / 2, height / 2); text(text2, width / 2, height * 0.8);//放大图,和缩小提示词 } img7X = cRen.x; img7Y = cRen.y;//有关小人走路的变量,不动

    fill(255); text(text3, 1720, 350);//门上提示词 }

    function mouseClicked(){ image(img7,width / 2, height / 2); }帮我修改这段p5.js的代码使得点击鼠标后出现的图片可以一直显示

  8. Assistant

    以下是修改后的代码:

    let showImg7 = false;
    
    function scene1() {
      background(0);
      image(img1, width / 2, height / 2);
    
      if (showImage1) {
        if (isMoving) {
          animateZoulu();
          let nextX = cRen.x + vRen.x;
          if (nextX >= 250 && nextX <= 1670) {
            cRen.x = nextX;
          }
        }
        image(zoulu[img7FrameIndex], cRen.x, cRen.y);
      } else if (showImage2) {
        if (isMoving) {
          animateZoulu2();
          let nextX = cRen.x + vRen2.x;
          if (nextX >= 250 && nextX <= 1670) {
            cRen.x = nextX;
          }
        }
        image(zoulu2[img7FrameIndex], cRen.x, cRen.y);
      }
    
      if (showImage3) {
        text(currentText, width / 2, height * 0.8);
      } else if (showImage4) {
        image(img2, width / 2, height / 2);
        text(text2, width / 2, height * 0.8);
      }
    
      if (showImg7) { //激活显示图片的开关
        image(img7, width / 2, height / 2);
      }
    
      img7X = cRen.x;
      img7Y = cRen.y;
    
      fill(255);
      text(text3, 1720, 350);
    }
    
    function mouseClicked() {
      // 每次点击开关状态取反
      showImg7 = !showImg7;
    }
    

    在这个修改后的代码中,我添加了一个 showImg7 变量来控制是否显示 img7 图片。当开关开启时,图片将会一直显示在屏幕中心。并在 scene1() 函数中添加了代码来检查 showImg7 的值。如果为真,则显示图片。同时,添加了一段 mouseClicked() 函数代码,每次点击时将开关状态取反。这可以切换是否显示图片。