diff --git a/app/src/main/java/com/ispring/gameplane/game/GameView.java b/app/src/main/java/com/ispring/gameplane/game/GameView.java index fad701e..f222f14 100644 --- a/app/src/main/java/com/ispring/gameplane/game/GameView.java +++ b/app/src/main/java/com/ispring/gameplane/game/GameView.java @@ -1,3 +1,13 @@ +/* + * This is a simple Android game - CombatAircraft + * This file is the main file of the game. + * It initialize almost all items that needed to display on the screen + * + * @Package com.ispring.gameplane.game + * @author iSpring + * @author ReactNativeX + * @link https://github.com/iSpring/GamePlane + */ package com.ispring.gameplane.game; import android.content.Context; @@ -27,46 +37,95 @@ public class GameView extends View { private CombatAircraft combatAircraft = null; private List sprites = new ArrayList(); private List spritesNeedAdded = new ArrayList(); - //0:combatAircraft - //1:explosion - //2:yellowBullet - //3:blueBullet - //4:smallEnemyPlane - //5:middleEnemyPlane - //6:bigEnemyPlane - //7:bombAward - //8:bulletAward - //9:pause1 - //10:pause2 - //11:bomb + /* + * Arraylist: List + * The arraylist stores all the *.png file that needed to use in the game + * There are 11 numbers, each represent one file. + * 0:combatAircraft + * 1:explosion + * 2:yellowBullet + * 3:blueBullet + * 4:smallEnemyPlane + * 5:middleEnemyPlane + * 6:bigEnemyPlane + * 7:bombAward + * 8:bulletAward + * 9:pause1 + * 10:pause2 + * 11:bomb + */ private List bitmaps = new ArrayList(); - private float density = getResources().getDisplayMetrics().density;//屏幕密度 - public static final int STATUS_GAME_STARTED = 1;//游戏开始 - public static final int STATUS_GAME_PAUSED = 2;//游戏暂停 - public static final int STATUS_GAME_OVER = 3;//游戏结束 - public static final int STATUS_GAME_DESTROYED = 4;//游戏销毁 - private int status = STATUS_GAME_DESTROYED;//初始为销毁状态 - private long frame = 0;//总共绘制的帧数 - private long score = 0;//总得分 - private float fontSize = 12;//默认的字体大小,用于绘制左上角的文本 - private float fontSize2 = 20;//用于在Game Over的时候绘制Dialog中的文本 - private float borderSize = 2;//Game Over的Dialog的边框 - private Rect continueRect = new Rect();//"继续"、"重新开始"按钮的Rect - - //触摸事件相关的变量 - private static final int TOUCH_MOVE = 1;//移动 - private static final int TOUCH_SINGLE_CLICK = 2;//单击 - private static final int TOUCH_DOUBLE_CLICK = 3;//双击 - //一次单击事件由DOWN和UP两个事件合成,假设从down到up间隔小于200毫秒,我们就认为发生了一次单击事件 + /* + * Float variable: density + * Variable to represent density of screen + */ + private float density = getResources().getDisplayMetrics().density; + /* + * Int varaible: STATUS_GAME_STARTED + * STATUS_GAME_PAUSED + * STATUS_GAME_OVER + * STATUS_GAME_DESTROYED + * 4 integers variable to represent 4 status of the game. + * 1 represents game is started + * 2 represents game is paused + * 3 represents game is over + * 4 represents game if not started yet + */ + public static final int STATUS_GAME_STARTED = 1; + public static final int STATUS_GAME_PAUSED = 2; + public static final int STATUS_GAME_OVER = 3; + public static final int STATUS_GAME_DESTROYED = 4; + private int status = STATUS_GAME_DESTROYED; + /* + * Long variable: frame + * score + * Frame is the total frame that needed to draw + * Score is the total score + */ + private long frame = 0; + private long score = 0; + private float fontSize = 12;//Default font size + /* + * Float variable: fontSize2 + * This size is used for the dialog displayed at the end of the game + */ + private float fontSize2 = 20; + private float borderSize = 2;//Default font size for dialog + /* + * Rect variable: continueRect + * The button rectangle for "Restart" and "Continue" + */ + private Rect continueRect = new Rect(); + + /* + * Variables of Touching events + * + * TOUCH_MOVE: Movements + * TOUCH_SINGLE_CLICK: Single Click on screen + * TOUCH_DOUBLE_CLICK: Double Click on screen + */ + private static final int TOUCH_MOVE = 1; + private static final int TOUCH_SINGLE_CLICK = 2; + private static final int TOUCH_DOUBLE_CLICK = 3; + /* + * Variables of differentiate single click and double cilck + * + * See Also: + * Function: resolveTouchType + */ private static final int singleClickDurationTime = 200; - //一次双击事件由两个点击事件合成,两个单击事件之间小于300毫秒,我们就认为发生了一次双击事件 private static final int doubleClickDurationTime = 300; - private long lastSingleClickTime = -1;//上次发生单击的时刻 - private long touchDownTime = -1;//触点按下的时刻 - private long touchUpTime = -1;//触点弹起的时刻 - private float touchX = -1;//触点的x坐标 - private float touchY = -1;//触点的y坐标 - + private long lastSingleClickTime = -1;//The time of last single click + private long touchDownTime = -1;//the time of touching down + private long touchUpTime = -1;//the time of touching up + private float touchX = -1;//X coordinate of touching position + private float touchY = -1;//Y coordiante of touching position + + /* + * Constructor: GameView + * Initializes the game + * @overload + */ public GameView(Context context) { super(context); init(null, 0); @@ -82,14 +141,20 @@ public GameView(Context context, AttributeSet attrs, int defStyle) { init(attrs, defStyle); } + /* + * Function: init + * Initilzes paint tool and font size + * + * @param ArrributeSet Android variable defined in XML + * @param int Android variable defined in XML + * @return None + */ private void init(AttributeSet attrs, int defStyle) { final TypedArray a = getContext().obtainStyledAttributes( attrs, R.styleable.GameView, defStyle, 0); a.recycle(); - //初始化paint paint = new Paint(); paint.setStyle(Paint.Style.FILL); - //设置textPaint,设置为抗锯齿,且是粗体 textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.FAKE_BOLD_TEXT_FLAG); textPaint.setColor(0xff000000); fontSize = textPaint.getTextSize(); @@ -98,7 +163,14 @@ private void init(AttributeSet attrs, int defStyle) { textPaint.setTextSize(fontSize); borderSize *= density; } - + + /* + * Function: start + * Starts the game + * + * @param int[] The ID of picture that will be used in this game. + * @return None + */ public void start(int[] bitmapIds){ destroy(); for(int bitmapId : bitmapIds){ @@ -108,39 +180,76 @@ public void start(int[] bitmapIds){ startWhenBitmapsReady(); } + /* + * Function: startWhenBitmapsReady + * Set the status of the game as started + * + * @param None + * @Return None + */ private void startWhenBitmapsReady(){ combatAircraft = new CombatAircraft(bitmaps.get(0)); - //将游戏设置为开始状态 status = STATUS_GAME_STARTED; - postInvalidate(); + postInvalidate();//Redraw function built in Android } + /* + * Function: restart + * Destroy previous data and set the status of the game as started + * + * @param None + * @Return None + */ private void restart(){ destroyNotRecyleBitmaps(); startWhenBitmapsReady(); } + /* + * Function: pause + * Set the status of the game as paused + * + * @param None + * @Return None + */ public void pause(){ - //将游戏设置为暂停状态 status = STATUS_GAME_PAUSED; } + /* + * Function: resume + * Set the status of the game as started + * + * @param None + * @Return None + */ private void resume(){ - //将游戏设置为运行状态 status = STATUS_GAME_STARTED; postInvalidate(); } + /* + * Funtion: getScore + * Return the score that player earned + * + * @param None + * @return long Long variable of score + */ private long getScore(){ - //获取游戏得分 return score; } /*-------------------------------draw-------------------------------------*/ + /* + * Function: onDraw + * Draw the status of the game + * + * @param Canvas Paint tool built in Android + * @return None + */ @Override protected void onDraw(Canvas canvas) { - //我们在每一帧都检测是否满足延迟触发单击事件的条件 if(isSingleClick()){ onSingleClick(touchX, touchY); } @@ -156,70 +265,76 @@ protected void onDraw(Canvas canvas) { } } - //绘制运行状态的游戏 + /* + * Function: drawGameStarted + * Draw all the items as the game is started + * + * @param Canvas Paint tool built in Android + * @return None + */ private void drawGameStarted(Canvas canvas){ drawScoreAndBombs(canvas); - //第一次绘制时,将战斗机移到Canvas最下方,在水平方向的中心 + /* + * At the begining of the game, put the aircraft to the bottome and mif of the screen + */ if(frame == 0){ float centerX = canvas.getWidth() / 2; float centerY = canvas.getHeight() - combatAircraft.getHeight() / 2; combatAircraft.centerTo(centerX, centerY); } - //将spritesNeedAdded添加到sprites中 + //Create enemy aircraft if(spritesNeedAdded.size() > 0){ sprites.addAll(spritesNeedAdded); spritesNeedAdded.clear(); } - //检查战斗机跑到子弹前面的情况 + //A fuction about bullets. See detail in the defination of this function. destroyBulletsFrontOfCombatAircraft(); - //在绘制之前先移除掉已经被destroyed的Sprite + //A function about enemy aircraft. See detail in the defination of this function. removeDestroyedSprites(); - //每隔30帧随机添加Sprite + //Create enemy aircraft every 30 seconds if(frame % 30 == 0){ createRandomSprites(canvas.getWidth()); } frame++; - //遍历sprites,绘制敌机、子弹、奖励、爆炸效果 + //Check status of Sprites. Sprites include enemy aircraft, bullet and bomb Iterator iterator = sprites.iterator(); while (iterator.hasNext()){ Sprite s = iterator.next(); - if(!s.isDestroyed()){ - //在Sprite的draw方法内有可能会调用destroy方法 s.draw(canvas, paint, this); } - - //我们此处要判断Sprite在执行了draw方法后是否被destroy掉了 + //Check whether enemy is destroyed if(s.isDestroyed()){ - //如果Sprite被销毁了,那么从Sprites中将其移除 iterator.remove(); } } - + //Draw the palyer aircraft if(combatAircraft != null){ - //最后绘制战斗机 combatAircraft.draw(canvas, paint, this); if(combatAircraft.isDestroyed()){ - //如果战斗机被击中销毁了,那么游戏结束 + //If player is attacked, game is over status = STATUS_GAME_OVER; } - //通过调用postInvalidate()方法使得View持续渲染,实现动态效果 postInvalidate(); } } - //绘制暂停状态的游戏 + /* + * Function: drawGamePaused + * Draw all the items that will dipaly if the game is paused + * + * @param Paint tool built in Android + * @return None + */ private void drawGamePaused(Canvas canvas){ drawScoreAndBombs(canvas); - - //调用Sprite的onDraw方法,而非draw方法,这样就能渲染静态的Sprite,而不让Sprite改变位置 for(Sprite s : sprites){ s.onDraw(canvas, paint, this); } @@ -227,44 +342,45 @@ private void drawGamePaused(Canvas canvas){ combatAircraft.onDraw(canvas, paint, this); } - //绘制Dialog,显示得分 - drawScoreDialog(canvas, "继续"); + //Draw dialog of scores + drawScoreDialog(canvas, "缁х画"); if(lastSingleClickTime > 0){ postInvalidate(); } } - //绘制结束状态的游戏 + /* + * Function: drawGameOver + * Draw all the items that will dispaly if game is over + * + * @param Canvas Paint tool built in Android + * @return None + */ private void drawGameOver(Canvas canvas){ - //Game Over之后只绘制弹出窗显示最终得分 - drawScoreDialog(canvas, "重新开始"); + drawScoreDialog(canvas, "閲嶆柊寮�濮�"); if(lastSingleClickTime > 0){ postInvalidate(); } } + /* + * Function: drawScoreDialog + * Draw the dialog to show score + * + * @param Canvas Paint tool built in Android + * @param string + * @return None + */ private void drawScoreDialog(Canvas canvas, String operation){ int canvasWidth = canvas.getWidth(); int canvasHeight = canvas.getHeight(); - //存储原始值 float originalFontSize = textPaint.getTextSize(); Paint.Align originalFontAlign = textPaint.getTextAlign(); int originalColor = paint.getColor(); Paint.Style originalStyle = paint.getStyle(); - /* - W = 360 - w1 = 20 - w2 = 320 - buttonWidth = 140 - buttonHeight = 42 - H = 558 - h1 = 150 - h2 = 60 - h3 = 124 - h4 = 76 - */ + int w1 = (int)(20.0 / 360.0 * canvasWidth); int w2 = canvasWidth - 2 * w1; int buttonWidth = (int)(140.0 / 360.0 * canvasWidth); @@ -276,39 +392,34 @@ private void drawScoreDialog(Canvas canvas, String operation){ int buttonHeight = (int)(42.0 / 558.0 * canvasHeight); canvas.translate(w1, h1); - //绘制背景色 + //background color is white paint.setStyle(Paint.Style.FILL); paint.setColor(0xFFD7DDDE); Rect rect1 = new Rect(0, 0, w2, canvasHeight - 2 * h1); canvas.drawRect(rect1, paint); - //绘制边框 + //draw dialog paint.setStyle(Paint.Style.STROKE); paint.setColor(0xFF515151); paint.setStrokeWidth(borderSize); - //paint.setStrokeCap(Paint.Cap.ROUND); paint.setStrokeJoin(Paint.Join.ROUND); canvas.drawRect(rect1, paint); - //绘制文本"飞机大战分数" textPaint.setTextSize(fontSize2); textPaint.setTextAlign(Paint.Align.CENTER); - canvas.drawText("飞机大战分数", w2 / 2, (h2 - fontSize2) / 2 + fontSize2, textPaint); - //绘制"飞机大战分数"下面的横线 + canvas.drawText("椋炴満澶ф垬鍒嗘暟", w2 / 2, (h2 - fontSize2) / 2 + fontSize2, textPaint); + //draw score canvas.translate(0, h2); canvas.drawLine(0, 0, w2, 0, paint); - //绘制实际的分数 String allScore = String.valueOf(getScore()); canvas.drawText(allScore, w2 / 2, (h3 - fontSize2) / 2 + fontSize2, textPaint); - //绘制分数下面的横线 canvas.translate(0, h3); canvas.drawLine(0, 0, w2, 0, paint); - //绘制按钮边框 + //draw dialog rectangle Rect rect2 = new Rect(); rect2.left = (w2 - buttonWidth) / 2; rect2.right = w2 - rect2.left; rect2.top = (h4 - buttonHeight) / 2; rect2.bottom = h4 - rect2.top; canvas.drawRect(rect2, paint); - //绘制文本"继续"或"重新开始" canvas.translate(0, rect2.top); canvas.drawText(operation, w2 / 2, (buttonHeight - fontSize2) / 2 + fontSize2, textPaint); continueRect = new Rect(rect2); @@ -317,35 +428,41 @@ private void drawScoreDialog(Canvas canvas, String operation){ continueRect.top = h1 + h2 + h3 + rect2.top; continueRect.bottom = continueRect.top + buttonHeight; - //重置 + //reset textPaint.setTextSize(originalFontSize); textPaint.setTextAlign(originalFontAlign); paint.setColor(originalColor); paint.setStyle(originalStyle); } - //绘制左上角的得分和左下角炸弹的数量 + /* + * Function: drawScoreAndBombs + * Draw score on top left corner. Draw the number of bomb on bottom left corner + * + * @param Canvas Paint tool built in Android + * @return None + */ private void drawScoreAndBombs(Canvas canvas){ - //绘制左上角的暂停按钮 + //Draw the pause button with picture 9 Bitmap pauseBitmap = status == STATUS_GAME_STARTED ? bitmaps.get(9) : bitmaps.get(10); RectF pauseBitmapDstRecF = getPauseBitmapDstRecF(); float pauseLeft = pauseBitmapDstRecF.left; float pauseTop = pauseBitmapDstRecF.top; canvas.drawBitmap(pauseBitmap, pauseLeft, pauseTop, paint); - //绘制左上角的总得分数 + //draw score dialog float scoreLeft = pauseLeft + pauseBitmap.getWidth() + 20 * density; float scoreTop = fontSize + pauseTop + pauseBitmap.getHeight() / 2 - fontSize / 2; canvas.drawText(score + "", scoreLeft, scoreTop, textPaint); - //绘制左下角 + //draw bomb if(combatAircraft != null && !combatAircraft.isDestroyed()){ int bombCount = combatAircraft.getBombCount(); if(bombCount > 0){ - //绘制左下角的炸弹 + //bomb icon Bitmap bombBitmap = bitmaps.get(11); float bombTop = canvas.getHeight() - bombBitmap.getHeight(); canvas.drawBitmap(bombBitmap, 0, bombTop, paint); - //绘制左下角的炸弹数量 + //the number of bomb float bombCountLeft = bombBitmap.getWidth() + 10 * density; float bombCountTop = fontSize + bombTop + bombBitmap.getHeight() / 2 - fontSize / 2; canvas.drawText("X " + bombCount, bombCountLeft, bombCountTop, textPaint); @@ -353,13 +470,20 @@ private void drawScoreAndBombs(Canvas canvas){ } } - //检查战斗机跑到子弹前面的情况 + /* + * Function destroyBulletsFrontOfCombatAircraft + * Check the position of bullet by coordinates. + * If bullet is on the front of aircraft, delete those bullets. + * If player move the aircraft too fast, there will be much bullets are on the front of aircraft which is never happen in reality + * + * @param None + * @return None + */ private void destroyBulletsFrontOfCombatAircraft(){ if(combatAircraft != null){ float aircraftY = combatAircraft.getY(); List aliveBullets = getAliveBullets(); for(Bullet bullet : aliveBullets){ - //如果战斗机跑到了子弹前面,那么就销毁子弹 if(aircraftY <= bullet.getY()){ bullet.destroy(); } @@ -367,7 +491,13 @@ private void destroyBulletsFrontOfCombatAircraft(){ } } - //移除掉已经destroyed的Sprite + /* + * Function: removeDestroyedSprites + * Remove destroyed Enemy aircraft, bullets and bombs + * + * @param None + * @return None + */ private void removeDestroyedSprites(){ Iterator iterator = sprites.iterator(); while (iterator.hasNext()){ @@ -378,38 +508,48 @@ private void removeDestroyedSprites(){ } } - //生成随机的Sprite + /* + * Function: createRandomSprites + * Create random Enemy aircraft + * Create random bomb + * Create double bullets + * + * @param None + * @return None + */ private void createRandomSprites(int canvasWidth){ Sprite sprite = null; int speed = 2; - //callTime表示createRandomSprites方法被调用的次数 int callTime = Math.round(frame / 30); if((callTime + 1) % 25 == 0){ - //发送道具奖品 + //create bomb if((callTime + 1) % 50 == 0){ - //发送炸弹 sprite = new BombAward(bitmaps.get(7)); } else{ - //发送双子弹 + //create double bullet sprite = new BulletAward(bitmaps.get(8)); } } else{ - //发送敌机 + /* + * Create enemy aircraft + * In this game, there are 3 types of enemy + * Enemy is defined in class "Sprite" + */ int[] nums = {0,0,0,0,0,1,0,0,1,0,0,0,0,1,1,1,1,1,1,2}; int index = (int)Math.floor(nums.length*Math.random()); int type = nums[index]; if(type == 0){ - //小敌机 + //samll enemy sprite = new SmallEnemyPlane(bitmaps.get(4)); } else if(type == 1){ - //中敌机 + //medium enemy sprite = new MiddleEnemyPlane(bitmaps.get(5)); } else if(type == 2){ - //大敌机 + //big enemy sprite = new BigEnemyPlane(bitmaps.get(6)); } if(type != 2){ @@ -436,11 +576,16 @@ else if(type == 2){ /*-------------------------------touch------------------------------------*/ + /* + * Function: onTouchEvent + * Define what event will happen as player click screen + * Inlcudes both single click and double click + * + * @param MotionEvent Event variable + * @return bool true if palyer never touch the screen + */ @Override public boolean onTouchEvent(MotionEvent event){ - //通过调用resolveTouchType方法,得到我们想要的事件类型 - //需要注意的是resolveTouchType方法不会返回TOUCH_SINGLE_CLICK类型 - //我们会在onDraw方法每次执行的时候,都会调用isSingleClick方法检测是否触发了单击事件 int touchType = resolveTouchType(event); if(status == STATUS_GAME_STARTED){ if(touchType == TOUCH_MOVE){ @@ -450,7 +595,7 @@ public boolean onTouchEvent(MotionEvent event){ }else if(touchType == TOUCH_DOUBLE_CLICK){ if(status == STATUS_GAME_STARTED){ if(combatAircraft != null){ - //双击会使得战斗机使用炸弹 + //Double click to use bomb combatAircraft.bomb(this); } } @@ -467,7 +612,13 @@ public boolean onTouchEvent(MotionEvent event){ return true; } - //合成我们想要的事件类型 + /* + * Function: resolveTouchType + * Define single click and double click + * + * @param MotionEvent Event variable + * @return int return touchtype + */ private int resolveTouchType(MotionEvent event){ int touchType = -1; int action = event.getAction(); @@ -476,36 +627,31 @@ private int resolveTouchType(MotionEvent event){ if(action == MotionEvent.ACTION_MOVE){ long deltaTime = System.currentTimeMillis() - touchDownTime; if(deltaTime > singleClickDurationTime){ - //触点移动 touchType = TOUCH_MOVE; } }else if(action == MotionEvent.ACTION_DOWN){ - //触点按下 + //record the time that player touching the screen touchDownTime = System.currentTimeMillis(); }else if(action == MotionEvent.ACTION_UP){ - //触点弹起 + //record the time player's finger leave scrren touchUpTime = System.currentTimeMillis(); - //计算触点按下到触点弹起之间的时间差 + //count the delta time of touching down and up long downUpDurationTime = touchUpTime - touchDownTime; - //如果此次触点按下和抬起之间的时间差小于一次单击事件指定的时间差, - //那么我们就认为发生了一次单击 + /* + * if the delta time is less than 200ms, we define it as single click + * if the delta time of two single click is less than 300ms, we define it as double click + */ if(downUpDurationTime <= singleClickDurationTime){ - //计算这次单击距离上次单击的时间差 + //record the delta time of two single click long twoClickDurationTime = touchUpTime - lastSingleClickTime; if(twoClickDurationTime <= doubleClickDurationTime){ - //如果两次单击的时间差小于一次双击事件执行的时间差, - //那么我们就认为发生了一次双击事件 touchType = TOUCH_DOUBLE_CLICK; - //重置变量 + //reset data for next clicking lastSingleClickTime = -1; touchDownTime = -1; touchUpTime = -1; }else{ - //如果这次形成了单击事件,但是没有形成双击事件,那么我们暂不触发此次形成的单击事件 - //我们应该在doubleClickDurationTime毫秒后看一下有没有再次形成第二个单击事件 - //如果那时形成了第二个单击事件,那么我们就与此次的单击事件合成一次双击事件 - //否则在doubleClickDurationTime毫秒后触发此次的单击事件 lastSingleClickTime = touchUpTime; } } @@ -513,19 +659,26 @@ private int resolveTouchType(MotionEvent event){ return touchType; } - //在onDraw方法中调用该方法,在每一帧都检查是不是发生了单击事件 + /* + * Function: isSingleClick + * Check the clicking is single click or not + * + * @param None + * @return bool true if the click is single click + */ private boolean isSingleClick(){ boolean singleClick = false; - //我们检查一下是不是上次的单击事件在经过了doubleClickDurationTime毫秒后满足触发单击事件的条件 if(lastSingleClickTime > 0){ - //计算当前时刻距离上次发生单击事件的时间差 + //count the delta time from last single click to now long deltaTime = System.currentTimeMillis() - lastSingleClickTime; if(deltaTime >= doubleClickDurationTime){ - //如果时间差超过了一次双击事件所需要的时间差, - //那么就在此刻延迟触发之前本该发生的单击事件 + /* + * if the delta time greater than the time needed for double click + * we define it as a single click + */ singleClick = true; - //重置变量 + //reset data for next clicking event lastSingleClickTime = -1; touchDownTime = -1; touchUpTime = -1; @@ -534,41 +687,79 @@ private boolean isSingleClick(){ return singleClick; } + /* + * Function: onSingleClick + * Define special single click + * if the coordiante of single is in the pause/continue/restart button, + * pause/continue.restart the game + * + * @param float X coordiante of this single click + * @param float Y coordiante of this single click + * @return None + */ private void onSingleClick(float x, float y){ if(status == STATUS_GAME_STARTED){ if(isClickPause(x, y)){ - //单击了暂停按钮 + //pause button is clicked pause(); } }else if(status == STATUS_GAME_PAUSED){ if(isClickContinueButton(x, y)){ - //单击了“继续”按钮 + //continue button is clicked resume(); } }else if(status == STATUS_GAME_OVER){ if(isClickRestartButton(x, y)){ - //单击了“重新开始”按钮 + //restart button is clicked restart(); } } } - //是否单击了左上角的暂停按钮 + /* + * Function: isClickPause + * Check whether pause button is clicked + * + * @param float X coordiante of this single click + * @param float Y coordiante of this single click + * @return bool true if coordination of single slick is in the range of pause button + */ private boolean isClickPause(float x, float y){ RectF pauseRecF = getPauseBitmapDstRecF(); return pauseRecF.contains(x, y); } - //是否单击了暂停状态下的“继续”那妞 + /* + * Function: isClickContinueButton + * Check whether pause button is clicked + * + * @param float X coordiante of this single click + * @param float Y coordiante of this single click + * @return bool true if coordination of single slick is in the range of continue button + */ private boolean isClickContinueButton(float x, float y){ return continueRect.contains((int)x, (int)y); } - //是否单击了GAME OVER状态下的“重新开始”按钮 + /* + * Function: isClickRestartButton + * Check whether pause button is clicked + * + * @param float X coordiante of this single click + * @param float Y coordiante of this single click + * @return bool true if coordination of single slick is in the range of restart button + */ private boolean isClickRestartButton(float x, float y){ return continueRect.contains((int)x, (int)y); } + /* + * Function: getPauseBitmapDstRecF + * Set the range of pause buuton + * + * @param None + * @return None + */ private RectF getPauseBitmapDstRecF(){ Bitmap pauseBitmap = status == STATUS_GAME_STARTED ? bitmaps.get(9) : bitmaps.get(10); RectF recF = new RectF(); @@ -580,34 +771,34 @@ private RectF getPauseBitmapDstRecF(){ } /*-------------------------------destroy------------------------------------*/ - + /* + * Function: destroyNotRecyleBitmaps + * clean all data and pictures if game is over or restart + */ private void destroyNotRecyleBitmaps(){ - //将游戏设置为销毁状态 status = STATUS_GAME_DESTROYED; - - //重置frame frame = 0; - - //重置得分 score = 0; - - //销毁战斗机 + //destroy aircraft if(combatAircraft != null){ combatAircraft.destroy(); } combatAircraft = null; - //销毁敌机、子弹、奖励、爆炸 + //destroy enemy,bullet,bomb for(Sprite s : sprites){ s.destroy(); } sprites.clear(); } + /* + * Function: destroy + * Release resources + */ public void destroy(){ destroyNotRecyleBitmaps(); - //释放Bitmap资源 for(Bitmap bitmap : bitmaps){ bitmap.recycle(); } @@ -616,12 +807,24 @@ public void destroy(){ /*-------------------------------public methods-----------------------------------*/ - //向Sprites中添加Sprite + /* + * Function: addSprite + * Add Sprite to Sprite class + * + * @param None + * @return None + */ public void addSprite(Sprite sprite){ spritesNeedAdded.add(sprite); } - //添加得分 + /* + * Function: addScore + * Add score that will display on the screen + * + * @param int the number of score palyer earned + * @return None + */ public void addScore(int value){ score += value; } @@ -646,7 +849,13 @@ public Bitmap getExplosionBitmap(){ return bitmaps.get(1); } - //获取处于活动状态的敌机 + /* + * Function: getAliveEnemyPlanes + * get the data of alive enemy plane + * + * @param None + * @return List return enemy plane list + */ public List getAliveEnemyPlanes(){ List enemyPlanes = new ArrayList(); for(Sprite s : sprites){ @@ -658,7 +867,13 @@ public List getAliveEnemyPlanes(){ return enemyPlanes; } - //获得处于活动状态的炸弹奖励 + /* + * Function: getAliveBombAwards + * get the data of alive bomb + * + * @param None + * @return List return bomb list + */ public List getAliveBombAwards(){ List bombAwards = new ArrayList(); for(Sprite s : sprites){ @@ -670,7 +885,15 @@ public List getAliveBombAwards(){ return bombAwards; } - //获取处于活动状态的子弹奖励 + /* + * Function: getAliveBulletAwards + * get the data of alive bullet award + * There will be random bullet award in this gmae + * if player touch it, bullet will upgrade to double bullet + * + * @param None + * @return List return bullet award list + */ public List getAliveBulletAwards(){ List bulletAwards = new ArrayList(); for(Sprite s : sprites){ @@ -682,7 +905,13 @@ public List getAliveBulletAwards(){ return bulletAwards; } - //获取处于活动状态的子弹 + /* + * Function: getAliveBulletAwards + * get the data of alive bullet + * + * @param None + * @return List return bullet list + */ public List getAliveBullets(){ List bullets = new ArrayList(); for(Sprite s : sprites){