是男人就下100层第四层——Crazy贪吃蛇

Posted clnchanpin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了是男人就下100层第四层——Crazy贪吃蛇相关的知识,希望对你有一定的参考价值。

在上一篇《是男人就下100层【第四层】——Crazy贪吃蛇(1)》中我们让贪吃蛇移动了起来,接下来我们来实现让贪吃蛇能够绕着手机屏幕边线移动而且能够改变方向

一、加入状态并改动代码

首先我们来用第二种方式实现上一版本号中的刷新界面,在Crazy贪吃蛇(1)中我们自己定义了一个线程每隔1s钟刷新界面,在线程中我们使用了postInvalidate()方法通知主线程重绘界面,我们打开View的源代码看看究竟是怎样通知主线程的,原代码例如以下:
   public void postInvalidate(int left, int top, int right, int bottom) {
        postInvalidateDelayed(0, left, top, right, bottom);
    }

    /**
     * Cause an invalidate to happen on a subsequent cycle through the event
     * loop. Waits for the specified amount of time.
     *
     * @param delayMilliseconds the duration in milliseconds to delay the
     *         invalidation by
     */
    public void postInvalidateDelayed(long delayMilliseconds) {
        // We try only with the AttachInfo because there‘s no point in invalidating
        // if we are not attached to our window
        if (mAttachInfo != null) {
            Message msg = Message.obtain();
            msg.what = AttachInfo.INVALIDATE_MSG;
            msg.obj = this;
            mAttachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
        }
    }

    /**
     * Cause an invalidate of the specified area to happen on a subsequent cycle
     * through the event loop. Waits for the specified amount of time.
     *
     * @param delayMilliseconds the duration in milliseconds to delay the
     *         invalidation by
     * @param left The left coordinate of the rectangle to invalidate.
     * @param top The top coordinate of the rectangle to invalidate.
     * @param right The right coordinate of the rectangle to invalidate.
     * @param bottom The bottom coordinate of the rectangle to invalidate.
     */
    public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
            int right, int bottom) {

        // We try only with the AttachInfo because there‘s no point in invalidating
        // if we are not attached to our window
        if (mAttachInfo != null) {
            final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
            info.target = this;
            info.left = left;
            info.top = top;
            info.right = right;
            info.bottom = bottom;

            final Message msg = Message.obtain();
            msg.what = AttachInfo.INVALIDATE_RECT_MSG;
            msg.obj = info;
            mAttachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
        }
    }
从上面源代码中我们能够看到最后一句代码mAttachInfo.mHandler.sendMessageDelayed(msg, delayMillisecods),原来也是通过Handler来实现界面刷新的。既然是这样我们就将我们的代码改动例如以下:
创建一个RefreshHandler类
    class RefreshHandler extends Handler{
    	@Override
    	public void handleMessage(Message msg) {
    		MySnake.this.update();
    		MySnake.this.invalidate();
    		
    	}
    	
    	public void sleep(long delayMillis) {
			this.removeMessages(0);
			sendMessageDelayed(obtainMessage(0), delayMillis);
		}
    }
定义了游戏中的四种状态
    private enum State{
    	READY,    //就绪
    	PAUSE,    //暂停
    	RUNNING,  //执行
    	LOSE      //失败
    }
    private void update(){
    	if(currentState == State.RUNNING){
			move();
			mRefreshHandler.sleep(1000);
    	}
    }
我们再来看看上个版本号中使蛇移动的核心代码:
        case LEFT:  
            /*for(int i=0; i<boxs.size(); i++){  
                box = boxs.get(i);  
                box.setX(box.getX() - boxSize);  
            } */
            boxs.add(0, new Box(boxs.get(0).getX() - boxSize, 0));
            boxs.remove(boxs.size() - 1);
            break;  
        case RIGHT:  
              
           /* for(int i=0; i<boxs.size(); i++){  
                box = boxs.get(i);  
                box.setX(box.getX() + boxSize);  
            }  */
            boxs.add(new Box(boxs.get(boxs.size() - 1).getX() + boxSize, 0));
            boxs.remove(0);
            break; 
我们不用遍历每个方块来实现蛇的移动,我们仅仅须要去改变蛇首和蛇未就可以实现。


改动后的MySnake.java文件
package com.example.crazysnake;  
  
import java.util.ArrayList;  
import java.util.List;  
  
import android.content.Context;  
import android.graphics.Canvas;  
import android.graphics.Color;  
import android.graphics.Paint;  
import android.graphics.RectF;  
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;  
import android.view.MotionEvent;  
import android.view.View;  
/**
 * CSDN博客:http://blog.csdn.net/dawanganban
 * @author 阳光小强
 */
public class MySnake extends View {  
    private Paint paint;  
    private RectF rect;  
      
    private int boxSize = 30;  
      
   // private SnakeThread snakeThread;  
      
    private List<Box> boxs = new ArrayList<Box>(); 
      
    private static final int[] colors = {  
        Color.RED,  
        Color.BLUE,   
        Color.GREEN,  
        Color.YELLOW  
    };  
      
    private enum Derectory{  
        LEFT,  
        RIGHT,  
        TOP,  
        BOTTOM;  
    }  
    
    private enum State{
    	READY,    //就绪
    	PAUSE,    //暂停
    	RUNNING,  //执行
    	LOSE      //失败
    }
      
    private Derectory currentDerect = Derectory.RIGHT;  
    private State currentState = State.PAUSE;
    
    private RefreshHandler mRefreshHandler = new RefreshHandler();
    class RefreshHandler extends Handler{
    	@Override
    	public void handleMessage(Message msg) {
    		MySnake.this.update();
    		MySnake.this.invalidate();
    		
    	}
    	
    	public void sleep(long delayMillis) {
			this.removeMessages(0);
			sendMessageDelayed(obtainMessage(0), delayMillis);
		}
    }
    
    public MySnake(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        paint = new Paint();  
        rect = new RectF();  
        initData();  
        //startThread();  
    }  
      
/*    public void startThread(){  
        if(snakeThread == null){  
            snakeThread = new SnakeThread();  
            snakeThread.start();  
        }  
    } */ 
    
    private void update(){
    	if(currentState == State.RUNNING){
			move();
			mRefreshHandler.sleep(1000);
    	}
    }
      
    private void initData(){  
        Box box;  
        for(int i=0; i<10; i++){  
            box = new Box(i*boxSize, 0);  
            boxs.add(box);  
        }  
    }  
      
    private float mDownX;  
    private float mDownY;  
    @Override  
    public boolean onTouchEvent(MotionEvent event) {
        System.out.println("onTouch");  
        switch (event.getAction()) {  
        case MotionEvent.ACTION_DOWN:  
            mDownX = event.getX();  
            mDownY = event.getY();  
            break;  
        case MotionEvent.ACTION_UP:  
            float disX = event.getX() - mDownX;  
            float disY = event.getY() - mDownY;  
            System.out.println("disX = " + disX);  
            System.out.println("dixY = " + disY);  
            if(Math.abs(disX) > Math.abs(disY)){  
                if(disX > 0){  
                	if(currentState != State.RUNNING){
                		currentState = State.RUNNING;
                		update();
                	}
                    currentDerect = Derectory.RIGHT;  
                }else{  
                    currentDerect = Derectory.LEFT;  
                }  
            }else{  
                if(disY > 0){  
                    currentDerect = Derectory.BOTTOM;  
                }else{  
                    currentDerect = Derectory.TOP;  
                }  
            }  
            break;  
        }  
        return true;  
    }  
      
/*    private class SnakeThread extends Thread{  
        private boolean stoped = false;  
        @Override  
        public void run() {  
            while(!stoped){  
                try {  
                    Thread.sleep(1000);  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
                move();  
                postInvalidate();  
            }  
        }  
    }  */
      
    private void move(){  
        Box box;  
        //推断边界条件
        if(boxs.get(0).getX() - boxSize < 0) {  
            currentDerect = Derectory.RIGHT;  
        }  
        if(boxs.get(boxs.size() - 1).getX() + 2 * boxSize > getWidth()){  
            currentDerect = Derectory.LEFT;  
        }  
        switch (currentDerect) {  
        case LEFT:  
            /*for(int i=0; i<boxs.size(); i++){  
                box = boxs.get(i);  
                box.setX(box.getX() - boxSize);  
            } */
            boxs.add(0, new Box(boxs.get(0).getX() - boxSize, 0));
            boxs.remove(boxs.size() - 1);
            break;  
        case RIGHT:  
              
           /* for(int i=0; i<boxs.size(); i++){  
                box = boxs.get(i);  
                box.setX(box.getX() + boxSize);  
            }  */
            boxs.add(new Box(boxs.get(boxs.size() - 1).getX() + boxSize, 0));
            boxs.remove(0);
            break;  
        case TOP:  
              
            break;  
        case BOTTOM:  
              
            break;  
        }  
    }  
  
    @Override  
    protected void onDraw(Canvas canvas) {  
        super.onDraw(canvas);  
        for(int i=0; i<boxs.size(); i++){  
            paint.setColor(colors[i % colors.length]);  
            rect.set(boxs.get(i).getX(), boxs.get(i).getY(), boxs.get(i).getX() + boxSize, boxSize);  
            canvas.drawRect(rect, paint);  
        }  
    }  
}

二、实现绕手机边界移动的贪吃蛇

先看看实现的效果:
技术分享
实现代码例如以下:
package com.example.crazysnake;  
  
import java.util.ArrayList;  
import java.util.List;  
  
import android.content.Context;  
import android.graphics.Canvas;  
import android.graphics.Color;  
import android.graphics.Paint;  
import android.graphics.RectF;  
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;  
import android.view.MotionEvent;  
import android.view.View;  
/**
 * CSDN博客:http://blog.csdn.net/dawanganban
 * @author 阳光小强
 */
public class MySnake extends View {  
    private Paint paint; 
    private Paint textPaint;
    private RectF rect;  
      
    private static int boxSize = 40;  
    
	private static int xMaxBoxCount;  //x轴方向最多的box数量
	private static int yMaxBoxCount;  //y轴方向最多的box数量
      
    private List<Box> boxs = new ArrayList<Box>(); 
      
    private static final int[] colors = {  
        Color.RED,  
        Color.BLUE,   
        Color.GRAY,  
        Color.YELLOW  
    };  
      
    private enum Derectory{  
        LEFT,  
        RIGHT,  
        TOP,  
        BOTTOM;  
    }  
    
    private enum State{
    	READY,    //就绪
    	PAUSE,    //暂停
    	RUNNING,  //执行
    	LOSE      //失败
    }
      
    private Derectory currentDerect = Derectory.LEFT;  
    private State currentState = State.READY;
    
    private RefreshHandler mRefreshHandler = new RefreshHandler();
    class RefreshHandler extends Handler{
    	@Override
    	public void handleMessage(Message msg) {
    		MySnake.this.update();
    		MySnake.this.invalidate();
    		
    	}
    	
    	public void sleep(long delayMillis) {
			this.removeMessages(0);
			sendMessageDelayed(obtainMessage(0), delayMillis);
		}
    }
    
    public MySnake(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        paint = new Paint(); 
        textPaint = new Paint();
        textPaint.setColor(Color.RED);
        textPaint.setTextSize(80);
        rect = new RectF();  
        initData();   
    }  

    private void update(){
    	if(currentState == State.RUNNING){
			move();
			mRefreshHandler.sleep(150);
    	}
    }
      
    private void initData(){  
        Box box;  
        for(int i=5; i<10; i++){  
            box = new Box(i, 0);  
            boxs.add(box);  
        }  
    }  
    
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    	super.onSizeChanged(w, h, oldw, oldh);
    	xMaxBoxCount = (int) Math.floor(w / boxSize);
		yMaxBoxCount = (int) Math.floor(h / boxSize);
    }
      
    private float mDownX;  
    private float mDownY;  
    @Override  
    public boolean onTouchEvent(MotionEvent event) {
        System.out.println("onTouch");  
        switch (event.getAction()) {  
        case MotionEvent.ACTION_DOWN:  
            mDownX = event.getX();  
            mDownY = event.getY();  
            break;  
        case MotionEvent.ACTION_UP:  
            float disX = event.getX() - mDownX;  
            float disY = event.getY() - mDownY;  
            System.out.println("disX = " + disX);  
            System.out.println("dixY = " + disY);  
            if(Math.abs(disX) > Math.abs(disY)){  
                if(disX > 0){  
                  //  currentDerect = Derectory.RIGHT;  
                }else{  
                	if(currentState != State.RUNNING){
                		currentState = State.RUNNING;
                		currentDerect = Derectory.LEFT;  
                		update();
                	}
                   
                }  
            }else{  
                if(disY > 0){  
                  //  currentDerect = Derectory.BOTTOM;  
                }else{  
                  //  currentDerect = Derectory.TOP;  
                }  
            }  
            break;  
        }  
        return true;  
    }  
    
    private void move(){  
        Box box;  
        if(currentDerect == Derectory.LEFT && boxs.get(0).getX() <= 0){
        	currentDerect = Derectory.BOTTOM;
        }
        if(currentDerect == Derectory.BOTTOM && boxs.get(0).getY() >= yMaxBoxCount -1){
        	currentDerect = Derectory.RIGHT;
        }
        if(currentDerect == Derectory.RIGHT && boxs.get(0).getX() >= xMaxBoxCount - 1){
        	currentDerect = Derectory.TOP;
        }
        if(currentDerect == Derectory.TOP && boxs.get(0).getY() <= 0){
        	currentDerect = Derectory.LEFT;
        }
        switch (currentDerect) {
        case LEFT:  
            boxs.add(0, new Box(boxs.get(0).getX() - 1, boxs.get(0).getY()));
            boxs.remove(boxs.size() - 1);
            break;  
        case RIGHT:  
        	 boxs.add(0, new Box(boxs.get(0).getX() + 1, boxs.get(0).getY()));
             boxs.remove(boxs.size() - 1);
            break;  
        case TOP:  
        	boxs.add(0, new Box(boxs.get(0).getX(), boxs.get(0).getY() - 1));
            boxs.remove(boxs.size() - 1);  
            break;  
        case BOTTOM:  
        	boxs.add(0, new Box(boxs.get(0).getX(), boxs.get(0).getY() + 1));
            boxs.remove(boxs.size() - 1);
            break;  
        }  
    }  
  
    @Override  
    protected void onDraw(Canvas canvas) {  
        super.onDraw(canvas);  
        for(int i=0; i<boxs.size(); i++){  
            paint.setColor(colors[i % colors.length]);  
            rect.set(boxs.get(i).getX() * boxSize, boxs.get(i).getY() * boxSize, 
            		(boxs.get(i).getX() + 1) * boxSize, (boxs.get(i).getY() + 1) * boxSize);  
            canvas.drawRect(rect, paint);  
        } 
        if(currentState == State.READY){
        	canvas.drawText("请向左滑动", (xMaxBoxCount * boxSize - textPaint.measureText("请向左滑动")) / 2,
        			xMaxBoxCount * boxSize / 2, textPaint);
        }
    }  
}

源代码下载说明:前一个版本号在GitHub上,这一版我将该项目上传到了CSDN的CODE上面,能够使用SVN或Git下载
CODE源代码下载地址:https://code.csdn.net/lxq_xsyu/crazysnake
CSDN下载地址:http://download.csdn.net/detail/lxq_xsyu/7629435

以上是关于是男人就下100层第四层——Crazy贪吃蛇的主要内容,如果未能解决你的问题,请参考以下文章

C语言项目实战:《是男人就下100层》零基础项目,160行源代码示例

100行Python代码,轻松完成贪吃蛇小游戏?

《结对-贪吃蛇游戏-开发环境搭建过程》

100行Python代码实现贪吃蛇小游戏

100行代码,使用 Pygame 制作一个贪吃蛇小游戏!

100行python代码,轻松完成贪吃蛇小游戏