gpt4 book ai didi

javascript - 带有对象的 min 和 max 函数

转载 作者:行者123 更新时间:2023-12-01 16:10:41 25 4
gpt4 key购买 nike

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

我目前正在上一门类(class),在该类(class)上,他们给了我们任务要做。我当前的任务有问题。

我将使用我当前的尝试发布下面的整个代码。我觉得我已经以各种合乎逻辑的方式尝试了这个,但它总是以失败告终。

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

/*

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();
}

最佳答案

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

例如x = min(a, b)传递参数 ab到函数 min() . min()a 的最小值和 b并返回它。返回值赋值给x .

例如y = max(y, c)y 的最大值和 c并将其分配给 y .

将其应用于您的代码:

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);
}

或者

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);
}

关于javascript - 带有对象的 min 和 max 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59281763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com