package lab.Memory;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class GameView extends SurfaceView implements SurfaceHolder.Callback
{
public static final int DRAW_THREAD=0;
public static final int MODIFY_THREAD=1;
public static final int EVENT_THREAD=2;
public static final int GAME_PAUSED=2;
public static final int GAME_IN=1;
public static final int GAME_OVER=0;
public static final Rect rcArea=new Rect(20,15,300,355);
private static final Rect rcMenu=new Rect(40,160,280,320);
private static final Rect rcBack=new Rect(28,355,65,390);
private static final Rect rcPause=new Rect(260,364,291,394);
private static final Rect rcMusic=new Rect(140,362,188,392);
private static final Point ptLevel=new Point(116,416);
private static final Point ptGoal=new Point(248,416);
private static final Point ptBall=new Point(116,433);
private static final Point ptDone=new Point(248,433);
private static final Point [] ptLifebar={new Point(60,452),new Point(206,452),new Point(60,467),new Point(206,467)};
private static final int [] colorArray={Color.rgb(255,0,0),Color.rgb(235,108,20),Color.rgb(255,255,0), Color.rgb(0,255,0),
Color.rgb(0,255,255),Color.rgb(0,0,255),Color.rgb(128,30,180),Color.rgb(0,0,0)};
private Memory activity;
public ArrayList<Ball> balls_array; //Array of balls in game
public ArrayList<Tools> tools_array; //Array of tools in game
private GameDrawThread drawThread; //Thread of drawing
private GameDataHandleThread dataThread; //Thread of data modification
private Timer mTimer; //Timer used to create tools in game
private TimerTask mTimerTask;
private boolean music=false; //Music allowed or not
private int[] preBallNumArray={0,2,3,4,5};
private int[] customBallNumArray={0,5,6,7,8};
private float[] percentageArray={0.0f,0.3f,0.4f,0.5f,0.6f};
private int [][] preBallsPosition={{160,140,180,120,160,200,100,140,180,220},
{160,200,200,240,240,240,280,280,280,280}};
private int level=1; //Game level
private int preDefinedBallNum=2; //Number of existed balls
private int customBallNumber=5; //Balls can create in current level
public float percentage=0.2f; //Target percentage of current level
public float percentageCompleted=0.0f; //Percentage completed of current level
public int threads_flag=DRAW_THREAD; //Used for synchronization between two threads
public int DataModifyCount=0; //Count the times of data modification before one draw operation
public int gameStatus=GAME_IN; //State of game
public boolean createBall=false; //Set when one ball is creating
public Ball oncreateBall; //Handle to the new born ball
public long startTime; //Start time of the new born ball, used to decide its radius
public long elapseTime=0; //Equals currentTime-startTime
public long startPauseTime; //The time when game is paused
public long pauseTime=0; //How long the paused state last, used to modify balls' life value
private int oldPositionX; //Used to decide the initial newBallspeedX
private int oldPositionY; //Used to decide the initial newBallspeedY
private double newBallspeedX=0.0;
private double newBallspeedY=0.0;
SensorManager sensorManager;
Sensor sensor; //Direction sensor handle
float orientation=0.0f; //Angle of direction changed of phone
public Bitmap image_ball;
public Bitmap image_over;
public Bitmap image_status;
public Bitmap image_bkg;
public Bitmap image_tools;
SensorEventListener sensorListener = new SensorEventListener(){
public void onSensorChanged(SensorEvent event) {
orientation = event.values[0]-orientation;
Log.i("Orentation : ",""+orientation);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
};
private class ToolTimerTask extends TimerTask{
public void run()
{
if((int)(Math.random()*(10-level))!=0) //High level means more tools
return;
if(tools_array.size()>=Tools.MAX_TOOL_NUM)
return;
Tools newTool=new Tools(level); //Create a new tool and add to tools_array
tools_array.add(newTool);
for(int i=0,j=newTool.iconPosi;i<Tools.MAX_TOOL_NUM;i++,j++) //Assign icon position to new tool
{
int k;
for(k=0;k<tools_array.size();k++)
if(j%Tools.POSI_NUM==tools_array.get(k).iconPosi)
break;
if(k==tools_array.size())
newTool.iconPosi=j%Tools.POSI_NUM;
}
}
}
public GameView(Context context,Memory activity){
super(context);
this.activity=activity;
drawThread=new GameDrawThread(getHolder(), this);
dataThread=new GameDataHandleThread(this);
mTimer = new Timer(true);
mTimerTask = new ToolTimerTask();
getHolder().addCallback(this);
balls_array=new ArrayList<Ball>();
tools_array=new ArrayList<Tools>();
balls_array.add(new Ball(160,160,8.0,6.0,5,Ball.COLOR_BLACK,Ball.PRE_DEFINED)); //Initial balls in game
balls_array.add(new Ball(140,200,8.0,-8.0,5,Ball.COLOR_BLACK,Ball.PRE_DEFINED));
sensorManager = (SensorManager)activity.getSystemService(Context.SENSOR_SERVICE);
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
boolean reg = sensorManager.registerListener(sensorListener, sensor,SensorManager.SENSOR_DELAY_NORMAL);
if(reg)
{
Log.e("Sensor Registe : ","Sensor registe failure in GameView !");
activity.mHandler.sendEmptyMessage(Memory.EXIT); //Register sensor fail and exit game
}
InitImage(); //Load image
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
StartGame();
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0)
{
sensorManager.unregisterListener(sensorListener, sensor);
mTimerTask.cancel();
mTimer.cancel();
boolean retry = true;
drawThread.setFlag(false); //End the drawThread
dataThread.setFlag(false); //End the dataThread
while (retry) { //Wait for threads to complete their tasks
try {
drawThread.join();
dataThread.join();
retry = false;
}
catch (InterruptedException e) {
}
}
}
@Override
public void onDraw(Canvas canvas)
{
if(gameStatus==GAME_OVER) //Game is over and show corresponding image
{
canvas.drawBitmap(image_over,160-image_over.getWidth()/2,180-image_over.getHeight()/2,null);
updateState(
cdprogram
- 粉丝: 1
- 资源: 1
最新资源
- 05-【培训签到】01-人力资源新员工培训签到表.xlsx
- 03-【培训流程】02-新员工入职培训流程.xlsx
- 08-【考核管理】08-企业新员工培训成绩统计表.xlsx
- 08-【考核管理】12-员工培训考核统计表.xlsx
- 10-【管理系统】01-人力资源员工培训管理系统.xlsx
- 10-【管理系统】02-员工培训管理系统 (2).xlsx
- 11-【其他】02-员工培训出勤统计表.xlsx
- 10-【管理系统】02-员工培训管理系统.xlsx
- 10-【管理系统】03-员工培训管理系统.xlsx
- 11-【其他】17-新员工入职培训管理流程相关表单(总部级).xlsx
- 11-【其他】05-新员工入职培训表.xlsx
- 12-【培训PPT】-01-新员工入职培训.ppt
- 12-【培训PPT】-28-新员工质量培训PPT.ppt
- 12-【培训PPT】-07-员工关系培训PPT.ppt
- 12-【培训PPT】-31-新员工安全生产培训PPT课件.ppt
- 电子电路202321111215.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
前往页