/* This is intended to be a "Memory" game, where the computer plays a series of flashes and sounds and you are challenged to repeat them back. */ import java.awt.*; import java.applet.AudioClip; import java.util.*; //import java.lang.Thread; public class VerySimpleApplet extends BufferedApplet { int x = 100, y = 100; // COORDINATES OF THE CENTER OF THE X AudioClip sound0,sound1,sound2,sound3,sound4,sound5,sound6,sound7; AudioClip[] soundarray = new AudioClip[4]; Image frog; Image dog; Image pig; Image lion; int boxcolor = 1; int inthebox = 0; int count = 0; Random rand = new Random(); Color offWhite = new Color(240,220,250); int m = 4; int margin1 = m; int margin2 = m; int margin3 = m; int margin4 = m; int marginA = m; int marginB = m; int buttonsize = 100; int gamecenterX = 200; int gamecenterY = 150; boolean mouseIsDown = false; boolean redClicked = false; boolean blueClicked = false; boolean greenClicked = false; boolean yellowClicked = false; boolean orangeClicked = false; boolean pinkClicked = false; boolean newgame; int shadowmargin = 3; int[] counts = new int[11]; int flag = 0; int j = 0; String presses = ""; String original = "(random string to match)"; String original2 = "RRB"; int[] randoms = new int[5]; int topbound = 1; public void render(Graphics g) { if (damage) { ///////// //BACKGROUND ///////// g.setColor(offWhite); g.fillRect(0,0,bounds().width,bounds().height); ///////// //SHADOWS ///////// g.setColor(Color.gray); // generate string g.fillRoundRect(bounds().height - buttonsize - 10+shadowmargin-marginA, bounds().width - buttonsize - 10+shadowmargin-marginA, buttonsize, buttonsize,7,7); // match strings g.fillRoundRect(10 + shadowmargin-marginB, bounds().height - buttonsize - 10+shadowmargin-marginB, buttonsize, buttonsize,7,7); //upper left button g.fillRect(gamecenterX-buttonsize-margin1+shadowmargin, gamecenterY-buttonsize-margin1+shadowmargin, buttonsize,buttonsize); //upper right button g.fillRect(gamecenterX+margin2+shadowmargin, gamecenterY-buttonsize-margin2+shadowmargin, buttonsize,buttonsize); //lower right g.fillRect(gamecenterX+margin3+shadowmargin, gamecenterY+margin3+shadowmargin, buttonsize,buttonsize); //lower left g.fillRect(gamecenterX-buttonsize-margin4+shadowmargin, gamecenterY+margin4+shadowmargin, buttonsize,buttonsize); ///////// // BUTTONS ///////// // generate string g.setColor(Color.pink); g.fillRoundRect(bounds().height - buttonsize - 10-marginA, bounds().width - buttonsize - 10-marginA, buttonsize, buttonsize,7,7); // match strings g.setColor(Color.orange); g.fillRoundRect(10-marginB, bounds().height - buttonsize - 10 - marginB, buttonsize, buttonsize,7,7); //upper left button g.setColor(redClicked ? Color.red.darker() : Color.red); g.fillRect(gamecenterX-buttonsize-margin1, gamecenterY-buttonsize-margin1, buttonsize,buttonsize); //upper right button g.setColor(blueClicked ? Color.blue.darker() : Color.blue); g.fillRect(gamecenterX+margin2, gamecenterY-buttonsize-margin2, buttonsize,buttonsize); //lower right g.setColor(yellowClicked ? Color.yellow.darker() : Color.yellow); g.fillRect(gamecenterX+margin3, gamecenterY+margin3, buttonsize,buttonsize); //lower left g.setColor(greenClicked ? Color.green.darker() : Color.green); g.fillRect(gamecenterX-buttonsize-margin4, gamecenterY+margin4, buttonsize,buttonsize); ///////// //OUTLINES ///////// g.setColor(Color.black); g.drawString("START", bounds().height - buttonsize/2-marginA-30, bounds().width - buttonsize/2-marginA-10); //generate string g.drawRoundRect(bounds().height - buttonsize - 10-marginA, bounds().width - buttonsize - 10-marginA, buttonsize, buttonsize,7,7); g.drawString("MATCH", 10-marginB+ buttonsize/2-20, bounds().height - buttonsize/2 - 10 - marginB); // match strings g.drawRoundRect(10-marginB, bounds().height - buttonsize - 10-marginB, buttonsize, buttonsize,7,7); //upper left button g.drawRect(gamecenterX-buttonsize-margin1, gamecenterY-buttonsize-margin1, buttonsize,buttonsize); //upper right button g.drawRect(gamecenterX+margin2, gamecenterY-buttonsize-margin2, buttonsize,buttonsize); //lower right g.drawRect(gamecenterX+margin3, gamecenterY+margin3, buttonsize,buttonsize); //lower left g.drawRect(gamecenterX-buttonsize-margin4, gamecenterY+margin4, buttonsize,buttonsize); ///////// //BOUNDING BOX ///////// g.setColor(Color.black); g.drawRect(0,0,bounds().width - 1,bounds().height - 1); /* // is the stupid array of randoms being filled? for (int i = 0; i < 5; i++) { g.drawString(Integer.toString(counts[i]),i*10+10,i*10+10); } */ g.drawImage(frog,125-margin1-15,50-margin1+15,75,75,Color.white,null); g.drawImage(dog,225+margin2-7,50-margin2+15,75,75,Color.white,null); g.drawImage(pig,225+margin3-7,150+margin3+15,75,75,Color.white,null); g.drawImage(lion,125-margin4-15,150+margin4+15,75,75,Color.white,null); /* if (flag == 10) { g.setColor(Color.black); g.drawString("the N key was pressed",150,150); flag = 0; } */ g.setColor(Color.black); g.drawString(presses,7,20); g.drawString(original,7,40); if (presses==original) sound2.play(); if (original2 == original) sound2.play(); } redClicked = false; blueClicked = false; yellowClicked = false; greenClicked = false; margin1 = m; margin2 = m; margin3 = m; margin4 = m; /* /////////// sleep code try { Thread.sleep( 100 ); } catch ( InterruptedException e ) { System.out.println( "awakened prematurely" ); }; /////////// sleep code */ } public void init() { super.init(); sound0 = getAudioClip(getCodeBase(), "frog1.wav"); sound1 = getAudioClip(getCodeBase(), "yip.wav"); sound2 = getAudioClip(getCodeBase(), "chess.wav"); sound3 = getAudioClip(getCodeBase(), "pig.wav"); sound4 = getAudioClip(getCodeBase(), "lion.wav"); sound5 = getAudioClip(getCodeBase(), "twip.wav"); sound6 = getAudioClip(getCodeBase(), "drop.wav"); sound7 = getAudioClip(getCodeBase(), "cartooncharge.wav"); dog = getImage(getCodeBase(), "dog.jpg"); frog = getImage(getCodeBase(), "frog.jpg"); lion = getImage(getCodeBase(), "lion.jpg"); pig = getImage(getCodeBase(), "pig.jpg"); sound2.play(); soundarray[0] = sound0; soundarray[1] = sound1; soundarray[2] = sound3; soundarray[3] = sound4; /* for (int i = 0; i < 10; i++) { counts[i] = rand.nextInt(4); //counts[2*i+1] = counts[2*i]; } counts[5] = -1; //sentinel for (int j = 0; j < 4; j++) { try { Thread.sleep( 500 ); } catch ( InterruptedException e ) { System.out.println( "awakened prematurely" ); }; soundarray[j].play(); } */ } public boolean mouseDown(Event e, int x, int y) { mouseIsDown = true; // decide where we clicked if (((x > 10) & (y > bounds().height - buttonsize - 10)) & ((x < 10 + buttonsize) & (y < bounds().height - 10))) { orangeClicked = true; sound5.play(); marginB = 0; if (presses.equals(original)) { // CELEBRATION! sound7.play(); presses = "YOU MATCHED!"; int nextsymbol = rand.nextInt(4); original = original.concat(Integer.toString(nextsymbol)); topbound++; presses = ""; for (int i = 0; i < original.length(); i++) { // int nextsymbol = rand.nextInt(4); soundarray[original.toCharArray()[i]].play(); //soundarray[(original.charAt(i)).digit].play(); ///////////// try { Thread.sleep( 500 ); } catch ( InterruptedException ex ) { System.out.println( "awakened prematurely" ); }; ///////////// original = original.concat(Integer.toString(nextsymbol)); } } } if (((x < bounds().width - 10) & (x>bounds().width-(10+buttonsize))) & ((y > bounds().height - (10+buttonsize)) & (y < bounds().height - 10))) { pinkClicked = true; // sound6.play(); marginA = 0; presses = ""; original = ""; // int topbound = rand.nextInt(4); for (int i = 0; i < topbound; i++) { int nextsymbol = rand.nextInt(4); soundarray[nextsymbol].play(); ///////////// try { Thread.sleep( 500 ); } catch ( InterruptedException ex ) { System.out.println( "awakened prematurely" ); }; ///////////// original = original.concat(Integer.toString(nextsymbol)); } } if (((x < gamecenterX) & (y < gamecenterY)) & ((x > gamecenterX - buttonsize) & (y > gamecenterY - buttonsize))) { redClicked = true; sound0.play(); margin1 = 0; presses = presses.concat("0"); } if (((gamecenterX < x) & (y < gamecenterY)) & ((x < gamecenterX + buttonsize) & (y > gamecenterY - buttonsize))) { blueClicked = true; sound1.play(); margin2 = 0; presses = presses.concat("1"); } if (((gamecenterX < x) & (gamecenterY < y)) & ((x < gamecenterX + buttonsize) & (y < gamecenterY + buttonsize))) { yellowClicked = true; sound3.play(); margin3 = 0; presses = presses.concat("2"); } if (((x < gamecenterX) & (gamecenterY < y)) & ((x > gamecenterX - buttonsize) & (y < gamecenterY + buttonsize))) { greenClicked = true; sound4.play(); margin4 = 0; presses = presses.concat("3"); } damage = true; return true; } public boolean mouseUp(Event e, int x, int y) { mouseIsDown = false; damage = true; redClicked = false; blueClicked = false; yellowClicked = false; greenClicked = false; margin1 = m; margin2 = m; margin3 = m; margin4 = m; marginA = m; marginB = m; // boolean mouseIsDown = false; return true; } public boolean keyDown (Event e, int key) { switch(key) { case 'n': for (int i = 0; i < 5; i++) { counts[i] = rand.nextInt(4); //counts[2*i+1] = counts[2*i]; flag = 10; newgame = true; j=0; } break; } return true; } }