• 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.Window;
22 import android.widget.TextView;
23 
24 /**
25  * Snake: a simple game that everyone can enjoy.
26  *
27  * This is an implementation of the classic Game "Snake", in which you control a
28  * serpent roaming around the garden looking for apples. Be careful, though,
29  * because when you catch one, not only will you become longer, but you'll move
30  * faster. Running into yourself or the walls will end the game.
31  *
32  */
33 public class Snake extends Activity {
34 
35     private SnakeView mSnakeView;
36 
37     private static String ICICLE_KEY = "snake-view";
38 
39     /**
40      * Called when Activity is first created. Turns off the title bar, sets up
41      * the content views, and fires up the SnakeView.
42      *
43      */
44     @Override
onCreate(Bundle savedInstanceState)45     public void onCreate(Bundle savedInstanceState) {
46         super.onCreate(savedInstanceState);
47 
48         setContentView(R.layout.snake_layout);
49 
50         mSnakeView = (SnakeView) findViewById(R.id.snake);
51         mSnakeView.setTextView((TextView) findViewById(R.id.text));
52 
53         if (savedInstanceState == null) {
54             // We were just launched -- set up a new game
55             mSnakeView.setMode(SnakeView.READY);
56         } else {
57             // We are being restored
58             Bundle map = savedInstanceState.getBundle(ICICLE_KEY);
59             if (map != null) {
60                 mSnakeView.restoreState(map);
61             } else {
62                 mSnakeView.setMode(SnakeView.PAUSE);
63             }
64         }
65     }
66 
67     @Override
onPause()68     protected void onPause() {
69         super.onPause();
70         // Pause the game along with the activity
71         mSnakeView.setMode(SnakeView.PAUSE);
72     }
73 
74     @Override
onSaveInstanceState(Bundle outState)75     public void onSaveInstanceState(Bundle outState) {
76         //Store the game state
77         outState.putBundle(ICICLE_KEY, mSnakeView.saveState());
78     }
79 
80 }
81