• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.snake;
18 
19 import android.app.Activity;
20 import android.os.Bundle;
21 import android.view.KeyEvent;
22 import android.view.MotionEvent;
23 import android.view.View;
24 import android.view.View.OnTouchListener;
25 import android.widget.TextView;
26 
27 /**
28  * Snake: a simple game that everyone can enjoy.
29  *
30  * This is an implementation of the classic Game "Snake", in which you control a serpent roaming
31  * around the garden looking for apples. Be careful, though, because when you catch one, not only
32  * will you become longer, but you'll move faster. Running into yourself or the walls will end the
33  * game.
34  *
35  */
36 public class Snake extends Activity {
37 
38     /**
39      * Constants for desired direction of moving the snake
40      */
41     public static int MOVE_LEFT = 0;
42     public static int MOVE_UP = 1;
43     public static int MOVE_DOWN = 2;
44     public static int MOVE_RIGHT = 3;
45 
46     private static String ICICLE_KEY = "snake-view";
47 
48     private SnakeView mSnakeView;
49 
50     /**
51      * Called when Activity is first created. Turns off the title bar, sets up the content views,
52      * and fires up the SnakeView.
53      *
54      */
55     @Override
onCreate(Bundle savedInstanceState)56     public void onCreate(Bundle savedInstanceState) {
57         super.onCreate(savedInstanceState);
58 
59         setContentView(R.layout.snake_layout);
60 
61         mSnakeView = (SnakeView) findViewById(R.id.snake);
62         mSnakeView.setDependentViews((TextView) findViewById(R.id.text),
63                 findViewById(R.id.arrowContainer), findViewById(R.id.background));
64 
65         if (savedInstanceState == null) {
66             // We were just launched -- set up a new game
67             mSnakeView.setMode(SnakeView.READY);
68         } else {
69             // We are being restored
70             Bundle map = savedInstanceState.getBundle(ICICLE_KEY);
71             if (map != null) {
72                 mSnakeView.restoreState(map);
73             } else {
74                 mSnakeView.setMode(SnakeView.PAUSE);
75             }
76         }
77         mSnakeView.setOnTouchListener(new OnTouchListener() {
78 
79             @Override
80             public boolean onTouch(View v, MotionEvent event) {
81                 if (mSnakeView.getGameState() == SnakeView.RUNNING) {
82                     // Normalize x,y between 0 and 1
83                     float x = event.getX() / v.getWidth();
84                     float y = event.getY() / v.getHeight();
85 
86                     // Direction will be [0,1,2,3] depending on quadrant
87                     int direction = 0;
88                     direction = (x > y) ? 1 : 0;
89                     direction |= (x > 1 - y) ? 2 : 0;
90 
91                     // Direction is same as the quadrant which was clicked
92                     mSnakeView.moveSnake(direction);
93 
94                 } else {
95                     // If the game is not running then on touching any part of the screen
96                     // we start the game by sending MOVE_UP signal to SnakeView
97                     mSnakeView.moveSnake(MOVE_UP);
98                 }
99                 return false;
100             }
101         });
102     }
103 
104     @Override
onPause()105     protected void onPause() {
106         super.onPause();
107         // Pause the game along with the activity
108         mSnakeView.setMode(SnakeView.PAUSE);
109     }
110 
111     @Override
onSaveInstanceState(Bundle outState)112     public void onSaveInstanceState(Bundle outState) {
113         // Store the game state
114         outState.putBundle(ICICLE_KEY, mSnakeView.saveState());
115     }
116 
117     /**
118      * Handles key events in the game. Update the direction our snake is traveling based on the
119      * DPAD.
120      *
121      */
122     @Override
onKeyDown(int keyCode, KeyEvent msg)123     public boolean onKeyDown(int keyCode, KeyEvent msg) {
124 
125         switch (keyCode) {
126             case KeyEvent.KEYCODE_DPAD_UP:
127                 mSnakeView.moveSnake(MOVE_UP);
128                 break;
129             case KeyEvent.KEYCODE_DPAD_RIGHT:
130                 mSnakeView.moveSnake(MOVE_RIGHT);
131                 break;
132             case KeyEvent.KEYCODE_DPAD_DOWN:
133                 mSnakeView.moveSnake(MOVE_DOWN);
134                 break;
135             case KeyEvent.KEYCODE_DPAD_LEFT:
136                 mSnakeView.moveSnake(MOVE_LEFT);
137                 break;
138         }
139 
140         return super.onKeyDown(keyCode, msg);
141     }
142 
143 }
144