AI Flappy Bird Processing Java

i made it easier to see.
run this code to see the visualisation of how the player sees

ArrayList<Ball> balls = new ArrayList<Ball>();
Player p ;
void setup() {
  noStroke();
  fullScreen();
  for (int i = 0; i < 10; i++) {
    balls.add(new Ball(random(width), random(height)));
  }
  p = new Player();
  rectMode(CENTER);
}

void draw() {
  background(0);
  for (Ball b : balls) {
    b.show();
    b.move();
  }
  for (int i=0; i < 10; i++) {
    text("Blind Area", 10, i * 100);
    text("Blind Area", width-60, i * 100);
  }
  p.look();
  p.show();
}

class Ball {
  PVector pos, vel;
  Ball(float x, float y) {
    pos = new PVector(x, y);
    vel = PVector.random2D();
  }

  void show() {
    fill(255, 100, 90);
    noStroke();
    ellipse(pos.x, pos.y, 100, 100);
  }
  
  void move(){
    pos.add(vel);
    loopy();
  }

  // this function is determine if any given location is in the ball
  // if it is a block, use rectangle collision detection instead of circle collision detection
  boolean lookForHit(PVector loc) {
   // circle collision detection
    if (dist(pos.x, pos.y, loc.x, loc.y)< 50) {
      return true;
    }
    return false;
  }
  
  // this is just wrapping edges
  void loopy() {
    // Wrap Edges
    if (pos.y < -50) {
      pos.y = height + 50;
    } else
      if (pos.y > height + 50) {
        pos.y = -50;
      }
    if (pos.x< -50) {
      pos.x = width +50;
    } else  if (pos.x > width + 50) {
      pos.x = -50;
    }
  }
}

class Player {
  PVector pos;
  Player() {
    pos = new PVector(width/2, height/2);
  }

  void show() {
    fill(255, 255, 0);
    noStroke();
    rect(pos.x, pos.y, 30, 30);
  }

  void look() {
    PVector direction;
    for (int i = 0; i< 16; i++) {
      // player sees in 16 direction, so we have to divide one full rotation (360 degrees) with 16
      // so angle between two nearest directions will be 22.5 degree
      direction = PVector.fromAngle(i*(TWO_PI/16));
      direction.mult(10);
      // get the normalized distance to the ball if found in the current direction
      // if got no ball in the current direction, mag will be zero
      float mag = lookInDirection(direction); 
      // inputs[i] = mag; //feed this to the input
      
      // this is how the visualisation
      stroke(255);
      if (mag > 0) {
        direction.mult(1/mag);
        direction.add(pos);
        fill(255);
        text(1/mag, direction.x, direction.y);
        stroke(0, 255,0);
      }else{
        //if no ball in current direction
        direction.mult(60);
        direction.add(pos);
        stroke(255, 0,0);
      }
      
      line(pos.x, pos.y, direction.x, direction.y);
    }
  }

  float lookInDirection(PVector direction) {

    PVector position = new PVector(pos.x, pos.y);//the position where we are currently looking for the balls
    float distance = 0;
    //move once in the desired direction before starting 
    position.add(direction);
    distance +=1;

    //look in the direction
    while (distance< 60) {
      for (Ball a : balls) {
        if (a.lookForHit(position) ) { 
          // found ball
          return  1/distance;
        }
      }

      //look further in the direction
      position.add(direction);

      // next distance
      distance +=1;
    }
    return 0;
  }
}

hope this helpful

1 Like