Skip to content

Instantly share code, notes, and snippets.

@dannycallaghan
Created April 23, 2019 08:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dannycallaghan/a83f42af6042768284afccf1b887dd8a to your computer and use it in GitHub Desktop.
Save dannycallaghan/a83f42af6042768284afccf1b887dd8a to your computer and use it in GitHub Desktop.
Case 303 - Stage 2
/*
Officer: 8018788
CaseNum: 303-2-34839955-8018788
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:
When the mouse button is pressed:
- Make RestrictedSafeCombination_0 equal to the value of mouseX
- Use the 'min' function to prevent RestrictedSafeCombination_0 from going above 8
When the mouse button is pressed:
- Decrement RestrictedSafeCombination_1 by 3
- Use the 'constrain' function to prevent RestrictedSafeCombination_1 from falling below 3 and going above 14
Whilst the mouse is moving:
- Make RestrictedSafeCombination_2 equal to the value of mouseY
- Use the 'constrain' function to prevent RestrictedSafeCombination_2 from falling below 3 and going above 17
Whilst the mouse is being dragged:
- Decrement RestrictedSafeCombination_3 by 1
- Use the 'max' function to prevent RestrictedSafeCombination_3 from falling below 2
Whilst the mouse is being dragged:
- Make RestrictedSafeCombination_4 equal to the value of mouseX
- Use the 'min' function to prevent RestrictedSafeCombination_4 from going above 78
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 RestrictedSafeCombination_0;
var RestrictedSafeCombination_1;
var RestrictedSafeCombination_2;
var RestrictedSafeCombination_3;
var RestrictedSafeCombination_4;
function preload()
{
//IMAGES WILL BE LOADED HERE
}
function setup()
{
createCanvas(512,512);
//initialise the variables
RestrictedSafeCombination_0 = 0;
RestrictedSafeCombination_1 = 0;
RestrictedSafeCombination_2 = 0;
RestrictedSafeCombination_3 = 0;
RestrictedSafeCombination_4 = 0;
}
///////////////////EVENT HANDLERS///////////////////
//Create event handlers here to open the safe ...
function mousePressed()
{
// spotlight.x = max(spotlight.x, 0);
// When the mouse button is pressed:
// - Make RestrictedSafeCombination_0 equal to the value of mouseX
// - Use the 'min' function to prevent RestrictedSafeCombination_0 from going above 8
//RestrictedSafeCombination_0 = min(mouseX, 8);
RestrictedSafeCombination_0 = mouseX;
RestrictedSafeCombination_0 = min(RestrictedSafeCombination_0, 8);
// When the mouse button is pressed:
// - Decrement RestrictedSafeCombination_1 by 3
// - Use the 'constrain' function to prevent RestrictedSafeCombination_1 from falling below 3 and going above 14
//RestrictedSafeCombination_1 = constrain(RestrictedSafeCombination_1 - 3, 3, 14);
RestrictedSafeCombination_1 -= 3;
RestrictedSafeCombination_1 = constrain(RestrictedSafeCombination_1, 3, 14);
}
function mouseMoved()
{
//console.log("mouseMoved", mouseX, mouseY);
// Whilst the mouse is moving:
// - Make RestrictedSafeCombination_2 equal to the value of mouseY
// - Use the 'constrain' function to prevent RestrictedSafeCombination_2 from falling below 3 and going above 17
//RestrictedSafeCombination_2 = constrain(mouseY, 3, 17);
RestrictedSafeCombination_2 = mouseY;
RestrictedSafeCombination_2 = constrain(RestrictedSafeCombination_2, 3, 17);
}
function mouseDragged()
{
//console.log("mouseDragged", mouseX, mouseY);
// Whilst the mouse is being dragged:
// - Decrement RestrictedSafeCombination_3 by 1
// - Use the 'max' function to prevent RestrictedSafeCombination_3 from falling below 2
//RestrictedSafeCombination_3 = max(RestrictedSafeCombination_3 - 1, 2);
RestrictedSafeCombination_3 -= 1;
RestrictedSafeCombination_3 = max(RestrictedSafeCombination_3, 2);
// Whilst the mouse is being dragged:
// - Make RestrictedSafeCombination_4 equal to the value of mouseX
// - Use the 'min' function to prevent RestrictedSafeCombination_4 from going above 78
//RestrictedSafeCombination_4 = min(mouseX, 78);
RestrictedSafeCombination_4 = mouseX;
RestrictedSafeCombination_4 = min(RestrictedSafeCombination_4, 78);
}
///////////////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,RestrictedSafeCombination_0, 11);
pop();
push();
translate(120,380);
drawDial(140,RestrictedSafeCombination_1, 17);
pop();
push();
translate(280,170);
drawDial(140,RestrictedSafeCombination_2, 22);
pop();
push();
translate(280,380);
drawDial(140,RestrictedSafeCombination_3, 13);
pop();
//Draw the lever
push();
translate(width - 125,256);
drawLever(RestrictedSafeCombination_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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment