//todo //DONE-- //rotate head ////make snowman snowballsdecreasing in size //make hat hittable? //better score presentation? //more room at bottom for longer shots HashMap font = new HashMap(); String endtext = "You have rescued your snowy field from the snowman menace!\n\nThanks for being part of the site and at least in that way part of my life --\n\nHope your holidays are great!"; String afterendtext = "\n(Click to Retry)"; PImage sig; String intotext; long timestart; int HOWMANY = 8; ArrayList snowmen = new ArrayList(); float BALLSIZE = 8; float GRAV = .2; float tx,ty; ArrayList snowballs = new ArrayList(); int PRE = 0; int PLAY = 1; int POST = 2; int gamemode = PLAY; void setup(){ size(500,500); frameRate(30); resetsnow(); textSize(20); lowLag.init(); lowLag.load("BOINGHI.mp3"); lowLag.load("AHH.mp3"); lowLag.load("POP3.mp3"); } void resetsnow(){ snowmen = new ArrayList(); snowballs = new ArrayList(); for(int i = 0; i < HOWMANY; i++){ snowmen.add(new snowman()); } ballsused = 0; timestart = millis(); } float GROUNDHEIGHT = 80; void draw(){ background(128,128,255); stroke(0); strokeWeight(2); fill(255); noStroke(); rect(0,height-80,width,80); Iterator i; snowman s; i = snowmen.iterator(); int alive = 0; while(i.hasNext()){ s = (snowman)i.next(); s.move(); s.draw(); if(s.life != s.DEAD){ alive++; } } drawslingsticks(); drawsling(); if(gamemode != PRE){ showscores(); } ArrayList snowballsToKill = new ArrayList(); i = snowballs.iterator(); while(i.hasNext()){ snowball b = (snowball)i.next(); b.move(); b.draw(); if(b.offscreenForGood()){ snowballsToKill.add(b); } else { Iterator j = snowmen.iterator(); while(j.hasNext()){ snowman m = (snowman)j.next(); if(m.hitBy(b)){ snowballsToKill.add(b); m.kill(b); lowLag.play("POP3.mp3"); } } } } for(snowball b : snowballsToKill){ snowballs.remove(b); } if(gamemode == PRE){ introtext(); } if(gamemode == PLAY){ if(alive == 0){ gamemode = POST; lowLag.play("AHH.mp3"); } } if(gamemode == POST){ aftertext(); } } void drawslingsticks(){ strokeWeight(5); stroke(150,131,68); line(width/2,height-GROUNDHEIGHT,width/2,height-(GROUNDHEIGHT+20)); line(width/2,height-(GROUNDHEIGHT+20),(width/2)-20,height-(GROUNDHEIGHT+40)); line(width/2,height-(GROUNDHEIGHT+20),(width/2)+20,height-(GROUNDHEIGHT+40)); } void drawsling(){ strokeWeight(2); stroke(255,128,128); tx = mouseX; ty = mouseY; if(ty < height - GROUNDHEIGHT) ty = height-(GROUNDHEIGHT); if(mousePressed){ noFill(); ellipse(tx,ty,BALLSIZE,BALLSIZE); line(tx,ty,(width/2)-20,height-(GROUNDHEIGHT+40)); line(tx,ty,(width/2)+20,height-(GROUNDHEIGHT+40)); } else { line((width/2)-20,height-(GROUNDHEIGHT+40),(width/2)+20,height-(GROUNDHEIGHT+40)); } } void showscores(){ fill(0); float showtime; if(gamemode == PLAY){ endtime = millis(); } showtime = round((endtime - timestart) / 100.0); showtime /= 10.0; String t = "time: "+showtime; if(showtime == round(showtime)) { t += ".0";} stroke(0); strokeWeight(2); textAlign(CENTER,CENTER); //generate a score normalized so a great performance is 100, //ok is 50-- min is 50 and can go above 100 if(gamemode == POST){ float score = round((1120 / (ballsused + showtime)) + 40); text("final score:"+score+"/100",250,250); } float y = height - 20; text(t,125, y); String b = "snowballs used: "+ballsused; text(b,375, y); } void fontText(float x, float y, String s,float w){ } int ballsused = 0; long endtime; void aftertext(){ fill(60,60,200); stroke(0); //!!text(afterendtext,170,height-100); //!!image(sig, width-109, height-200); } class snowman{ float ball3,ball2,ball1; float x1,y1; float x2,y2; float x3,y3; float s1,s2,s3,s4,s5,s6; float s1x, s1y,s2x,s2y; float hatx,haty; float hatsize; float nx,ny; float speedxmain, speedymain; int ALIVE = 0; int DEAD = 1; int life; float speedx1, speedy1; float speedx2, speedy2; float speedx3, speedy3; float speedxstick1, speedystick1; float speedxstick2, speedystick2; float speedxhat, speedyhat; void move(){ if(life == ALIVE){ if(x3 < width / 2){ speedxmain += .3; } else { speedxmain -= .3; } // speedxmain += (random(2)-1); x1 += speedxmain; x2 += speedxmain; x3 += speedxmain; s1x += speedxmain; s2x += speedxmain; hatx += speedxmain; if(x3 < ball3 ){ speedxmain = abs(speedxmain) * .75; } if(x3 > width-ball3){ speedxmain = abs(speedxmain) * -.75; } } else { //DEAD x1 += speedx1; y1 += speedy1; x2 += speedx2; y2 += speedy2; x3 += speedx3; y3 += speedy3; s1x += speedxstick1; s1y += speedystick1; s2x += speedxstick2; s2y += speedystick2; hatx += speedxhat; haty += speedyhat; speedy1 += GRAV; speedy2 += GRAV; speedy3 += GRAV; speedystick1 += GRAV; speedystick2 += GRAV; speedyhat += GRAV; hata += hatas; //println(hatas); stick1a += stick1as; stick2a += stick2as; heada += headas; } } float hata; float hatas; float stick1a,stick1as; float stick2a,stick2as; float heada,headas; void draw(){ stroke(0); strokeWeight(6); fill(255); ellipse(x1,y1,ball1,ball1); ellipse(x2,y2,ball2,ball2); ellipse(x3,y3,ball3,ball3); noStroke(); ellipse(x1,y1,ball1,ball1-1); ellipse(x2,y2,ball2,ball2-1); ellipse(x3,y3,ball3,ball3-1); // ellipse(x1,y1,ball1*.66,ball1*.66); //ellipse(x2,y2,ball2*.75,ball2*.75); //ellipse(x3,y3,ball3*.75,ball3*.75); strokeWeight(3); stroke(150,131,68); pushMatrix(); translate(s1x,s1y); rotate(stick1a); line(0,0, 0-(ball2*.5),0+s1); line( 0-(ball2*.5),0+s1,0-ball2,0+s2); line( 0-(ball2*.5),0+s1,0-ball2,0+s3); popMatrix(); pushMatrix(); translate(s2x,s2y); rotate(stick2a); line(0,0, 0+(ball2*.5),0+s4); line( 0+(ball2*.5),0+s4,0+ball2,0+s5); line( 0+(ball2*.5),0+s4,0+ball2,0+s6); popMatrix(); fill(0); stroke(0); pushMatrix(); translate(hatx,haty); rotate(hata); rect( - (hatsize/4), - hatsize*.75, hatsize/2, hatsize*.75); rect(- (hatsize/2), 0, hatsize,hatsize/8); popMatrix(); pushMatrix(); translate(x1,y1); rotate(heada); ellipse(0-(ball1/4),0-(ball1/6),2,2); ellipse(0+(ball1/4),0-(ball1/6),2,2); stroke(247,179,33); line(0,0,nx,ny); popMatrix(); } boolean hitBy(snowball b){ if(dist(hatx,haty,b.x,b.y) * 2 <= BALLSIZE + hatsize){ whathit = 4; return true; } // float dist1 = sqrt( pow((x1 - b.x),2) + pow( (y1 - b.y),2)); if(dist(x1,y1,b.x,b.y) *2 <= BALLSIZE+ball1) { whathit = 1; return true; } // float dist2 = sqrt( pow((x2 - b.x),2) + pow( (y2 - b.y),2)); if(dist(x2,y2,b.x,b.y) *2<= BALLSIZE+ball2) { whathit = 2; return true; } if(dist(x3,y3,b.x,b.y) *2<= BALLSIZE+ball3) { whathit = 3; return true; } return false; } snowman(){ float x = random(20,width-20); float y = random(40,height-150); ball1 = random(10,25);//ball1 + (random(4,10)); //head ball2 = ball1 + (random(4,15)); ball3 = ball2 + (random(4,15)); x1 = x2 = x3 = x; y1 = y - (ball2*.4)-(ball1*.4); y2 = y; y3 = y + (ball2*.4)+(ball3*.4); s1 = random(0,12) - 3; s2 = random(0,12) - 3; s3 = random(0,12) - 3; s4 = random(0,12) - 3; s5 = random(0,12) - 3; s6 = random(0,12) - 3; s1x = x - (ball2*.5); s1y = y; s2x = x + (ball2*.5); s2y = y; hatx = x; haty = y1 - (ball1/2); hatsize = ball1; nx = random(0,16) - 4; ny = random(0,16) - 4; life = ALIVE; speedxmain = random(0,10) - 5; } float plusorminus(float range){ return random(range * 2) - range; } int whathit; void kill(snowball b){ float basespeed = 3; if(life != DEAD){ life = DEAD; speedx1 = ((speedxmain + b.xs) / 2) + plusorminus(basespeed); speedy1 = ((speedymain + b.xs) / 2) + plusorminus(basespeed); speedx2 = ((speedxmain + b.xs) / 2) + plusorminus(basespeed); speedy2 = ((speedymain + b.xs) / 2) + plusorminus(basespeed); speedx3 = ((speedxmain + b.xs) / 2) + plusorminus(basespeed); speedy3 = ((speedymain + b.xs) / 2) + plusorminus(basespeed); speedxstick1 = ((speedxmain + b.xs) / 2) + plusorminus(basespeed); speedystick1 = ((speedymain + b.xs) / 2) + plusorminus(basespeed); speedxstick2 = ((speedxmain + b.xs) / 2) + plusorminus(basespeed); speedystick2 = ((speedymain + b.xs) / 2) + plusorminus(basespeed); speedxhat = ((speedymain + b.xs) / 2) + plusorminus(basespeed); speedyhat = ((speedymain + b.xs) / 2) + plusorminus(basespeed); hatas = random(-.3,.3); stick1as = random(-.1,.1); stick2as = random(-.1,.1); headas = random(-.3,.3); } else { if(whathit == 1){ speedx1 = ((speedxmain + b.xs) / 2) + plusorminus(basespeed); speedy1 = ((speedymain + b.xs) / 2) + plusorminus(basespeed); } if(whathit == 2){ speedx2 = ((speedxmain + b.xs) / 2) + plusorminus(basespeed); speedy2 = ((speedymain + b.xs) / 2) + plusorminus(basespeed); } if(whathit == 3){ speedx3 = ((speedxmain + b.xs) / 2) + plusorminus(basespeed); speedy3 = ((speedymain + b.xs) / 2) + plusorminus(basespeed); } if(whathit == 4){ speedxhat = ((speedxmain + b.xs) / 2) + plusorminus(basespeed); speedyhat = ((speedymain + b.xs) / 2) + plusorminus(basespeed); hatas = random(-.3,.3); } } } } void mouseReleased(){ // if(gamemode == PLAY){ snowballs.add(new snowball()); ballsused++; lowLag.play("BOINGHI.mp3"); //} } void reset(){ resetsnow(); gamemode = PLAY; } void keyPressed(){ if(key == ' '){ reset(); } } class snowball{ float xs,ys,x,y; snowball(){ x = tx; y = ty; float midx = width/2; float midy = height-90; xs = (midx - tx)/6; ys = (midy - ty)/6; } void draw(){ stroke(0); strokeWeight(3); fill(255); ellipse(x,y,BALLSIZE,BALLSIZE); } void move(){ x += xs; y += ys; ys += GRAV; } boolean offscreenForGood(){ if(x < -20 && xs < 0){ return true; } if(x > width+20 && xs > 0){ return true; } if(y < - 20 && ys < 0){ return true; } return false; } }