import ddf.minim.spi.*; import ddf.minim.signals.*; import ddf.minim.*; import ddf.minim.analysis.*; import ddf.minim.ugens.*; import ddf.minim.effects.*; float sz = 600; Player p1, p2; int p1Score = 0; int p2Score = 0; color COLORGRASS = color(64, 200, 64); float GROUNDY = sz * .8; float GRAV = sz / 1200; float FLAP = GRAV * 22; int BLEFT = 1; int BRIGHT = -1; color YELLOW = color(255, 255, 0); float BASEBALLXSPEEDMULTIPLIER = .015; float PLAYERHEIGHT = sz / 8; int WALLSPERSIDE = 6; ArrayList walls = new ArrayList(); float COMPFLAPFRAC = 1; int AICHARGETIME = 6; float BIRDSIZEMUL = 1.75; Ball b = new Ball(); boolean playing = false, played = false; Pterry pt; void setup() { size(600, 600); soundsStart(); soundsPlayMusic(); makeLetters(); frameRate(45); pt = new Pterry(); //makeWalls(); p1 = new Player(BLEFT, color(0, 0, 200)); p2 = new Player(BRIGHT, color(200, 0, 0)); //ellipseMode(CENT); b.reset(BRIGHT); } void startGame() { p1Score = 0; p2Score = 0; playing = true; b.reset(BRIGHT); } void keyPressed() { p1.flap(1); //p2.flap() ; } void mousePressed() { // p2.flap() ; if (! playing) { if(mouseInGoButton()){ played = true; startGame(); } } if(playing) p1.flap(1); } void draw() { background(200, 200, 255); //sky noStroke(); fill(COLORGRASS); rect(0, GROUNDY, sz, sz - GROUNDY); if (! playing) { drawTitle(); drawGo(); } if(played){ drawScore(); } for (Wall w : walls) { w.draw(); } p1.move(); p2.move(); p2.ai(); p1.draw(); p2.draw(); pt.move(); pt.draw(); if (playing) { b.move(p1, p2); b.draw(); } } void makeWalls() { float ww = sz/30; float wh = GROUNDY / WALLSPERSIDE; for (int i = 0; i < WALLSPERSIDE; i++) { walls.add(new Wall(ww, i * wh, ww, wh, BLEFT)); walls.add(new Wall(sz - (2*ww), i * wh, ww, wh, BRIGHT)); } } class Wall { float x, y, w, h; color c; boolean removed = false; int side; float r[] = new float[8]; Wall(float px, float py, float pw, float ph, int pside) { x = px; y = py; w = pw; h = ph; c = color(random(100, 200)); side = pside; for (int i= 0; i < 8 ; i++) { r[i] = random(-sz/100, sz/100); } } float ys = 0; void draw() { if (removed) { ys += sz / 300; y += ys; x += - side * sz / 100; } noStroke(); fill(c); quad(x+r[0], y+r[1], x+w+r[2], y+r[3], x+w+r[4], y+h+r[5], x+r[6], y+h+r[7]); } } class Player { int dir; float w, h, x, y, ys; float wa; //wing angle color c; Player(int pDir, color pc) { c = pc; dir = pDir; h = PLAYERHEIGHT; w = sz / 30; if (dir == BLEFT) x = w *3 ; else x = sz - (4 *w); y = GROUNDY - h; } float center() { return x + (w / 2); } void flap(float val ) { ys -= FLAP * val; wa = 1; } void move() { wa *= .6; ys += GRAV; y += ys; if (hitPterry()) { if(playing)soundsCaw(); flap(-2); } if (y + h > GROUNDY) { y = GROUNDY - h; ys = - abs(ys) / 2; } if (y < 0) { y = 0; ys = abs(ys) ; } } int aicharge; void ai() { aicharge++; if (b.xs > 0 && y > b.y && aicharge > AICHARGETIME) { flap(COMPFLAPFRAC); aicharge = 0; } } boolean hitPterry() { return overlap2D(pt.x, pt.y, pt.w, pt.h, x, y-w*BIRDSIZEMUL, w, w); } void draw() { pushMatrix(); translate(x, y); drawBird(c, 0, 0, w, dir, wa, true); noStroke(); fill(255); rect(0, 0, w, h); popMatrix(); /*noFill(); stroke(255, 0, 0); rect(x, y-w*BIRDSIZEMUL, w, w);*/ } } void drawBird(color c, float x, float y, float w, int dir, float wa, boolean leg) { pushMatrix(); translate(x, y); float b = w * 1.5; strokeWeight(2); //BEAK noStroke(); translate(w/2, -w/2 - b/2); //WING pushMatrix(); rotate((.2 - wa) * dir); fill(c); triangle(0, -b/2, b * -dir, 0, 0, b/2); popMatrix(); //BEAK fill(YELLOW); triangle(0, b/2, b * dir, 0, 0, -b/2); if (leg) { //LEGS stroke(YELLOW); strokeWeight(2); line(0, 0, 0, b); } noStroke(); fill(c); //BODY ellipse(0, 0, b, b); fill(255); ellipse(0, 0, b/3, b/3); fill(0); ellipse(0, 0, b/4, b/4); popMatrix(); } float getRandomYspeed() { float r = sz / 100; return random(-r, r); } class Ball { float x, y, xs, ys; float w, h; Ball() { w = h = (sz / 30) * 1.5 ; } void reset(int headTowards) { x = sz / 2 - w /2; if (headTowards == BRIGHT) x -= sz / 4; else x += sz /4; y = GROUNDY/2 - h/2 ; xs = abs(sz * BASEBALLXSPEEDMULTIPLIER); if (headTowards == BLEFT) xs *= -1; ys = getRandomYspeed(); } void move(Player p1, Player p2) { x += xs; y += ys; if (y < 0) { y = 0; ys = abs(ys); } if (y + h > GROUNDY) { y = GROUNDY-h; ys = -abs(ys); } if (hitPterry()) { if(playing)soundsCaw(); xs *= -1; if (x > pt.x) x = pt.x + pt.w; else x = pt.x - w; } if (hitPlayer(p1)) { soundsDing1(); ys = (ys + p1.ys )/ 2; if (p1.center() < this.center()) xs = abs(xs) ; else { xs = -abs(xs) ; } } if (hitPlayer(p2)) { soundsDing2(); ys = (ys + p2.ys)/2; xs = -abs(xs) ; if (p2.center() < this.center()) xs = abs(xs) ; else { xs = -abs(xs) ; } } for (Wall wall : walls) { if (hitWall(wall)) { wall.removed = true; xs = abs(xs) * wall.side; ys = getRandomYspeed(); if (wall.side == BLEFT) { x = wall.x+ wall.w + 1; } else { x = wall.x - b.w - 1; } } } if (x + w < 0) { addPoint(BRIGHT); reset(BRIGHT); } if (x + w > sz) { addPoint(BLEFT); reset(BLEFT); } } void addPoint(int who) { if(! playing) return; if (who == BRIGHT) p2Score++; else p1Score++; if(p1Score >= 10 || p2Score >= 10) { playing = false; soundsPlayMusic(); } } boolean hitPterry() { return hitThing(pt.x, pt.y, pt.w, pt.h); } float center() { return x + (w / 2); } void draw() { fill(255); noStroke(); ellipse(x+w/2, y+h/2, w, h); /*noFill(); stroke(255, 0, 0); strokeWeight(1); rect(x, y, w, h);*/ } boolean hitPlayer(Player p) { return hitThing(p.x, p.y, p.w, p.h); } boolean hitWall(Wall w) { if (w.removed) return false; return hitThing(w.x, w.y, w.w, w.h); } boolean hitThing(float thingx, float thingy, float thingw, float thingh) { return overlap2D(x, y, w, h, thingx, thingy, thingw, thingh); } } boolean overlap2D(float px, float py, float pw, float ph, float qx, float qy, float qw, float qh) { return overlap1D(px, pw, qx, qw) && overlap1D(py, ph, qy, qh); } boolean overlap1D(float px, float pw, float qx, float qw) { float b1left = qx; float b1right = qx+qw; float b2left = px; float b2right = px+pw; if (b1left < b2left && b1right < b2left) return false; if (b1left > b2right && b1right > b2right) return false; return true; } float PTERRYXEPEED = sz / 100; class Pterry { float x, y, w, h; float xs, ys, wa; int CHARGETIME = 10; int charge; float basespeed; int dir = BLEFT; final int MODECOUNT = 4; final int MODEGLIDE1 = 0; final int MODECLIMB = 1; final int MODEGLIDE2 = 2; final int MODEDIVE = 3; int mode = MODEGLIDE1; int modeTime; int MODERESET = 38; Pterry() { x = sz / 2; y = sz / 2; w = sz / 30; h = sz / 30; basespeed = w / 4; xs = basespeed; dir = BLEFT; } void move() { x += xs; switch(mode) { case MODEGLIDE1: case MODEGLIDE2: modeTime--; if (modeTime < 0) { modeTime = MODERESET; mode ++; } break; case MODECLIMB: y -= basespeed; if (y < GROUNDY * .2) mode++; break; case MODEDIVE: y += basespeed; if (y > GROUNDY *.8) mode++; break; } mode = mode % MODECOUNT; if (x > p2.x + p2.w) { xs = -abs(xs); dir = BRIGHT; } if (x + w < p1.x) { xs = abs(xs); dir = BLEFT; } if (mode == MODEDIVE) wa = 0; else { if (wa < .01) wa = 1; wa *= .6; } } void draw() { drawBird(color(0), x, y + BIRDSIZEMUL * h, w, dir, wa, false); /*noFill(); stroke(255, 0, 0); rect(x, y, w, h);*/ } } HashMap refLetters = new HashMap(); ArrayList refDigits = new ArrayList(); void makeLetters() { Letter F = new Letter(); F.add(new LetterSeg(0, 0, 1, .2)); F.add(new LetterSeg(0, .4, 1, .2)); F.add(new LetterSeg(0, 0, .2, 1)); refLetters.put("F", F); Letter L = new Letter(); L.add(new LetterSeg(0, 0, .2, 1)); L.add(new LetterSeg(0, .8, 1, .2)); refLetters.put("L", L); Letter A = new Letter(); A.add(new LetterSeg(0, 0, .2, 1)); A.add(new LetterSeg(.8, 0, .2, 1)); A.add(new LetterSeg(0, 0, 1, .2)); A.add(new LetterSeg(0, .5, 1, .2)); refLetters.put("A", A ); Letter P = new Letter(); P.add(new LetterSeg(0, 0, .2, 1)); P.add(new LetterSeg(.8, 0, .2, .5)); P.add(new LetterSeg(0, 0, 1, .2)); P.add(new LetterSeg(0, .5, 1, .2)); refLetters.put("P", P ); Letter I = new Letter(); I.add(new LetterSeg(.4, 0, .2, 1)); I.add(new LetterSeg(0, 0, 1, .2)); I.add(new LetterSeg(0, .8, 1, .2)); refLetters.put("I", I ); Letter N = new Letter(); N.add(new LetterSeg(0, 0, .2, 1)); N.add(new LetterSeg(0, 0, .35, .2)); N.add(new LetterSeg(.35, 0, .2, .5)); N.add(new LetterSeg(.45, .5, .2, .5)); N.add(new LetterSeg(.65, .8, .35, .2)); N.add(new LetterSeg(.8, 0, .2, 1)); refLetters.put("N", N ); /* Letter N = new Letter(); N.add(new LetterSeg(0, 0, .2, 1)); N.add(new LetterSeg(0, 0, .4, .2)); N.add(new LetterSeg(.4, 0, .2, 1)); N.add(new LetterSeg(.6, .8, .4, .2)); N.add(new LetterSeg(.8, 0, .2, 1)); */ Letter G = new Letter(); G.add(new LetterSeg(0, 0, .2, 1)); G.add(new LetterSeg(0, 0, 1, .2)); G.add(new LetterSeg(0, .8, 1, .2)); G.add(new LetterSeg(.5, .4, .5, .2)); G.add(new LetterSeg(.8, .4, .2, .6)); refLetters.put("G", G ); Letter W = new Letter(); W.add(new LetterSeg(0, 0, .2, 1)); W.add(new LetterSeg(.8, 0, .2, 1)); W.add(new LetterSeg(0, 0.8, 1, .2)); W.add(new LetterSeg(.4, .5, .2, .5)); refLetters.put("W", W ); Letter O = new Letter(); O.add(new LetterSeg(0, 0, .2, 1)); O.add(new LetterSeg(0, .8, 1, .2)); O.add(new LetterSeg(0, 0, 1, .2)); O.add(new LetterSeg(.8, 0, .2, 1)); refLetters.put("O", O ); Letter S = new Letter(); S.add(new LetterSeg(0, 0, 1, .2)); S.add(new LetterSeg(0, 0, .2, .4)); S.add(new LetterSeg(0, .4, 1, .2)); S.add(new LetterSeg(.8, .6, .2, .4)); S.add(new LetterSeg(0, .8, 1, .2)); refLetters.put("S", S ); Letter E = new Letter(); E.add(new LetterSeg(0, 0, 1, .2)); E.add(new LetterSeg(0, .4, 1, .2)); E.add(new LetterSeg(0, .8, 1, .2)); E.add(new LetterSeg(0, 0, .2, 1)); refLetters.put("E", E ); Letter R = new Letter(); R.add(new LetterSeg(0, 0, .2, 1)); R.add(new LetterSeg(.8, 0, .2, .5)); R.add(new LetterSeg(0, 0, 1, .2)); R.add(new LetterSeg(0, .5, 1, .2)); R.add(new LetterSeg(.5, .5, .2, .5)); refLetters.put("R", R); Letter no0 = new Letter(); no0.add(new LetterSeg(0, 0, .2, 1)); no0.add(new LetterSeg(0, .8, 1, .2)); no0.add(new LetterSeg(0, 0, 1, .2)); no0.add(new LetterSeg(.8, 0, .2, 1)); refDigits.add(no0 ); Letter no1 = new Letter(); no1.add(new LetterSeg(.4, 0, .2, 1)); no1.add(new LetterSeg(0, 0, .6, .2)); no1.add(new LetterSeg(0, .8, 1, .2)); refDigits.add(no1 ); Letter no2 = new Letter(); no2.add(new LetterSeg(0, 0, 1, .2)); no2.add(new LetterSeg(.8, 0, .2, .4)); no2.add(new LetterSeg(0, .4, 1, .2)); no2.add(new LetterSeg(0, .6, .2, .4)); no2.add(new LetterSeg(0, .8, 1, .2)); refDigits.add(no2 ); Letter no3 = new Letter(); no3.add(new LetterSeg(0, 0, 1, .2)); no3.add(new LetterSeg(0, .4, 1, .2)); no3.add(new LetterSeg(0, .8, 1, .2)); no3.add(new LetterSeg(.8, 0, .2, 1)); refDigits.add(no3 ); Letter no4 = new Letter(); no4.add(new LetterSeg(0, 0, .2, .6)); no4.add(new LetterSeg(0, .4, 1, .2)); no4.add(new LetterSeg(.8, 0, .2, 1)); refDigits.add(no4 ); Letter no5 = new Letter(); no5.add(new LetterSeg(0, 0, 1, .2)); no5.add(new LetterSeg(0, 0, .2, .4)); no5.add(new LetterSeg(0, .4, 1, .2)); no5.add(new LetterSeg(.8, .6, .2, .4)); no5.add(new LetterSeg(0, .8, 1, .2)); refDigits.add(no5 ); Letter no6 = new Letter(); no6.add(new LetterSeg(0, 0, 1, .2)); no6.add(new LetterSeg(0, 0, .2, 1)); no6.add(new LetterSeg(0, .4, 1, .2)); no6.add(new LetterSeg(.8, .6, .2, .4)); no6.add(new LetterSeg(0, .8, 1, .2)); refDigits.add(no6 ); Letter no7 = new Letter(); no7.add(new LetterSeg(0, 0, 1, .2)); no7.add(new LetterSeg(.8, 0, .2, 1)); refDigits.add(no7 ); Letter no8 = new Letter(); no8.add(new LetterSeg(0, 0, 1, .2)); no8.add(new LetterSeg(0, 0, .2, 1)); no8.add(new LetterSeg(0, .4, 1, .2)); no8.add(new LetterSeg(.8, 0, .2, 1)); no8.add(new LetterSeg(0, .8, 1, .2)); refDigits.add(no8 ); Letter no9 = new Letter(); no9.add(new LetterSeg(0, 0, .2, .5)); no9.add(new LetterSeg(.8, 0, .2, 1)); no9.add(new LetterSeg(0, 0, 1, .2)); no9.add(new LetterSeg(0, .4, 1, .2)); refDigits.add(no9 ); } class Letter { ArrayList segs= new ArrayList(); color c = color(random(100, 200)); void add(LetterSeg s) { segs.add(s); } void draw(float x, float y, float w, float h) { drawColor(x,y,w,h,c); } void drawColor(float x, float y, float w, float h, color pc) { for (LetterSeg s : segs) { noStroke(); fill(pc); s.draw(x, y, w, h); } } } class LetterSeg { float x, y, w, h; float r[] = new float[8]; LetterSeg(float px, float py, float pw, float ph) { x = px;// + random(-.1,.1); y = py;//+ random(-.1,.1); w = pw;//+ random(-.1,.1); h = ph;//+ random(-.1,.1); for (int i= 0; i < 8 ; i++) { r[i] = random(-sz/200, sz/200); } } void draw(float bx, float by, float bw, float bh) { float dx = bx + (this.x * bw); float dy = by + (this.y * bh); float dw = this.w * bw; float dh = this.h * bh; quad(dx+r[0], dy+r[1], dx+dw+r[2], dy+r[3], dx+dw+r[4], dy+dh+r[5], dx+r[6], dy+dh+r[7]); //rect(dx,dy,dw,dh); } } void drawScore(){ float s = sz / 10; float t = sz-s*1.45; fill(255); if(played && !playing){ if(p1Score >= 10){ drawBottomWord("WIN"); } else { drawBottomWord("LOSE"); } } if(p1Score < 10) { refDigits.get(p1Score).draw(s,t,s,s); } else { refDigits.get(1).draw(s/4,t,s,s); refDigits.get(0).draw(s*1.5,t,s,s); } if(p2Score < 10) { refDigits.get(p2Score).draw(sz - s*2,t,s,s); } else { refDigits.get(1).draw(sz - s*2.5,t,s,s); refDigits.get(0).draw(sz - s*1.25,t,s,s); } } void drawBottomWord(String word){ float s = sz / 10; float t = sz-s*1.45; float w = s * 1.2; float l = (sz - (w * word.length())) / 2;; for(int i = 0; i < word.length(); i++){ String let = word.substring(i,i+1); //refLetters.get(let).drawColor(l + (w * i), t, s, s,color(random(200,255))); refLetters.get(let).drawColor(l + (w * i),t, s, s,color(random(200,255))); } } void drawTitle() { float s = sz/8; float x = sz / 8; float y = sz / 8; refLetters.get("F").draw(x, y, s, s); x += s * 1.5; refLetters.get("L").draw(x, y, s, s); x += s * 1.5; refLetters.get("A").draw(x, y, s, s); x += s * 1.5; refLetters.get("P").draw(x, y, s, s); x = sz / 8; y += s * 1.5; refLetters.get("P").draw(x, y, s, s); x += s * 1.5; refLetters.get("I").draw(x, y, s, s); x += s * 1.5; refLetters.get("N").draw(x, y, s, s); x += s * 1.5; refLetters.get("G").draw(x, y, s, s); x += s * 1.5; } void drawGo() { float x = sz * .35; float y = sz * .60; float s = sz / 8; fill(100, 100, 200); rect(x - s / 2 - s/20, y-s/4 -s/20, s * 3.5, s*1.5); fill(25, 25, 50); rect(x - s / 2 + s/20, y-s/4 +s/20, s * 3.5, s*1.5); fill(50, 50, 100); rect(x - s / 2, y-s/4, s * 3.5, s*1.5); refLetters.get("G").draw(x, y, s, s); refLetters.get("O").draw(x + s * 1.5, y, s, s); } boolean mouseInGoButton(){ float x = sz * .35; float y = sz * .60; float s = sz / 8; float left = x - s / 2; float top = y-s/4; return mouseX > left && mouseX < left + s *3.5 && mouseY > top && mouseY < top + s*1.5; } Minim minim; AudioPlayer song; AudioPlayer caw; AudioPlayer ding1,ding2; void soundsStart(){ minim = new Minim(this); song = minim.loadFile("music.wav"); caw = minim.loadFile("caw.wav"); ding1 = minim.loadFile("ding1.wav"); ding2 = minim.loadFile("ding2.wav"); } void soundsPlayMusic(){ song.rewind(); song.play(); } void soundsCaw(){ caw.rewind(); caw.play(); } void soundsDing1(){ ding1.rewind(); ding1.play(); } void soundsDing2(){ ding2.rewind(); ding2.play(); }