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(
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Memory.rar (48个子文件)
Memory
.project 842B
default.properties 383B
bin
Memory.apk 1.46MB
lab
Memory
R$string.class 403B
R$layout.class 370B
GameDataHandleThread.class 3KB
R$drawable.class 595B
MenuView$TutorialThread.class 2KB
HelpView.class 3KB
R.class 477B
Memory.class 2KB
R$raw.class 392B
GameView$ToolTimerTask.class 1KB
R$attr.class 316B
MenuView.class 3KB
GameView.class 14KB
GameView$1.class 1KB
Ball.class 5KB
Tools.class 1KB
GameDrawThread.class 2KB
Memory$1.class 844B
resources.ap_ 1.44MB
classes.dex 28KB
AndroidManifest.xml 736B
src
lab
Memory
Memory.java 2KB
Tools.java 1KB
GameDrawThread.java 1KB
HelpView.java 2KB
GameDataHandleThread.java 4KB
GameView.java 17KB
MenuView.java 4KB
Ball.java 6KB
res
values
strings.xml 163B
layout
main.xml 382B
raw
gamesound.mp3 626KB
go.mp3 1KB
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable-mdpi
game_tools.png 27KB
help1.png 159KB
game_over.png 67KB
game_balls.png 83KB
game_bkg.png 803KB
help2.png 204KB
menu.png 633KB
icon.png 3KB
.classpath 280B
gen
lab
Memory
R.java 1KB
assets
共 48 条
- 1
cdprogram
- 粉丝: 1
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页