Skip to content

Instantly share code, notes, and snippets.

@bmoren
Created March 8, 2017 04:11
Show Gist options
  • Save bmoren/d18013179c1873c2ced7a7420b3a0f05 to your computer and use it in GitHub Desktop.
Save bmoren/d18013179c1873c2ced7a7420b3a0f05 to your computer and use it in GitHub Desktop.
basic p5js high low with keys
var currentNumber = 0
var previousNumber = 0
var score = 0
function setup() {
createCanvas(windowWidth,windowHeight)
}
function draw() {
}
function keyPressed(){
if(key == 'H' || key == 'L'){
previousNumber = currentNumber //save the current number as the previous before getting a new one!
currentNumber = int( random(1,6) )
}else{
console.log('choose higher or lower with the h or l keys');
}
if(key == 'H'){
if(currentNumber >= previousNumber){
//we won
score++
}else{
// we lost
score = 0
}
}
if(key == 'L'){
if(currentNumber <= previousNumber){
//we won
score++
}else{
// we lost
score = 0
}
}
console.log('~~~~');
console.log(previousNumber);
console.log(currentNumber);
console.log(score);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment