// PHONE specific // //import processing.phone.*; //import mjs.processing.mobile.msound.*; //import processing.sound.*; //import processing.core.*; //Phone phone = new Phone(this); // end PHONE specific // Player plr; Road[] roads; Finish finish; // PHONE specific // //MMidi midi; // end PHONE specific // // APPLET specific // import ddf.minim.*; Minim minim; AudioSample squash, win, jump; // end APPLET specific // int cowBell = 0x38; int crashCymbal = 0x31; int hiTom = 0x32; int closeHitHat = 0x2A; int level = 1; int score = 0; int lives = 3; boolean gameover = false; int numroads = 5; int numcars = 2; int jumpsize; int stepsize; int offset = 0; int silly_text_y = 400; PFont FreeSans; void setup() { //////////////////// // PHONE specific // //framerate(30); // use this to determine which audio format we can use //for (int i = 0; i < Sound.supportedTypes().length; i++) { // println(Sound.supportedTypes()[i]); //} //textFont(loadFont(FACE_SYSTEM, STYLE_BOLD, SIZE_MEDIUM)); // end PHONE specific // //////////////////////// // APPLET specific // // Nokia 6230 //size(128, 128); // Sun "Default Colour" emulator size(240, 291); frameRate(30); textFont(loadFont("FreeSansBold-12.vlw")); minim = new Minim(this); squash = minim.loadSample("squash.wav.mp3", 2048); win = minim.loadSample("win.wav.mp3", 2048); jump = minim.loadSample("jump.wav.mp3", 2048); // end APPLET specific // noStroke(); //imageMode(CORNERS); generate_level(level); } void generate_level(int lvl) { // reset the font // PHONE specific // // textFont(loadFont(FACE_SYSTEM, STYLE_BOLD, SIZE_MEDIUM)); // reset midi // midi = new MMidi(); // end PHONE specific // // APPLET specific // textFont(loadFont("FreeSansBold-12.vlw")); // end APPLET specific // //numroads = lvl + 4; plr = new Player("frog.png", "frog_splat.png"); // create player, and set the step size jumpsize = (plr.img.height); stepsize = plr.img.width; roads = new Road[numroads]; int ystep = (height/numroads); int ylane = 0; // initialize with a dummy value for (int i = 0; i < numroads; i++) { // add a road Road road = new Road("road.png"); ystep = road.img.height; ylane = (ystep*i) + (ystep*numroads/2) - ystep; road.y = ylane; road.x = width/2; roads[i] = road; // put cars on the road we just made Car[] cars = new Car[numcars]; int spd = (int) random(lvl) + 1; int dir = (int) random(2); String carfn; if (dir == 0) { dir = -1; carfn = "car.png"; } else { carfn = "car2.png"; } for (int c = 0; c < numcars; c++) { cars[c] = new Car(carfn, (width*c)/numcars, ylane); cars[c].speed = spd; cars[c].direction = dir; } roads[i].cars = cars; } // place the win pond finish = new Finish("water.png", width/2, (ystep*numroads/2)-2*ystep); // place the player plr.y = ylane + ystep - plr.img.height/2; plr.x = (width/2); } void playnote(int v) { // PHONE specific // //midi.noteOn(9,v,120); //midi.noteOff(9,v); // end PHONE specific // return; } // input void keyPressed() { //if (key == CODED) { // PHONE specific // //int firekey = FIRE; // end PHONE specific // // APPLET specific // char firekey = ' '; // end APPLET specific // if (gameover) { if (keyCode == firekey) { gameover = false; level = 1; lives = 3; score = 0; generate_level(level); } else { return; } } if (plr.dead) { return; } if (keyCode == UP) { plr.y -= jumpsize / 10; offset = jumpsize; playnote(closeHitHat); // APPLET specific // jump.trigger(); // end APPLET specific // update_bg(); } else if (keyCode == DOWN) { offset = -1*jumpsize; plr.y += jumpsize / 10; playnote(closeHitHat); // APPLET specific // jump.trigger(); // end APPLET specific // update_bg(); } else if (keyCode == LEFT) { plr.x -= stepsize; playnote(closeHitHat); // APPLET specific // jump.trigger(); // end APPLET specific // } else if (keyCode == RIGHT) { plr.x += stepsize; playnote(closeHitHat); // APPLET specific // jump.trigger(); // end APPLET specific // } //} // ensure player can't leave the screen plr.y = constrain(plr.y, 0, height); plr.x = constrain(plr.x, 0, width); } // update the background sprite and enemy positions upon scroll void update_bg() { finish.y = finish.y + offset; for (int o = 0; o < roads.length; o++) { Road i = roads[o]; i.xy(i.x, i.y + offset); Car[] cars = roads[o].cars; for (int c = 0; c < cars.length; c++) { cars[c].xy(cars[c].x, cars[c].y + offset); } } silly_text_y += offset; } void collision_detect() { for (int r = 0; r < numroads; r++) { Car[] cars = roads[r].cars; for (int i = 0; i < numcars; i++) { Car car = cars[i]; if ( (car.left > plr.left && car.left < plr.right) || (car.right < plr.right && car.right > plr.left) ) { int car_y_center = (car.y + car.img.width/2); if (car_y_center < plr.bottom && car_y_center > plr.top) { //println("Collide !"); // PHONE specific // //phone.vibrate(50); //playnote(crashCymbal); // end PHONE specific // // APPLET specific // squash.trigger(); // end APPLET specific // if (!plr.dead) { lives -= 1; } plr.dead = true; } } } } // win !! if (finish.y+finish.img.height/2 > plr.y) { //println("Win"); score += 1; // PHONE specific // //phone.vibrate(10); //playnote(hiTom); // end PHONE specific // // APPLET specific // win.trigger(); // end APPLET specific // level += 1; generate_level(level); } } void draw() { collision_detect(); background(0); // roads for (int o = 0; o < numroads; o++) { Sprite i = (Sprite) roads[o]; i.draw(); } // the win pond finish.draw(); //player plr.update(); plr.draw(); //cars for (int o = 0; o < numroads; o++) { Car[] cars = roads[o].cars; for (int c = 0; c < numcars; c++) { cars[c].update(); cars[c].draw(); } } // text HUD fill(255, 128, 0); text("Score: " + str(score), 10, 20); fill(255, 255, 0); text("Level: " + str(level), width-70, 40); fill(0, 255, 0); text("Lives: " + str(lives), width-70, 20); //silly text fill(255, 255, 255); text("There is nothing down here", 20, silly_text_y); fill(255, 128, 0); text("Seriously, you are wasting your time", 20, silly_text_y+400); fill(0, 0, 255); text("Okay ... um ... you win.", 20, silly_text_y+800); text("Pat yourself on the back.", 20, silly_text_y+830); text("Now go and do something productive.", 20, silly_text_y+860); // gameover condition if (lives <= 0) { gameover = true; // PHONE specific // // textFont(loadFont(FACE_SYSTEM, STYLE_BOLD, SIZE_LARGE)); // end PHONE specific // // APPLET specific // textFont(loadFont("FreeSansBold-12.vlw")); // end APPLET specific // fill(255, 128, 0); text("GAME OVER SLIMY LOSER", 30, height/2); text("PRESS FIRE TO PLAY AGAIN", 30, height/2+20); } } // APPLET specific // void stop() { // closedown audio win.close(); jump.close(); squash.close(); minim.stop(); super.stop(); } // end APPLET specific //