Back to project page Sneik.
The source code is released under:
Apache License
If you think the Android project Sneik listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package net.plastboks.gameobjects; /*ww w . j a v a2 s . com*/ import com.badlogic.gdx.math.Vector2; import net.plastboks.shared.Directions; import java.util.LinkedList; /** * Created by alex on 12/12/14. */ public class Snake extends Creature { private final int startBodySize = 4; private final float startLvl = 0.8f; private LinkedList<Node> body; private int bodySize = startBodySize; private float lvl = startLvl; private float lvlInc = 0.2f; private float maxLvl = 1.6f; public Snake(float x, float y, int width, int height, int gameHeight) { super(x, y, width, height); setGameHeight(gameHeight); setAlive(true); this.body = new LinkedList<Node>(); } public void incrementBodySizeBy(int n) { bodySize += n; } private void pushToBody(float delta, int max) { if (body.size() >= max) { body.removeFirst(); } body.add(new Node(new Vector2(getX(), getY()), getDir())); } public void changeDir(Directions d) { switch (d) { case NORTH: if (getDir() != Directions.SOUTH) { setDir(d); setRotation(Directions.NORTH); } break; case SOUTH: if (getDir() != Directions.NORTH) { setDir(d); setRotation(Directions.SOUTH); } break; case WEST: if (getDir() != Directions.EAST) { setDir(d); setRotation(Directions.WEST); } break; case EAST: if (getDir() != Directions.WEST) { setDir(d); setRotation(Directions.EAST); } break; } } public void incrementSpeed() { if (lvl + lvlInc <= maxLvl) { lvl += lvlInc; } } public void update(float delta) { pushToBody(delta, bodySize); move(delta + lvl); } public void onClick(float x, float y) { if (isAlive()) { switch (getDir()) { case EAST: case WEST: if (y > getY()) { changeDir(Directions.SOUTH); } else { changeDir(Directions.NORTH); } break; case NORTH: case SOUTH: if (x > getX()) { changeDir(Directions.EAST); } else { changeDir(Directions.WEST); } break; } } } public void onRestart(int y) { setRotation(Directions.WEST); setY(y); setAlive(true); body = new LinkedList<Node>(); bodySize = startBodySize; lvl = startLvl; } public LinkedList<Node> getBody() { return body; } public void die() { setAlive(false); } }