Android Open Source - Sneik Snake






From Project

Back to project page Sneik.

License

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.

Java Source Code

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); }
}




Java Source Code List

net.plastboks.gameobjects.Artificial.java
net.plastboks.gameobjects.Autonomous.java
net.plastboks.gameobjects.Bird.java
net.plastboks.gameobjects.Creature.java
net.plastboks.gameobjects.Mouse.java
net.plastboks.gameobjects.Node.java
net.plastboks.gameobjects.Snake.java
net.plastboks.gameworld.GamePlay.java
net.plastboks.gameworld.GameRenderer.java
net.plastboks.gameworld.GameWorld.java
net.plastboks.screens.GameScreen.java
net.plastboks.shared.Directions.java
net.plastboks.sneik.IOSLauncher.java
net.plastboks.sneik.SneikGame.java
net.plastboks.sneik.android.AndroidLauncher.java
net.plastboks.sneik.client.HtmlLauncher.java
net.plastboks.sneik.desktop.DesktopLauncher.java
net.plastboks.sneikhelpers.AssetLoader.java
net.plastboks.sneikhelpers.InputHandler.java