Skip to content

Instantly share code, notes, and snippets.

@bmoren
Created September 14, 2015 17:13
Show Gist options
  • Save bmoren/7f371ea05a974473d2b5 to your computer and use it in GitHub Desktop.
Save bmoren/7f371ea05a974473d2b5 to your computer and use it in GitHub Desktop.
High/Low Game Shell
var randStorage = 0; //Store a random number here
var previous = 0; //Store the previous random number
var choice; //store our choice of higher or lower based on keystroke
var score = 0; // store the players high score
function setup() {
createCanvas(windowWidth, windowHeight); //create a canvas
}
function draw() {
background(255); //reset the bkg every frame
text("RS: " + randStorage, 100,100,100,100); // write the current random # to the screen
text("SCORE: " + score, 200,200,200,200); // write the HS to the screen
}
function mousePressed() {
//we clicked.....
//we need to store our random from the last time around in the previous variable before we generate a new random number
previous = randStorage; // set our previous variable to be our current rand storage.
randStorage = round(random(1, 100)); //get a new random # and round it to a whole integer
print("RS: " + randStorage);
print("P: " + previous);
if (choice === true && randStorage > previous) { // if our choice is higher (set below) & the new random # is greater than the previous random number, we won.
//win
score = score + 1; // increase the score by 1 point
print("higherWin");
} else if(choice === false && randStorage < previous) { // if our choice is lower (set below) & the new random # is less than the previous random number, we won.
//win
print("lowerWin");
score = score + 1; // increase the score by 1 point
}else { // we lose, bummer dude....
//lose
score = 0; //set the score back to 0
}
}
function keyTyped() { //we pressed any key
if (key == "h"){ //we pressed the h key
choice = true; //set our choice to true, which represents 'higher'
}
if (key == "l"){ // we pressed the l key
choice = false; //set our choice to false, which represents 'lower'
}
print(choice);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment