Tree t = new Tree(); boolean gameIsWon = true; int COLS = 5; int ROWS = 4; Gift gifts[][] ; //= new Gift[ROWS][COLS]; Star star; boolean gameOver = false; void reset(){ boolean left = true; gifts = new Gift[ROWS][COLS]; for (int r = 0; r < ROWS; r++) { color col = color(random(128, 255), random(128, 255), random(128, 255)); color ribcol = color(random(128, 255), random(128, 255), random(128, 255)); float xsz = random(20, 30); float ysz = random(20, 30); for (int c = 0; c < COLS; c++) { gifts[r][c] = new Gift(50 + (c * (400 / COLS)), 50 + r * 50, xsz, ysz, col, ribcol, left); } left = !left; } dir = RIGHT; resetCounter(); } void keyPressed(){ if(key == ' ')reset(); } int invaderCount = COLS * ROWS; int counter; int dir = RIGHT; int nextDir; //Gift g;// = new Gift(); void setup() { size(500, 500); reset(); star = new Star(); rectMode(CENTER); noStroke(); } void resetCounter() { counter = invaderCount * 2 ; } void draw() { background(0, 0, 50); fill(255); boolean invadersMove = false; counter--; if (counter <= 0) { resetCounter(); invadersMove = true; if (dir == DOWN) { Gift bottemest = findLowestGift(); if(bottemest.y > 400){ gameOver = true; } dir = nextDir; } else { if (dir == RIGHT) { Gift rightmost = findRightmostGift(); if (rightmost != null) { if (rightmost.x > 450) { dir = DOWN; nextDir = LEFT; } } } if (dir == LEFT) { Gift leftmost = findLeftmostGift(); if (leftmost != null) { if (leftmost.x < 50) { dir = DOWN; nextDir = RIGHT; } } } } } if(! gameOver) t.move(); fill(255); if(gameOver) fill(128,128,0); rect(250, 450, 500, 100); t.draw(); if(!gameOver){ star.move(); if (starHit()) { star.didHit(); } star.draw(); } for (int r = 0; r < ROWS; r++) { for (int c = 0; c < COLS; c++) { if (invadersMove && !gameOver) gifts[r][c].move(dir); gifts[r][c].draw(); } } } void mousePressed() { star.fire(); } boolean starHit() { for (int r = 0; r < ROWS; r++) { for (int c = 0; c < COLS; c++) { Gift g = gifts[r][c]; if (g.alive && dist(g.x, g.y, star.x, star.y) <= (g.circ/2) + star.SIZE) { g.alive = false; invaderCount--; return true; } } } return false; } Gift findRightmostGift() { for (int c = COLS-1; c >= 0; c--) { for (int r = 0; r < ROWS; r++) { if (gifts[r][c].alive) { return gifts[r][c]; } } } return null; } Gift findLeftmostGift() { for (int c = 0; c < COLS; c++) { for (int r = 0; r < ROWS; r++) { if (gifts[r][c].alive) { return gifts[r][c]; } } } return null; } Gift findLowestGift() { for (int r = ROWS-1; r >= 0; r--) { for (int c = 0; c < COLS; c++) { if (gifts[r][c].alive) { return gifts[r][c]; } } } return null; } class Ornament { color c; float x, y; Ornament( color pc, float px, float py) { c = pc; x = px; y = py; } void draw() { fill(c); noStroke(); //strokeWeight(2); ellipse(x, y, 8, 8); } } class Tree { float x = 250; float y = 400; float sz = 30; float a = -PI/2, xs, ys; float ornangle = 0; Ornament red, yellow, blue; Tree() { // star = new Star(0, 0); red = new Ornament(color(255, 0, 0), 7.5*cos(-PI/2), 7.5*sin(-PI/2)); yellow = new Ornament(color(255, 255, 0), 7.5*cos(PI*5/6), 7.5*sin(PI*5/6)); blue = new Ornament(color(0, 0, 255), 7.5*cos(PI/6), 7.5*sin(PI/6)); } /* Prop getNewProp() { return new Prop(x + .75*sz*cos(a+PI), y+sz*.75*sin(a+PI)); } */ void move() { if (x < mouseX) x+= 2.5; if (x > mouseX) x-= 2.5; } float getStarY() { return y - sz * .9; } void draw() { strokeWeight(2); pushMatrix(); translate(x, y); rotate(a+PI/2); //ellipse(0,0,sz,sz); stroke(0); fill(128, 64, 0); rect(0, 10, 10, 18); fill(50, 200, 50); triangle(0, -sz*.8, -20, 10, 20, 10); // translate(0, -sz*.9); red.draw(); yellow.draw(); blue.draw(); popMatrix(); } } class Star { float x, y; float SIZE = 10; float angle = .9;//PI/4; Star(){ } Star(float px, float py) { x = px; y = py; } boolean flying = false; void fire() { flying = true; } void didHit() { flying = false; stickToTree(); } void move() { if (flying) { y-= 3; if (y < 0) { didHit(); } } else { stickToTree(); } } void stickToTree() { y = t.getStarY(); x = t.x; } void draw() { /* stroke(255); noFill(); ellipse(x,y,SIZE*2,SIZE*2); */ fill(255, 255, 0); // angle+=.01; // angle = mouseX / 250.0; noStroke(); // println(angle); pushMatrix(); translate(x, y); beginShape(); vertex(cos(angle) * SIZE, sin(angle)*SIZE); vertex(cos(angle + TWO_PI*.4) * SIZE, sin(angle+ TWO_PI*.4)*SIZE); vertex(cos(angle + TWO_PI*.8) * SIZE, sin(angle+ TWO_PI*.8)*SIZE); vertex(cos(angle + TWO_PI*.2) * SIZE, sin(angle+ TWO_PI*.2)*SIZE); vertex(cos(angle + TWO_PI*.6) * SIZE, sin(angle+ TWO_PI*.6)*SIZE); endShape(CLOSE); popMatrix(); } } class Gift { float x, y, xsz, ysz, ribwidth; color col, ribcol; boolean tilt; boolean alive = true; float circ; Gift(float x, float y, float pxsz, float pysz, color col1, color col2, boolean ptilt) { this.x = x; this.y = y; xsz = pxsz; ysz = pysz; ribwidth = min(xsz, ysz)/4; col = col1; ribcol = col2; tilt = ptilt; circ = xsz + ysz / 2; } boolean move(int dir) { tilt = !tilt; if (dir == DOWN) { y += 20; if (y > 400) return true; } if (dir == LEFT) { x -= 20; if (x < 50) return true; } if (dir == RIGHT) { x += 20; if (x > 50) return true; } return false; } void draw() { if (!alive) return ; rectMode(CENTER); ellipseMode(CENTER); pushMatrix(); translate(x, y); /* stroke(255); noFill(); ellipse(0,0,circ,circ); */ fill(col); stroke(0); if (tilt) rotate(-.5); else rotate(.5); rect(0, 0, xsz, ysz); // println(x+":"+y+":::"+xsz+":::"+ysz); fill(ribcol); rect(0, 0, ribwidth, ysz); rect(0, 0, xsz, ribwidth); noStroke(); rect(0, 0, ribwidth, ysz); // rect(0, 0, xsz, ribwidth); stroke(0); ellipse(0, -xsz/2, ribwidth*2, ribwidth*2); popMatrix(); } }