《android课程设计贪吃蛇手机安卓游戏.doc》由会员分享,可在线阅读,更多相关《android课程设计贪吃蛇手机安卓游戏.doc(20页珍藏版)》请在三一办公上搜索。
1、课 程 设 计 课程名称 游戏基础开发 题目名称_ 贪吃蛇手机安卓游戏_学生学院专业班级学 号学生姓名指导教师2011 年 12 月 13 日一、 程序代码:(1) TileView.java/*TileView是基于Android的View类实现的方块图类,用来支撑上层类的调用,绘*制方块图的显示界面*/package mobile.android.demo.snake;import com.example.android.snake.R;import android.content.Context;import android.content.res.TypedArray;import a
2、ndroid.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.view.View;public class TileView extends Viewprotected static int mTileSize;protected static int mXTileCount;protected static i
3、nt mYTileCount;private static int mXOffset;private static int mYOffset;private Bitmap mTileArray;private int mTileGrid;private final Paint mPaint = new Paint();public TileView(Context context, AttributeSet attrs, int defStyle)super(context, attrs, defStyle);TypedArray a = context.obtainStyledAttribu
4、tes(attrs,R.styleable.TileView);mTileSize = a.getInt(R.styleable.TileView_tileSize, 12);a.recycle();public TileView(Context context, AttributeSet attrs)super(context, attrs);TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.TileView);mTileSize = a.getInt(R.styleable.TileView_tileSize,
5、12);a.recycle();public void resetTiles(int tilecount)mTileArray = new Bitmaptilecount;Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh)mXTileCount = (int) Math.floor(w / mTileSize);mYTileCount = (int) Math.floor(h / mTileSize);mXOffset = (w - (mTileSize * mXTileCount) / 2);mYOff
6、set = (h - (mTileSize * mYTileCount) / 2);mTileGrid = new intmXTileCountmYTileCount;clearTiles();public void loadTile(int key, Drawable tile)Bitmap bitmap = Bitmap.createBitmap(mTileSize, mTileSize,Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(bitmap);tile.setBounds(0, 0, mTileSize, mTileSize)
7、;tile.draw(canvas);mTileArraykey = bitmap;public void clearTiles()for (int x = 0; x mXTileCount; x+)for (int y = 0; y mYTileCount; y+)setTile(0, x, y);public void setTile(int tileindex, int x, int y)mTileGridxy = tileindex;Overridepublic void onDraw(Canvas canvas)super.onDraw(canvas);for (int x = 0;
8、 x mXTileCount; x += 1)for (int y = 0; y 0)canvas.drawBitmap(mTileArraymTileGridxy, mXOffset + x* mTileSize, mYOffset + y * mTileSize, mPaint);(2) SnakeView.java/*SnakeView调用了TileView,实现了游戏逻辑 和 具体的显示。*/package mobile.android.demo.snake;import java.util.ArrayList;import java.util.Random;import com.ex
9、ample.android.snake.R;import android.content.Context;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.
10、view.View;import android.widget.TextView;public class SnakeView extends TileViewprivate static final String TAG = SnakeView;private int mMode = READY;public static final int PAUSE = 0;public static final int READY = 1;public static final int RUNNING = 2;public static final int LOSE = 3; private int
11、mWidth, mHeight; 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;private static final int RED_STAR = 1;private static final int YELLOW_STAR = 2;
12、private static final int GREEN_STAR = 3;private long mScore = 0;private long mMoveDelay = 600;private long mLastMove;private TextView mStatusText;private ArrayList mSnakeTrail = new ArrayList();private ArrayList mAppleList = new ArrayList();private static final Random RNG = new Random();private Refr
13、eshHandler mRedrawHandler = new RefreshHandler();class RefreshHandler extends HandlerOverridepublic void handleMessage(Message msg)SnakeView.this.update();SnakeView.this.invalidate();public void sleep(long delayMillis)this.removeMessages(0);sendMessageDelayed(obtainMessage(0), delayMillis);public Sn
14、akeView(Context context, AttributeSet attrs)super(context, attrs);initSnakeView();public SnakeView(Context context, AttributeSet attrs, int defStyle)super(context, attrs, defStyle);initSnakeView();private void initSnakeView()setFocusable(true);Resources r = this.getContext().getResources();resetTile
15、s(4);loadTile(RED_STAR, r.getDrawable(R.drawable.redstar);loadTile(YELLOW_STAR, r.getDrawable(R.drawable.yellowstar);loadTile(GREEN_STAR, r.getDrawable(R.drawable.greenstar);private void initNewGame()mSnakeTrail.clear();mAppleList.clear();mSnakeTrail.add(new Coordinate(7, 7);mSnakeTrail.add(new Coor
16、dinate(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);mNextDirection = NORTH;addRandomApple();addRandomApple();mMoveDelay = 600;mScore = 0;private int coordArrayListToArray(ArrayList cvec)int c
17、ount = cvec.size();int rawArray = new intcount * 2;for (int index = 0; index count; index+)Coordinate c = cvec.get(index);rawArray2 * index = c.x;rawArray2 * index + 1 = c.y;return rawArray;public Bundle saveState()Bundle map = new Bundle();map.putIntArray(mAppleList, coordArrayListToArray(mAppleLis
18、t);map.putInt(mDirection, Integer.valueOf(mDirection);map.putInt(mNextDirection, Integer.valueOf(mNextDirection);map.putLong(mMoveDelay, Long.valueOf(mMoveDelay);map.putLong(mScore, Long.valueOf(mScore);map.putIntArray(mSnakeTrail, coordArrayListToArray(mSnakeTrail);return map;private ArrayList coor
19、dArrayToArrayList(int rawArray)ArrayList coordArrayList = new ArrayList();int coordCount = rawArray.length;for (int index = 0; index coordCount; index += 2)Coordinate c = new Coordinate(rawArrayindex, rawArrayindex + 1);coordArrayList.add(c);return coordArrayList;public void restoreState(Bundle icic
20、le)setMode(PAUSE);mAppleList = coordArrayToArrayList(icicle.getIntArray(mAppleList);mDirection = icicle.getInt(mDirection);mNextDirection = icicle.getInt(mNextDirection);mMoveDelay = icicle.getLong(mMoveDelay);mScore = icicle.getLong(mScore);mSnakeTrail = coordArrayToArrayList(icicle.getIntArray(mSn
21、akeTrail);public void setTextView(TextView newView)mStatusText = newView;public void setMode(int newMode)int oldMode = mMode;mMode = newMode;if (newMode = RUNNING & oldMode != RUNNING)mStatusText.setVisibility(View.INVISIBLE);update();return;Resources res = getContext().getResources();CharSequence s
22、tr = ;if (newMode = PAUSE)str = res.getText(R.string.mode_pause);if (newMode = READY)str = res.getText(R.string.mode_ready);if (newMode = LOSE)str = res.getString(R.string.mode_lose_prefix) + mScore+ res.getString(R.string.mode_lose_suffix);mStatusText.setText(str);mStatusText.setVisibility(View.VIS
23、IBLE);private void addRandomApple()Coordinate newCoord = null;boolean found = false;while (!found)int newX = 1 + RNG.nextInt(mXTileCount - 2);int newY = 1 + RNG.nextInt(mYTileCount - 2);newCoord = new Coordinate(newX, newY);boolean collision = false;int snakelength = mSnakeTrail.size();for (int inde
24、x = 0; index mMoveDelay)clearTiles();updateWalls();/更新小蛇的位置updateSnake();updateApples();mLastMove = now;/产生动画效果mRedrawHandler.sleep(mMoveDelay);/绘制围墙private void updateWalls()for (int x = 0; x mXTileCount; x+)setTile(GREEN_STAR, x, 0);setTile(GREEN_STAR, x, mYTileCount - 1);for (int y = 1; y mYTileC
25、ount - 1; y+)setTile(GREEN_STAR, 0, y);setTile(GREEN_STAR, mXTileCount - 1, y); /绘制食物private void updateApples()for (Coordinate c : mAppleList)setTile(YELLOW_STAR, c.x, c.y);private void updateSnake()boolean growSnake = false; /获得小蛇的头Coordinate head = mSnakeTrail.get(0);Coordinate newHead = new Coor
26、dinate(1, 1);mDirection = mNextDirection;switch (mDirection)case EAST:newHead = new Coordinate(head.x + 1, head.y);break;case WEST:newHead = new Coordinate(head.x - 1, head.y);break;case NORTH:newHead = new Coordinate(head.x, head.y - 1);break;case SOUTH:newHead = new Coordinate(head.x, head.y + 1);
27、break;if (newHead.x 1) | (newHead.y mXTileCount - 2)| (newHead.y mYTileCount - 2)setMode(LOSE);return;int snakelength = mSnakeTrail.size();for (int snakeindex = 0; snakeindex snakelength; snakeindex+)Coordinate c = mSnakeTrail.get(snakeindex);if (c.equals(newHead)setMode(LOSE);return;int applecount
28、= mAppleList.size();for (int appleindex = 0; appleindex mWidth / 4 & event.getX() mWidth * 3 / 4& event.getY() mWidth / 4 & event.getX() mHeight / 2)if (mDirection != NORTH)mNextDirection = SOUTH;else if (event.getX() (mWidth * 3 / 4 )if (mDirection != WEST)mNextDirection = EAST;elseinitNewGame();se
29、tMode(RUNNING);update(); /update()实现对游戏数据的更新 if (mDirection != SOUTH)mNextDirection = NORTH;return super.onTouchEvent(event); /*键盘事件*/ public boolean onKeyDown(int keyCode, KeyEvent msg) if (keyCode = KeyEvent.KEYCODE_DPAD_UP) if (mMode = READY | mMode = LOSE) initNewGame(); setMode(RUNNING); update
30、(); /update()实现对游戏数据的更新 return (true); if (mMode = PAUSE) /* setMode(RUNNING); update(); return (true); /*如果按键的方向 跟蛇本身的运动方向完全相反,则无法执行*/ if (mDirection != SOUTH) mNextDirection = NORTH; return (true); if (keyCode = KeyEvent.KEYCODE_DPAD_DOWN) if (mDirection != NORTH) mNextDirection = SOUTH; return (t
31、rue); 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); (3) Snake.java /*Snake为主Activity类*/package mobi
32、le.android.demo.snake;import com.example.android.snake.R;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class Snake extends Activityprivate SnakeView mSnakeView;private static String ICICLE_KEY = snake-view;Overridepublic void onCreate(Bundle savedInstanceState)super.onCreate(savedInstanceState);setContentView(R.layout.snake_layout);mSnakeView = (SnakeView) fin