Processing Dino Game

I ran your code through the cleaner. You would do well to try to KISS when coding. KISS stands for Keep It Super Simple. Notice that the code now has more functions, but each one only does a very specific thing. Also notice that any “dino” variables are now inside the Dino class.

class Dino {
  float baseY, px, py, vy, ay, w, h, speed;
  boolean canJump, isJumping;
  Dino(float lineY) {
    baseY = lineY;
    px = 100;
    py = baseY;
    vy = 0;
    ay = 0;
    h = 100;
    w = 50;
    speed = 0.001;
    canJump = true;
    isJumping = false;
  }
  void draw() {
    vy+=ay;
    py+=vy;
    landed_check();
    rect(px, py-h, w, h);
  }
  void jump(){
    if( canJump ){
      canJump = false;
      isJumping = true;
      vy = -10;
      ay = 0.32;
    }
  }
  void landed_check(){
    if( py >= baseY ){
      py = baseY;
      vy = 0;
      ay = 0;
      isJumping = false;
      canJump = true;
    }
  }
}

float lineY;
Dino dino;

void drawGround() {
  stroke(0);
  fill(0);
  rect(0, lineY, width, 5);
}

void setup() {
  size(1000, 600);
  lineY = 480;
  dino = new Dino(lineY);
}

void draw() {
  background(255);
  drawGround();
  dino.draw();
}

void keyPressed(){
  if(key == ' '){
    dino.jump();
  }
}
2 Likes