Android Open Source - Tilt-snake Movement






From Project

Back to project page Tilt-snake.

License

The source code is released under:

Apache License

If you think the Android project Tilt-snake 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 com.awhittle.tiltsnake;
//w  w  w . j a v  a  2s  .com
public class Movement {

  private static int previousDir = 0;
  
  public static void moveSnake(int dir) {
    
    float units = 0.1f;
    
    //Do not suddenly reverse direction, do a u-turn
    if (dir == oppositeDir(previousDir)){
      if (dir != 4) {
        dir += 1;
      } else {
        dir = 1;
      }
    }
    
    if (MainActivity.isPhone){
      //Device is likely a phone with default portrait orientation
      if (!MainActivity.insanityFlag){
        switch(dir){
          case 1: setHeadCoords(units, 0.0f);
          setHeadLocation(1,0);
            break;
          case 2:  setHeadCoords(0.0f, units);
          setHeadLocation(0,1);
            break;
          case 3:  setHeadCoords(-1.0f * units, 0.0f);
          setHeadLocation(-1,0);
            break;
          case 4:  setHeadCoords(0.0f, -1.0f*units);
          setHeadLocation(0,-1);
            break;
        } 
      } else { //Insanity mode active. Reverse directions!
        switch(dir){
          case 1:  setHeadCoords( -1.0f * units, 0.0f);
          setHeadLocation(-1,0);
            break;
          case 2:setHeadCoords(0.0f, -1.0f * units);
          setHeadLocation(0,-1);
            break;
          case 3:  setHeadCoords( units, 0.0f);
          setHeadLocation(1,0);
            break;
          case 4:  setHeadCoords( 0.0f, units);
          setHeadLocation(0,1);
            break;
        }
      }
      
    } else {
      //Device is likely a tablet with default landscape orientation
      if (!MainActivity.insanityFlag){
        switch(dir){
          case 1:  setHeadCoords( 0.0f, units);
          setHeadLocation(0,1);
            break;
          case 2:setHeadCoords( -1.0f * units, 0.0f);
          setHeadLocation(-1,0);
            break;
          case 3:setHeadCoords( 0.0f, -1.0f*units);
          setHeadLocation(0,-1);
            break;
          case 4:  setHeadCoords( units, 0.0f);
          setHeadLocation(1,0);
            break;
        }
      } else { //Insanity mode active. Reverse directions!
        switch(dir){
          case 1:  setHeadCoords( 0.0f, -1.0f*units);
          setHeadLocation(0,-1);
            break;
          case 2:setHeadCoords( units, 0.0f);
          setHeadLocation(1,0);
            break;
          case 3:setHeadCoords( 0.0f, units);
          setHeadLocation(0,1);
            break;
          case 4:  setHeadCoords(-1.0f*units, 0.0f);
          setHeadLocation(-1,0);
            break;
        }
      }
    }
    
    previousDir = dir;
    
  }

  private static void setHeadCoords(float x, float y) {
    
    Head.headCoords = ShapeTools.translateMatrix(Head.headCoords, x, y);
    
  }

  private static void setHeadLocation(int x, int y) {
    Head.headLocation[0] += x;
    Head.headLocation[1] += y;
  }

  private static int oppositeDir(int direction) {
    switch(direction){
    case 1:  direction = 3;
      break;
    case 2:  direction = 4;
      break;
    case 3:  direction = 1;
      break;
    case 4:  direction = 2;
      break;
    }
    
    return direction;
  }
}




Java Source Code List

com.awhittle.tiltsnake.Direction.java
com.awhittle.tiltsnake.Egg.java
com.awhittle.tiltsnake.GameActivity.java
com.awhittle.tiltsnake.Head.java
com.awhittle.tiltsnake.MainActivity.java
com.awhittle.tiltsnake.Movement.java
com.awhittle.tiltsnake.MyGLRenderer.java
com.awhittle.tiltsnake.MyGLSurfaceView.java
com.awhittle.tiltsnake.Saving.java
com.awhittle.tiltsnake.ShapeTools.java
com.awhittle.tiltsnake.Square.java
com.awhittle.tiltsnake.Tail.java