RNG Based Cellular Automata Cave Generation Platformer

//int areaCounter=0;
//int[][] caves;
int cols;
int rows;
int bias=42;
int[][] grid;
int[][] finalGrid;
int onGroundTimer=1000000000;
boolean isColliding;
int onGround=1;
boolean switch1;
boolean wasd=true;
boolean isLeft, isRight, isUp, isDown;
float x;
float y;
int unit;
int c;
float yVel=0;
float xVel=0;
boolean gravity=true;
int[] endingBlackValues;
boolean doneWithSimulation=false;

void setup() {
  fullScreen();
  unit=width/50;
  x=width/2;
  y=height/2;
  x=400;
  y=200;
  cols = width/20;
  rows = height/20;
  grid = new int[cols][rows];
  finalGrid = new int[cols][rows];
  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      grid[i][j] = int(random(100));
      if(grid[i][j]<bias)grid[i][j]=0;
      if(grid[i][j]>=bias)grid[i][j]=1;
    }
  }
}
void draw() {
  if(yVel<-20)yVel=-20;
  println("x: "+x);
  println("y: "+y);
  println(c);
  c+=1;
  smooth();
  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      
      stroke(grid[i][j]*256);
      textSize(4);
      rectMode(CENTER);
      fill(grid[i][j]*256,127);
      rect(20*i,20*j,20,20);
      rectMode(CORNER);      
    }
  } 
  
  onGroundTimer-=1;
  if(onGroundTimer>0)onGround=2;
  y=y-yVel/3.0;
  x=x+xVel/3.0;
  if(y>height-15)y=height-15;
  if(y==height-15)yVel=-yVel*.30;
  if(isLeft && isRight)xVel=xVel*0.8;
  if(!(isRight && isLeft)){
   if (isLeft && abs(xVel)<3){
     if(xVel<-0.1)xVel=xVel*1.1;
     if(xVel>0.1)xVel=xVel-0.5;
    if(xVel<0.1 && xVel>-0.1)xVel=0.2;
   }  
   if (isRight && abs(xVel)<3){
    if(xVel>0.1)xVel=xVel*1.1;
    if(xVel<-0.1)xVel=xVel+0.5;
    if(xVel<0.1 && xVel>-0.1)xVel=0.2;
   }
  }
  if(isUp && onGround>1)yVel=12;
  if(isUp && onGround>0)onGround=onGround-1;
  if (!isRight && !isLeft)xVel=xVel*0.8;
  
  //Gravity
  if(gravity){
    if(yVel>0.1)yVel=yVel*0.95-0.06;
    if(yVel<0.1)yVel=-abs(yVel*1.05-0.06);
  } 
  
  
  
  
  //Buttons
  fill(255,127);
  rect(20, 20, 50, 30);
  fill(0,127);
  textSize(unit/2);
  text("close", 45, 35);
  
  
  
  
  //Key Indacator
  fill(255);
  rect(unit/4,height-2.75*unit,3.25*unit,2.5*unit);
  //W or Up
  fill(128,127);
  if(isUp)fill(255,127);
  rect(unit*3/2.0,height-7.0/4*unit,unit*3.0/4,unit/2.0);
  
  //A or Left
  fill(128,127);
  if(isLeft)fill(255,127);
  rect(unit/2.0,height-unit,unit*3.0/4,unit/2.0);
  
  //S or Down
  fill(128,127);
  if(isDown)fill(255,127);
  rect(unit*3/2.0,height-unit,unit*3.0/4,unit/2.0);
  
  //D or Right
  fill(128,127);
  if(isRight)fill(255,127);  
  rect(unit*5/2.0,height-unit,unit*3.0/4,unit/2.0);
  
  //WASD and arrow keys switcher
  textSize(unit/3);
  textAlign(CENTER, CENTER);
  fill(0);
  if(!switch1)text("WASD Mode",unit*1.875,height-8.875/4*unit);
  if(switch1)text("Arrow Keys Mode",unit*1.875,height-8.875/4*unit);
  fill(256);
  rect(unit/2.0,height-10.0/4*unit,unit*2.75,unit/2.0);
  
  noStroke();
  fill(255,10);
  rect(0,0,width,height);
  fill(255,0,0);
  rectMode(CENTER);
  
  rect(x,y+6,3,3);
  if(!isDown)rect(x,y,3,9);
  rectMode(CORNER);
  
}
 
void mousePressed() {
  if (mouseX >= unit*0.5 && mouseX < unit*3.25 && mouseY >= height-2.5*unit && mouseY < height-2*unit) {
      if(switch1==true)switch1=false;
      else if(switch1==false)switch1=true;
  }
  if (mouseX >= 20 && mouseX < 70 && mouseY >= 20 && mouseY < 50) {
      exit();
  }
}

void keyPressed() {
  if(!switch1)setMove(key, true);
  if(switch1)setMove(keyCode, true);
}
 
void keyReleased() {
  if(!switch1)setMove(key, false);
  if(switch1)setMove(keyCode, false);
}
boolean setMove(int k, boolean b) {
  switch (k) {
  case 'w':
    return isUp = b;
  case UP:
    return isUp = b;
  case 's':
    return isDown = b;
  case DOWN:
    return isDown = b;
  case 'a':
    return isLeft = b;
  case LEFT:
    return isLeft = b;
  case 'd':
    return isRight = b;
  case RIGHT:
    return isRight = b;
  default:
    return b;
  }
}
void rectCollisions(int rectX,int rectY,int side){
 rect(rectX, rectY, side, side);
 collision(rectX, rectY, side);
  
}
void collision(int squareX, int squareY, int sideLength){
  if(x-squareX*x-squareX+y-squareY*y-squareY<50){
    rectMode(CENTER); 
    if(x<squareX+sideLength/2+3&&x>squareX-sideLength/2-3&&y<squareY+sideLength/2+9&&y>squareY-sideLength/2-9){
      if(abs(squareX-x)<abs(squareY-y)){
        if(y-squareY<0)y=squareY-sideLength/2-9; //top
        if(y-squareY<0)yVel=0; //top
        if(y-squareY<0)onGroundTimer=25;
        if(y-squareY>0)y=squareY+sideLength/2+9; //top
        if(y-squareY>0)yVel=0; //bottom
      }
      if(abs(squareX-x)>abs(squareY-y)){
        if(x-squareX<0)xVel=0; //left
        if(x-squareX<0)x=x-1; //left
        if(x-squareX>0)xVel=0; //right
        if(x-squareX>0)x=x+1; //right
      }
    }
    rectMode(CORNER);
  }
}


int numberOfNeighbors(int gridX, int gridY){
  int c=0;
  for (int i = gridX-1; i < gridX+2; i++) {
    for (int j = gridY-1; j < gridY+2; j++) {
      if(j!=-1 && i!=-1 && i!=cols && j!=rows){
        if(grid[i][j]==0){
          if(!(i==gridX && j==gridY)){
            c=c+1;
          }
        }
      }else{
       c=c+1;    
      }
    }
  }
  return(c);
}
void smooth(){
  fill(255);
  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      
      if(numberOfNeighbors(i,j)<4)finalGrid[i][j]=1;
      if(numberOfNeighbors(i,j)>6)finalGrid[i][j]=0;
      stroke(finalGrid[i][j]*256,127);
      rectMode(CENTER);
      fill(finalGrid[i][j]*256,127);
      rect(20*i,20*j,20,20);   
      if(grid[i][j]==0 && numberOfNeighbors(i,j)!=8)rectCollisions(20*i,20*j,20);   
    }
  }
  if(finalGrid==grid && doneWithSimulation==false){
    for (int i = 0; i < cols; i++) {
      for (int j = 0; j < rows; j++) {
        if(grid[i][j]==0){
          
        }
      }
    }   
  doneWithSimulation=true;
  }
  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      grid[i][j]=finalGrid[i][j];
    }
  }
}
1 Like

The screen is displayed, but when using the keys in both modes, a small red line moves only a little bit up and down, nothing more.

I don’t think i understand what you’re saying. The “little red line” can move with WASD or ARROW mode with the switcher at bottom left

Only slowly up and down, not to the right or left.

Try using A or D. You need to hold them down, not press them quickly

I hold them down but It’s just really really slow. Maybe it’s my computer.