这个游戏的逻辑主要在上升和下降以及触碰原理
图片:没有做开始游戏 暂停游戏 和结束游戏,但是主要逻辑全部都做出来了
思路,那个类创建的数据那个类管理
链式模拟Android 的Handler做出来的
d代码如下:
//存入梯子数据
public void saveInitData(LadderData ladderData) {
if (mLadderData == null) {
mLadderData = ladderData;
//mLadderData.setIndex(mLadderData.getIndex() == 0 ? 0 : -1);
} else {
LadderData ladderData1 = new LadderData();
ladderData1.setLadderX(ladderData.getLadderX());
ladderData1.setLadderY(ladderData.getLadderY());
ladderData1.setmLadderData(mLadderData);
mLadderData = ladderData1;
}
initIndex();
}
这块逻辑是类似于一个个引用在一块,Handler的代码逻辑:
Message next() {
// Return here if the message loop has already quit and been disposed.
// This can happen if the application tries to restart a looper after quit
// which is not supported.
final long ptr = mPtr;
if (ptr == 0) {
return null;
}
int pendingIdleHandlerCount = -1; // -1 only during first iteration
int nextPollTimeoutMillis = 0;
for (;;) {
if (nextPollTimeoutMillis != 0) {
Binder.flushPendingCommands();
}
nativePollOnce(ptr, nextPollTimeoutMillis);
synchronized (this) {
// Try to retrieve the next message. Return if found.
final long now = SystemClock.uptimeMillis();
Message prevMsg = null;
Message msg = mMessages;
if (msg != null && msg.target == null) {
// Stalled by a barrier. Find the next asynchronous message in the queue.
do {
//-------------------------------------------------------------------------------------------------------------------------
prevMsg = msg;
//这块↓↓
msg = msg.next;
} while (msg != null && !msg.isAsynchronous());
}
if (msg != null) {
if (now < msg.when) {
// Next message is not ready. Set a timeout to wake up when it is ready.
nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);
} else {
// Got a message.
mBlocked = false;
if (prevMsg != null) {
prevMsg.next = msg.next;
} else {
mMessages = msg.next;
}
msg.next = null;
if (DEBUG) Log.v(TAG, "Returning message: " + msg);
msg.markInUse();
return msg;
}
} else {
// No more messages.
nextPollTimeoutMillis = -1;
}
// Process the quit message now that all pending messages have been handled.
if (mQuitting) {
dispose();
return null;
}
// If first time idle, then get the number of idlers to run.
// Idle handles only run if the queue is empty or if the first message
// in the queue (possibly a barrier) is due to be handled in the future.
if (pendingIdleHandlerCount < 0
&& (mMessages == null || now < mMessages.when)) {
pendingIdleHandlerCount = mIdleHandlers.size();
}
if (pendingIdleHandlerCount <= 0) {
// No idle handlers to run. Loop and wait some more.
mBlocked = true;
continue;
}
if (mPendingIdleHandlers == null) {
mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)];
}
mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers);
}
// Run the idle handlers.
// We only ever reach this code block during the first iteration.
for (int i = 0; i < pendingIdleHandlerCount; i++) {
final IdleHandler idler = mPendingIdleHandlers[i];
mPendingIdleHandlers[i] = null; // release the reference to the handler
boolean keep = false;
try {
keep = idler.queueIdle();
} catch (Throwable t) {
Log.wtf(TAG, "IdleHandler threw exception", t);
}
if (!keep) {
synchronized (this) {
mIdleHandlers.remove(idler);
}
}
}
// Reset the idle handler count to 0 so we do not run them again.
pendingIdleHandlerCount = 0;
// While calling an idle handler, a new message could have been delivered
// so go back and look again for a pending message without waiting.
nextPollTimeoutMillis = 0;
}
}
根据这个原理,它官方这个就是一个套一个
这个是重力加速度,复习了一下初中知识啊啊啊啊。。。。。。。
/**
* 返回抛物位置
*
* @return
*/
private double getParabolic(long time) {
return mV * (time * 0.001) - (0.5 * g * ((time * 0.001) * (time * 0.001)));
}
/**
* 返回重力坐标
*
* @param time
* @return
*/
private double gravityM(long time) {
return 0.5 * g * kg * ((time * 0.001) * (time * 0.001));
}
主要控制阶梯往上移在这块:
//开始弹跳
private void startJumpUp() {
final int[] x = {0};
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
x[0]++;
if (arrayListladder != null && arrayListladder.size() > 0) {
for (int i = 0; i < arrayListladder.size(); i++) {
arrayListladder.get(i).setLadderY(arrayListladder.get(i).getLadderY() + x[0]);
}
}
if (x[0] > 40) {
break;
}
}
}
}).start();
}
这块是链式最RAO的一段,可能网络上的大神一下就看明白了,哈哈哈
//删除最后一个
private void deleteLast() {
if (mLadderData == null) {
return;
} else {
LadderData temp = mLadderData;
LadderData temp2 = null;
if (mLadderData.getmLadderData() == null) {
mLadderData = null;
}
while (true) {
if (temp == null) {
if (temp2 != null) {
temp2.setmLadderData(null);
} else {
mLadderData = null;
}
break;
}
if (temp != null && temp.getmLadderData() != null)
temp2 = temp;
temp = temp.getmLadderData();
}
// showData();
initIndex();
}
}
碰撞的检测功能类
public void collision(ArrayList<LadderTempData> arrayList, int mLocationY, int x, GravityThread gravityThread, Context mContext) {
garr.clear();
for (int i = 0; i < arrayList.size(); i++) {
LadderTempData ladderTempData = arrayList.get(i);
int ladderY = ladderTempData.getLadderY();
if (ladderY > 0) {
garr.add(ladderTempData);
}
if (arrayList.size() < 5) {
Ladder.temp.addAll(arrayList);
arrayList.clear();
Ladder.getInstance().initData(mContext);
return;
}
if (arrayList.get(i).getLadderY() > mContext.getResources().getDisplayMetrics().heightPixels) {
LadderTempData remove = arrayList.remove(i);
i--;
}
}
for (int i = 0; i < garr.size(); i++) {
LadderTempData ladderTempData = garr.get(i);
int ladderY = ladderTempData.getLadderY();
int ladderX = ladderTempData.getLadderX();
Log.e("坐标 X", "x:" + x + " ladderX:" + ladderX + " stopladderX:" + (ladderX + 300));
Log.e("坐标 Y", "t:" + (ladderY + 5) + " b:" + (ladderY - 5) + " mLocationY:" + mLocationY);
if (mLocationY < (ladderY + 50) && mLocationY > (ladderY - 50) && x > (ladderX - 40) && x < (ladderX + 300)) {
gravityThread.height = ladderY + 20;
Log.e("坐标", "--------------------------");
break;
} else {
gravityThread.initX();
}
//ladderY 底部坐标
//ladderY -40 顶部坐标
//ladderX 左部坐标
// ladderX + 300右部坐标
/* int t = ladderY - 50;
int b = ladderY;
int l = ladderX;
int r = ladderX + 300;
int pY = mLocationY;
int pX = x;
Log.e("坐标", "top: " + (ladderY - 50) + " b:" + b + " mLocationY:" + mLocationY);
if (pY < b && pY > t && pX > l && pY < r) {
gravityThread.height = ladderY + 20;
break;
} else {
gravityThread.initX();
}
*/
}
}
类的主要说明
类说明:
XHApplication : 主要的Context
Accelerometer: 方向重力控制类
ControlTheCollision : 碰撞控制类
Gravity: 重力控制类
GravityThread : 重力控制线程
Ladder:梯子数据制造者
GameMainView:绘画