Skip to content

Instantly share code, notes, and snippets.

@spoike
Created September 1, 2011 19:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save spoike/1187023 to your computer and use it in GitHub Desktop.
Save spoike/1187023 to your computer and use it in GitHub Desktop.
Space Invaders Generator
/**
* Space invaders generator. Generates and draws invaders. Inspired
* by invaders fractals:
* http://www.levitated.net/daily/levInvaderFractal.html
*
* Mouse press will create new invaders and draw the new ones.
*/
/** Scaling factor */
float sc = 3f;
/** Invader amount x-axis */
int invx = 25;
/** Invader amount y-axis */
int invy = 25;
/** Invader width */
int invw = 5;
/** Invader height */
int invh = 5;
/** Invader pixel width */
float pw = 1f;
/** Margin between invaders (in pixels) */
int margin = 5;
/** Background color */
color backCol = color(0);
/** Front (invader) color */
color frontCol = color(0,255,50); // green color
//color frontCol = color(250,180,50); // amber color
/** Blur factor, is ignored at 0*/
int blurFactor = 2;
void setup() {
float w = (invx * invw * sc * pw) + (margin*2*(invx))+1;
float h = (invy * invh * sc * pw) + (margin*2*(invy))+1;
int iw = (int) w;
int ih = (int) h;
size(iw, ih);
background(backCol);
pg = createGraphics(iw, ih, P2D);
pgBuff = createGraphics(iw, ih, P2D);
createInvaders();
}
void mousePressed() {
createInvaders();
}
void keyPressed() {
createInvaders();
}
void createInvaders() {
Invader[][] invtemp = new Invader[invx][invy];
for (int i=0; i<invx; i++) {
for (int j=0; j<invy; j++) {
invtemp[i][j] = new Invader(invw, invh);
}
}
inv = invtemp;
loop();
}
void redrawInvaders() {
pg.beginDraw();
pg.background(0f, 0f);
pg.pushMatrix();
pg.translate(margin, margin);
for (int i=0; i<invx; i++) {
pg.pushMatrix();
for (int j=0; j<invy; j++) {
pg.pushMatrix();
pg.scale(sc, sc);
inv[i][j].drawMe(pg, 0f, 0f);
pg.popMatrix();
pg.translate(0, (invh*pw*sc+(margin*2)));
}
pg.popMatrix();
pg.translate(invw*pw*sc+(margin*2), 0);
}
pg.popMatrix();
pg.endDraw();
}
PGraphics pg;
PGraphics pgBuff;
Invader[][] inv;
void draw() {
redrawInvaders();
pgBuff.beginDraw();
pgBuff.background(backCol);
pgBuff.image(pg, 0, 0);
if (blurFactor > 0) {
pgBuff.filter(BLUR, blurFactor);
pgBuff.image(pg, 0, 0);
}
pgBuff.endDraw();
image(pgBuff, 0, 0);
noLoop();
}
class Invader {
boolean[][] grid;
int w = 0;
int h = 0;
Invader(int w, int h) {
this.w = (int) (w/2.0f+0.5f);
this.h = h;
createInvader();
}
void createInvader() {
grid = new boolean[h][w];
for(int y=0; y<h; y++) {
for (int x=0; x<w; x++) {
boolean b = random(2) >= 1 ? true : false;
grid[y][x] = b;
}
}
}
void drawMe(PGraphics buf, float px, float py) {
buf.noStroke();
buf.fill(frontCol);
for(int y=0; y<h; y++) {
for (int x=0; x<w; x++) {
if(grid[y][x]) {
buf.rect(px + (x*pw), py + (y*pw), pw, pw);
buf.rect((w*pw)*2-(1*pw) - (x*pw), py + (y*pw), pw, pw);
}
}
}
}
}
@spoike
Copy link
Author

spoike commented Sep 1, 2011

Invaders 5 x 5px large in green color.

Screenshot of invaders that are 5 x 5 pixels large

It is also possible to generate invaders that are not square in size. Here they are 5 x 7 "pixels" large in amber color.

Screenshot of invaders that are 5 x 7 pixels large in amber color

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment