package com.example.android.snake;
import java.util.ArrayList;
import java.util.Random;
import java.util.StringTokenizer;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class SnakeView extends BasicView implements OnClickListener{
private int mMode = READY;
public static final int READY = 1;
public static final int RUNNING = 2;
// /////////////20160511////////////////
public static final int PAUSE = 0;
public static final int LOSE = 3;
private int mDirection = NORTH;
private int mNextDirection = NORTH;
private static final int NORTH = 1;
private static final int SOUTH = 2;
private static final int EAST = 3;
private static final int WEST = 4;
// /////////////20160511////////////////
private static final int RED_STAR = 1;
private static final int YELLOW_STAR = 2;
private static final int GREEN_STAR = 3;
private static final int APPLE = 4;
private long mMoveDelay = 100;
private long mLastMove;
int score=0;
// /////////////////20160510/////////////////
private ArrayList<Coordinate> mSnakeTrail = new ArrayList<Coordinate>();
// //////////////////////20160517//////////////////////
private ArrayList<Coordinate> mAppleList = new ArrayList<Coordinate>();
///////////////////////////0518//////////////////////////////////////
private TextView mstatusText;
public void setTextView(TextView textView)
{
mstatusText=textView;
}
///////////////////////////0518///////////////////////////////////
private Button mStart; // 下面的五个按钮分别是我加的。因为原来的只能响应上下左右键的操作。
//这边是满足触屏的操作效果。可以在布局文件中看相关的布局。
private Button mLeft;
private Button mRight;
private Button mTop;
private Button mBottom;
public void setStartButton(Button button) {
mStart = button;
mStart.setOnClickListener(this);
}
private static final Random RNG = new Random();
private class Coordinate {
public int x;
public int y;
public Coordinate(int newX, int newY) {
x = newX;
y = newY;
}
public boolean equals(Coordinate other) {
if (x == other.x && y == other.y) {
return true;
}
return false;
}
@Override
public String toString() {
return "Coordinate: [" + x + "," + y + "]";
}
}
// /////////////////20160510/////////////////
private MyHandler mHandler = new MyHandler();
private String d;
private int index;
private Object localSharedPreferences;
class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
SnakeView.this.update();
SnakeView.this.invalidate();
}
public void sleep(long delayMillis) {
this.removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMillis);
}
};
public SnakeView(Context context, AttributeSet attrs) {
super(context, attrs);
// ///////////////////////20160504//////////////////////////
initSnakeView();
}
public SnakeView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// ///////////////////////20160504//////////////////////////
initSnakeView();
}
// ///////////////////////20160504//////////////////////////
private void initSnakeView() {
setFocusable(true);
Resources r = this.getContext().getResources();
resetPic(5);
loadPic(RED_STAR, r.getDrawable(R.drawable.redstar));
loadPic(YELLOW_STAR, r.getDrawable(R.drawable.yellowstar));
loadPic(GREEN_STAR, r.getDrawable(R.drawable.greenstar));
loadPic(APPLE, r.getDrawable(R.drawable.apple));
}
// ///////////////////////20160504//////////////////////////
private void initNewGame() {
mMoveDelay = 600;
// /////////////////20160510/////////////////
mSnakeTrail.clear();
mSnakeTrail.add(new Coordinate(7, 7));
mSnakeTrail.add(new Coordinate(6, 7));
mSnakeTrail.add(new Coordinate(5, 7));
mSnakeTrail.add(new Coordinate(4, 7));
mSnakeTrail.add(new Coordinate(3, 7));
mSnakeTrail.add(new Coordinate(2, 7));
// /////////////////20160510/////////////////
// ////////////////20160511/////////////////////
mNextDirection = NORTH;
// //////////////////20160517//////////////////////
// 添加苹果
mAppleList.clear();
addRandomApple();
addRandomApple();
}
// ////////////////////20160517////////////////////////////
private void addRandomApple() {
// TODO Auto-generated method stub
Coordinate newCoord = null;
//
// int newX = 1 + RNG.nextInt(xCount - 2);
// int newY = 1 + RNG.nextInt(yCount - 2);
//
// newCoord = new Coordinate(newX, newY);
//
// mAppleList.add(newCoord);
boolean found = false;
///循环结束的条件是什么?
while (!found) {
int newX = 1 + RNG.nextInt(xCount - 2);
int newY = 1 + RNG.nextInt(yCount - 2);
newCoord = new Coordinate(newX, newY);
boolean collision = false;
int snakelength = mSnakeTrail.size();
for (int i = 0; i < snakelength; i++) {
if (mSnakeTrail.get(i).equals(newCoord)) {
collision = true;
}
}
//生成的苹果与原有苹果位置重合
for(int j=0;j<mAppleList.size();j++)
{
if(mAppleList.get(j).equals(newCoord))
{
collision=true;
}
}
found = !collision;
}
//作者写的时候没有考虑新的苹果坐标与原有的苹果坐标相重合的事
mAppleList.add(newCoord);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent msg) {
if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
if (mMode == READY | mMode == LOSE) {
initNewGame();
setMode(RUNNING);
update();
return (true);
}
if (mDirection != SOUTH) {
mNextDirection = NORTH;
}
return (true);
}
if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
if (mDirection != NORTH) {
mNextDirection = SOUTH;
}
return (true);
}
if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
if (mDirection != EAST) {
mNextDirection = WEST;
}
return (true);
}
if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
if (mDirection != WEST) {
mNextDirection = EAST;
}
return (true);
}
return super.onKeyDown(keyCode, msg);
}
public int getGameState() {
return mMode;
}
public void setMode(int newMode) {
int oldMode = mMode;
mMode = newMode;
// if (newMode == RUNNING & oldMode != RUNNING) {
// update();
// return;
// }
//
if (newMode == RUNNING & oldMode != RUNNING) {
mstatusText.setVisibility(View.INVISIBLE);
mStart.setVisibility(View.INVISIBLE);
update();
return;
}
/**
* 非运行状态时,文本显示内容有几种情况
* 1、ready状态,提示按UP键开始游戏
* 2、Pause状态,提示按UP键接着进行、
* 3、如果是Lose状态,提示失败,显示得分,提示重新开始
* */
Resources res = getContext().getResources();
CharSequence str = "";
if (newMode == PAUSE) {
str = "Press Start to resume";
}
if (newMode == READY) {
str = "Press Start to play";
}
if (newMode == LOSE) {
str ="Game over\n Score=" +score
+ "\n Press Start to play";
}
mstatusText.setText(str);
mstatusText.setVisibility(View.VISIBLE);
mStart.setVisibility(View.VISIBLE);
mLeft.setVisibility(View.INVISIBLE);
mRight.setVisibility(View.INVISIBLE);
mTop.setVisibility(View.INVISIBLE);
mBottom.setVisibility(View.INVISIBLE);
}
//设计暂停时保存
public Bundle saveState() {
Bundle map = new Bundle();
map.putIntArray("mAppleList", coordArrayListToArray(mAppleList));
map.putInt("mDirection", Integer.valueOf(mDirection));
map.putInt("mNextDirection", Integer.valueOf(mNextDirection));
map.putLong("mMoveDelay", Long.valueO
评论0