import java.util.Random; class DeadGrid { int blockSize=16; // [x] [y] bitmap public int[][] grid = new int[width/blockSize][height/blockSize]; public DeadGrid() { // init grid for (int i=0; i < grid.length; i++) { for (int j=0; j < grid[0].length; j++) { grid[i][j] = 0; } } } public void draw() { stroke(255,255,255,120); strokeWeight(3); fill(255,0,0); // draw view of DeadGrid for (int i=0; i < grid.length; i++) { for (int j=0; j < grid[0].length; j++) { int x = i * blockSize; int y = j * blockSize; int width = blockSize; int height = blockSize; if(grid[i][j] == 1) { rect(x, y, width, height); } } } } public void randomize() { Random randomGenerator = new Random(); // create stuff in DeadGrid for (int i=0; i < grid.length; i++) { for (int j=0; j < grid[0].length; j++) { // random 0 or 1 int randomInt = randomGenerator.nextInt(2); // 1 means filled on grid grid[i][j] = randomInt; } } } public boolean canMoveLeft(int xPosition, int yPosition, int distance) { // moving left is x minus distance int checkX = xPosition - distance; int checkY = yPosition; // hit left wall if (xPosition != 0) { if (isThere(checkX, checkY)) { return false; } else { // nothing there, we can move return true; } } else { return false; } } public boolean canMoveRight(int xPosition, int yPosition, int distance) { // moving right is x plus distance int checkX = xPosition + distance; int checkY = yPosition; // hit right wall if (xPosition != grid.length - 1) { if (isThere(checkX, checkY)) { return false; } else { // nothing there, we can move return true; } } else { return false; } } public boolean canMoveUp(int xPosition, int yPosition, int distance) { // moving up is y minus distance (inverted Y drawing) int checkX = xPosition; int checkY = yPosition - distance; if (yPosition != 0) { if (isThere(checkX, checkY)) { return false; } else { return true; } } else { return false; } } public boolean canMoveDown(int xPosition, int yPosition, int distance) { // moving down is y plus distance (inverted Y drawing) int checkX = xPosition; int checkY = yPosition + distance; if (yPosition != grid[0].length - 1) { if (isThere(checkX, checkY)) { return false; } else { return true; } } else { return false; } } boolean isThere(int checkX, int checkY) { if (grid[checkX][checkY] == 1) { return true; } else { return false; } } } // used for player class Block { public int blockSize=16; int x; int y; int width; int height; public Block(int x, int y) { this.x = x; // grid position x this.y = y; // grid position y this.width = blockSize; this.height = blockSize; } public void draw() { fill(55,0,255); rect(x * blockSize, y * blockSize, width, height); } } int blockSize = 16; DeadGrid deadGrid; Block player; void setup() { size(256, 256); deadGrid = new DeadGrid(); player = new Block(5,5); background(0); } void draw() { background(0); drawGridLines(); deadGrid.draw(); player.draw(); } void keyPressed() { if (key == ' ') { println("RANDOMIZE"); deadGrid.randomize(); // if grid spawns under player, fix it if (deadGrid.grid[player.x][player.y] == 1) { println("spawn collision: " + deadGrid.grid[player.x][player.y]); deadGrid.grid[player.x][player.y] = 0; println("fixed: " + deadGrid.grid[player.x][player.y]); } } if (keyCode == LEFT) { if (deadGrid.canMoveLeft(player.x, player.y, 1)) { player.x = player.x - 1; } } if (keyCode == RIGHT) { if (deadGrid.canMoveRight(player.x, player.y, 1)) { player.x = player.x + 1; } } if (keyCode == UP) { if (deadGrid.canMoveUp(player.x, player.y, 1)) { player.y = player.y - 1; } } if (keyCode == DOWN) { if (deadGrid.canMoveDown(player.x, player.y, 1)) { player.y = player.y + 1; } } } void drawGridLines() { int x = 0; int y = 0; strokeWeight(1); stroke(255,255,255,220); // draw grid lines for (int i=0; i < width/blockSize + 1; i++) { x = i * blockSize; // vertical lines line(x, 0, x, height); for (int j=0; j < height/blockSize + 1; j++) { y = j * blockSize; // horizontal lines line(0, y, width, y); } } }