谁可以帮我用JAVA做个贪吃蛇
java吧
全部回复
仅看楼主
level 4
2012年09月20日 02点09分 1
level 16
[大哭]好难啊
2012年09月20日 02点09分 2
[开心]
2012年09月20日 06点09分
回复 nadesico19 :[不要]
2012年09月20日 06点09分
怎么又贪吃蛇啊,我想跳过去了...
2012年09月20日 15点09分
level 10
我刚做了android坦克 要不要,3P的
2012年09月20日 02点09分 3
3p亮了[抛媚眼]
2012年09月20日 02点09分
回复 梦碎此生 :敌人只有三个坦克陪我玩的[抖胸]
2012年09月20日 02点09分
回复 Adefengshen :怎么个发法
2012年09月20日 02点09分
回复 Adefengshen :我都不知道怎么搞,留邮箱发你吧
2012年09月20日 03点09分
level 11
听别人说网上有啊,搜搜吧。
2012年09月20日 02点09分 4
level 12
我前几周刚好写了一个贪吃蛇。要的话留言,放学发给你
2012年09月20日 02点09分 7
也发给我一份吧,想学习一下。我的邮箱 [email protected] 谢啦
2012年09月20日 06点09分
放学回去发给你。现在在上课。
2012年09月20日 06点09分
回复 春来发几枝__ : 已发,请查收
2012年09月20日 10点09分
回复 冲258 :原来是大神啊,自学一个月就能做这个,是你太牛了,还是我太笨或者太懒了……嗯,继续努力,加油!!!
2012年09月20日 13点09分
level 9
[鬼脸]
2012年09月20日 03点09分 8
level 9
[打酱油]
2012年09月20日 06点09分 9
level 11
额,这么难。我们都是算算数的
2012年09月20日 06点09分 10
level 7
[吐舌]
2012年09月20日 08点09分 12
吧务
level 13
网上有代码吧。。
2012年09月20日 08点09分 13
level 6
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView; /**
* Snake: a simple game that everyone can enjoy.
*
* This is an implementation of the classic Game "Snake", in which you control a
* serpent roaming around the garden looking for apples. Be careful, though,
* because when you catch one, not only will you become longer, but you'll move
* faster. Running into yourself or the walls will end the game.
*
*/
public class Snake extends Activity { private SnakeView mSnakeView;
private static String ICICLE_KEY = "snake-view"; /**
* Called when Activity is first created. Turns off the title bar, sets up
* the content views, and fires up the SnakeView.
*
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // No Title bar
requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.snake_layout); mSnakeView = (SnakeView) findViewById(R.id.snake);
mSnakeView.setTextView((TextView) findViewById(R.id.text)); if (savedInstanceState == null) {
// We were just launched -- set up a new game
mSnakeView.setMode(SnakeView.READY);
} else {
// We are being restored
Bundle map = savedInstanceState.getBundle(ICICLE_KEY);
if (map != null) {
mSnakeView.restoreState(map);
} else {
mSnakeView.setMode(SnakeView.PAUSE);
}
}
} @Override
protected void onPause() {
super.onPause();
// Pause the game along with the activity
mSnakeView.setMode(SnakeView.PAUSE);
} @Override
public void onSaveInstanceState(Bundle outState) {
//Store the game state
outState.putBundle(ICICLE_KEY, mSnakeView.saveState());
} }

2012年09月20日 08点09分 14
level 6
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
/**
* TileView: a View-variant designed for handling arrays of "icons" or other
* drawables.
*
*/
public class TileView extends View { /**
* Parameters controlling the size of the tiles and their range within view.
* Width/Height are in pixels, and Drawables will be scaled to fit to these
* dimensions. X/Y Tile Counts are the number of tiles that will be drawn.
*/ protected static int mTileSize; protected static int mXTileCount;
protected static int mYTileCount; private static int mXOffset;
private static int mYOffset;
/**
* A hash that maps integer handles specified by the subclasser to the
* drawable that will be used for that reference
*/
private Bitmap[] mTileArray; /**
* A two-dimensional array of integers in which the number represents the
* index of the tile that should be drawn at that locations
*/
private int[][] mTileGrid; private final Paint mPaint = new Paint(); public TileView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle); TypedArray a = context.obtainStyledAttributes(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, 12);
a.recycle();
}
/**
* Rests the internal array of Bitmaps used for drawing tiles, and
* sets the maximum index of tiles to be inserted
*
* @param tilecount
*/
public void resetTiles(int tilecount) {
mTileArray = new Bitmap[tilecount];
}
@Override
protected 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);
mYOffset = ((h - (mTileSize * mYTileCount)) / 2); mTileGrid = new int[mXTileCount][mYTileCount];
clearTiles();
} /**
* Function to set the specified Drawable as the tile for a particular
* integer key.
*
* @param key

2012年09月20日 08点09分 15
level 6
* @param tile
*/
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);
tile.draw(canvas);
mTileArray[key] = bitmap;
} /**
* Resets all tiles to 0 (empty)
*
*/
public void clearTiles() {
for (int x = 0; x < mXTileCount; x++) {
for (int y = 0; y < mYTileCount; y++) {
setTile(0, x, y);
}
}
} /**
* Used to indicate that a particular tile (set with loadTile and referenced
* by an integer) should be drawn at the given x/y coordinates during the
* next invalidate/draw cycle.
*
* @param tileindex
* @param x
* @param y
*/
public void setTile(int tileindex, int x, int y) {
mTileGrid[x][y] = tileindex;
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int x = 0; x < mXTileCount; x += 1) {
for (int y = 0; y < mYTileCount; y += 1) {
if (mTileGrid[x][y] > 0) {
canvas.drawBitmap(mTileArray[mTileGrid[x][y]],
mXOffset + x * mTileSize,
mYOffset + y * mTileSize,
mPaint);
}
}
} } }
2012年09月20日 08点09分 16
level 6
import java.util.ArrayList;
import java.util.Random; import android.content.Context;
import android.content.res.Resources;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView; /**
public class SnakeView extends TileView { private 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; /**
* Current direction the snake is headed.
*/
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; /**
* Labels for the drawables that will be loaded into the TileView class
*/
private static final int RED_STAR = 1;
private static final int YELLOW_STAR = 2;
private static final int GREEN_STAR = 3;
private long mScore = 0;
private long mMoveDelay = 600;
private long mLastMove;
private TextView mStatusText; /**
* mSnakeTrail: a list of Coordinates that make up the snake's body
* mAppleList: the secret location of the juicy apples the snake craves.
*/
private ArrayList<Coordinate> mSnakeTrail = new ArrayList<Coordinate>();
private ArrayList<Coordinate> mAppleList = new ArrayList<Coordinate>();
private static final Random RNG = new Random(); private RefreshHandler mRedrawHandler = new RefreshHandler(); class RefreshHandler 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);
initSnakeView();
} public SnakeView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initSnakeView();
} private void initSnakeView() {
setFocusable(true); Resources r = this.getContext().getResources();
resetTiles(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(); // For now we're just going to load up a short default eastbound snake

2012年09月20日 08点09分 17
level 6
// that's just turned north
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));
mNextDirection = NORTH; // Two apples to start with
addRandomApple();
addRandomApple(); mMoveDelay = 600;
mScore = 0;
}
private int[] coordArrayListToArray(ArrayList<Coordinate> cvec) {
int count = cvec.size();
int[] rawArray = new int[count * 2];
for (int index = 0; index < count; index++) {
Coordinate c = cvec.get(index);
rawArray[2 * index] = c.x;
rawArray[2 * index + 1] = c.y;
}
return rawArray;
}
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.valueOf(mMoveDelay));
map.putLong("mScore", Long.valueOf(mScore));
map.putIntArray("mSnakeTrail", coordArrayListToArray(mSnakeTrail)); return map;
}
private ArrayList<Coordinate> coordArrayToArrayList(int[] rawArray) {
ArrayList<Coordinate> coordArrayList = new ArrayList<Coordinate>(); int coordCount = rawArray.length;
for (int index = 0; index < coordCount; index += 2) {
Coordinate c = new Coordinate(rawArray[index], rawArray[index + 1]);
coordArrayList.add(c);
}
return coordArrayList;
}
public void restoreState(Bundle icicle) {
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("mSnakeTrail"));
}
@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 (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 (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 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 str = "";
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.VISIBLE);
}

2012年09月20日 08点09分 18
level 11
2012年09月20日 10点09分 20
1