欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

android贪吃蛇代码,android贪吃蛇

来源: 开发者 投稿于  被查看 21266 次 评论:156

android贪吃蛇代码,android贪吃蛇


适用于android平台的贪吃蛇代码

1.[文件] Snake.java~8KB 下载(1592)

/* 
 * Copyright (C) 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xmobileapp.Snake;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
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 implements OnClickListener {

	private final static int PLAY = 1;
	
	private final static int LEFT = 2;
	
	private final static int RIGHT= 3;
	
	private final static int UP= 4;
	
	private final static int DOWN= 5;
	
    private SnakeView mSnakeView;
    
    private static String ICICLE_KEY = "snake-view";

    private Button play;
    
    private ImageButton left;
    
    private ImageButton right;
    
    private ImageButton up;
    
    private ImageButton down;
    
    private UpdateStatus updateStatus;
    
    private Handler handler;
    
    protected static final int GUINOTIFIER = 0x1234;

    /**
     * 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));
        play = (Button)findViewById(R.id.play);
        play.setId(PLAY);
        play.setOnClickListener(this);
        play.setBackgroundColor(Color.argb(0, 0, 255, 0));
        left = (ImageButton)findViewById(R.id.left);
        left.setId(LEFT);
        left.setOnClickListener(this);
        left.setBackgroundColor(Color.argb(1, 1, 255, 1));
        left.setVisibility(View.GONE);
        
        right = (ImageButton)findViewById(R.id.right);
        right.setId(RIGHT);
        right.setOnClickListener(this);
        right.setBackgroundColor(Color.argb(1, 1, 255, 1));
        right.setVisibility(View.GONE);
        
        up = (ImageButton)findViewById(R.id.up);
        up.setId(UP);
        up.setOnClickListener(this);
        up.setBackgroundColor(Color.argb(1, 1, 255, 1));
        up.setVisibility(View.GONE);
        
        down = (ImageButton)findViewById(R.id.down);
        down.setId(DOWN);
        down.setOnClickListener(this);
        down.setBackgroundColor(Color.argb(1, 1, 255, 1));
        down.setVisibility(View.GONE);
        
        if (savedInstanceState == null) {
            // We were just launched -- set up a new game
            mSnakeView.setMode(mSnakeView.READY);
        } else {
            // We are being restored
            Bundle map = savedInstanceState.getBundle(ICICLE_KEY);
            if (map != null) {
                mSnakeView.restoreState(map);
            } else {
                mSnakeView.setMode(SnakeView.PAUSE);
            }
        }
        
        handler = new Handler()
        {
          public void handleMessage(Message msg) 
          {
        
            switch (msg.what)
            { 
              case Snake.GUINOTIFIER:
                       
                play.setVisibility(View.VISIBLE);
                left.setVisibility(View.GONE);
                right.setVisibility(View.GONE);
                up.setVisibility(View.GONE);
                down.setVisibility(View.GONE);
                break; 
            } 
            super.handleMessage(msg); 
          }
        };

        

        
    }

    @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());
    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		
		case PLAY: 
                play.setVisibility(View.GONE);
                left.setVisibility(View.VISIBLE);
                right.setVisibility(View.VISIBLE);
                up.setVisibility(View.VISIBLE);
                down.setVisibility(View.VISIBLE);
            if (mSnakeView.mMode == mSnakeView.READY | mSnakeView.mMode == mSnakeView.LOSE) {
                /*
                 * At the beginning of the game, or the end of a previous one,
                 * we should start a new game.
                 */
                mSnakeView.initNewGame();
                mSnakeView.setMode(mSnakeView.RUNNING);
                mSnakeView.update();
                updateStatus = new UpdateStatus();                
                updateStatus.start();
                break;
            }

            if (mSnakeView.mMode == mSnakeView.PAUSE) {
                /*
                 * If the game is merely paused, we should just continue where
                 * we left off.
                 */
                mSnakeView.setMode(mSnakeView.RUNNING);
                mSnakeView.update();
       
                break;
            }

            if (mSnakeView.mDirection != mSnakeView.SOUTH) {
                mSnakeView.mNextDirection = mSnakeView.NORTH;

                break;
            }
            
			
			break;
			
		case LEFT:	
		
            if (mSnakeView.mDirection != mSnakeView.EAST) {
                mSnakeView.mNextDirection = mSnakeView.WEST;
            }
		    break;
	  	    
		case RIGHT:	
			
            if (mSnakeView.mDirection != mSnakeView.WEST) {
                mSnakeView.mNextDirection = mSnakeView.EAST;
            }
		    break;  
		case UP:	
			
            if (mSnakeView.mDirection != mSnakeView.SOUTH) {
                mSnakeView.mNextDirection = mSnakeView.NORTH;
            }
		    break;      
		  
		case DOWN:	
			
            if (mSnakeView.mDirection != mSnakeView.NORTH) {
                mSnakeView.mNextDirection = mSnakeView.SOUTH;
            }
		    break;    
		    
	    default :
	    	
	    	break;
		    
		}
	}
	

    class UpdateStatus extends Thread{   	
    	@Override
		public void run() {
             
    		
    		super.run();
    		
    		
    
    			
    		  while(true){
    			  
    		   if(mSnakeView.mMode == mSnakeView.LOSE){
    			   
    			   
    			   
    	           Message m = new Message();
    	           m.what = Snake.GUINOTIFIER;
    	           Snake.this.handler.sendMessage(m);

    			   break;
    			   
    		      }	
        
                   
    			try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

    		 }
    	
    	}	
    		
    }
    
}

2.[文件] SnakeView.java~16KB 下载(866)

3.[文件] TileView.java~4KB 下载(672)



package com.xmobileapp.Snake;

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;

import java.util.Map;

/**
 * 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
     * @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);
                }
            }
        }

    }

}

用户评论