Skip to content

Commit

Permalink
Merge pull request #824 from lizicong/master
Browse files Browse the repository at this point in the history
#90 #91 #92 #93 #94 #95 #544 实验4-9
  • Loading branch information
zengsn committed May 25, 2016
2 parents 604a1a6 + bfc2c73 commit 36322e9
Show file tree
Hide file tree
Showing 8 changed files with 524 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,149 @@
package edu.hzuapps.androidworks.homeworks.net1314080903116;

import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

public class Net_1314080903116_MainActivity extends ActionBarActivity {
import edu.hzuapps.androidworks.homeworks.net1314080903116.R;
import edu.hzuapps.androidworks.homeworks.net1314080903116.runnable.MyThread;

public class Net_1314080903116_MainActivity extends Activity implements SurfaceHolder.Callback, View.OnTouchListener {
private SurfaceView mSurfaceView;
private MyThread thread;
private int w, h;

private int gameType = 2;
private int gameSpan;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_net_1314080903116__main);
// setContentView(R.layout.activity_main);
starGameView();
}

private void starGameView() {
mSurfaceView = new SurfaceView(this);
mSurfaceView.getHolder().addCallback(this);
mSurfaceView.setOnTouchListener(this);
setContentView(mSurfaceView);

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
// 创建画布是初始化
// 得到画板 然后画板和线程绑定
w = mSurfaceView.getWidth();// 画板宽
h = mSurfaceView.getHeight();// 画板高
// 得到游戏区域
// 留1/5显示时间
gameSpan = h * 4 / (5 * gameType);
thread = new MyThread(this, holder, w, h, gameType);
// mSurfaceView.
thread.start();
}

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
thread.setStart(false);
}

@Override
public boolean onTouch(View v, MotionEvent event) {
// 点击区域让精灵跳起来
switch (thread.getGameStatu())
{
case MyThread.RUNNING:
confirmRole(event);
break;
case MyThread.STANDOFF:
resStart(event);
break;

default:
break;
}

return true;
}

private void resStart(MotionEvent event) {
int action = event.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
// 第一根手指按下
float y = event.getY();
// 点击了重来
if (y >= h * 0.5 / 5 && y <= h * 1 / 5){
gameType=2;
starGameView();
}else if(y >= h * 1 / 5 && y <= h * 1.5 / 5){
gameType=3;
starGameView();
}

break;

default:
break;
}

}

private void confirmRole(MotionEvent event) {
int action = event.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
// 第一根手指按下
float y = event.getY();
if (thread.isStart())
roleJump(y);
break;
case MotionEvent.ACTION_POINTER_DOWN:
// 其他手指陆续按下
float y2 = event.getY(event.getPointerCount() - 1);
if (thread.isStart())
roleJump(y2);
break;
default:
break;
}
}

/**
* 获取对应区域让对应区域人物跳起来
*
* @param y
*/
private void roleJump(float y) {
// 找到区域
try {
for (int i = 0; i < gameType; i++) {
// 判断在那一个游戏区域
int lineD = h / 10 + (i + 1) * gameSpan;
int lineU = h / 10 + (i) * gameSpan;
if (y >= lineU && y < lineD && !thread.roles[i].isJump()) {
thread.roles[i].setSpeedY(-h / 48);
// 设置跳起状态
thread.roles[i].setJump(true);
break;
}
}
} catch (Exception e) {
// TODO: handle exception
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package edu.hzuapps.androidworks.homeworks.net1314080903116.role;

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

public class Role {
private Bitmap bitmap;// 当前图片
private Bitmap[] bms;// 完成一个动作的图片数组
private float x, y; // 坐标
private int width, height;// 图片的宽高
private int index;// 记录图片下标
private boolean isJump;// 是否跳起来了

private float speedX, speedY;

private long lastTime = 0;

// 创建精灵
public Role(Bitmap[] bms) {
this.bms = bms;
this.bitmap = bms[0];
this.width = bitmap.getWidth();
this.height = bitmap.getHeight();

}

// 画自己
public void drawSelf(Canvas canvas) {

if (System.currentTimeMillis() - lastTime >= 200) {
index++;
if (index == bms.length) {
index = 0;
}
bitmap = bms[index];
lastTime=System.currentTimeMillis();
}
//画自己
canvas.drawBitmap(bitmap, this.getX(), this.getY()-8,null);

}

public boolean isJump() {
return isJump;
}

public void setJump(boolean isJump) {
this.isJump = isJump;
}

public Bitmap getBitmap() {
return bitmap;
}

public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}

public Bitmap[] getBms() {
return bms;
}

public void setBms(Bitmap[] bms) {
this.bms = bms;
}

public float getX() {
return x;
}

public void setX(float x) {
this.x = x;
}

public float getY() {
return y;
}

public void setY(float y) {
this.y = y;
}

public int getWidth() {
return width;
}

public void setWidth(int width) {
this.width = width;
}

public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}

public float getSpeedX() {
return speedX;
}

public void setSpeedX(float speedX) {
this.speedX = speedX;
}

public float getSpeedY() {
return speedY;
}

public void setSpeedY(float speedY) {
this.speedY = speedY;
}

/**
* 获取任务矩形
* @return
*/
public Rect getRectFromRole() {
Rect rect=new Rect();
//根据role本身
rect.left=(int) this.getX()+8;
rect.right=(int) (this.getX()+this.getWidth()-10);
rect.top=(int) this.getY();
rect.bottom=(int) (this.getY()+this.getHeight());

return rect;
}


}
Loading

0 comments on commit 36322e9

Please sign in to comment.