首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有对象的最小和最大函数

带有对象的最小和最大函数
EN

Stack Overflow用户
提问于 2019-12-11 16:41:41
回答 1查看 482关注 0票数 4

亲爱的各位,这是我在这里的第一篇文章,我最近才开始尝试编写代码,所以请善待我。

我目前正在上一门课程,在这门课上,他们给我们布置了任务。我当前的任务遇到了问题。

我将用我当前的尝试发布下面的全部代码。我觉得我已经尝试了所有的逻辑方法,但它总是失败。

任何帮助都会非常,非常感谢。

代码语言:javascript
复制
/*

Officer: 1585754
CaseNum: 303-2-49385492-1585754

Case 303 - The Case of the Crooked Attorney
Stage 3 - The Gates Bank

I’ve made an appointment for you at the Gates Bank to retrieve your safe deposit box from the vault.
Actually you will break into Torvalds’ one.

Crack the safe by doing the following:

    Whilst the mouse is moving:
    - Make CrypticSafeCombination_0 equal to the value of mouseY
    - Use the 'min' function to prevent CrypticSafeCombination_0 from going above 11

    Whilst the mouse is moving:
    - Decrement CrypticSafeCombination_1 by 2
    - Use the 'max' function to prevent CrypticSafeCombination_1 from falling below 4

    Whilst the mouse is being dragged:
    - Make CrypticSafeCombination_2 equal to the value of mouseX
    - Use the 'max' function to prevent CrypticSafeCombination_2 from falling below 2

    Whilst the mouse is moving:
    - Decrement CrypticSafeCombination_3 by 1
    - Use the 'max' function to prevent CrypticSafeCombination_3 from falling below 3

    Whilst the mouse is moving:
    - Make CrypticSafeCombination_4 equal to the value of mouseX
    - Use the 'max' function to prevent CrypticSafeCombination_4 from falling below 13



This time you'll need to create the relevant event handlers yourself.

There are many possible ways of investigating this case, but you
should use ONLY the following commands:

    - The assignment operator aka. the equals sign !
    - mouseX, mouseY
    - Incrementing +=
    - Decrementing -=
    - min, max
    - constrain

*/

//declare the variables

var CrypticSafeCombination_0;
var CrypticSafeCombination_1;
var CrypticSafeCombination_2;
var CrypticSafeCombination_3;
var CrypticSafeCombination_4;


function preload()
{
    //IMAGES WILL BE LOADED HERE

}

function setup()
{
    createCanvas(512,512);

    //initialise the variables
    CrypticSafeCombination_0 = 0;
    CrypticSafeCombination_1 = 0;
    CrypticSafeCombination_2 = 0;
    CrypticSafeCombination_3 = 0;
    CrypticSafeCombination_4 = 0;

}

///////////////////EVENT HANDLERS///////////////////

//Create event handlers here to open the safe ...

**This is my current attempt**

function mouseMoved()
{

    CrypticSafeCombination_0 = mouseY;
    min = (mouseY, 11);

    CrypticSafeCombination_1 -= 2;
    max = (2, 4);

    CrypticSafeCombination_3 -=1;
    max = (1, 3);

    CrypticSafeCombination_4 = mouseX;
    max = (mouseX, 13);
}

function mouseDragged()
{

    CrypticSafeCombination_2 = mouseX;
    max = (mouseX, 2);
}


///////////////DO NOT CHANGE CODE BELOW THIS POINT///////////////////

function draw()
{

    //Draw the safe door
    background(70);
    noStroke();
    fill(29,110,6);
    rect(26,26,width-52,width-52);

    //Draw the combination dials
    push();
    translate(120,170);
    drawDial(140,CrypticSafeCombination_0, 16);
    pop();

    push();
    translate(120,380);
    drawDial(140,CrypticSafeCombination_1, 23);
    pop();

    push();
    translate(280,170);
    drawDial(140,CrypticSafeCombination_2, 19);
    pop();

    push();
    translate(280,380);
    drawDial(140,CrypticSafeCombination_3, 15);
    pop();

    //Draw the lever
    push();
    translate(width - 125,256);
    drawLever(CrypticSafeCombination_4);
    pop();


}

function drawDial(diameter,num,maxNum)
{
    //the combination lock

    var r = diameter * 0.5;
    var p = r * 0.6;

    stroke(0);
    fill(255,255,200);
    ellipse(0,0,diameter,diameter);
    fill(100);
    noStroke();
    ellipse(0,0,diameter*0.66,diameter*0.66);
    fill(150,0,0);
    triangle(
        -p * 0.4,-r-p,
        p * 0.4,-r-p,
        0,-r-p/5
    );

    noStroke();

    push();
    var inc = 360/maxNum;

    rotate(radians(-num * inc));
    for(var i = 0; i < maxNum; i++)
    {
        push();
        rotate(radians(i * inc));
        stroke(0);
        line(0,-r*0.66,0,-(r-10));
        noStroke();
        fill(0);
        text(i,0,-(r-10));
        pop();
    }

    pop();
}

function drawLever(rot)
{
    push();
    rotate(radians(-rot))
    stroke(0);
    fill(100);
    rect(-10,0,20,100);
    ellipse(0,0,50,50);
    ellipse(0,100,35,35);
    pop();
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-12 00:06:45

您误解了如何使用function%s。min()max()是接收2个参数并返回1个参数的函数。

例如,x = min(a, b)将参数ab传递给函数min()min()查找ab的最小值并返回它。返回值被赋值给x

例如,y = max(y, c)找到yc的最大值,并将其分配给y

将其应用于您的代码:

代码语言:javascript
复制
function mouseMoved()
{ 
    // Make CrypticSafeCombination_0 equal to the value of mouseY  
    CrypticSafeCombination_0 = mouseY;
    // Use the 'min' function to prevent CrypticSafeCombination_0 from going above 11
    CrypticSafeCombination_0 = min(CrypticSafeCombination_0, 11);

    // Decrement CrypticSafeCombination_1 by 2
    CrypticSafeCombination_1 -= 2;
    // Use the 'max' function to prevent CrypticSafeCombination_1 from falling below 4
    CrypticSafeCombination_1 = max(CrypticSafeCombination_1, 4);

    // Decrement CrypticSafeCombination_3 by 1
    CrypticSafeCombination_3 -=1;
    //Use the 'max' function to prevent CrypticSafeCombination_3 from falling below 3
    CrypticSafeCombination_3 = max(CrypticSafeCombination_3, 3);

    //Make CrypticSafeCombination_4 equal to the value of mouseX
    CrypticSafeCombination_4 = mouseX;
    // Use the 'max' function to prevent CrypticSafeCombination_4 from falling below 13
    CrypticSafeCombination_4 = max(CrypticSafeCombination_4, 13);
}

function mouseDragged()
{
    // Make CrypticSafeCombination_2 equal to the value of mouseX
    CrypticSafeCombination_2 = mouseX;
    // Use the 'max' function to prevent CrypticSafeCombination_2 from falling below 2
    CrypticSafeCombination_2 = max(CrypticSafeCombination_2, 2);
}

代码语言:javascript
复制
function mouseMoved()
{ 
    // Make CrypticSafeCombination_0 equal to the value of mouseY  
    // Use the 'min' function to prevent CrypticSafeCombination_0 from going above 11
    CrypticSafeCombination_0 = min(mouseY, 11);

    // Decrement CrypticSafeCombination_1 by 2
    // Use the 'max' function to prevent CrypticSafeCombination_1 from falling below 4
    CrypticSafeCombination_1 = max(CrypticSafeCombination_1-2, 4);

    // Decrement CrypticSafeCombination_3 by 1
    //Use the 'max' function to prevent CrypticSafeCombination_3 from falling below 3
    CrypticSafeCombination_3 = max(CrypticSafeCombination_3-1, 3);

    // Make CrypticSafeCombination_4 equal to the value of mouseX
    // Use the 'max' function to prevent CrypticSafeCombination_4 from falling below 13
    CrypticSafeCombination_4 = max(mouseX, 13);
}

function mouseDragged()
{
    // Make CrypticSafeCombination_2 equal to the value of mouseX
    // Use the 'max' function to prevent CrypticSafeCombination_2 from falling below 2
    CrypticSafeCombination_2 = max(mouseX, 2);
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59281763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档