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 // No Title bar 49 requestWindowFeature(Window.FEATURE_NO_TITLE); 50 51 setContentView(R.layout.snake_layout); 52 53 mSnakeView = (SnakeView) findViewById(R.id.snake); 54 mSnakeView.setTextView((TextView) findViewById(R.id.text)); 55 56 if (savedInstanceState == null) { 57 // We were just launched -- set up a new game 58 mSnakeView.setMode(SnakeView.READY); 59 } else { 60 // We are being restored 61 Bundle map = savedInstanceState.getBundle(ICICLE_KEY); 62 if (map != null) { 63 mSnakeView.restoreState(map); 64 } else { 65 mSnakeView.setMode(SnakeView.PAUSE); 66 } 67 } 68 } 69 70 @Override onPause()71 protected void onPause() { 72 super.onPause(); 73 // Pause the game along with the activity 74 mSnakeView.setMode(SnakeView.PAUSE); 75 } 76 77 @Override onSaveInstanceState(Bundle outState)78 public void onSaveInstanceState(Bundle outState) { 79 //Store the game state 80 outState.putBundle(ICICLE_KEY, mSnakeView.saveState()); 81 } 82 83 } 84