0

I am playing with Android sample app Snake. The code for the original snake-move direction math is below. It is very simple. The direction is determined by which quadrant the touch is in based on the screen center. However this is rough. Sometime the snake is at right edge of the screen and I want to move it left but if my touch is at left of the snake but still within the right quadrant, its direction is still right. So I need an updated direction math centered with Snake head rather than the center of the screen. I was not successful making such update. Someone is good at math please help. Please note the 4 quadrants are divided by 2 diagonal lines.

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (mSnakeView.getGameState() == SnakeView.RUNNING) {
            // Normalize x,y between 0 and 1
            float x = event.getX() / v.getWidth();
            float y = event.getY() / v.getHeight();

            // Direction will be [0,1,2,3] depending on quadrant
            int direction = 0;
            direction = (x > y) ? 1 : 0;
            direction |= (x > 1 - y) ? 2 : 0;

            // Direction is same as the quadrant which was clicked
            mSnakeView.moveSnake(direction);

        } else {
            // If the game is not running then on touching any part of the screen
            // we start the game by sending MOVE_UP signal to SnakeView
            mSnakeView.moveSnake(MOVE_UP);
        }
        return false;
    }
Michael SM
  • 715
  • 3
  • 11
  • 25

1 Answers1

0

Edit

@Override
    public boolean onTouch(View v, MotionEvent event) {
        if (mSnakeView.getGameState() == SnakeView.RUNNING) {
            // Normalize x,y between 0 and 1
            float x = event.getX() / v.getWidth();
            float y = event.getY() / v.getHeight();
            Coordinate head = mSnakeTrail.get(0);

            // Direction will be [0,1,2,3] depending on quadrant
            int direction = 0;

            //This probably won't work very well
            //direction = (x > head.x) ? 1 : 0; //Right or left
            //direction = (y > head.y) ? 2 : 3; //Down or up

            //this will make sort of quadrants as well, but looked from the head
            direction = (x > head.x + 10) ? 1:(nowhere); //Right or nowhere (you'll have to see what to fill in instead of 0)
            direction = (x < head.x - 10) ? 0:(nowhere); //Left or nowhere
            direction = (y > head.y + 10) ? 2:(nowhere); //Down or nowhere
            direction = (y < head.y - 10) ? 3:(nowhere); //Up or nowhere

            // Direction is same as the quadrant which was clicked
            mSnakeView.moveSnake(direction);

        } else {
            // If the game is not running then on touching any part of the screen
            // we start the game by sending MOVE_UP signal to SnakeView
            mSnakeView.moveSnake(MOVE_UP);
        }
        return false;
    }

Anyways, I think it would be better to swipe snake to the direction you want. This is the easiest way to move your snake on a phone.

So instead of adding a touchListener I would suggest adding a gestureOverlay. You can look over here to see how you add a gesture:

http://developer.android.com/reference/android/gesture/GestureOverlayView.html

You can also use GestureDetector. Here are the functions of GestureDetector:

http://developer.android.com/reference/android/view/GestureDetector.html

And here is an example:

https://stackoverflow.com/a/938657/2767703

Community
  • 1
  • 1
Kevin van Mierlo
  • 9,554
  • 5
  • 44
  • 76
  • Yes that is a good suggestion. However, I want to keep touch listener - the traditional way to play. I want multiple ways to play with: touch, long touch and fling gesture - all work in the same app. So the first thing to do is to make touch, long touch to do right. – Michael SM Sep 11 '13 at 13:05
  • Kevin, you nail the head. That is what I am looking for. The current ad-hoc algorithm is based on the middle of the screen. Now I want the algorithm is based on the the coordinates of the snake head. – Michael SM Sep 12 '13 at 02:23
  • Kevin, but still I want the algorithm to be updated with coordinate transformation. not sure how to do it. – Michael SM Sep 20 '13 at 09:35
  • sorry to keep you waiting, i've been very busy. I edited my answer, is this the algorithm what you wanted? – Kevin van Mierlo Nov 01 '13 at 12:41