import krister.Ess.*; import java.util.Set; import java.util.Calendar; AudioStream myStream; AudioFile themesong; AudioChannel titlesound; PFont fontA; color kolor = color(220,180,190); PImage[] faces = new PImage[16]; PImage[] selectedFaces = new PImage[8]; PImage[][] grid = new PImage[4][4]; PImage credits, cctv, titlescreen, teneastern, wtf, wtf2; boolean[][] showingQ = new boolean[4][4]; boolean drawingflower,sleeping,GameOver = false; Set s = new HashSet(); int[] selected = new int[4]; int scroller = 210; long start, timespace; final int ZERO_SELECTED = 0; final int ONE_SELECTED = 1; final int TWO_SELECTED = 2; int GameState = -2; int interp, tries, facesmatched=0; int x2, y2, cv = 10; int j =1; int lag = 1200; void setup(){ size(300,300); background(200,215,255); noStroke(); smooth(); frameRate(10); //noLoop(); pushMatrix(); fontA = loadFont("MonotypeCorsiva-48.vlw"); Ess.start(this); themesong=new AudioFile("hawaii.mp3",0,Ess.READ); titlesound=new AudioChannel("titlesound.wav"); myStream=new AudioStream(32*1024); // 32k samples myStream.sampleRate(themesong.sampleRate); titlesound.play(); //load all the face pictures in faces array for (int i=0;i<16;i++){ faces[i] = loadImage(NumToFace(i+1)); } credits = loadImage("credits.png"); titlescreen = loadImage("titlescreen.jpg"); teneastern = loadImage("10eastern.png"); wtf = loadImage("10eastern_ribz.png"); wtf2 = loadImage("10eastern_deer.png"); //select 8 of those faces to be used in the game int yy, xx, times, facecount = 0; while (s.size() != 8){ int r = int(random(0,16)); if (s.contains(String.valueOf(r)) == false){ s.add(String.valueOf(r)); selectedFaces[facecount] = faces[r]; facecount++; //populate the grid randomly with the 8 faces, each twice times = 0; while (times < 2){ xx = int(random(0,4)); yy = int(random(0,4)); if (grid[xx][yy] == null){ grid[xx][yy] = faces[r]; times++; } } } } } void mouseClicked(){ if(GameOver){ } else { x2 = mouseX/70; y2 = mouseY/70; if (x2 > 3) x2 = 3; if (y2 > 3) y2 = 3; if (showingQ[x2][y2]==false) { switch (GameState){ case -2: //10eastern productions logo GameState = -1; break; case -1: //game title GameState = 0; titlesound.fadeTo(0,2000); myStream.start(); break; case ZERO_SELECTED: GameState = ONE_SELECTED; selected[0] = x2; selected[1] = y2; showingQ[x2][y2]=true; break; case ONE_SELECTED: showingQ[x2][y2] = true; if (grid[x2][y2] == grid[selected[0]][selected[1]]) { //println("WE'VE GOT A MATCH"); facesmatched++; showingQ[x2][y2] = true; showingQ[selected[0]][selected[1]] = true; lag -= 200; if (facesmatched == 8){ EndTheGame(); } GameState = ZERO_SELECTED; } else { tries+=1; selected[2] = x2; selected[3] = y2; GameState = TWO_SELECTED; start = (new Date()).getTime(); } break; case TWO_SELECTED: showingQ[selected[0]][selected[1]] = false; showingQ[selected[2]][selected[3]] = false; GameState = ZERO_SELECTED; break; } } } redraw(); } void keyPressed(){ if (key == 'p'){ EndTheGame(); } if (key == CODED && GameState < 0) { if (keyCode == UP) { image(wtf,0,30); } else if (keyCode == DOWN) { image(wtf2,0,30); } } } void draw(){ if(GameState==-2){ background(255); image(teneastern,0,30); return; } else if(GameState==-1){ image(titlescreen,0,30); return; } if (drawingflower==false) { background(200,215,255); drawWater(); } if(GameOver==false){ //Game On for (int i=0;i<4;i++){ for (int j=0;j<4;j++){ if(showingQ[i][j]==true) image(grid[i][j],70*i+20,70*j+20); else { fill(kolor); ellipse(70*i+50,70*j+50,40,40); } } } //show "score" fill(120); rect(0,3,302,15); textFont(fontA, 25); fill(kolor); text("tries : " + tries, 120, 18); } else{ //Game Over if (drawingflower==false) //Done drawing flower { noSmooth(); translate(0,scroller); set(0,0,cctv); fill(20); rect(40,0,302,45); textFont(fontA, 25); fill(kolor); if (tries != 1) text("You won in " + tries + " tries", 120, 20); else text("You won in " + tries + " try", 120, 20); text("For a grade of " + getScore(tries), 126, 35); image(credits,20,60); //PImage d = copy(0, 0, 300, 300, 30, 30, 100, 100); // redraw(); // println("about to sleep"); // println("just slept"); // fill(0); // rect(0,175,302,45); // textFont(fontA, 25); // fill(kolor); // text("That's All Folks!", 120, 200); scroller--; } else{ //Drawing Flower drawingflower = true; noStroke(); frameRate(30); translate(190, 190); rotate(PI*random(6)); color c = color(random(190)+50,random(220)+30,random(150)+40); fill(c); rect(0, 0, cv, cv); cv+=1; if (cv>260) { cv=cv*-1; } else { j=1; } smooth(); rotate(PI/random(3)); if (cv==0){ drawingflower = false; popMatrix(); cctv = get(0,0,300,300); } drawWater(); } } } String NumToFace(int r){ return "face" + r + ".png"; } //void sleep(int n){ // sleeping=true; // long start = (new Date()).getTime(); // long timespace = 0; // while (timespace < lag){ // timespace = (long)(new Date()).getTime() - start; // } // sleeping=false; //} String getScore(int tries){ if (tries==0) return "A++"; else if (tries < 5) return "A+"; else if (tries < 6) return "A"; else if (tries < 7) return "A-"; else if (tries < 8) return "B+"; else if (tries < 9) return "B"; else if (tries < 10) return "B-"; else if (tries < 11) return "C+"; else if (tries < 12) return "C"; else if (tries < 13) return "C-"; else if (tries < 14) return "D+"; else if (tries < 15) return "D"; else if (tries < 16) return "D-"; else return "F"; } void EndTheGame(){ GameOver = true; drawingflower=true; loop(); } void audioStreamWrite(AudioStream theStream) { int samplesRead=themesong.read(myStream); if (samplesRead==0) { // start over themesong.close(); themesong.open("",myStream.sampleRate,Ess.READ); samplesRead=themesong.read(myStream); } } // we are done, clean up Ess public void stop() { Ess.stop(); super.stop(); } void drawWater(){ for (int i=0;i<300;i++) { int top=200; interp=(int)max(0,(((millis()-myStream.bufferStartTime)/(float)myStream.duration)*myStream.size)); if (i+interp