xxxxxxxxxx
161
/*
RULES:
when the screen says playing both players can press their button
player one has A and player two has L
you can bet point by holding the button for x amount of time
at the end of the clock the player with the most points wins
UNLESS the difference in time held is bigger than 3000 (3 seconds),
then the player with the lower score wins
*/
const ROUNDTIME = 8 * 1000;
const RULE_THRESHOLD = 2 * 1000;
// game has 3 states: pause, round and results
let gameState = 0
let phaseTime = 0;
let phaseStart = 0;
let p1bet = 0, p2bet = 0;
let p1counter, p2counter;
let winner;
function setup() {
createCanvas(400, 400);
textSize(48)
textStyle(BOLD)
textAlign(CENTER)
noStroke()
}
function draw() {
phaseTime = millis() - phaseStart;
if(gameState==0){
background(255)
fill(0)
text(ceil((ROUNDTIME-phaseTime)/1000), width * 0.5,height/2)
text('START IN', width/2,height/2 - 50)
}
if(gameState==1){
background(0)
fill(255)
text(ceil((ROUNDTIME-phaseTime)/1000), width/2,height/2)
text('PLAYING', width/2,height/2 + 50)
}
if(gameState==2){
background(100);
fill(255)
if(winner==0){
rect(0,0,width*0.5,height)
fill(0)
text(p1bet, width*0.25,height*0.5)
text('A', width * 0.25, height * 0.5 + 50)
text('WINS', width * 0.25, height * 0.5 + 100)
text(p2bet, width*0.75,height*0.5)
text('L', width * 0.75, height * 0.5 + 50)
}
else{
rect(width*0.5,0,width*0.5,height)
fill(0)
text(p1bet, width*0.25,height*0.5)
text('A', width * 0.25, height * 0.5 + 50)
text(p2bet, width*0.75,height*0.5)
text('L', width * 0.75, height * 0.5 + 50)
text('WINS', width * 0.75, height * 0.5 + 100)
}
}
if(phaseTime>ROUNDTIME){
phaseStart = millis();
gameState = (gameState + 1) %3
if(gameState==1){
p1bet = 0
p2bet = 0
p1counter = millis()
p2counter = millis()
}
if(gameState==2){
if(keyIsDown(65)){
p1bet += round(millis() - p1counter)
}
if(keyIsDown(76)){
p2bet += round(millis() - p2counter)
}
// if small difference the higher score wins
if(p1bet>p2bet){
winner = 0;
} else{
winner = 1;
}
//otherwise the other one
if( abs(p1bet-p2bet) > RULE_THRESHOLD ){
winner = 1 - winner;
}
}
}
}
function keyPressed(){
if(gameState == 1){
if(key == "a" || key == "A"){
print('press a')
p1counter = millis()
}
if(key == "l" || key == "L"){
print('press l')
p2counter = millis()
}
}
}
function keyReleased(){
if(gameState == 1){
if(key == "a" || key == "A"){
print('release a')
p1bet += round(millis() - p1counter);
}
if(key == "l" || key == "L"){
print('release l')
p2bet += round(millis() - p2counter)
}
}
}