/** * Project for coursera.org course "Creative Programming for Digital Media & Mobile Apps ". * Author: Sergey Marchenko aka Junkman Collector. * Web: heapup.info */ int currentLevelNumber = 1; int[] bubblesSeq = new int[2]; int seqLength = 2; int curPositionCompPlay; int delayBetweenWinAndLose = 0; PFont titleText; ArrayList bubbles; AudioPlayer[] players = new AudioPlayer[5]; AudioPlayer winSound; AudioPlayer loseSound; Maxim maxim; String waitTurn = "Wait for the computer to play the sound sequence."; String yourTurn = "Your turn."; String wrongSeq = "Wrong sequence."; String corectSeq = "Well done!"; String currentAction; void setup() { size(800, 600); //smooth(); maxim = new Maxim(this); bubbles = new ArrayList(); bubbles.add(new Bubble(100, height * 0.5 + 50, color(224, 50, 0), 0, "random")); bubbles.add(new Bubble(250, height * 0.5 + 50, color(50, 240, 30), 1, "circleLeft")); bubbles.add(new Bubble(width * 0.5, height * 0.5 + 50, color(50, 110, 255), 2, "line")); bubbles.add(new Bubble(550, height * 0.5 + 50, color(255, 219, 50), 3, "circleRight")); bubbles.add(new Bubble(700, height * 0.5 + 50, color(204, 13, 130), 4, "random2")); players[0] = maxim.loadFile("bubble1.wav"); players[1] = maxim.loadFile("bubble2.wav"); players[2] = maxim.loadFile("bubble3.wav"); players[3] = maxim.loadFile("bubble4.wav"); players[4] = maxim.loadFile("bubble5.wav"); winSound = maxim.loadFile("win.wav"); loseSound = maxim.loadFile("lose.wav"); winSound.isLooping = false; loseSound.isLooping = false; for (int i = 0; i < players.length; i++) { players[i].isLooping = false; } titleText = createFont("GROBOLD", 28); textFont(titleText); bubblesSeq[0] = 0; bubblesSeq[1] = 0; fillComputersSeq(); } void draw() { background(0); if (delayBetweenWinAndLose == 0) { for (Bubble bubble : bubbles) { bubble.display(); if (currentAction == waitTurn) { if (bubble.playerNumber == bubblesSeq[curPositionCompPlay] && bubble.duration == 0) { bubble.playAnim = true; players[bubble.playerNumber].cue(0); players[bubble.playerNumber].play(); } } if (bubble.duration == 100) { bubble.duration = 0; bubble.playAnim = false; bubble.xparam = bubble.startX; // Check our turn if (currentAction == yourTurn) { if (bubble.playerNumber != bubblesSeq[curPositionCompPlay]) { currentAction = wrongSeq; delayBetweenWinAndLose = 1; loseSound.cue(0); loseSound.play(); } } curPositionCompPlay++; if (curPositionCompPlay == seqLength && currentAction == yourTurn) { currentAction = corectSeq; delayBetweenWinAndLose = 1; winSound.cue(0); winSound.play(); } if (curPositionCompPlay == seqLength && currentAction == waitTurn) { currentAction = yourTurn; curPositionCompPlay = 0; } } } textAlign(LEFT); fill(255, 200, 0); text("Level " + currentLevelNumber, 50, 100); fill(255); text(currentAction, 50, 150); } else { for (Bubble bubble : bubbles) { bubble.display(); } textAlign(LEFT); fill(255, 200, 0); text("Level " + currentLevelNumber, 50, 100); fill(255); text(currentAction, 50, 150); delayBetweenWinAndLose++; if (delayBetweenWinAndLose == 200) { delayBetweenWinAndLose = 0; if (currentAction == wrongSeq) { seqLength = 2; currentLevelNumber = 1; corectSeq = "Well done!"; waitTurn = "Wait for the computer to play the sound sequence."; fillComputersSeq(); } if (currentAction == corectSeq) { seqLength++; currentLevelNumber++; if (currentLevelNumber > 7) { corectSeq = "Fantastic!"; waitTurn = "You never win me. Ha ha ha!"; } fillComputersSeq(); } } } } void mousePressed() { if (currentAction == yourTurn) { for (Bubble bubble : bubbles) { if (bubbleOver (bubble.startX, bubble.startY, 60)) { bubble.playAnim = true; players[bubble.playerNumber].cue(0); players[bubble.playerNumber].play(); } } } } boolean bubbleOver(float x, float y, int diameter) { float disX = x - mouseX; float disY = y - mouseY; if (sqrt(sq(disX) + sq(disY)) < diameter * 0.5) { return true; } else { return false; } } void fillComputersSeq() { int num; for (int j = 0; j < seqLength; j++) { num = int(random(5)); bubblesSeq = splice(bubblesSeq, num, j); } curPositionCompPlay = 0; currentAction = waitTurn; } class Bubble { int num = 60; float mx[] = new float[num]; float my[] = new float[num]; float a = 0.0; float inc = TWO_PI/25.0; float xparam; color bubbleColor; boolean playAnim = false; float startX; float startY; int radius = 50; int playerNumber; String AnimationType; int duration = 0; Bubble(float x, float y, color c, int pn, String animType) { startX = x; startY = y; bubbleColor = c; xparam = startX; playerNumber = pn; AnimationType = animType; noStroke(); } void display() { if (playAnim == false) { fill(bubbleColor); ellipse(startX, startY, radius, radius); } if (playAnim) { int which = frameCount % num; if (AnimationType == "random") { my[which] = startY + sin(a) * random(radius); mx[which] = startX + cos(a) * radius; } if (AnimationType == "circleLeft") { my[which] = startY + sin(a) * radius; mx[which] = startX + cos(a) * radius; } if (AnimationType == "circleRight") { my[which] = startY + cos(a) * radius; mx[which] = startX + sin(a) * radius; } if (AnimationType == "random2") { my[which] = startY + sin(a) * radius; mx[which] = startX + cos(a) * random(radius); } if (AnimationType == "line") { my[which] = startY + sin(a) * 120; mx[which] = startX + atan(a) * 1; } for (int i = 0; i < num; i++) { int index = (which+1 + i) % num; fill(bubbleColor, 70); ellipse(mx[index], my[index], i, i); } a = a + inc; xparam++; duration++; } } // end class }