The Snake Game



Code of snake.pde:

class Snake {
  ArrayList<PVector> body;
  float xdir, ydir, len;

  Snake() {
    this.body = new ArrayList<PVector>();
    this.body.add(new PVector(floor(w / 2), floor(h / 2)));
    this.xdir = 0;
    this.ydir = 0;
    this.len = 0;
  }

  void setDir(float x, float y) {
    this.xdir = x;
    this.ydir = y;
  }

  void update() {
    PVector head = this.body.get(this.body.size() -1).copy();
    this.body.remove(0);
    head.x += this.xdir;
    head.y +=this.ydir;
    this.body.add(head);
  }

  void grow() {
    PVector head = this.body.get(this.body.size() -1).copy();
    this.len++;
    this.body.add(head);
  }

  boolean endGame() {
    float x = this.body.get(this.body.size() -1).x;
    float y = this.body.get(this.body.size() -1).y;
    if (x > w -1 || x < 0 || y > h -1 || y < 0) {
      return true;
    }
    for (int i = 0; i < this.body.size() - 1; i++){
      PVector part = this.body.get(i);
      if (part.x == x && part.y == y) {
       return true;
      } 
    }
     return false;
  }

  boolean eat(PVector pos){
    float x = this.body.get(this.body.size() -1).x;
    float y = this.body.get(this.body.size() -1).y;
    if (x == pos.x && y == pos.y) {
      this.grow();
       return true;
    }
    return false;
  }

  void show() {
    for (int i = 0; i < this.body.size(); i++) {
      fill(0);
      noStroke();
      rect(this.body.get(i).x, this.body.get(i).y, 1, 1);
    }
  }
}

Code of Snake_Game:

Snake snake;
int rez = 20;
PVector food;
float w;
float h;

void setup() {
  size(400, 400);
  w = floor(width / rez);
  h = floor(height / rez);
  frameRate(5);
  snake = new Snake();
  foodLocation();
}

void foodLocation() {
  float x = floor(random(w));
  float y = floor(random(h));
  food = new PVector(x, y);
}

void keyPressed() {
  if (keyCode == LEFT) {
    snake.setDir(-1, 0);
  }else if (keyCode == RIGHT) {
    snake.setDir(1, 0);
  }else if (keyCode == DOWN) {
    snake.setDir(0, 1);
  }else if (keyCode == UP) {
    snake.setDir(0, -1);
  }else if (key == ' '){
    snake.grow();
  }
}

void draw() {
  scale(rez);
  background(220);
  if (snake.eat(food)) {
    foodLocation();
  }
  snake.update();
  snake.show();
 
  if (snake.endGame()) {
    print("END GAME");
    background(255, 0, 0);
    noLoop();
  }
 
  noStroke();
  fill(255, 0, 0);
  rect(food.x, food.y, 1, 1);
}

Comentarios

Entradas populares de este blog

How to do an Email Bomber with Python

How to make a Christmas animation in HTML

Chat with Firebase