color BROWN = color(153,102,51); color YELLOW = color( 238,221,130); Deer d,d1,d2; ArrayList walls; boolean stopped; int HISTORY = 100; float heightHistory[] = new float[HISTORY]; float angleHistory[] = new float[HISTORY]; float GRAV = .06; void setup(){ textSize(20); frameRate(30); size(500,500); reset(); } float tunnelFloor ; float tunnelHeight; float wallWidth ; float tunnelDrift ; float fr ; int score; int offset; void reset(){ tunnelFloor = 496; tunnelHeight = 150; wallWidth = 20; tunnelDrift = -1; fr = 50; score = -1; offset = 0; slam = false; d = new Deer(75,true); for(int i = 0; i< HISTORY; i++){ heightHistory[i] = d.y; angleHistory[i] = d.a; } d1 =new Deer(50,false); d2 = new Deer(25,false); // pack.add(new Deer(0,false)); walls = new ArrayList(); for(int v = 240; v < 520; v += wallWidth){ addWalls(v); } } void keyPressed(){ if(key == ' '){ reset(); } } boolean slam; void draw(){ if(! stopped && !slam) { offset++; score++; } background(0,0,100); strokeWeight(4); noFill(); stroke(255); line(40-offset,0,40-offset,10); line(180-offset,0,180-offset,10); rect(10-offset,10,200,140); fill(255); // text(frameRate,120,20); textAlign(CENTER); text("REINDEER",110-offset,40); text("TRAINING",110-offset,60); text("press to fly,",110-offset,90); text("release to fall",110-offset,110); text("how far can you go?",110-offset,135); float rightside = maxWallRight(); if(rightside < 520){ addWalls(rightside); } if(!slam){ d.move(); record(d); d1.y = heightHistory[25]; d2.y = heightHistory[50]; d1.a = angleHistory[25]; d2.a = angleHistory[50]; } d.draw(); d1.draw(); d2.draw(); ArrayList wallsToKill = new ArrayList(); for(Wall w : walls){ w.move(); w.draw(); } for(Wall w : walls){ if(w.hit(d)){ slam = true; } if(w.x + w.w < -10){ wallsToKill.add(w); } } for(Wall w : wallsToKill){ walls.remove(w); } fill(255); int myoff = 160 - offset; if(myoff < 0) myoff = 0; String sc = ""+score; if(sc.length() < 2) sc = "0"+sc; if(sc.length() < 3) sc = "0"+sc; text("score: "+sc,250+myoff,40); if(slam){ text("reset to replay",250,480); } } void record(Deer d){ for(int i = HISTORY-1; i > 0; i--){ heightHistory[i] = heightHistory[i-1]; angleHistory[i] = angleHistory[i-1]; } heightHistory[0] = d.y; angleHistory[0] = d.a; } float maxWallRight(){ float rightest = 0; for(Wall w : walls){ if(w.x + w.w > rightest){ rightest = w.x + w.w; } } return rightest; } void addWalls(float left){ walls.add(new Wall(left,tunnelFloor,wallWidth,500-tunnelFloor,color(100,100,200,200))); walls.add(new Wall(left,0,wallWidth,tunnelFloor-tunnelHeight,color(100,100,200,200))); tunnelFloor +=tunnelDrift; if(tunnelHeight > 50){ tunnelHeight -= ((float)score) / 400.0; } if(tunnelFloor < tunnelHeight +10 ){ tunnelFloor = tunnelHeight +10; tunnelDrift /= -2; } else if(tunnelFloor > 490){ tunnelFloor = 490; tunnelDrift /= -2; } else {//in range float mod = 1 + ((float)score) / 300.0; tunnelDrift += random(-mod,mod); if(random(1) < .05){ tunnelDrift *= -1; } if(abs(tunnelDrift) < 2){ tunnelDrift = 2 * (tunnelDrift/abs(tunnelDrift)); } if(abs(tunnelDrift) > 15){ tunnelDrift = .5 * (tunnelDrift/abs(tunnelDrift)); } // tunnelDrift = -20; } if(fr <= 120){ fr+=.1; frameRate(fr); } } class Wall{ float x,y,w,h; color c; Wall(float px, float py, float pw, float ph, color pc){ x = px; y = py; h = ph; w=pw; c = pc; } void move(){ if(! stopped && !slam) { x--; } } void draw(){ noStroke(); if(slam) fill(200,0,0); else fill(c); rect(x,y,w,h); } boolean hit(Deer d) { if(overlap(this.x,this.x+this.w,d.hx,d.hx+d.w)&& overlap(this.y,this.y+this.h,d.hy,d.hy+d.h)) { return true; } return false; } } boolean overlap(float line1val1,float line1val2,float line2val1,float line2val2) { float line1min = min(line1val1,line1val2); float line1max = max(line1val1,line1val2); float line2min = min(line2val1,line2val2); float line2max = max(line2val1,line2val2); if(line1min < line2min && line1max < line2min) { return false; } if(line1min > line2max && line1max > line2max) { return false; } return true; } float DEERLOW = 492; class Deer{ float ys; float x,y; float a; // rect(-5,-8,14,11); float hx, hy, w,h; //values used for colisions.... boolean lead; Deer(float px, boolean plead){ x = px; y = DEERLOW; w = 14; h = 11; lead = plead; } void move(){ // x = mouseX-40; // y = mouseY-40; stopped = false; if(mousePressed) { ys -= GRAV; a += .3; } else { ys += GRAV; } y += ys; if(y < 0){ y = 0; ys = ys * -1; } if(y > DEERLOW){ y = DEERLOW; ys = ys /- 2; if(abs(ys) < 1){ stopped = true; } } hx = x -5; hy = y - 8; } void draw(){ pushMatrix(); translate(x,y); drawDeer2(lead,a); popMatrix(); } } void drawDeer2(boolean nose, float leg){ fill(BROWN); noStroke(); stroke(BROWN); strokeWeight(2); line(0,0,-4-2*cos(leg),8);//front leg line(0,0,4+2*cos(leg),8); //backleg noStroke(); rect(-5,-4,10,8); //body rect(-7,-3,2,2); //tail stroke(YELLOW); line(4,-4,0,-12); line(2,-8,6,-12); noStroke(); rect(2,-8,8,7); //head if(nose) fill(255,0,0); else fill(128); rect(10,-6,2,2); /* noFill(); strokeWeight(1); stroke(255,0,0); rect(-5,-8,14,11); */ }