Removing from an ArrayList
in
Programming Questions
•
11 months ago
I'm trying to make Rush Hour, and it's working fine, but the blocks don't reset for each level. I've tried using the arrraylist.remove command, but it keeps drawing them. Help?
Main code:
- color grid[][]=new color[6][6];
- ArrayList blocks=new ArrayList();
- int active;
- int level=1;
- void setup() {
- strokeWeight(2);
- for (int x=5; x>=0; x--) {
- for (int y=5; y>=0; y--) {
- grid[x][y]=color(176);
- }
- }
- blocks.add(new Block(1, 0, 2));
- size(600, 600);
- initializeLevel(level);
- }
- void draw() {
- Block current=(Block) blocks.get(active);
- background(0);
- if (!didIwin()) {
- updateBlocks();
- drawGrid();
- fill(255, 0, 255);
- rect(current.xpos*100, current.ypos*100, 100, 100);
- }
- else {
- if (level < 10) {
- level++;
- initializeLevel(level);
- }
- }
- }
- void keyPressed() {
- Block current=(Block) blocks.get(active);
- switch (keyCode) {
- case TAB:
- active++;
- active%=blocks.size();
- break;
- case RIGHT:
- current.moveRight();
- break;
- case LEFT:
- current.moveLeft();
- break;
- case UP:
- current.moveUp();
- break;
- case DOWN:
- current.moveDown();
- break;
- }
- }
- void updateBlocks() {
- for (int i=blocks.size()-1; i>=0; i--) {
- Block block=(Block) blocks.get(i);
- block.update();
- }
- }
- void drawGrid() {
- for (int x=5; x>=0; x--) {
- for (int y=5; y>=0; y--) {
- fill (grid[x][y]);
- rect(x*100, y*100, 100, 100);
- }
- }
- }
- void initializeLevel(int lvl) {
- for (int i=1; i<blocks.size(); i++) {
- blocks.remove(i);
- }
- for (int x=0; x<6; x++) {
- for (int y=0; y<6; y++) {
- grid[x][y]=color(176);
- }
- }
- Block player=(Block) blocks.get(0);
- switch (lvl) {
- case 0:
- blocks.add(new Block(2, 0, 0));
- break;
- case 1:
- player.xpos=1;
- blocks.add(new Block(2, 0, 0));
- blocks.add(new Block(5, 5, 0));
- blocks.add(new Block(5, 0, 1));
- blocks.add(new Block(5, 3, 1));
- blocks.add(new Block(4, 0, 4));
- blocks.add(new Block(2, 4, 4));
- blocks.add(new Block(3, 2, 5));
- break;
- case 2:
- player.xpos=0;
- blocks.add(new Block(4, 0, 0));
- blocks.add(new Block(3, 3, 0));
- blocks.add(new Block(4, 3, 1));
- blocks.add(new Block(4, 4, 2));
- blocks.add(new Block(5, 5, 3));
- blocks.add(new Block(4, 2, 4));
- blocks.add(new Block(2, 3, 4));
- blocks.add(new Block(2, 0, 5));
- blocks.add(new Block(2, 3, 5));
- break;
- }
- }
- boolean didIwin() {
- Block player=(Block) blocks.get(0);
- if (player.xpos==4) {
- return true;
- }
- else {
- return false;
- }
- }
- class Block {
- int xpos, ypos;
- int blocktype;
- Block (int b, int x, int y) {
- blocktype = b;
- xpos = x;
- ypos = y;
- }
- void update() {
- switch (blocktype) {
- case 1:
- grid[xpos][ypos]=color(255, 0, 0);
- grid[xpos+1][ypos]=color(255, 0, 0);
- break;
- case 2:
- grid[xpos][ypos]=color(0, 255, 255);
- grid[xpos+1][ypos]=color(0, 255, 255);
- break;
- case 3:
- grid[xpos][ypos]=color(0, 0, 255);
- grid[xpos+1][ypos]=color(0, 0, 255);
- grid[xpos+2][ypos]=color(0, 0, 255);
- break;
- case 4:
- grid[xpos][ypos]=color(255, 255, 0);
- grid[xpos][ypos+1]=color(255, 255, 0);
- break;
- case 5:
- grid[xpos][ypos]=color(0, 255, 0);
- grid[xpos][ypos+1]=color(0, 255, 0);
- grid[xpos][ypos+2]=color(0, 255, 0);
- break;
- }
- }
- void moveLeft() {
- switch (blocktype) {
- case 1:
- if (xpos>0&&grid[xpos-1][ypos]==color(176)) {
- grid[xpos][ypos]=color(176);
- grid[xpos+1][ypos]=color(176);
- xpos--;
- }
- break;
- case 2:
- if (xpos>0&&grid[xpos-1][ypos]==color(176)) {
- grid[xpos][ypos]=color(176);
- grid[xpos+1][ypos]=color(176);
- xpos--;
- }
- break;
- case 3:
- if (xpos>0&&grid[xpos-1][ypos]==color(176)) {
- grid[xpos][ypos]=color(176);
- grid[xpos+1][ypos]=color(176);
- grid[xpos+2][ypos]=color(176);
- xpos--;
- }
- break;
- }
- }
- void moveRight() {
- switch (blocktype) {
- case 1:
- if (xpos+1<5&&grid[xpos+2][ypos]==color(176)) {
- grid[xpos][ypos]=color(176);
- grid[xpos+1][ypos]=color(176);
- xpos++;
- }
- break;
- case 2:
- if (xpos+1<5&&grid[xpos+2][ypos]==color(176)) {
- grid[xpos][ypos]=color(176);
- grid[xpos+1][ypos]=color(176);
- xpos++;
- }
- break;
- case 3:
- if (xpos+2<5) {
- grid[xpos][ypos]=color(176);
- grid[xpos+1][ypos]=color(176);
- grid[xpos+2][ypos]=color(176);
- xpos++;
- }
- break;
- }
- }
- void moveUp() {
- switch (blocktype) {
- case 4:
- if (ypos>0&&grid[xpos][ypos-1]==color(176)) {
- grid[xpos][ypos]=color(176);
- grid[xpos][ypos+1]=color(176);
- ypos--;
- }
- break;
- case 5:
- if (ypos>0&&grid[xpos][ypos-1]==color(176)) {
- grid[xpos][ypos]=color(176);
- grid[xpos][ypos+1]=color(176);
- grid[xpos][ypos+2]=color(176);
- ypos--;
- }
- break;
- }
- }
- void moveDown() {
- switch (blocktype) {
- case 4:
- if (ypos<4&&grid[xpos][ypos+2]==color(176)) {
- grid[xpos][ypos]=color(176);
- grid[xpos+1][ypos]=color(176);
- ypos++;
- }
- break;
- case 5:
- if (ypos<3&&grid[xpos][ypos+3]==color(176)) {
- grid[xpos][ypos]=color(176);
- grid[xpos][ypos+1]=color(176);
- grid[xpos][ypos+2]=color(176);
- ypos++;
- }
- break;
- }
- }
- }
1