Advertisement
Guest User

Snake.java

a guest
Nov 15th, 2018
100
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.codef0x.snake;
  2. import java.util.ArrayList;
  3.  
  4. public class Snake {
  5.     ArrayList<SnakePart> coordinates;
  6.     int dx = 10;
  7.     int dy = 0;
  8.  
  9.     public Snake(ArrayList<SnakePart> coords) {
  10.         this.coordinates = coords;
  11.     }
  12.  
  13.     public void move() {
  14.         SnakePart head = new SnakePart(this.coordinates.get(0).x + this.dx, this.coordinates.get(0).y + this.dy);
  15.  
  16.         this.coordinates.add(0, head);
  17.         this.coordinates.remove(this.coordinates.size() - 1);
  18.     }
  19.  
  20.     public void grow() {
  21.         SnakePart newPart = new SnakePart(0, 0);
  22.         newPart.x = this.coordinates.get(this.coordinates.size() - 1).x - 10;
  23.         newPart.y = this.coordinates.get(this.coordinates.size() - 1).y;
  24.  
  25.         this.coordinates.add(this.coordinates.size() - 1, newPart);
  26.     }
  27. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement