Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.70 KB | None | 0 0
  1. package ru.ps.habrsnake;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Random;
  5. import java.util.Timer;
  6. import java.util.TimerTask;
  7.  
  8. import android.app.Activity;
  9. import android.content.pm.ActivityInfo;
  10. import android.graphics.Canvas;
  11. import android.graphics.Paint;
  12. import android.os.Bundle;
  13. import android.view.MotionEvent;
  14. import android.view.View;
  15. import android.view.Window;
  16. import android.view.WindowManager;
  17. import android.view.View.OnTouchListener;
  18. public class A extends Activity {
  19.     static final int RESULT_FINISH = 0, RESULT_OK = 1, RESULT_LOCK = 2,RESULT_SELF = 3, RESULT_INC = 4, RESULT_WIN = 5, RATE = 250, cSnake = 0xAACC33AA, cMeat = 0xEEAACC33, cBG = 0xFF000000, cText = 0xFFFFFFFF, width_cells = 10;
  20.     public V gv;
  21.     protected void onCreate(Bundle savedInstanceState) {
  22.         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  23.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  24.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  25.         super.onCreate(savedInstanceState);
  26.         gv = new V(this);
  27.         setContentView(gv);}
  28.     protected void onDestroy() {
  29.         super.onDestroy();
  30.         gv.ph.timer.cancel();}
  31.     class V extends View implements OnTouchListener{
  32.         public float diam_cell = 10.f,x,y;
  33.         public P ph;
  34.         public Paint paintSnake = new Paint(), paintMeat = new Paint();
  35.         public V(Activity activity) {
  36.             super(activity);
  37.             setBackgroundColor(cBG);
  38.             paintSnake.setColor(cSnake);
  39.             paintMeat.setColor(cMeat);
  40.             setOnTouchListener(this);
  41.             diam_cell = ((float) A.this.getWindowManager().getDefaultDisplay().getWidth()) / ((float) width_cells);
  42.             ph = new P(width_cells, (int) (A.this.getWindowManager().getDefaultDisplay().getHeight() / diam_cell));}
  43.         public void invalidateWrapper(){
  44.                 A.this.runOnUiThread(new Runnable() {public void run() {invalidate();}});}
  45.         public void onDraw(Canvas canvas) {
  46.             for (int i = 0; i < ph.arSnake.size(); canvas.drawCircle((ph.arSnake.get(i) % width_cells +.5f) * diam_cell, (ph.arSnake.get(i) / width_cells +.5f) * diam_cell, diam_cell*.5f, paintSnake),i++){}
  47.             canvas.drawCircle((ph.posMeat % width_cells +.5f) * diam_cell, (ph.posMeat / width_cells +.5f) * diam_cell, diam_cell*.5f, paintMeat);
  48.             canvas.drawText(ph.scores + "", canvas.getWidth() * .5f ,paintMeat.getTextSize(), paintMeat);}
  49.         public boolean onTouch(View v, MotionEvent event) {
  50.                 x = (event.getActionMasked() == MotionEvent.ACTION_DOWN)?event.getX():x;
  51.                 y = (event.getActionMasked() == MotionEvent.ACTION_DOWN)?event.getY():y;
  52.                 ph.setDir((event.getActionMasked() == MotionEvent.ACTION_UP)?((Math.abs(event.getX() - x) > Math.abs(event.getY() - y) && (event.getX() - x) > 0)?3:(((Math.abs(event.getX() - x) > Math.abs(event.getY() - y)) && (event.getX() - x) <= 0)?2:((event.getY() - y) > 0?1:0))):ph.GlobDir);
  53.             return true;}
  54.         class P {
  55.             public int[] arI = new int[]{5,4,3,2};
  56.             public ArrayList<Integer> arSnake = new ArrayList<Integer>();
  57.             public Timer timer;
  58.             public int posMeat, GlobDir = 1,scores;
  59.             public Random r = new Random(System.currentTimeMillis());
  60.             public P(final int w, final int h) {
  61.                 timer = new Timer();
  62.                 reset(w,h,0);
  63.                 timer.scheduleAtFixedRate(new TimerTask() {public void run() {
  64.                         final int res = next(GlobDir,posMeat,w,h);
  65.                         posMeat = (res==RESULT_INC)?nextPosMeat(w,h,-1,0,0,0):posMeat;
  66.                         scores += (res==RESULT_INC)?1:0;
  67.                         V.this.invalidateWrapper();
  68.                         if (res == RESULT_FINISH || res == RESULT_SELF || res == RESULT_WIN) reset(w,h,0);
  69.                     }}, 0, RATE);}
  70.             public void reset(int width, int height,int i){
  71.                 for(GlobDir = 1,scores = 0,arSnake.clear(); i < arI.length;arSnake.add(arI[i]),i++){}
  72.                 posMeat = nextPosMeat(width, height,-1,0,0,0);}
  73.             public void setDir(int dir){
  74.                 GlobDir = ((dir + GlobDir == 1) || (dir + GlobDir == 5))?GlobDir:dir;}
  75.             public int nextPosMeat(int width, int height,int resI,int i,int count,int exit) {
  76.                 for (int r0 = r.nextInt(width*height - arSnake.size()) + 1; i < height*width && exit == 0;count+=(arSnake.contains(Integer.valueOf(i))?0:1),exit = ((count == r0)?1:0),resI = (exit==1?i:resI),i+=(exit == 1)?0:1){}
  77.                 return resI;}
  78.             public int next(int d, int p, int width, int height){//0-up,1-down,2-left,3-right
  79.             if (arSnake.size() >= width*height - 1) return RESULT_WIN;
  80.             int y = arSnake.get(0) / width, x = arSnake.get(0) % width;
  81.             if ((d == 0 && --y < 0) || (d == 1 && ++y >= height) || (d == 2 && --x < 0) || (d == 3 && ++x >= width) ) return RESULT_FINISH;
  82.             if (arSnake.contains(Integer.valueOf(x + y * width))) return RESULT_SELF;
  83.             arSnake.add(0,x + y * width);
  84.             if (x + y * width == p) return RESULT_INC;
  85.             arSnake.remove(arSnake.size() - 1);
  86.             return RESULT_OK;}}}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement