android贪吃蛇(超级简陋版)
public class body {
public int ax;//代表X周变量
public int ay;//代表Y轴变量
public int getAx() {
return ax;
}
public void setAx(int ax) {
this.ax = ax;
}
public int getAy() {
return ay;
}
public void setAy(int ay) {
this.ay = ay;
}
public body(int a, int b){//构造函数
this.ax = a;
this.ay = b;
}
}
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory; public class GetStone {//获取图片
public static Bitmap createBitmap(Context context,int resId){
Bitmap map = BitmapFactory.decodeResource(context.getResources(),resId);
return map;
}
}
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager; public class MainActivity extends Activity { private rungame game;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//全屏显示
super.onCreate(savedInstanceState);
int width =getWindowManager().getDefaultDisplay().getWidth();//获得屏幕宽度
int height = getWindowManager().getDefaultDisplay().getHeight();//获得屏幕高度
game = new rungame(this,width,height);
setContentView(game);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
import java.util.ArrayList;
import java.util.Random;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;
import android.view.View;
import android.view.View.OnTouchListener; public class rungame extends SurfaceView implements Callback,OnTouchListener{ private Context context;
private SurfaceHolder holder;
private int screenw;//传递屏幕宽度
private int screenh;//传递屏幕高度
private static int HH = 0;//获得砖块的高度,设置为全局变量
private static int WW = 0;//获得砖块的宽度,设置为全局变量
// private static int [][] bodystart = {{3,8},{3,7},{3,6},{3,5}};//贪吃蛇初始化出现的位置
private static ArrayList<body> snakeBodies = new ArrayList<body>();//LIST数组的蛇身位置
private static ArrayList<body> star = new ArrayList<body>();//LIST数组的星星位置
private static final int UP = 1;//移动方向向上
private static final int DOWN = 2;//移动方向向下
private static final int LEFT = 3;//移动方向向左
private static final int RIGHT = 4;//移动方向向右
private static int DirectionMove;//当前移动方向,并初始化为向下
private static int NextDirectionMove=DOWN;//当前移动方向,并初始化为向下
private static long StayTime = 600;//绘制停留时间,初始化为600毫秒
private static int Score = 0;//分数,默认为0
public rungame(Context context,int screenwidth,int screenheight) {
super(context);
this.context = context;
this.screenh = screenheight;
this.screenw = screenwidth;
holder = this.getHolder();
holder.addCallback(this);
setOnTouchListener(this);
getWH();//初始化,取得砖块的长宽(游戏以砖墙的长宽为基础)
setArrayList();//初始化蛇身的位置
setstar();//初始化星星的位置
} private void draw(){//绘制初始化
Canvas canvas = holder.lockCanvas();
drawwall(canvas);//绘制墙壁
drawstar(canvas);//绘制星星
drawsnake(canvas);//绘制蛇身
holder.unlockCanvasAndPost(canvas);
}
public void drawstar(Canvas canvas){//随即绘制两个个星星
super.onDraw(canvas);
// Canvas canvas = holder.lockCanvas();
Bitmap mapstar =GetStone.createBitmap(context, R.drawable.star);
// canvas.drawBitmap(mapstar, star.get(0).ax*WW, star.get(0).ay*HH,null);
// canvas.drawBitmap(mapstar, star.get(1).ax*WW, star.get(1).ay*HH,null);
for(int i=0;i<star.size();i++ ){//画出星星
int px = star.get(i).ax;
int py = star.get(i).ay;
canvas.drawBitmap(mapstar, px*WW, py*HH, null);
}
// holder.unlockCanvasAndPost(canvas);
}
private ArrayList<body> setstar(){
int [][] yes1 = randompoint();
int [][] yes2 = randompoint();
Log.i("FFF", "star的坐标:"+yes1[0][0]+" "+yes1[0][1]);
Log.i("FFF", "star的坐标:"+yes2[0][0]+" "+yes2[0][1]);
body boss1 = new body(yes1[0][0], yes1[0][1]);
body boss2 = new body(yes2[0][0], yes2[0][1]);
star.add(boss1);
star.add(boss2);
Log.i("FFF", "star的长度:"+star.size());
return star;
}
public int[][] randompoint(){//在空白处随机产生一个数组,用于产生星星的位置
Random random = new Random();
int b,d;
int a = screenh/HH;
int c = screenw/WW;
ArrayList<body> bossBody = new ArrayList<body>();
do{
d = random.nextInt(c-2)+1;
b = random.nextInt(a-2)+1;
body booss = new body(d, b);
bossBody.add(booss);
}
while(snakeBodies.equals(bossBody));//最初的位置不能跟蛇身的位置重合
int [][] arraystar = {{d,b}};
Log.i("Width", "宽度格数"+c);
Log.i("Height", "高度格数"+a);
return arraystar;
}
public ArrayList<body> setArrayList(){//蛇身的位置
body bos1 = new body(1,8);
body bos2 = new body(1,7);
body bos3 = new body(1,6);
body bos4 = new body(1,5);
snakeBodies.add(bos1);
snakeBodies.add(bos2);
snakeBodies.add(bos3);
snakeBodies.add(bos4);
return snakeBodies;
}
public void drawsnake(Canvas canvas){//初始化一条蛇,由数组控制蛇的样子
int movex,movey;
super.onDraw(canvas);
DirectionMove=NextDirectionMove;//将变初始向设置为变更方向
switch (DirectionMove) {
case UP:
movex = 0;
movey = -1;
break;
case DOWN:
movex = 0;
movey = 1;
break;
case LEFT:
movex = -1;
movey = 0;
break;
default:
movex = 1;
movey = 0;
break;
}
Bitmap mapbody = GetStone.createBitmap(context, R.drawable.body);
body mms = new body(snakeBodies.get(0).ax, snakeBodies.get(0).ay);//获得蛇头的坐标
mms.ax+=movex;//蛇头下一步X轴的坐标
mms.ay+=movey;//蛇头下一步Y轴的坐标
Log.i("SHOWmovex", " "+movex);
Log.i("SHOWmovex", " "+movey);
Log.i("添加前snakeBodiesSIZE", " "+snakeBodies.size());
snakeBodies.add(0,mms);//将该蛇头下一步的坐标插入蛇头的位置,后面蛇身的位置向后移动
Log.i("添加后snakeBodiesSIZE", " "+snakeBodies.size());
if(!eatcheck()){
snakeBodies.remove(snakeBodies.size()-1);//删除蛇的最后
}
for(int i=0;i<snakeBodies.size();i++ ){//画出蛇身
int px = snakeBodies.get(i).ax;
int py = snakeBodies.get(i).ay;
canvas.drawBitmap(mapbody, px*WW, py*HH, null);
}
Log.i("EVEPOINT", "每次蛇头的坐标"+snakeBodies.get(0).ax+" "+snakeBodies.get(0).ay);
try {
Thread.sleep(StayTime);
} catch (Exception e) {
// TODO: handle exception
}
}
private void getWH(){
Bitmap mapcrack =GetStone.createBitmap(context, R.drawable.crack);
WW = mapcrack.getWidth();
HH = mapcrack.getHeight();
}
public void drawwall(Canvas canvas){//绘制墙壁
super.onDraw(canvas);
// Canvas canvas = holder.lockCanvas();
Bitmap mapcrack =GetStone.createBitmap(context, R.drawable.crack);
// WW = mapcrack.getWidth();
// HH = mapcrack.getHeight();
int ssh = screenh/HH;
int ssw = screenw/WW;
canvas.drawColor(Color.WHITE);
for(int i=0;i<=ssh*HH;i+=HH){//绘制墙壁左边
canvas.drawBitmap(mapcrack, 0, i,null);
}
for(int i=0;i<=ssh*HH;i+=HH){//绘制墙壁右边
canvas.drawBitmap(mapcrack, (ssw-1)*WW, i,null);//这里有一点问题
}
for(int i=WW;i<=ssw*WW-WW;i+=WW){//绘制墙壁上边
canvas.drawBitmap(mapcrack, i, 0,null);
}
for(int i=WW;i<=ssw*WW-WW;i+=WW){//绘制墙壁下边
canvas.drawBitmap(mapcrack, i, ssh*HH,null);
}
// holder.unlockCanvasAndPost(canvas);
}
public void drawScore(){//绘制分数
Canvas canvas = holder.lockCanvas();
// canvas.drawColor(Color.WHITE);//注释掉后会直接绘制到游戏界面上
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setTextSize(30);
canvas.drawText("总分数:"+Score, screenw/3,screenh/2, paint);//确定绘制分数位置
holder.unlockCanvasAndPost(canvas);
}
class moveThread implements Runnable{//创建线程
@Override
public void run() {
while(runinto()){
draw();
}
drawScore();
}
}
public boolean runinto(){//碰撞检测
if(snakeBodies.get(0).ax == 0 || snakeBodies.get(0).ax == (screenw/WW-1)
|| snakeBodies.get(0).ay == 0 || snakeBodies.get(0).ay == (screenh/HH-1)){//蛇头装墙壁上
Log.i("RRR", "false1");
return false;
}else if(runbody(snakeBodies.get(0))){//蛇头撞蛇身上
Log.i("RRR", "false2");
return false;
}else {
Log.i("RRR", "true");
return true;
}
}
public boolean runbody(body oop){//蛇头位置相对于蛇身位置的判断
ArrayList<body> fuu = new ArrayList<body>();
// fuu = snakeBodies;//直接赋值有严重错误,操作fuu相当于直接操作snakeBodies
for(int i=0;i<snakeBodies.size();i++){
body puk = new body(snakeBodies.get(i).ax, snakeBodies.get(i).ay);
fuu.add(puk);
}
fuu.remove(0);//获得除蛇头以外的躯干坐标
/* for(int i=0;i<fuu.size();i++){
if(fuu.get(i).equals(oop)){
return true;
}
}*/
/* if(fuu.contains(oop)){
return true;
}
return false;
*/
// return fuu.contains(oop);
int nx = snakeBodies.get(0).ax;
int ny = snakeBodies.get(0).ay;
for(int i=0;i<fuu.size();i++){
if(fuu.get(i).ax==nx&&fuu.get(i).ay==ny){
return true;
}
}
return false;
}
public boolean eatcheck(){//吃到星星检测
/* if(snakeBodies.get(0).equals(star.get(0))){
star.remove(0);
int [][] aa = randompoint();
body ppo = new body(aa[0][0], aa[0][1]);
star.add(ppo);
Score++;
Log.i("EEE","吃到星星"+1);
return true;
}else if (snakeBodies.get(0).equals(star.get(1))) {
star.remove(1);
int [][] aa = randompoint();
body ppo = new body(aa[0][0], aa[0][1]);
star.add(ppo);
Score++;
Log.i("EEE","吃到星星"+2);
return true;
}else {
Log.i("EEE","没有吃到星星");
return false;
}*/
/* if(star.contains(snakeBodies.get(0))){
int amp = star.indexOf(snakeBodies.get(0));//找出与蛇头位置一样的星星序号
Log.i("NUM", "星星序号"+amp);
Log.i("STAR", "星星坐标"+star.get(amp).ax+" "+star.get(amp).ay);
star.remove(amp);
int [][] aa = randompoint();
body ppo = new body(aa[0][0], aa[0][1]);
star.add(ppo);
Score++;
Log.i("EEE","吃到星星"+amp);
return true;
}else {
Log.i("EEE","没有吃到星星");
return false;
}*/
/* for(int i=0;i<star.size();i++){
if(snakeBodies.get(0).equals(star.get(i))){
Log.i("NUM", "星星序号"+i);
Log.i("STAR", "星星坐标"+star.get(i).ax+" "+star.get(i).ay);
star.remove(i);
int [][] aa = randompoint();
body ppo = new body(aa[0][0], aa[0][1]);
star.add(ppo);
Score++;
Log.i("EEE","吃到星星"+i);
return true;
}
}
return false;*/
int nx = snakeBodies.get(0).ax;
int ny = snakeBodies.get(0).ay;
for(int i=0;i<star.size();i++){
if(star.get(i).ax==nx&&star.get(i).ay==ny){
Log.i("NUM", "星星序号"+i);
Log.i("STAR", "星星坐标"+star.get(i).ax+" "+star.get(i).ay);
star.remove(i);
int [][] aa = randompoint();
body ppo = new body(aa[0][0], aa[0][1]);
star.add(ppo);
Score++;
Log.i("EEE","吃到星星"+i);
StayTime *= 0.9;
return true;
}
}
return false;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("BBB","初始方向"+DirectionMove);//初始方向
if(DirectionMove == UP||DirectionMove == DOWN){
if(event.getX()>snakeBodies.get(0).ax*WW){
NextDirectionMove = RIGHT;
}else {
NextDirectionMove = LEFT;
}
}else if(DirectionMove == RIGHT||DirectionMove == LEFT){
if (event.getY()>snakeBodies.get(0).ay*HH) {
NextDirectionMove = DOWN;
}else {
NextDirectionMove = UP;
}
}
Log.i("CCC","变更方向"+NextDirectionMove);//变更方向
return false;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub }
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
new Thread(new moveThread()).start();//启动线程
} @Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub } }
地址:http://pan.baidu.com/share/link?shareid=2604684104&uk=1259703658
android贪吃蛇(超级简陋版)的更多相关文章
- 贪吃蛇(简易版)Leslie5205912著
# include <stdio.h># include <string.h># include <windows.h># include <stdlib.h ...
- 如何用python制作贪吃蛇以及AI版贪吃蛇
用python制作普通贪吃蛇 哈喽,大家不知道是上午好还是中午好还是下午好还是晚上好! 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很 ...
- 人生就像一条加速奔向死亡的贪吃蛇【winform版】
群里聊天的时候,一个学妹说她在做贪吃蛇的小作业,于是昨晚(5.20无聊只好撸代码/(ㄒoㄒ)/~~)花了2个小时撸了一个出来,早上又花了些时间完善功能,就有了这个还算比较完善的版本,当然代码结构比较混 ...
- 【转】Android贪吃蛇源代码
/*TileView:tile有瓦片的意思,用一个个tile拼接起来的就是地图.TileView就是用来呈现地图的类*/ public class TileView extends View { /* ...
- 用最少的JS代码写出贪吃蛇游戏---迷你版
游戏进行页面展示 GAME OVER 页面展示 代码如下: <!doctype html> <html> <body> <canvas id=&q ...
- 贪吃蛇(C语言版)链表实现
贪吃蛇 gitee:贪吃蛇C语言版: Snake 蛇的结构 typedef struct Snake { int x; int y; struct Snake *next; }; 游戏开始欢迎界面 / ...
- C语言 贪吃蛇
贪吃蛇(单人版): 本人先来介绍一个函数 -- bioskey函数: int bioskey (int cmd) 参数 (cmd) 基本功能 0 返回下一个从键盘键入的值(若不键入任何值,则将等下一个 ...
- Android快乐贪吃蛇游戏实战项目开发教程-01项目概述与目录
一.项目简介 贪吃蛇是一个很经典的游戏,也很适合用来学习.本教程将和大家一起做一个Android版的贪吃蛇游戏. 我已经将做好的案例上传到了应用宝,无病毒.无广告,大家可以放心下载下来把玩一下.应用宝 ...
- 原生js写的贪吃蛇网页版游戏特效
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <bo ...
随机推荐
- mysql常用命令(2)
1.连接Mysql 格式: mysql -h主机地址 -u用户名 -p用户密码 1.连接到本机上的MYSQL.首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root ...
- Dynamics AX 2012 R2 堆栈跟踪:不能对客户端调用'unchecked'
有一个Custom Service一直在正常使用.今天,Reinhard尝试在JOB中以X++代码Debug Custom Service的Method时,收到以下错误提示: 'unchecked' ...
- C#:判断软件运行的环境是否是Pad(PC)
一.需求:Pad上显示某功能块,PC机上隐藏. 二.方法:从外围设备获取值判断是否是Pad. 三.具体参考代码如下: 1.外围设备值类型如下: public enum ChassisTypes { O ...
- [问题2014A04] 复旦高等代数 I(14级)每周一题(第六教学周)
[问题2014A04] 设 \(A,B,C,D\) 均为 \(n\) 阶方阵. (1) 若 \(A^2=A\), \(B^2=B\), \((A+B)^2=A+B\), 证明: \(AB=BA=0\ ...
- Windows Store Apps, Error: The certificate specified has expired.(转)
Windows Store Apps, Error: The certificate specified has expired. 0 comments|Posted on October 7th, ...
- WINDOWS Composer & PHPUnit 安装记录
Windows: 安装Composer: 下载了composer_setup.exe 运行之后提示和Xdebug冲突,在php.ini中注释掉php_xdebug.dll,再次运行.通过 compos ...
- centos6服务器YUM安装LNMP(LINUX+NGINX+MYSQL+PHP)
之前都用的lamp,这次配置一个lnmp来看看,试试Nginx是不是好用 关闭SELINUXvi /etc/selinux/config#SELINUX=enforcing #注释掉#SELINUXT ...
- ubuntu14.04 64bit安装teamviewer
1.下载teamviewer,链接如下: http://downloadus2.teamviewer.com/download/version_10x/teamviewer_10.0.36281_i3 ...
- Linux文件管理相关命令
Linux文件管理相关命令 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在了解了Linux文件管理背景知识之后, 我们可以 ...
- Codeforces Round #384 (Div. 2)A,B,C,D
A. Vladik and flights time limit per test 2 seconds memory limit per test 256 megabytes input standa ...