xxxxxxxxxx
294
let Val = 0;
let alpha = 255;
let left = 0;
let right = 0;
let gameState = "start";
var clicks = 0;
var currentDollars = 0;
var totalDollars = 0;
var countButtonClicks = 0;
circleWidth = 20;
circleHeight = 25;
let cloud1;
let cloud2;
function preload() {
instructionsFont = loadFont("beanbagFont.ttf");
gameFont = loadFont("gemunuLibreFont.ttf");
popSound = loadSound("poppingSound.wav");
collectSound = loadSound("collect.mp3");
gamePic = loadImage("clouds.jpg");
endPic = loadImage("endBackground.jpg");
}
function setup() {
createCanvas(windowWidth, windowHeight);
cloud1 = new Cloud(10, 120);
cloud2 = new Cloud(240, 150);
inflateButton = createButton("INFLATE");
collectButton = createButton("COLLECT");
replayButton = createButton("REPLAY");
inflateButton.style("color:orange");
collectButton.style("color:orange");
inflateButton.position(250, 400);
inflateButton.mousePressed(inflate);
collectButton.position(350, 400);
collectButton.mousePressed(collect);
replayButton.position(250, 250);
replayButton.hide();
inflateButton.hide();
collectButton.hide();
restartGame();
}
function draw() {
// one value from Arduino controls the background's red color
// Val = map(Val, 0, 1023, 0, 255);
// background(Val, Val, Val);
// fill(20);
// circle(200, 200, Val);
// the other value controls the text's transparency value
//fill(255, 0, 255, map(alpha, 0, 1023, 0, 255));
if (!serialActive) {
text("Press Space Bar to select Serial Port", 20, 30);
} else {
text("Connected", 20, 30);
}
if (gameState == "start") {
drawInstructions();
// get start button from arduino
} else if (gameState == "playing") {
drawGame();
} else if (gameState == "end") {
drawEndScreen();
}
// click on one side of the screen, one LED will light up
// click on the other side, the other LED will light up
if (mouseIsPressed) {
if (mouseX <= width / 2) {
left = 1;
} else {
right = 1;
}
} else {
left = right = 0;
}
}
function keyPressed() {
if (key == " ") {
// important to have in order to start the serial connection!!
setUpSerial();
}
}
function drawGame() {
background(188, 220, 239);
//image(gamePic, 0, 0, windowWidth, windowHeight);
cloud1.moveX();
cloud1.display();
cloud2.moveX();
cloud2.display();
noFill();
//stroke("black");
fill(250, 237, 153);
ellipse(width / 2, 250, circleWidth, circleHeight);
updateEarned();
replayButton.hide();
inflateButton.show();
collectButton.show();
if (countButtonClicks > 8) {
gameState = "end";
collectButton.hide();
inflateButton.hide();
}
}
function drawEndScreen() {
background(190);
image(endPic, 0, 0, windowWidth, windowHeight);
textSize(16);
text("You have earned $" + totalDollars, 250, 200);
replayButton.show();
replayButton.position(280, 280);
replayButton.mousePressed(function () {
gameState = "start";
clicks = 0;
currentDollars = 0;
totalDollars = 0;
countButtonClicks = 0;
countButtonClicks = 0;
});
}
function inflate() {
circleWidth += 10;
circleHeight += 15;
clicks++;
currentDollars = currentDollars + 1.75;
if (circleWidth > random(50, 130)) {
popSound.play();
circleWidth = 0;
circleHeight = 0;
currentDollars = 0;
clicks = 0;
}
}
function collect() {
collectSound.play();
circleWidth = 20;
circleHeight = 25;
clicks = 0;
totalDollars = currentDollars + totalDollars;
currentDollars = 0;
countButtonClicks++;
}
function updateEarned() {
stroke('black');
fill('black');
textFont(gameFont);
textSize(16);
text("Total Earned = $" + totalDollars, 20, 20);
text("Current Earned = $" + currentDollars, 20, 45);
text("Total Pumps = " + clicks, 20, 70);
}
function drawInstructions() {
background(188, 220, 239);
let instructions =
"On the screen, you will be presented with an inflatable balloon. To inflate it, press the “Inflate” button. The further you manage to inflate the balloon without it bursting, the more $ you will earn. You can click “Collect” to bank the money at any stage. However, take care to not over-inflate balloons to the point of bursting. Every time you burst a balloon, you will receive no money for that particular balloon. There are 8 balloons to get through, all with varying burst points. This means that different balloons will burst at different stages, so you must tackle each balloon differently.";
textFont(instructionsFont);
textSize(20);
text("Instructions", 250, 30);
textSize(14);
textFont("Georgia");
textLeading(30);
text(instructions, 30, 60, 550, 500);
text("Click on the screen to begin!", 230, 350);
}
function mousePressed() {
print(gameState);
if (gameState == "start") {
gameState = "playing";
}
// else if (gameState == "playing") {
// gameState = "end";
// } else if (gameState == "end") {
// restartGame();
// }
return false;
}
function restartGame() {
gameState = "start";
}
class Cloud {
constructor(startX, startY) {
this.x = startX;
this.y = startY;
this.colour = color(400, 400, 400);
}
moveX() {
this.x += 1;
if (this.x > width) {
this.x = 0;
}
}
display() {
noStroke();
fill(this.colour);
ellipse(this.x, this.y, 100, 50);
ellipse(this.x + 50, this.y + 20, 100, 50);
ellipse(this.x - 40, this.y + 15, 100, 50);
}
}
function readSerial(data) {
////////////////////////////////////
//READ FROM ARDUINO HERE
////////////////////////////////////
if (data != null) {
// make sure there is actually a message
// split the message
let fromArduino = split(trim(data), ",");
// if the right length, then proceed
if (fromArduino.length == 2) {
// only store values here
// do everything with those values in the main draw loop
Val = fromArduino[0];
alpha = fromArduino[1];
}
//////////////////////////////////
//SEND TO ARDUINO HERE (handshake)
//////////////////////////////////
let sendToArduino = left + "," + right + "\n";
writeSerial(sendToArduino);
}
}
//Arudino Code
/*
void setup() {
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(5, OUTPUT);
// start the handshake
while (Serial.available() <= 0) {
Serial.println("0,0"); // send a starting message
delay(300); // wait 1/3 second
}
}
void loop() {
// wait for data from p5 before doing something
while (Serial.available()) {
int left = Serial.parseInt();
int right = Serial.parseInt();
if (Serial.read() == '\n') {
digitalWrite(2, left);
digitalWrite(5, right);
int sensor = analogRead(A0);
delay(1);
int sensor2 = analogRead(A1);
delay(1);
Serial.print(sensor);
Serial.print(',');
Serial.println(sensor2);
}
}
}
*/