//TODO: //snowman blocked by shrroms? //extra heads coming in //scorpion husky? (yellow snow ;-) //snow ball rolling down to add shrooms //DONE //snow man player //shroom //centipede basics //eyes for heads //shoot centipede //--becomes shroom //--follower is head //shoot shroom //points //death w/ reset //lives //snowman blast //speeding up //centiped rebounds off bottom //spider //snowman only at bottom float SZ=10; float NUMSPOT=25; int score = 0; int SHROOMVALUE = 10; int SEGVALUE = 100; int SPIDERVALUE = 1000; Shroom shroomgrid[][] = new Shroom[25][25]; Shot shot = null; ArrayList segsToKill = new ArrayList(); ArrayList segsToAdd = new ArrayList(); ArrayList heads = new ArrayList(); Spider spider ;//= new Spider(); int lives = 3; //int framesToReset = -1; /* void mousePressed(){ println("--"); c.move(); } */ void mousePressed(){ if(!gameOn){ restartGame(); } } boolean freeze(){ return (p.falling); } void restartGame(){ score = 0; lives = 3; virgin = false; gameOn = true; frameRate(40); resetPede(); } void resetPede(){ heads.clear(); heads.add(new Seg(true,1,1,12)); } void showTitle(){ textAlign(CENTER); fill(255); stroke(0); rect(75,80,100,50); fill(0); text("SNOWPEDE\nclick to play",125,100); } void addShrooms(){ for(int c = 0; c < 30; c++){ int x = (int)Math.floor(random(25)); int y = 1+(int)Math.floor(random(19)); shroomgrid[x][y] = new Shroom(x,y); } } void setup(){ size(250,250); frameRate(60); textSize(10); lowLag.init(); lowLag.load("spider.mp3"); lowLag.load("dng.mp3"); lowLag.load("ijh.mp3"); lowLag.load("pkw.mp3"); smooth(); ellipseMode(CENTER); addShrooms(); heads.add(new Seg(true,1,1,12)); // restartGame(); } int segCount = 0; Seg c; Player p = new Player(); boolean virgin = true; boolean gameOn = false; void draw(){ // println(frameRate); background(255); fill(0); if(gameOn){ textAlign(LEFT); //text("LIVES:",200,10); drawLives(); } String sc = new String(score); while(sc.length() < 6){ sc = "0"+sc; } text(sc,10,10); pushMatrix(); translate(5,7); noStroke(); fill(255,0,0); for(int y = 0; y < 25; y++){ for(int x = 0; x < 25; x++){ if(shroomgrid[x][y] != null){ shroomgrid[x][y].draw(); if(shot != null){ if(shot.hit(shroomgrid[x][y])) { score += SHROOMVALUE; shot = null; lowLag.play("dng.mp3"); if(shroomgrid[x][y].damage() == false){ shroomgrid[x][y] = null; }}}//shot wasnt null }//shroom wasnt null }//x }//y if(mousePressed && shot == null && gameOn && !freeze()){ shot = new Shot(p); } if(shot != null){ if(shot.move()) shot.draw(); else shot = null; } if(! freeze()){ for(Seg c : heads){ c.move(); c.draw(); c.checkHit(); } } p.move(); if(spider != null){ if(! spider.move()) { //spider offscreen spider = null; } else { spider.draw(); if(spider.hitShot()){ spider = null; score += SPIDERVALUE; shot = null; } else if(p.hitSpider()){ loseLife(); spider = null; } } } else { //make a new spider? float r = random(1000); if(gameOn&& r < 3){ spider = new Spider(); } } if(gameOn) p.draw(); heads.removeAll(segsToKill);heads.addAll(segsToAdd);segsToKill.clear();segsToAdd.clear(); if((p.falling) || heads.size() == 0){ if(heads.size() == 0){ frameRate(frameRate+10); } resetPede(); } popMatrix(); //if(!gameOn) showTitle(); } void loseLife(){ lowLag.play("pkw.mp3"); lives--; if(lives >= 0){ //slow down but not below 40 frameRate(frameRate-10 > 40 ? frameRate - 10 : 40); // framesToReset = 30; } else { gameOn =false; frameRate(60); } } class Shroom{ float x, y; int px,py; Shroom(int pxp, int pyp){ px = pxp; py = pyp; x = pxp * SZ; y = pyp * SZ; } int health = 2; boolean damage(){ health--; if(health <= 0){ return false; } return true; } void draw(){ strokeWeight(2); stroke(0); fill(255); arc(x,y,10,10,PI,2*PI) ; // arc(x,y,5,10,PI,2*PI) ; line(x-5,y,x+5,y); // line(x,y,x,y-5); //rect(x-5,y,3,4); // rect(x+2,y,5,4);// rect(x+4,y+1,1,1); if(health == 2){ rect(x-2,y,4,4); } } } class Seg{ float x = 125, y=125; float goalx,goaly; int goalSpotX; int goalSpotY; boolean head; Seg follower = null; int tag; boolean dead = false; Seg(boolean phead, int psx, int psy, int sz){ tag = segCount++; goalSpotX = psx; goalSpotY = psy; head = phead; goalx = goalSpotX * SZ; goaly = goalSpotY * SZ; x = goalx; y = goaly; for(int i = 0; i < sz-1; i++){ addFollower(); } } boolean hitShot(){ if(shot == null) return false; return(shot.hit(this)); } void checkHit(){ if(hitShot()){ lowLag.play("ijh.mp3"); score += SEGVALUE; shot = null; segsToKill.add(this); if(follower != null){ follower.head = true; segsToAdd.add(follower); } dead = true; shroomgrid[goalSpotX][goalSpotY] = new Shroom(goalSpotX,goalSpotY); return; } if(follower != null){ follower.checkHit(); } } void addFollower(){ if(follower != null){ follower.addFollower(); } else{ follower = new Seg(false,goalSpotX-1,goalSpotY,0); } } int MAXSTEP = 5; int step = MAXSTEP; int dir = 1; int genupdown = 1; void draw(){ if(follower != null){ follower.draw(); } strokeWeight(2); stroke(0); fill(255); ellipse(x,y,SZ,SZ) ; if(head){ ellipse(x-2+dir,y,1,1); ellipse(x+2+dir,y,1,1); } } void drawDead(){ strokeWeight(2); stroke(0); fill(128,0,0); ellipse(x,y,SZ,SZ) ; if(head){ ellipse(x-2+dir,y,1,1); ellipse(x+2+dir,y,1,1); } } //follower gets its goal point set: void setGoal(int px,int py){ if(follower != null){ follower.setGoal(goalSpotX,goalSpotY); } goalSpotX = px; goalSpotY = py; goalx = goalSpotX * SZ; goaly = goalSpotY * SZ; } void move(){ if(p.hit(this)){ loseLife(); } if(follower != null && follower.dead){ follower = null; } if(head){ if(step > MAXSTEP){ //time to decide int updown = 0; if(dir == 1){ if(goalSpotX >= NUMSPOT-1 || hasShroom(goalSpotX+dir,goalSpotY)){ dir = -1; updown = genupdown; } } if(dir == -1){ if(goalSpotX <= 0 || hasShroom(goalSpotX+dir,goalSpotY)){ dir = 1; updown = genupdown; } } if(follower != null){ // println("my name is "+tag+" and I am calling setgaol w/ my own "+goalSpotX+" where I was"); follower.setGoal(goalSpotX,goalSpotY); } if(updown == 0){ goalSpotX += dir; } else { goalSpotY += updown; if(goalSpotY >= 24 || goalSpotY < 1){ genupdown *= -1; updown *= -1; goalSpotY += 2 * updown; } } goalx = goalSpotX * SZ; goaly = goalSpotY * SZ; // println("now at "+y+" goaly is "+goaly); step = 1; } else { step++; } } else { if(step > MAXSTEP){ step = 1; } else { step++; } } float per = (float)step/(float)MAXSTEP; // if(head)println("head step is "+step); x = lerp(x, goalx, per); y = lerp(y, goaly, per); if(follower != null){ follower.move(); } } boolean hasShroom(int px, int py){ if(px < 0 || px >= 25 || py < 0 || py >= 25) return false; if(shroomgrid[px][py] != null){ return true; } return false; } } class Spider{ float x = 200, y = 180; float fullsound = 80; float soundtimer = 0; float dir; float updown = 1; boolean sidetoside = true; Spider(){ if(random(1) < .5) dir = -1; else dir = 1; x = (width / 2) - dir * ((width/2)+20); //random extra room because of universal translate } void move(){ if(soundtimer <= 0){ lowLag.play("spider.mp3"); soundtimer = fullsound; } soundtimer--; //y = mouseY - 100; if(sidetoside) x += dir * 1.5; if(x < -20 || x > width + 20) return false; y += updown; boolean mightSwitch = false; if(y > 230) { updown = -2; mightSwitch = true; } if(y < 180) { updown = 2; mightSwitch = true; } if(mightSwitch) { if(random(1) < .3){ sidetoside = !sidetoside; } } return true; } void draw(){ line(x-10,y,x+10,y); line(x-8,y-3,x+8,y+3); line(x-8,y+3,x+8,y-3); ellipse(x,y,10,10) ; // line(125,125,x,y); } boolean hitShot(){ if(shot == null) return false; return(shot.hitSpider()); } } class Player{ boolean falling = false; float x=125, y=225; void draw(){ strokeWeight(2); stroke(0); fill(255); if(! falling){ ellipse(x,y,10,10) ; ellipse(x,y-6,6,6); if(shot == null){ fill(0); rect(x-2,y-12,4,4); line(x-4,y-8,x+4,y-8); } } else {//falling ellipse(bx,by,10,10) ; ellipse(hx,hy,6,6); fill(0); rect(tx-2,ty-12,4,4); line(tx-4,ty-8,tx+4,ty-8); } } float hxs,bxs,bys,hys,bx,by,hx,hy,tx,ty,txs,tys; boolean hit(Seg s){ if(falling || ! gameOn) return false; if(dist(x,y,s.x,s.y) <= 10 || dist(x,y-6,s.x,s.y) <= 8 ){ fallApart(); return true; } return false; } boolean hitSpider(){ if(falling || ! gameOn) return false; if(dist(x,y,spider.x,spider.y) <= 10 || dist(x,y-6,spider.x,spider.y) <= 8 ){ fallApart(); return true; } return false; } void fallApart(){ falling = true; hys = random(-3,0);bys = 0; hxs = random(-3,3);; bxs = random(-3,3); bx = x; by = y; hx = x; hy = y - 6; tx = x; ty = y; txs = random(-3,3); tys = hys * 1.5; } void move(){ if(!falling){ x = mouseX-10; y = mouseY-10; if(y < 180) y = 180; if(x<0) x = 0; if(x>240) x = 240; } else { hys += .2; bys +=.2; bx += bxs; hx += hxs; by += bys; hy += hys; tys += .2; tx += txs; ty += tys; if(ty > 250 ){ if(lives >0){ falling = false; } else { gameOn = false; } } } } } class Shot{ float x,y; Shot(Player p){ x = p.x; y = p.y; } boolean move(){ y = y - 5; if(y > 0){ return true; } return false; } boolean hit(Seg s){ if(s == null) return false; if(dist(s.x,s.y,x,y-8) < 10) return true; return false; } boolean hitSpider(){ if(spider == null) return false; if(dist(spider.x,spider.y,x,y-8) < 10) return true; return false; } boolean hit(Shroom s){ if(s == null) return false; if(dist(s.x,s.y,x,y-8) < 10) return true; return false; } void draw(){ fill(0); rect(x-2,y-12,4,4); line(x-4,y-8,x+4,y-8); } } void drawLives(){ for(int i = 0; i < lives - 1; i++){ strokeWeight(2); fill(255); stroke(0); ellipse(232+8*i,8,4,4); ellipse(232+8*i,4,3,3); fill(0); noStroke(); rect(232+8*i-2,0,5,4); strokeWeight(1); stroke(0); line(232+3*i-3,3,232+8*i+3,3); } }