//TODO //plums follow floating target //player gets hit //life display //message at end boolean stopAll = false; HashSet sugarplums = new HashSet(); HashSetbullets = new HashSet(); HashSetdeadPlums = new HashSet(); HashSetwings = new HashSet(); int BEFORE = 1; int DURING = 2; int AFTER = 3; int MODE = BEFORE; Tank t = new Tank(); Attractor a = new Attractor(); void setup(){ smooth(); size(500,500); frameRate(30); // resetgame(); // sugarplums.add(new Sugarplum()); textFont(createFont("",20)); } void addSugarplum(){ //float cr = random(1); //color c = color(139+(cr*115),102+(cr*85),139+(cr*115)); // color c = color(random(139,255),random(102,187),random(139,255)); sugarplums.add(new Sugarplum()); } void resetgame(){ MODE = DURING; for(int i = 0;i < 40; i++){ addSugarplum(); } gotime = millis() ; } int gotime; int stoptime; void showPre(){ fill(255); textAlign(CENTER); text("SUGARPLUM INVADERS\n\nvisions of sugarplums danced --\nAND IT'S PAYBACK TIME!\n\nUse the Dreamcatcher 3000 tank to keep the slumbering tot safe from the machinations of the evil sugarplum fairy zombies!\n\n(arrow keys move spacebar fires)",20,20,480,480); } void showPost(){ fill(255); textAlign(CENTER); text("Well Done! You staved off nightmares innumerable in only "+showTime(stoptime-gotime)+" seconds!\n\nHAPPY HOLIDAYS!\n\nI love all you trainwrecking folk!\n\n-Kirk\n\nhit enter to restart\n\n",20,20,480,480); } String showTime(int diff){ return ""+round(diff/100)/10.0; } void draw(){ background(0,0,100); if(MODE==BEFORE){ showPre(); } if(MODE==AFTER){ showPost(); } if(MODE == DURING || MODE == AFTER){ textAlign(LEFT); fill(255); if(MODE == DURING){ } String show = showTime(millis() - gotime); if( MODE==AFTER){ show = showTime(stoptime - gotime); } text("time:"+show, 10,480); } kid(); z(); a.move(); a.draw(); if(MODE == DURING){ if(sugarplums.size() == 0){ MODE = AFTER; stoptime = millis() ; } } for(Sugarplum s : sugarplums){ s.move(); s.draw(); } t.move(); //might add to bullets... t.draw(); HashSet bulletsToKill = new HashSet(); for(Bullet b : bullets){ if(!b.move()){ bulletsToKill.add(b); }; for(Sugarplum s : sugarplums){ if(b.hit(s)){ bulletsToKill.add(b); deadPlums.add(s); s.hit(b); } } b.draw(); } HashSetfallenPlums = new HashSet(); for(Sugarplum p : deadPlums){ if(! p.fall()){ fallenPlums.add(p); } p.draw(); } /* HashSetwingsToKill = new HashSet(); for(Wing w:wings){ if(!w.move()){ wingsToKill.add(w); } w.draw(); } wings.removeAll(wingsToKill); */ deadPlums.removeAll(fallenPlums); sugarplums.removeAll(deadPlums); sugarplums.removeAll(fallenPlums); bullets.removeAll(bulletsToKill); // sugarplums.removeAll(plumsToKill); } void mouseMoved(){ Iterator it = sugarplums.iterator(); while(it.hasNext()){ Sugarplum i = (Sugarplum)it.next(); i.tx = mouseX; i.ty = mouseY; } } class Bullet { float x, y; Bullet(Tank t) { x = t.x + (t.WIDTH/2); y = t.y; //stopAll = true; } float SIZE = 5; void draw() { stroke(0); strokeWeight(2); fill(255); ellipse(x,y,SIZE,SIZE); } boolean move(){ y -= 10; // x = mouseX; y = mouseY; if(y < -10) return false; return true; } boolean hit(Sugarplum s){ if(dist(x,y,s.x,s.y) < (SIZE+s.SIZE)/2){ return true; } return false; } } class Attractor{ float x=250,y=250; float xs = random(-2,2); float ys = -random(2); void draw(){ return; /* noFill(); stroke(0); ellipse(x,y,40,40); */ } void move(){ x += xs; y += ys; if(y<0) { ys = abs(ys); } if(y > 350){ ys = -abs(ys); } if(x<0){ xs = abs(xs); } if(x>500){ xs = -abs(xs); } } } void kid() { //bed stroke(0); strokeWeight(2); fill(200,0,0); rect(200,450,140,30); fill(128,128,0); rect(200,410,10,90); rect(340,430,10,70); fill(200,0,0); arc(280,451,100,40,-PI,0); fill(200,150,150); ellipse(230,440,20,20); fill(64,64,0); arc(230,440,20,20,PI*2/3,PI*5/3); } int start = 0; void z(){ // frameRate(10); fill(200,0,200); start++; for(int i = 0; i < 20; i++){ text("Z",start+230+(i*20),420+ 2*sin(i+start)); } start = start%20; } class Sugarplum{ float x,y; float angle = 0; //-HALF_PI; float SIZE = 20; float tx,ty; float SPEED = 2; float TURNSPEED = .1; float startx,starty; float xs,ys; float flap = .1; color c = color(0,250,0); Sugarplum(){ this.x = random(100,400); this.y = -50; this.tx = x; this.ty = y; this.c = color(random(130,255)/2,random(100,187)/2,random(135,255)/2); this.SIZE = random(width/30,width/20); this.SPEED = random(width/200,width/75); this.TURNSPEED = random(.05,.1); this.startx = x; this.starty = y; } boolean fall(){ ys += .1; y += ys; x += xs; if(y > height + SIZE){ return false; } return true; } boolean dead = false; void draw(){ //if(frameCount % 30 == 0){ // println(frameRate); //} noStroke(); pushMatrix(); translate(x,y); rotate(angle); fill(180); pushMatrix(); float wingangle = 2;// = random(.5,1); rotate((-PI/2)-wingangle -sin(flap)); // translate(0,SIZE/8); ellipse(0,SIZE/2,SIZE*3/4,SIZE*3/4); popMatrix(); pushMatrix(); rotate((-PI/2)+wingangle +sin(flap)); // translate(0,SIZE/6); ellipse(0,SIZE/2,SIZE*3/4,SIZE*3/4); popMatrix(); if(!dead){fill(c);} else{fill(40);} //rect(-SIZE/2,-SIZE/2,SIZE,SIZE); ellipse(0,0,SIZE*1.2,SIZE); if(!dead){ //eyes float EYESIZE = SIZE/8; fill(0); pushMatrix(); translate(0,-SIZE/4); ellipse(0,0,EYESIZE,EYESIZE); // rect(EYESIZE/2,0,EYESIZE,EYESIZE); popMatrix(); pushMatrix(); translate(0,SIZE/4); ellipse(0,0,EYESIZE,EYESIZE); popMatrix(); //eyebrows noFill(); stroke(0); strokeWeight(2); line(SIZE/4,-SIZE/4,SIZE/8,-SIZE/8); line(SIZE/4,SIZE/4,SIZE/8,SIZE/8); //arc(-SIZE*3/8,0,SIZE/4,SIZE/2,-PI/2,PI/2); } else { //dead fill(0); stroke(0); strokeWeight(2); pushMatrix(); translate(-SIZE/8,-SIZE/4); line(-SIZE/8,-SIZE/8,SIZE/8,SIZE/8); line(-SIZE/8,SIZE/8,SIZE/8,-SIZE/8); popMatrix(); pushMatrix(); translate(-SIZE/8,SIZE/4); line(-SIZE/8,-SIZE/8,SIZE/8,SIZE/8); line(-SIZE/8,SIZE/8,SIZE/8,-SIZE/8); popMatrix(); } popMatrix(); } //float anglespeed = 0; void move(){ if(stopAll) return; if(!dead){ flap += SPEED/3;} tx = a.x; ty = a.y; // if(dead) fill (100); else fill(c); float dx = (tx -x); float dy = ( ty - y); float wantangle = atan2(dy, dx); float anglediff = (angle - wantangle); anglediff /= PI; //this next bit catches the "runaround" if(dead) return; if(anglediff > 1){ anglediff -= 2; } if(anglediff < -1){ anglediff += 2; } if(anglediff > 0){ // println(" <-\\"); angle -= TURNSPEED; } else { // println(" /->"); angle += TURNSPEED; } float angcos= cos(angle); float angsin = sin(angle); x += angcos* SPEED; y += angsin * SPEED; } void hit(Bullet b){ if(!dead){ xs = SPEED*cos(angle) ; ys = sin(angle) - 1; dead = true; /*wings.add(new Wing(this)); wings.add(new Wing(this));*/ } else { ys -= 1; xs += (x - b.x)/8; } } } class Tank { float x, y; Tank(){ x = 250; y = 380; } int guncharge = 10; int CHARGENEEDED = 5; void draw(){ stroke(0); strokeWeight(2); fill(255,128,0); rect(x+20,y-10,10,30); rect(x,y,WIDTH,HEIGHT); noStroke(); rect(x+22,y-8,7,24); } float MOVE = 5; float WIDTH = 50; float HEIGHT = 20; void move(){ guncharge++; if(isKeyDown(32) && guncharge > CHARGENEEDED){ guncharge = 0; bullets.add(new Bullet(this)); } if(isKeyDown(LEFT)){ x-=MOVE; } if(isKeyDown(RIGHT)){ x+=MOVE; } } } class Wing{ float SIZE; float x,y,xs,ys; Wing(Sugarplum s){ SIZE = s.SIZE; x = s.x; y = s.y; xs = random(-3,3); ys = random(-3,3); } void draw(){ fill(180); noStroke(); ellipse(x,y,SIZE*3/4,SIZE*3/4); } boolean move(){ ys += .1; x += xs; y += ys; if(y > 520){ return false; } return true; } } HashSet keysDown = new HashSet(); void keyPressed(){ if(keyCode == 32){ if(MODE == BEFORE){ resetgame(); } } if(keyCode == 10){ if(MODE == AFTER){ resetgame(); } } this.keysDown.add(this.keyEvent.getKeyCode()); } void keyReleased(){ this.keysDown.remove(this.keyEvent.getKeyCode()); } boolean isKeyDown(int keyCode){ if(keysDown.contains(keyCode)) return true; return false; }