1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
| public class SnakeView extends View{ public static final String TAG = "SnakeView";
private int mWidth; private int mHeight;
private static final int sXOffset = 0 ; private static final int sYOffset = 0 ;
private final int BOXWIDTH = 30; private Random mRandom = new Random(); private Point mFoodPosition; private boolean mIsFoodDone = true;
private ArrayList<Point> mSnakeList; private Paint mSnakePaint; private int mSnakeDirection = 0; private final int UP = 1; private final int DOWN = 2; private final int LEFT = 3; private final int RIGHT =4;
private Paint mBgPaint; private Paint mFoodPaint;
public SnakeView(Context context) { super(context); mSnakeList = new ArrayList<Point>(); mSnakePaint = new Paint(); mSnakePaint.setColor(Color.RED); mSnakePaint.setStyle(Paint.Style.FILL_AND_STROKE); mSnakeList.add(new Point(500,500)); mSnakeList.add(new Point(500,530));
mSnakeDirection = RIGHT; mIsFoodDone = true; mFoodPosition= new Point();
mFoodPaint = new Paint(); mFoodPaint.setColor(Color.CYAN); mFoodPaint.setStyle(Paint.Style.FILL);
mBgPaint = new Paint(); Paint paint = new Paint(); paint.setColor(Color.WHITE);
}
@Override public boolean onTouchEvent(MotionEvent event) {
int x = (int)(event.getX()); int y = (int)(event.getY()); Log.e(TAG, "x =" + x + " y = " + y + " mSnakeDirection = " + mSnakeDirection); if(mSnakeDirection == UP || mSnakeDirection == DOWN) { if(x < head.x) mSnakeDirection = LEFT; if(x > head.x) mSnakeDirection = RIGHT; } else if(mSnakeDirection == LEFT || mSnakeDirection == RIGHT) { if(y < head.y) mSnakeDirection = UP; if(y > head.y) mSnakeDirection= DOWN; } return super.onTouchEvent(event); }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas);
drawBg(canvas, mBgPaint); drawFood(canvas, mFoodPaint); drawSnake(canvas, mSnakePaint);
}
@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mWidth = w; mHeight = h; }
private void drawBg(Canvas canvas, Paint paint) { canvas.drawColor(Color.WHITE); }
private void drawSnake(Canvas canvas, Paint paint) { for(int i = 0 ; i < mSnakeList.size() ; i++ ) { Point point = mSnakeList.get(i); Rect rect = new Rect(point.x , point.y , point.x + BOXWIDTH , point.y + BOXWIDTH); canvas.drawRect(rect, paint); } snakeMove(mSnakeList, mSnakeDirection); if(isFoodEaten()) { mIsFoodDone = true; } else { mSnakeList.remove(mSnakeList.size() - 1); } }
private void drawFood(Canvas canvas, Paint paint) { if(mIsFoodDone) { mFoodPosition.x = mRandom.nextInt(mWidth - 2*sXOffset - BOXWIDTH) + sXOffset ; mFoodPosition.y = mRandom.nextInt(mWidth - 2*sYOffset - BOXWIDTH) + sYOffset ; mIsFoodDone = false; } Rect food = new Rect(mFoodPosition.x , mFoodPosition.y , mFoodPosition.x + BOXWIDTH , mFoodPosition.y + BOXWIDTH); canvas.drawRect(food, paint);
}
public void snakeMove(ArrayList<Point> list , int direction) { Point orighead = list.get(0); Point newhead = new Point(); switch(direction) { case UP: newhead.x = orighead.x; newhead.y = orighead.y - BOXWIDTH ; break; case DOWN: newhead.x = orighead.x; newhead.y = orighead.y + BOXWIDTH ; break; case LEFT: newhead.x = orighead.x - BOXWIDTH; newhead.y = orighead.y; break; case RIGHT: newhead.x = orighead.x + BOXWIDTH ; newhead.y = orighead.y; break; default: break; } adjustHead(newhead); list.add(0, newhead); }
private boolean isOutBound(Point point) { if(point.x < sXOffset || point.x > mWidth - sXOffset) return true; if(point.y < sYOffset || point.y > mHeight - sYOffset) return true; return false; }
private void adjustHead(Point point) { if(isOutBound(point)){ if(mSnakeDirection == UP) point.y = mHeight - sYOffset - BOXWIDTH; if(mSnakeDirection == DOWN) point.y = sYOffset; if(mSnakeDirection == LEFT) point.x = mWidth - sYOffset - BOXWIDTH; if(mSnakeDirection == RIGHT) point.x = sXOffset; } }
private boolean isFoodEaten() { if(!mIsFoodDone) { Rect foodrect = new Rect(mFoodPosition.x, mFoodPosition.y, mFoodPosition.x + BOXWIDTH, mFoodPosition.y + BOXWIDTH); Point head = mSnakeList.get(0); Rect headrect = new Rect(head.x, head.y, head.x + BOXWIDTH , head.y + BOXWIDTH); return foodrect.intersect(headrect); } return false; }
}
|