Android制作贪吃蛇游戏

 网络112 梁海亮

打开eclipse新建项目TCS。项目结构:

Android制作贪吃蛇游戏_第1张图片

然后开始编写java代码:

TCSActivity.java代码:

package lianghailiang.f;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;

public class TCSActivity extends Activity implements OnClickListener {
	gameView game;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        game=new gameView(this);
        setContentView(game);
        game.setOnClickListener(this);
    }
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		game.s.ZengZhang();
	}
	public boolean onKeyDown(int keyCode, KeyEvent event){
		switch(keyCode)
		{
		case KeyEvent.KEYCODE_DPAD_UP:
			game.s.GaiBianFangXiang(1);
			return true;
		case KeyEvent.KEYCODE_DPAD_DOWN:
			game.s.GaiBianFangXiang(2);
			return true;
		case KeyEvent.KEYCODE_DPAD_LEFT:
			game.s.GaiBianFangXiang(3);
			return true;
		case KeyEvent.KEYCODE_DPAD_RIGHT:
			game.s.GaiBianFangXiang(4);
			return true;
		}
		return true;
	}
}

gameView.java代码:

package lianghailiang.f;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.view.View;

public class gameView extends View implements Runnable {
	Bitmap bmp;
	Thread thread;
	public She s;
	
	public gameView(Context context){
		super(context);
		bmp=Bitmap.createBitmap(600,400,Config.ARGB_8888);
		s=new She(30,30,3,6);
		thread=new Thread(this);
		thread.start();
	}

	public void run() {
		// TODO Auto-generated method stub
		while(thread!=null)
		{
			this.postInvalidate();
			try{Thread.sleep(300);}
			catch(InterruptedException e)
			{
				e.printStackTrace();
			}
		}
	}
	protected void onDraw(Canvas canvas){
		super.onDraw(canvas);
		s.YiDong();
		s.hua(bmp);
		canvas.drawBitmap(bmp, 0, 0, null);
	}
	
}

Kuai.java:

package lianghailiang.f;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;

public class Kuai {
	public int hang, lie;
	public int BianChang;
	public int YanSe;
	public void Hua(Bitmap bmp)
	{
		Canvas c=new Canvas(bmp);
		Rect rc=new Rect(lie*BianChang, hang*BianChang,(lie+1)*BianChang,(hang+1)*BianChang);
		Paint paint = new Paint();
		paint.setColor(YanSe);
		paint.setStyle(Style.STROKE);
		paint.setStrokeWidth(2);
		c.drawRect(rc, paint);
	}
}


She.java:

package lianghailiang.f;

import java.util.LinkedList;
import java.util.List;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;

public class She {
	private List k;
	private int fangXiang;
	private int BianChang;
	
	public She(int hang,int lie,int fangxiang,int BianChang)
	{
		this.fangXiang=fangxiang;
		this.BianChang=BianChang;
		k=new LinkedList();
		Kuai tou=new Kuai();
		tou.hang=hang;
		tou.lie=lie;
		k.add(tou);
		Kuai shen=new Kuai();
		shen.hang=tou.hang;
		shen.lie=tou.lie+1;
		k.add(shen);
	}
	public void hua(Bitmap bmp)
	{
		Canvas c=new Canvas(bmp);
		c.drawColor(Color.BLACK);
		for(int i=0;i=1;i--)
		{
			k.get(i).hang=k.get(i-1).hang;
			k.get(i).lie=k.get(i-1).lie;
		}
		if(fangXiang==1)
			k.get(0).hang--;
		else if(fangXiang==2)
			k.get(0).hang++;
		else if(fangXiang==3)
			k.get(0).lie--;
		else
			k.get(0).lie++;
	}
	public void GaiBianFangXiang(int fx)
	{
		if(fangXiang==2&&fx==1)
			return;
		if(fangXiang==1&&fx==2)
			return;
		if(fangXiang==3&&fx==4)
			return;
		if(fangXiang==4&&fx==3)
			return;
		fangXiang=fx;
		
	}
}


运行结果:Android制作贪吃蛇游戏_第2张图片

鼠标点击虚拟机屏幕蛇体变长,点击虚拟机方向键改变蛇体移动方向。


 

你可能感兴趣的:(Android制作贪吃蛇游戏)