Modified Snake Sample app to run on non-touch devices like Google Tv and also devices with no dpad support
2D canvas DrawVertex to create a vector graphic background touch & keypad playable landscape & portrait for all known device sizes. All measurements in DP and nicely scaling. using xml drawables to rotate "dpad" arrow Change-Id: I995acaaf0935c13c03fcbcf974ce5af06f46780e
This commit is contained in:
85
samples/Snake/src/com/example/android/snake/Snake.java
Normal file → Executable file
85
samples/Snake/src/com/example/android/snake/Snake.java
Normal file → Executable file
@@ -18,27 +18,38 @@ package com.example.android.snake;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.view.Window;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.View.OnTouchListener;
|
||||
import android.widget.TextView;
|
||||
|
||||
/**
|
||||
* Snake: a simple game that everyone can enjoy.
|
||||
*
|
||||
* This is an implementation of the classic Game "Snake", in which you control a
|
||||
* serpent roaming around the garden looking for apples. Be careful, though,
|
||||
* because when you catch one, not only will you become longer, but you'll move
|
||||
* faster. Running into yourself or the walls will end the game.
|
||||
* This is an implementation of the classic Game "Snake", in which you control a serpent roaming
|
||||
* around the garden looking for apples. Be careful, though, because when you catch one, not only
|
||||
* will you become longer, but you'll move faster. Running into yourself or the walls will end the
|
||||
* game.
|
||||
*
|
||||
*/
|
||||
public class Snake extends Activity {
|
||||
|
||||
private SnakeView mSnakeView;
|
||||
|
||||
/**
|
||||
* Constants for desired direction of moving the snake
|
||||
*/
|
||||
public static int MOVE_LEFT = 0;
|
||||
public static int MOVE_UP = 1;
|
||||
public static int MOVE_DOWN = 2;
|
||||
public static int MOVE_RIGHT = 3;
|
||||
|
||||
private static String ICICLE_KEY = "snake-view";
|
||||
|
||||
private SnakeView mSnakeView;
|
||||
|
||||
/**
|
||||
* Called when Activity is first created. Turns off the title bar, sets up
|
||||
* the content views, and fires up the SnakeView.
|
||||
* Called when Activity is first created. Turns off the title bar, sets up the content views,
|
||||
* and fires up the SnakeView.
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
@@ -48,7 +59,8 @@ public class Snake extends Activity {
|
||||
setContentView(R.layout.snake_layout);
|
||||
|
||||
mSnakeView = (SnakeView) findViewById(R.id.snake);
|
||||
mSnakeView.setTextView((TextView) findViewById(R.id.text));
|
||||
mSnakeView.setDependentViews((TextView) findViewById(R.id.text),
|
||||
findViewById(R.id.arrowContainer), findViewById(R.id.background));
|
||||
|
||||
if (savedInstanceState == null) {
|
||||
// We were just launched -- set up a new game
|
||||
@@ -62,6 +74,31 @@ public class Snake extends Activity {
|
||||
mSnakeView.setMode(SnakeView.PAUSE);
|
||||
}
|
||||
}
|
||||
mSnakeView.setOnTouchListener(new OnTouchListener() {
|
||||
|
||||
@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;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -73,8 +110,34 @@ public class Snake extends Activity {
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(Bundle outState) {
|
||||
//Store the game state
|
||||
// Store the game state
|
||||
outState.putBundle(ICICLE_KEY, mSnakeView.saveState());
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles key events in the game. Update the direction our snake is traveling based on the
|
||||
* DPAD.
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public boolean onKeyDown(int keyCode, KeyEvent msg) {
|
||||
|
||||
switch (keyCode) {
|
||||
case KeyEvent.KEYCODE_DPAD_UP:
|
||||
mSnakeView.moveSnake(MOVE_UP);
|
||||
break;
|
||||
case KeyEvent.KEYCODE_DPAD_RIGHT:
|
||||
mSnakeView.moveSnake(MOVE_RIGHT);
|
||||
break;
|
||||
case KeyEvent.KEYCODE_DPAD_DOWN:
|
||||
mSnakeView.moveSnake(MOVE_DOWN);
|
||||
break;
|
||||
case KeyEvent.KEYCODE_DPAD_LEFT:
|
||||
mSnakeView.moveSnake(MOVE_LEFT);
|
||||
break;
|
||||
}
|
||||
|
||||
return super.onKeyDown(keyCode, msg);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user