Created
November 3, 2013 20:55
-
-
Save nossidge/7294699 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Save as animated GIF | |
// Requires gifAnimation Processing Library | |
// http://www.extrapixel.ch/processing/gifAnimation/ | |
import gifAnimation.*; | |
GifMaker gifExport; | |
boolean saveToGif = false; | |
int gifFrameDelay = 180; | |
int gifFrameCount = 0; | |
// Each square is 5 pixels wide, and there is a border of 1 pixel. | |
int cellsX, cellsY, cellDim = 6; | |
// Greyness of the gradients. Works nicely with 64. | |
int gradientIncrement = (256/4); | |
////////////////////////////////////////////////////////////////////// | |
// GIF stuff. | |
void gifCreateNew() { | |
if (saveToGif) { | |
gifExport = new GifMaker(this, System.currentTimeMillis() + ".gif"); | |
gifExport.setRepeat(0); | |
gifFrameCount = 0; | |
} | |
} | |
void gifAddFrame() { | |
if (saveToGif) { | |
gifExport.setDelay(gifFrameDelay); | |
gifExport.addFrame(); | |
gifFrameCount++; | |
} | |
} | |
void gifSave() { | |
if (saveToGif) { | |
gifExport.setDelay(gifFrameDelay); | |
gifExport.finish(); | |
saveToGif = false; | |
println("Frames saved: " + gifFrameCount ); | |
} | |
} | |
void keyPressed(){ | |
switch( int(key) ) { | |
case 114: saveToGif=true; gifCreateNew(); break; // _R_ecord new gif | |
case 115: gifSave(); break; // _S_ave | |
} | |
} | |
void stop() { | |
gifSave(); | |
} | |
////////////////////////////////////////////////////////////////////// | |
void setup() { | |
strokeWeight(0); | |
stroke(255); | |
frameRate(7); | |
background(255); | |
// Divisible by six, plus one | |
size(599,371); | |
//size(600,370); | |
cellsX = int(width / cellDim)+1; | |
cellsY = int(height / cellDim)+1; | |
draw(); | |
} | |
////////////////////////////////////////////////////////////////////// | |
void draw() { | |
//background(255); | |
tetrisBackground(); | |
tetrisGround(); | |
// All "objects" are static & hard-coded in this prototype | |
// Block type, brick or question mark | |
int blockBrickMin = 1, blockBrickMax = 5; | |
int blockQuestMin = 1, blockQuestMax = 5; | |
int blockPipeMin = 1, blockPipeMax = 5; | |
// Top block | |
for (int y=15; y < 19; y++) { | |
for (int x=48; x < 52; x++) { tetrisSquare(x*cellDim, y*cellDim, blockQuestMin, blockQuestMax); } | |
} | |
// First line of blocks (Same Y levels) | |
for (int y=35; y < 39; y++) { | |
for (int x=20; x < 24; x++) { tetrisSquare(x*cellDim, y*cellDim, blockQuestMin, blockQuestMax); } | |
for (int x=40; x < 44; x++) { tetrisSquare(x*cellDim, y*cellDim, blockBrickMin, blockBrickMax); } | |
for (int x=44; x < 48; x++) { tetrisSquare(x*cellDim, y*cellDim, blockQuestMin, blockQuestMax); } | |
for (int x=48; x < 52; x++) { tetrisSquare(x*cellDim, y*cellDim, blockBrickMin, blockBrickMax); } | |
for (int x=52; x < 56; x++) { tetrisSquare(x*cellDim, y*cellDim, blockQuestMin, blockQuestMax); } | |
for (int x=56; x < 60; x++) { tetrisSquare(x*cellDim, y*cellDim, blockBrickMin, blockBrickMax); } | |
} | |
// Pipe | |
int pipeX = 76; | |
int pipeW = 10; | |
for (int y=44; y < 48; y++) { | |
for (int x=pipeX; x < (pipeX+pipeW); x++) { tetrisSquare(x*cellDim, y*cellDim, blockPipeMin, blockPipeMax); } | |
} | |
for (int y=48; y < 55; y++) { | |
for (int x=pipeX+1; x < (pipeX+pipeW-1); x++) { tetrisSquare(x*cellDim, y*cellDim, blockPipeMin, blockPipeMax); } | |
} | |
gifAddFrame(); | |
} | |
////////////////////////////////////////////////////////////////////// | |
void tetrisBackground() { | |
int greyPrev; | |
int[] gradient = new int[3]; | |
for (int x=0; x < cellsX; x++) { | |
for (int y=0; y < 55; y++) { | |
tetrisSquare(x*cellDim, y*cellDim,1,88); | |
} | |
} | |
} | |
void tetrisGround() { | |
int greyPrev; | |
int[] gradient = new int[3]; | |
for (int x=0; x < cellsX; x++) { | |
for (int y=55; y < cellsY; y++) { | |
tetrisSquare(x*cellDim, y*cellDim,1,5); | |
} | |
} | |
} | |
////////////////////////////////////////////////////////////////////// | |
// Make sure that we don't choose the same colour for adjoining rings | |
void tetrisSquare(int x, int y, int greyRandMin, int greyRandMax) { | |
int[] gradient = new int[3]; | |
getGreyGradient(gradient,greyRandMin,greyRandMax); | |
tetrisSquare(x, y, gradient[0], gradient[1], gradient[2]); | |
} | |
////////////////////////////////////////////////////////////////////// | |
void tetrisSquare(int x, int y, int gradient1, int gradient2, int gradient3) { | |
translate(x,y); | |
fill(min(255,gradient1*gradientIncrement)); | |
rect(0,0,5,5); | |
fill(min(255,gradient2*gradientIncrement)); | |
rect(1,1,3,3); | |
fill(min(255,gradient3*gradientIncrement)); | |
rect(2,2,1,1); | |
translate(-x,-y); | |
} | |
////////////////////////////////////////////////////////////////////// | |
// Arrays are passed by ref in Java. | |
void getGreyGradient(int[] gradientArray, int randMin, int randMax) { | |
int greyPrev = 0; | |
for (int iGrad=0; iGrad<gradientArray.length; iGrad++) { | |
do { | |
gradientArray[iGrad] = int(random(randMin,randMax)); | |
} while (gradientArray[iGrad] == greyPrev); | |
greyPrev = gradientArray[iGrad]; | |
} | |
} | |
////////////////////////////////////////////////////////////////////// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment