Code
original
int w = 8, h = 8, scl = 100;
PImage source;
ArrayList<PImage> sprites = new ArrayList<PImage>();
int grid[][] = new int[w][h];
void setup() {
size(800, 800);
loadImg();
}
void loadImg() {
imageMode(3);
source = loadImage("chessPieces2.png");
float m = 1.333, scl = 130*m, xoff = 10*m, yoff = 10*m, xbuffer=20*m, ybuffer=20*m;
for (int j = 0; j < 2; j++) for (int i = 0; i < 6; i++) sprites.add(source.get(floor(xoff+i*scl+(i*xbuffer)), floor(yoff+j*scl+(j*ybuffer)), floor(scl), floor(scl)));
for (int i = 0; i < sprites.size(); i++) {
sprites.set(i, shrink(sprites.get(i), 0.5, 0.5));
}
/*displaying images*/ // for (int i = 0; i < 6; i++) for (int j = 0; j < 2; j++) image(sprites.get(i+j*6), floor(xoff+i*scl+(i*xbuffer)), floor(yoff+j*scl+(j*ybuffer)));
}
PImage shrink(PImage input, float sx, float sy) {
input.resize(floor(input.width*sx), floor(input.height*sy));
return( input );
}
void displayGrid() {
for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) {
if (grid[i][j] > 0) image(sprites.get(grid[i][j]), (i+0.5)*scl, (j+0.5)*scl);
if (grid[i][j] < 0) image(sprites.get(-grid[i][j]+5), (i+0.5)*scl, (j+0.5)*scl);
}
}
void draw() {
background(0);
noStroke();
background(#50c878);//#d5fcad);
for (int j = 0; j < h; j++) for (int i = j % 2; i < w; i+=2) rect(i*scl, j*scl, scl, scl);
image(sprites.get((int)random(sprites.size())), mouseX, mouseY);
displayGrid();
}
short
int w = 8, h = 8, scl = 100;
PImage source;
ArrayList<PImage> sprites = new ArrayList<PImage>();
int grid[][] = new int[w][h];
void setup() {
size(800, 800);
loadImg();
}
void loadImg() {
imageMode(3);
source = loadImage("chessPieces2.png");
float m = 1.333, scl = 130*m, xoff = 10*m, yoff = 10*m, xbuffer=20*m, ybuffer=20*m;
for (int j = 0; j < 2; j++) for (int i = 0; i < 6; i++) sprites.add(source.get(floor(xoff+i*scl+(i*xbuffer)), floor(yoff+j*scl+(j*ybuffer)), floor(scl), floor(scl)));
for (int i = 0; i < sprites.size(); i++) sprites.set(i, shrink(sprites.get(i), 0.5, 0.5));
}
PImage shrink(PImage input, float sx, float sy) {
input.resize(floor(input.width*sx), floor(input.height*sy));
return( input );
}
void displayGrid() {
for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) {
if (grid[i][j] > 0) image(sprites.get(grid[i][j]), (i+0.5)*scl, (j+0.5)*scl);
if (grid[i][j] < 0) image(sprites.get(-grid[i][j]+5), (i+0.5)*scl, (j+0.5)*scl);
}
}
void draw() {
background(0);
noStroke();
background(#50c878);//#d5fcad);
for (int j = 0; j < h; j++) for (int i = j % 2; i < w; i+=2) rect(i*scl, j*scl, scl, scl);
image(sprites.get((int)random(sprites.size())), mouseX, mouseY);
displayGrid();
}
explained
int w = 8, h = 8, scl = 100;
PImage source;
ArrayList<PImage> sprites = new ArrayList<PImage>();
int grid[][] = new int[w][h];
void setup() {
size(800, 800);
loadImg(); //loading and splicing images
}
void loadImg() {
imageMode(3);
source = loadImage("chessPieces2.png"); //loading from data folder
float m = 1.333, scl = 130*m, xoff = 10*m, yoff = 10*m, xbuffer=20*m, ybuffer=20*m; //setting variables to fit the image
for (int j = 0; j < 2; j++) for (int i = 0; i < 6; i++) sprites.add(source.get(floor(xoff+i*scl+(i*xbuffer)), floor(yoff+j*scl+(j*ybuffer)), floor(scl), floor(scl)));
for (int i = 0; i < sprites.size(); i++) sprites.set(i, shrink(sprites.get(i), 0.5, 0.5)); //shrinking images to half the size
}
PImage shrink(PImage input, float sx, float sy) { //resizing
input.resize(floor(input.width*sx), floor(input.height*sy));
return( input );
}
void displayGrid() { //displaying the grid
noStroke();
background(#50c878);//#d5fcad);
for (int j = 0; j < h; j++) for (int i = j % 2; i < w; i+=2) rect(i*scl, j*scl, scl, scl); //drawing the grid [] [] [] []
for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) {
if (grid[i][j] > 0) image(sprites.get(grid[i][j]), (i+0.5)*scl, (j+0.5)*scl);
if (grid[i][j] < 0) image(sprites.get(-grid[i][j]+5), (i+0.5)*scl, (j+0.5)*scl);
}
}
void draw() {
background(0);
displayGrid();
image(sprites.get((int)random(sprites.size())), mouseX, mouseY);
}
Code
int w = 8, h =8, scl = 100, sScl = 173, v = 0, sel = -1;
boolean creative = true;
PImage source;
ArrayList<PImage> sprites = new ArrayList<PImage>();
int grid[][] = new int[w][h];
String board = "11,12,0,0,0,0,6,5,10,12,0,0,0,0,6,4,9,12,0,0,0,0,6,3,8,12,0,0,0,0,6,2,7,12,0,0,0,0,6,1,9,12,0,0,0,0,6,3,10,12,0,0,0,0,6,4,11,12,0,0,0,0,6,5";
void setup() {
loadBoard(board);
loadImages();
imageMode(3);
size(800, 800);
}
void draw() {
noStroke();
displayGrid();
}
void displayGrid() {
background(#50c878);
fill(255);
for (int j = 0; j < h; j++) for (int i = j % 2; i < w; i+=2) rect(i*scl, j*scl, scl, scl);
if(!creative) fill(0, 0, 255);
else noFill();
if (!creative && sel != -1) rect((sel % w)*scl, floor(sel/w)*scl,scl,scl);
for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) if (grid[i][j] != 0) image(sprites.get(grid[i][j]-1), (i+0.5)*scl, (j+0.5)*scl);
if (creative) image(sprites.get(v), mouseX, mouseY);
}
String saveBoard() {
String output = "";
for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) output += grid[i][j]+((( (i == w-1 && j == h-1)? "" : ",")));
return output;
}
void loadBoard(String input) {
int info[] = int(split(input, ","));
for (int i = 0; i < info.length; i++) {
int y = i % w, x = floor(i/w);
grid[x][y] = info[i];
}
}
void mousePressed() {
int mx = constrain(floor(map(mouseX, 0, w*scl, 0, w)), 0, w-1), my = constrain(floor(map(mouseY, 0, h*scl, 0, h)), 0, h-1);
if (creative) grid[mx][my] = ((((mouseButton == LEFT)? v+1 : 0)));
else {
if(grid[mx][my] == 0) sel = sel;
else sel = mx+my*w;
}
}
void mouseWheel(MouseEvent e) {
v -= e.getCount();
if (v > sprites.size()-1) v = 0;
if (v < 0) v = sprites.size()-1;
}
void loadImages() {
source = loadImage("sprites.png");
for (int i = 0; i < 12; i++) {
sprites.add(shrink(source.get(i*sScl, 0, sScl, sScl), 0.5));
}
for (int i = 0; i < sprites.size(); i++) {
image(sprites.get(i), i*sScl, 0);
}
}
PImage shrink(PImage input, float value) {
input.resize(floor(input.width*value), floor(input.height*value));
return(input);
}
void keyPressed() {
if (key == ' ') println(saveBoard()+"\n");
else if (key == 'c') {
creative = !creative;
}
}
image “sprites.png”