是男人就下100层【第四层】——Crazy贪吃蛇(3)
上一篇《是男人就下100层【第四层】——Crazy贪吃蛇(2)》实现了贪吃蛇绕着屏幕四周移动,这一篇我们来完成贪吃蛇的所有功能。
一、随机产生苹果
private void addAppleBox(){
int randomX = random.nextInt(xMaxBoxCount);
int randomY = random.nextInt(yMaxBoxCount);
for(int i=0; i<boxs.size(); i++){
if(boxs.get(i).getX() == randomX){
addAppleBox();
break;
}
if(boxs.get(i).getY() == randomY){
addAppleBox();
break;
}
appleBox = new Box(randomX, randomY);
}
}
二、判断边界值
//判断是否撞墙了
if(headBox.getX() < 0 || headBox.getY() < 0 ||
headBox.getX() > xMaxBoxCount || headBox.getY() > yMaxBoxCount){
currentState = State.LOSE;
} //判断是否装到自己身上了
for(int i=0; i<boxs.size(); i++){
if(boxs.get(i).getX() == headBox.getX() &&
boxs.get(i).getY() == headBox.getY()){
currentState = State.LOSE;
}
} isGroup = false;
//判断是否吃到苹果了~~
for(int i=0; i<boxs.size(); i++){
if(boxs.get(i).getX() == appleBox.getX() &&
boxs.get(i).getY() == appleBox.getY()){
isGroup = true;
addAppleBox();
}
}
三、全部代码
package com.example.crazysnake; import java.util.ArrayList;
import java.util.List;
import java.util.Random; 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 = 20; private static int xMaxBoxCount; //x轴方向最多的box数量
private static int yMaxBoxCount; //y轴方向最多的box数量 private List<Box> boxs = new ArrayList<Box>(); private Box appleBox;
private Random random; private boolean isGroup = false; 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(60);
rect = new RectF();
random = new Random();
} private void update(){
if(currentState == State.RUNNING){
move();
mRefreshHandler.sleep(150);
}
} private void initData(){
Box box;
for(int i=xMaxBoxCount - 5; i<xMaxBoxCount; i++){
box = new Box(i, 3);
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){
if(currentDerect != Derectory.LEFT){
currentDerect = Derectory.RIGHT;
}
}else{
if(currentState != State.RUNNING){
currentState = State.RUNNING;
currentDerect = Derectory.LEFT;
boxs.clear();
initData();
addAppleBox();
update();
}else if(currentDerect != Derectory.RIGHT){
currentDerect = Derectory.LEFT;
}
}
}else{
if(disY > 0){
if(currentDerect != Derectory.TOP){
currentDerect = Derectory.BOTTOM;
}
}else{
if(currentDerect != Derectory.BOTTOM){
currentDerect = Derectory.TOP;
}
}
}
break;
}
return true;
} private void move(){
Box headBox = new Box(0, 0);
switch (currentDerect) {
case LEFT:
headBox = new Box(boxs.get(0).getX() - 1, boxs.get(0).getY());
break;
case RIGHT:
headBox = new Box(boxs.get(0).getX() + 1, boxs.get(0).getY());
break;
case TOP:
headBox = new Box(boxs.get(0).getX(), boxs.get(0).getY() - 1);
break;
case BOTTOM:
headBox = new Box(boxs.get(0).getX(), boxs.get(0).getY() + 1);
break;
} //判断是否撞墙了
if(headBox.getX() < 0 || headBox.getY() < 0 ||
headBox.getX() > xMaxBoxCount || headBox.getY() > yMaxBoxCount){
currentState = State.LOSE;
} //判断是否装到自己身上了
for(int i=0; i<boxs.size(); i++){
if(boxs.get(i).getX() == headBox.getX() &&
boxs.get(i).getY() == headBox.getY()){
currentState = State.LOSE;
}
} isGroup = false;
//判断是否吃到苹果了~~
for(int i=0; i<boxs.size(); i++){
if(boxs.get(i).getX() == appleBox.getX() &&
boxs.get(i).getY() == appleBox.getY()){
isGroup = true;
addAppleBox();
}
} boxs.add(0, headBox);
if(!isGroup){
boxs.remove(boxs.size() - 1);
}
} private void addAppleBox(){
int randomX = random.nextInt(xMaxBoxCount);
int randomY = random.nextInt(yMaxBoxCount);
for(int i=0; i<boxs.size(); i++){
if(boxs.get(i).getX() == randomX){
addAppleBox();
break;
}
if(boxs.get(i).getY() == randomY){
addAppleBox();
break;
}
appleBox = new Box(randomX, randomY);
}
} @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(appleBox != null){
paint.setColor(Color.RED);
rect.set(appleBox.getX() * boxSize, appleBox.getY() * boxSize,
(appleBox.getX() + 1) * boxSize, (appleBox.getY() + 1) * boxSize);
canvas.drawRect(rect, paint);
} if(currentState == State.READY){
canvas.drawText("请向左滑动", (xMaxBoxCount * boxSize - textPaint.measureText("请向左滑动")) / 2,
xMaxBoxCount * boxSize / 2, textPaint);
} if(currentState == State.LOSE){
canvas.drawText("失败!左滑继续", (xMaxBoxCount * boxSize - textPaint.measureText("失败!左滑继续")) / 2,
(float)xMaxBoxCount * boxSize / 2, textPaint);
canvas.drawText("长度:" + boxs.size() , (xMaxBoxCount * boxSize - textPaint.measureText("长度:" + boxs.size())) / 2,
(float)xMaxBoxCount * boxSize / 4 * 3, textPaint);
}
}
}
CODE代码地址:https://code.csdn.net/lxq_xsyu/crazysnake
是男人就下100层【第四层】——Crazy贪吃蛇(3)的更多相关文章
- 是男人就下100层【第四层】——Crazy贪吃蛇(2)
在上一篇<是男人就下100层[第四层]--Crazy贪吃蛇(1)>中我们让贪吃蛇移动了起来,接下来我们来实现让贪吃蛇能够绕着手机屏幕边线移动而且能够改变方向 一.加入状态并改动代码 首先我 ...
- 超多经典 canvas 实例,动态离子背景、移动炫彩小球、贪吃蛇、坦克大战、是男人就下100层、心形文字等等等
超多经典 canvas 实例 普及:<canvas> 元素用于在网页上绘制图形.这是一个图形容器,您可以控制其每一像素,必须使用脚本来绘制图形. 注意:IE 8 以及更早的版本不支持 &l ...
- Unity经典游戏教程之:是男人就下100层
版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...
- 是男人就下100层【第四层】——Crazy贪吃蛇(1)
贪吃蛇是一款非常经典的游戏,这些经典游戏给我们的童年添加了不少乐趣,今天開始我们来一步一步的在Android设备上实现一款贪吃蛇游戏,我也是第一次写这个游戏,有可能会写错,或者走弯路,可是终于希望能有 ...
- HTML5游戏开发_是男人就下100层
项目流程 市场调研(可行性分析)--> 可行性报告书 需求分析师.客户经理 需求分析--> 需求说明书 项目经理.需求分析师 概要设计--> 概要设计说明书(ER图, UML) ...
- Cocos2d-x 版本小游戏 《是男人就下100层》 项目开源
这个是很久就开始动手写的一个小游戏了,直到最近才把它收尾了,拖拖拉拉的毛病总是很难改啊. 项目是基于 cocos2d-x v2.2 版本 ,目前只编译到了 Win8 平台上,并且已经上传到了商店,支持 ...
- 『HTML5挑战经典』是英雄就下100层-开源讲座(二)危险!英雄
本篇为<『HTML5挑战经典』是英雄就下100层-开源讲座>第二篇,需要用到开源引擎lufylegend,可以到这里下载: 下载地址:http://lufylegend.googlecod ...
- 『HTML5挑战经典』是英雄就下100层-开源讲座(一)从天而降的英雄
是英雄就下100层是一款经典的手机小游戏,以前是在诺基亚手机上十分有名.今天我们就用HTML5和lufylegend一步步地实现它. 一,准备工作 首先,你需要下载lufylegend,下载地址如下: ...
- gdufe1538-是男人就上100层-(三维bfs)
Problem Description: 桐老爷和UGO终于来到了名为“是男人就上一百层的塔”的下面,听说大祭司在第100层沉睡.为了题目需要,我把这个塔的层数随机打乱,层数的话大家就忘了前面的100 ...
随机推荐
- groupmod---更改群组识别码或名称
groupmod命令用于更改群组识别码或名称. 需要更改群组的识别码或名称时,可用groupmod指令来完成这项工作. 语法 groupmod [-g <群组识别码> <-o> ...
- docker部署mysql 实现远程连接
1. docker search mysql # 查看mysql版本 2. docker pull mysql:5.7 # 拉取mysql 5.7 3. docker images # 查 ...
- Nim游戏算法实现
- Java反射之getInterfaces()方法
今天学习Spring3框架,在理解模拟实现Spring Ioc容器的时候遇到了getInterfaces()方法.getInterfaces()方法和Java的反射机制有关.它能够获得这个对象所实现的 ...
- iOS_31_cocos2d_微信飞机
终于效果图: 纹理素材 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHJlX2VtaW5lbnQ=/font/5a6L5L2T/fontsize/400 ...
- php数组时按值传递还是按地址传递
php数组时按值传递还是按地址传递 一.总结 1.数组都是按值:php普通变量和数组的赋值(=)是按值传递,对象的赋值(=)是按址传递 2.对象和按值和按址:对象的clone(用clone关键字)是按 ...
- less中混合
@charset "UTF-8"; //1 普通混合 //2 不带输出的混合:加() .font_hx(){ font-size: 28px; color: red; } h1{ ...
- JS和安卓 IOS的交互 例子式记录
(function () { var u = navigator.userAgent; var isAndroid = u.indexOf('Android') > -1 || u.indexO ...
- 【2017 Multi-University Training Contest - Team 6】Inversion
[链接]h在这里写链接 [题意] 给出一个序列,求2~n每一个数,下标不是这个数倍数的最大值是什么? [题解] 把a数组从大到小排序. 每个位置i,逆序枚举b数组,找到第一个对应下标不是i的倍数的,直 ...
- HDU 1506 Largest Rectangle in a Histogram(DP)
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...