博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
20135208 JAVA第三次实验
阅读量:4950 次
发布时间:2019-06-11

本文共 29543 字,大约阅读时间需要 98 分钟。

课程:Java实验   班级:201352     姓名:贺邦  学号:20135208

成绩:             指导教师:娄佳鹏   实验日期:15.06.03

实验密级:         预习程度:             实验时间:

仪器组次:          必修/选修:选修          实验序号:3

实验名称:     

             

 

一、敏捷开发与XP

 

 

二、编码标准

1.编码标准中的版式就是一个很好的例子,版式虽然不会影响程序的功能,但会影响可读性。程序的版式追求清晰、美观,是程序风格的重要因素。单击Eclipse菜单中的source->Format 或用快捷键Ctrl+Shift+F就可以按Eclipse规定的规范缩进。

2. 代码标准中很重要的一项是如何给包、类、变量、方法等标识符命名,能很好的命名可以让自己的代码立马上升一个档次。Java中的一般的命名规则有:

  • 要体现各自的含义
  • 包、类、变量用名词
  • 方法名用动宾
  • 包名全部小写,如:io,awt
  • 类名第一个字母要大写,如:HelloWorldApp
  • 变量名第一个字母要小写,如:userName
  • 方法名第一个字母要小写:setName

三、重构

重构(Refactor),就是在不改变软件外部行为的基础上,改变软件内部的结构,使其更加易于阅读、易于维护和易于变更 。

 

重构之后的Student Test代码:

 

别踩白块:

齐岳:负责代码的实现(一)

blogs:

codes:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

package com.orange.block;

 

import android.graphics.Color;

import android.graphics.Typeface;

import android.os.Bundle;

 

import com.orange.engine.camera.ZoomCamera;

import com.orange.engine.options.PixelPerfectEngineOptions;

import com.orange.engine.options.PixelPerfectMode;

import com.orange.engine.options.ScreenOrientation;

import com.orange.res.FontRes;

import com.orange.res.RegionRes;

import com.orange.ui.activity.GameActivity;

import com.orange.block.res.Res;

import com.orange.block.scene.GameScene;

import com.orange.block.util.ConstantUtil;

import com.orange.block.util.LogUtil;

 

/**

 * 入口主Activity 类

 * @author lch<OGEngine@orangegame.cn>

 * @

 */

public class MainActivity extends GameActivity {

    @Override

    protected void onCreate(Bundle pSavedInstanceState) {

        super.onCreate(pSavedInstanceState);

 

    }

 

    @Override

    protected PixelPerfectEngineOptions onCreatePixelPerfectEngineOptions() {

        PixelPerfectEngineOptions pixelPerfectEngineOptions = new PixelPerfectEngineOptions(

                this, ZoomCamera.class);

        pixelPerfectEngineOptions

                .setScreenOrientation(ScreenOrientation.PORTRAIT_FIXED); // 设置竖屏

        pixelPerfectEngineOptions

                .setPixelPerfectMode(PixelPerfectMode.CHANGE_HEIGHT);// 适配模式,这里设置为“保持宽度不变,改变高”

        pixelPerfectEngineOptions.setDesiredSize(ConstantUtil.DESIRED_SIZE);// 参考尺寸

 

        return pixelPerfectEngineOptions;

    }

 

    @Override

    protected void onLoadResources() {

        // 加载相关初始的资源等

        LogUtil.d("开始加载资源...");

        RegionRes.loadTexturesFromAssets(Res.ALL_XML);

        FontRes.loadFont(128, 128,

                Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 40, true,

                Color.RED, ConstantUtil.FONT_NAME_TIMER);

         

        FontRes.loadFont(256, 512,

                Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 50, true,

                Color.BLACK, ConstantUtil.FONT_NAME_RESULT);

 

    }

 

    @Override

    protected void onLoadComplete() {

        // 加载资源完成后

        LogUtil.d("加载资源完成...");

        this.startScene(GameScene.class);// 启动游戏场景

    }

     

    @Override

    protected void onPause() {

        super.onPause();

        this.getEngine().stop();

    }

     

    @Override

    protected synchronized void onResume() {

        super.onResume();

        this.getEngine().start();

    }

 

    @Override

    protected void onDestroy() {

        super.onDestroy();

         

        android.os.Process.killProcess(android.os.Process.myPid());

    }

}

  

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

package com.orange.block.util;

 

public class ConstantUtil {

 

    /**屏幕参考尺寸**/

    public static final float DESIRED_SIZE = 480;

     

    /**标示白颜色**/

    public static final int COLOR_WHITE = 0;

    /**标示黑颜色**/

    public static final int COLOR_BLACK = 1;

     

    /**游戏进行状态**/

    public static final int GAME_START = 1;

    /**游戏结束状态**/

    public static final int GAME_OVER = 2;

     

    /** 游戏总共有多少行 **/

    public static final int LINES_LEN = 50;

     

    /**字体颜色的 key**/

    public static final String FONT_NAME_TIMER = "timer";

    public static final String FONT_NAME_RESULT = "result";

}

  

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

package com.orange.block.util;

 

import android.util.Log;

 

/**

 *

 * @author lch

 *

 */

public class LogUtil {

    private static boolean showLogEnabled = true;

    private static final String TAG = "OG";

 

    public static void out(String msg) {

        if (showLogEnabled)

            System.out.println(msg);

    }

 

    public static void d(String msg) {

        if (showLogEnabled)

            Log.d(TAG, msg);

    }

 

    public static void i(String msg) {

        if (showLogEnabled)

            Log.i(TAG, msg);

    }

 

    public static void v(String msg) {

        if (showLogEnabled)

            Log.v(TAG, msg);

    }

 

    public static void w(String msg) {

        if (showLogEnabled)

            Log.w(TAG, msg);

    }

 

    public static void e(String msg) {

        if (showLogEnabled)

            Log.e(TAG, msg);

    }

 

}

  

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

package com.orange.block.util;

 

import android.content.Context;

import android.content.SharedPreferences.Editor;

 

/**

 * 保存到 SharedPreferences 的数据

 *

 * @author lch

 *

 */

public class SharedUtil {

 

    private static final String SHARED_OG = "Shared_og";

 

    private static final String RESULT_RECORD = "result_record";

 

    /**

     * 保持最新的记录

     * @param pContext

     * @param pMillisPass

     */

    public static void setRecord(Context pContext, long pMillisPass) {

        Editor edit = pContext.getSharedPreferences(SHARED_OG,

                Context.MODE_PRIVATE).edit();

        edit.putLong(RESULT_RECORD, pMillisPass);

        edit.commit();

    }

 

    /**

     * 获取记录

     * @param context

     * @return

     */

    public static long getRecord(Context context) {

        return context

                .getSharedPreferences(SHARED_OG, Context.MODE_PRIVATE)

                .getLong(RESULT_RECORD, 0);

    }

 

}

  

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

package com.orange.block.scene;

 

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

 

import android.view.KeyEvent;

 

import com.orange.content.SceneBundle;

import com.orange.entity.IEntity;

import com.orange.entity.modifier.ColorModifier;

import com.orange.entity.modifier.DelayModifier;

import com.orange.entity.modifier.IEntityModifier.IEntityModifierListener;

import com.orange.entity.modifier.LoopEntityModifier;

import com.orange.entity.modifier.MoveYModifier;

import com.orange.entity.modifier.ScaleModifier;

import com.orange.entity.modifier.SequenceEntityModifier;

import com.orange.entity.primitive.DrawMode;

import com.orange.entity.primitive.Mesh;

import com.orange.entity.scene.Scene;

import com.orange.entity.sprite.AnimatedSprite;

import com.orange.entity.text.Text;

import com.orange.res.FontRes;

import com.orange.util.color.Color;

import com.orange.util.modifier.IModifier;

import com.orange.block.control.TimerTool;

import com.orange.block.entity.Block;

import com.orange.block.entity.FailGroup;

import com.orange.block.entity.SuccGroup;

import com.orange.block.res.Res;

import com.orange.block.util.ConstantUtil;

import com.orange.block.util.LogUtil;

 

/**

 * 游戏场景

 *

 * @author lch

 *

 */

public class GameScene extends Scene {

    /** 块的宽 **/

    private float blockWidth = 0;

    /** 块的高 **/

    private float blockHight = 0;

    /** 用于装当前所有生成但未被删除的block **/

    private List<Block[]> blockList;

    /** 当前生成的块的行数 **/

    private int linesLength = 5;

    /** 游戏状态 **/

    private int gameStatus = ConstantUtil.GAME_START;

    /** 移动步数 **/

    private int moveNum = 0;

    /** 成功界面 **/

    private SuccGroup mSuccGroup;

    /** 失败界面 **/

    private FailGroup mFailGroup;

    /** 计时管理类 **/

    private TimerTool mTimerTool;

 

    /** 显示计时的Text **/

    private Text timerText;

     

    /**游戏提示语**/

    private AnimatedSprite game_tip;

 

    // 用于Z索引排序

    private static final int pZIndex_middle = 2;

    private static final int pZIndex_top = 3;

 

    public Text getTimerText() {

        return timerText;

    }

 

    public TimerTool getmTimerTool() {

        return mTimerTool;

    }

 

    @Override

    public void onSceneCreate(SceneBundle bundle) {

        super.onSceneCreate(bundle);

        // 镜头里显示的是4*4的块,所以用镜头宽的四分之一作为块宽

        blockWidth = this.getCameraWidth() / 4;

        blockHight = this.getCameraHeight() / 4;

 

        this.blockList = new ArrayList<Block[]>();

        mTimerTool = new TimerTool(this);

        initView();

    }

 

    private void initView() {

        // 初始化blocks

        initBlocks();

 

        timerText = new Text(getCameraCenterX(), 10,

                FontRes.getFont(ConstantUtil.FONT_NAME_TIMER), "00.000\"",

                "00:00.000\"".length(), getVertexBufferObjectManager());

        this.attachChild(timerText);

        timerText.setCentrePositionX(getCameraCenterX());

        timerText.setZIndex(pZIndex_top);

 

        mSuccGroup = new SuccGroup(getCameraWidth(), getCameraHeight(), this);

        mSuccGroup.setZIndex(pZIndex_middle);

        mSuccGroup.setVisible(false);

        this.attachChild(mSuccGroup);

 

        mFailGroup = new FailGroup(getCameraWidth(), getCameraHeight(), this);

        this.attachChild(mFailGroup);

        mFailGroup.setZIndex(pZIndex_middle);

         

        game_tip = new AnimatedSprite(0, 0, Res.GAME_TIP, getVertexBufferObjectManager());

        game_tip.setCentrePositionX(this.getCameraCenterX());

        game_tip.setBottomPositionY(this.getCameraBottomY()-60);

        this.attachChild(game_tip);

        game_tip.setZIndex(pZIndex_top);

         

    }

 

    /**

     * 初始化blocks

     */

    private void initBlocks() {

        Random mRandom = new Random();

 

        int blackIndex = 0;

        // 初始blocks,先创建4*5表格,使用时候再一行行增加

        for (int row = 0; row < linesLength; row++) {// 行

            // 一行blocks

            Block[] rowBolcks = new Block[4];

            // 随机一个黑块所在位置

            blackIndex = mRandom.nextInt(4);

            for (int column = 0; column < 4; column++) {// 列

                rowBolcks[column] = new Block(this, row, column, blockWidth,

                        blockHight, blackIndex, getVertexBufferObjectManager());

                this.attachChild(rowBolcks[column]);

            }

            blockList.add(rowBolcks);

        }

    }

 

    /**

     * 重来时,重置游戏相关

     */

    public void resetGame() {

        gameStatus = ConstantUtil.GAME_START;

        linesLength = 5;

        moveNum = 0;

        mSuccGroup.showItems(false);

        timerText.setText("00.000\"");

        timerText.setVisible(true);

        mTimerTool.resetTimer();

        mSuccGroup.setVisible(false);

        game_tip.setVisible(true);

        deletBlocks();

        initBlocks();

        // 按Z索引排序一下上下层顺序

        this.sortChildren();

    }

 

    /**

     * 创建添加新的一行

     */

    private void createNewRowBolcks() {

        // 超出屏幕没用的部分移除掉

        if (blockList.size() > 5) {

            Block[] rowBolcks = blockList.get(0);

            for (Block block : rowBolcks) {

                block.detachSelf();

            }

            blockList.remove(0);

        }

 

        // 目前顶部的一行块的Y坐标

        float topBlocksY = blockList.get(blockList.size() - 1)[0].getY();

 

        // 一行块

        Block[] rowBolcks = new Block[4];

        int blackIndex = new Random().nextInt(4);

        for (int column = 0; column < 4; column++) {

            // 新创建 Block

            rowBolcks[column] = new Block(this, column, blockWidth, blockHight,

                    blackIndex, getVertexBufferObjectManager());

            // 设置Y坐标

            rowBolcks[column].setBottomPositionY(topBlocksY - 1);

            // 设置已经是第几行

            rowBolcks[column].setRow(linesLength);

            this.attachChild(rowBolcks[column]);

        }

        blockList.add(rowBolcks);

 

        // 按Z索引排序一下上下层顺序

        this.sortChildren();

 

        // 当前生成的块的行数增加

        linesLength++;

    }

 

    /**

     * 最后一个移动,游戏胜利

     *

     * @param pBlock

     */

    private void gameSucc(Block pBlock) {

        gameStatus = ConstantUtil.GAME_OVER;

        // 最后一个移动,游戏胜利

        moveDown(pBlock, 0);

        // 停止计时

        mTimerTool.stop();

        // 显示游戏胜利画面

        mSuccGroup.showItems(true);

        // 隐藏显示时间的Text

        timerText.setVisible(false);

    }

 

    /**

     * 点击错误后弹出红色的失败界面,游戏失败

     */

    private void gameFail() {

        gameStatus = ConstantUtil.GAME_OVER;

        // 游戏失败,停止计时

        mTimerTool.stop();

        // 隐藏显示时间的Text

        timerText.setVisible(false);

    }

 

    /**

     * 游戏快到顶部时开始出现绿色的胜利界面

     */

    private void showSuccGroup() {

        // 目前顶部的一行块的Y坐标

        float topBlocksY = blockList.get(blockList.size() - 1)[0].getY();

        mSuccGroup.setBottomPositionY(topBlocksY);

        mSuccGroup.setVisible(true);

    }

 

    /**

     * 移除剩下的block,清空blockList

     */

    private void deletBlocks() {

        for (Block[] rowBlocks : blockList) {

            for (Block block : rowBlocks) {

                this.detachChild(block);

            }

        }

 

        blockList.clear();

    }

 

    /**

     * 点击到Block时进行的逻辑处理

     *

     * @param pBlock

     *            所点击的block

     */

    public void touchBlock(Block pBlock) {

 

        if (gameStatus == ConstantUtil.GAME_START) {

 

            if (pBlock.getRow() == moveNum + 2) {// 表示是在底部往上数的倒数第三行

                // 判断是不是点击了该点击的黑块的上一格,如果是,我们也判定这是正确点击了,做出相应移动

                upBlockTouch(pBlock);

            } else if (pBlock.getRow() == moveNum + 1) {// 表示是在底部往上数的倒数第二行

                if (pBlock.getColorType() == ConstantUtil.COLOR_BLACK) {

                    if (linesLength == moveNum + 2) {

                        // 游戏胜利

                        gameSucc(pBlock);

                    } else {

                        // 整体blocks下移

                        moveDown(pBlock, 1);

                    }

 

                } else if (pBlock.getColorType() == ConstantUtil.COLOR_WHITE) {

                    // 误点了白块,游戏失败

                    gameFail();

                    // 失败时pBlock的一个闪红效果

                    LoopEntityModifier loop = failAction();

                    // 播放效果

                    pBlock.registerEntityModifier(loop);

                }

 

            }

 

        }

 

    }

 

    /**

     * 失败时pBlock的一个闪红效果

     * @return

     */

    private LoopEntityModifier failAction() {

        SequenceEntityModifier sequence = new SequenceEntityModifier(

                new ColorModifier(0.1f, Color.RED, Color.WHITE),

                new DelayModifier(0.07f), new ColorModifier(0.1f,

                        Color.WHITE, Color.RED));

        LoopEntityModifier loop = new LoopEntityModifier(sequence,

                3, new IEntityModifierListener() {

 

                    @Override

                    public void onModifierStarted(

                            IModifier<IEntity> pModifier,

                            IEntity pItem) {

                    }

                    @Override

                    public void onModifierFinished(

                            IModifier<IEntity> pModifier,

                            IEntity pItem) {

                        //效果播放完毕,显示游戏失败界面

                        mFailGroup.showView();

                    }

                });

        return loop;

    }

 

    /**

     * 判断是不是点击了该点击的黑块的上一格,如果是,我们也判定这是正确点击了,做出相应移动

     *

     * @param pBlock

     *            所被点击的块

     */

    private void upBlockTouch(Block pBlock) {

        int touchColumn = pBlock.getColumn();

        for (Block[] blocks : blockList) {

            for (Block block : blocks) {

                if (block.getRow() == moveNum + 1

                        && block.getColorType() == ConstantUtil.COLOR_BLACK) {

                    if (block.getColumn() == touchColumn) {

                        // 整体blocks下移

                        moveDown(block, 1);

                    }

                    return;

                }

            }

 

        }

    }

 

    /**

     * 正确点击该点击的黑块,或者上一行的块,整体向下移动、创建新的一样块,改变黑块颜色

     *

     * @param pBlock

     * @param stepNum

     *            一般为1,到最后出现绿色成功界面的最后一步为0

     */

    private void moveDown(Block pBlock, int stepNum) {

 

        if(moveNum == 0){

            // 开始计时

            mTimerTool.start();

            // 隐藏提示文字

            game_tip.setVisible(false);

        }

         

 

        if (moveNum < ConstantUtil.LINES_LEN) {

            // 创建添加新的一行

            createNewRowBolcks();

        } else if (moveNum == ConstantUtil.LINES_LEN) {

            // 开始显示绿色胜利界面,即将胜利,但还没有胜利

            showSuccGroup();

        }

 

        // 被点击的黑块变灰

        pBlock.setColor(0.63f, 0.63f, 0.63f);

        pBlock.registerEntityModifier(new ScaleModifier(0.1f, 0.5f, 1.0f));

 

        // 移动步数加1

        moveNum++;

        // 需要移动的距离

        float moveDistance = this.getCameraHeight() - blockHight * stepNum

                - pBlock.getY();

        for (Block[] rowBlocks : blockList) {

            for (Block block : rowBlocks) {

                // 遍历,所有block向下移动指定距离

                moveToY(block, moveDistance);

            }

        }

 

        // 快到胜利出现绿色胜利界面时,胜利界面跟着移动

        if (mSuccGroup.isVisible()) {

            moveToY(mSuccGroup, moveDistance);

        }

    }

 

    /**

     * 在Y轴方向上,由当前位置移动指定的距离

     *

     * @param entity

     *            要移动的实体

     * @param moveDistance

     *            需要移动的距离

     */

    private void moveToY(IEntity entity, float moveDistance) {

        float pFromY = entity.getY();

        float pToY = pFromY + moveDistance;

        entity.registerEntityModifier(new MoveYModifier(0.1f, pFromY, pToY));

    }

 

    @Override

    public boolean onKeyUp(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {

            this.getActivity().finish();

            return true;

        }

        return false;

    }

     

    @Override

    public void onSceneResume() {

        super.onSceneResume();

        this.setIgnoreUpdate(false);

    }

     

    @Override

    public void onScenePause() {

        super.onScenePause();

        this.setIgnoreUpdate(true);

    }

 

    @Override

    public void onSceneDestroy() {

        super.onSceneDestroy();

 

        LogUtil.d("onSceneDestroy");

    }

 

    // ================================================================

    /**

     * 获取镜头的 右边 x 坐标

     */

    public float getCameraRightX() {

        return this.getCameraWidth();

    }

 

    /**

     * 获取镜头的 中点 x 坐标

     *

     * @return

     */

    public float getCameraCenterX() {

        return this.getCameraWidth() * 0.5f;

    }

 

    /**

     * 获取镜头底部Y坐标

     *

     * @return

     */

    public float getCameraBottomY() {

        return this.getCameraHeight();

    }

 

    /**

     * 获取镜头中心Y坐标

     *

     * @return

     */

    public float getCameraCenterY() {

        return this.getCameraHeight() * 0.5f;

    }

 

}

  

池彬宁:负责代码的实现(二)

blogs:  

codes:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

package com.orange.block.res;

 

public class Res {

 

    public static final String XML_GFX_GAME = "gfx/game.xml";

 

    public static final String[] ALL_XML = new String[]{

        Res.XML_GFX_GAME

    };

 

    public static final String BTN_AGAIN = "btn_again";

 

    public static final String BTN_BACK = "btn_back";

 

    public static final String BTN_START = "btn_start";

 

    public static final String GAME_MODEL = "game_model";

 

    public static final String GAME_TIP = "game_tip";

 

    public static final String GAME_TITEL = "game_titel";

 

}

  

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

package com.orange.block.entity;

 

import com.orange.entity.primitive.Rectangle;

import com.orange.input.touch.TouchEvent;

import com.orange.opengl.vbo.VertexBufferObjectManager;

import com.orange.util.color.Color;

import com.orange.block.scene.GameScene;

import com.orange.block.util.ConstantUtil;

 

/**

 * 单个块元素

 *

 * @author lch

 *

 */

public class Block extends Rectangle {

    // 游戏场景

    private GameScene mGameScene;

    // 此block的颜色类型,白色还是黑色?

    private int colorType;

    // block 所在的行

    private int row;

    // block 所在的列

    private int column;

 

    // ======================get&set========================

    public int getRow() {

        return row;

    }

    public void setRow(int row) {

        this.row = row;

    }

    public int getColumn() {

        return column;

    }

    public int getColorType() {

        return colorType;

    }

    // =====================================================

 

    /**

     * 构造器1,初始化blocks时用到

     * @param pGameScene 游戏场景

     * @param row block所在的行

     * @param column block所在的列

     * @param pWidth block的宽

     * @param pHeight block的高

     * @param blackIndex 用来确定是否是黑块,如果blackIndex == column时设为黑块

     * @param pVertexBufferObjectManager

     */

    public Block(GameScene pGameScene, int row, int column, float pWidth,

            float pHeight, int blackIndex,

            VertexBufferObjectManager pVertexBufferObjectManager) {

        super(column * pWidth, (3 - row) * pHeight, pWidth - 1, pHeight - 1,

                pVertexBufferObjectManager);

        this.mGameScene = pGameScene;

        this.row = row;

        this.column = column;

        if (row == 0) {

            // 第一行设置为黄块

            this.setColor(Color.YELLOW);

        } else {

            // 初始化block的颜色数据,是白块还是黑块?

            initBlockData(column, blackIndex);

        }

        // 设置可以相应触碰事件

        this.setIgnoreTouch(false);

    }

 

    /**

     * 构造器2,新增blocks时用到

     * @param pGameScene 游戏场景

     * @param column block所在的列

     * @param pWidth block的宽

     * @param pHeight block的高

     * @param blackIndex 来确定是否是黑块,如果blackIndex == column时设为黑块

     * @param pVertexBufferObjectManager

     */

    public Block(GameScene pGameScene, int column, float pWidth, float pHeight,

            int blackIndex, VertexBufferObjectManager pVertexBufferObjectManager) {

        super(column * pWidth, 0, pWidth - 1, pHeight - 1,

                pVertexBufferObjectManager);

        this.mGameScene = pGameScene;

        this.column = column;

        // 初始化block的颜色数据,是白块还是黑块?

        initBlockData(column, blackIndex);

        // 设置可以相应触碰事件

        this.setIgnoreTouch(false);

    }

     

    /**

     * 初始化block的颜色数据,是白块还是黑块?

     * @param column

     * @param blackIndex

     */

    private void initBlockData(int column, int blackIndex) {

        if (blackIndex == column) {

            // 设置为黑块

            this.setColor(Color.BLACK);

            this.colorType = ConstantUtil.COLOR_BLACK;

        } else {

            // 设置为白块

            this.setColor(Color.WHITE);

            this.colorType = ConstantUtil.COLOR_WHITE;

        }

    }

 

    @Override

    public boolean onAreaTouched(TouchEvent pSceneTouchEvent,

            float pTouchAreaLocalX, float pTouchAreaLocalY) {

        // 触碰事件监听

        if (pSceneTouchEvent.isActionDown()) {

            // 点击到Block时进行的逻辑处理

            mGameScene.touchBlock(this);

        }

        return true;

    }

}

  

 

package com.orange.block.entity;

 

import android.app.AlertDialog;

import android.content.DialogInterface;

 

import com.orange.entity.group.EntityGroup;

import com.orange.entity.modifier.ScaleModifier;

import com.orange.entity.primitive.Rectangle;

import com.orange.entity.sprite.AnimatedSprite;

import com.orange.entity.sprite.ButtonSprite;

import com.orange.entity.sprite.ButtonSprite.OnClickListener;

import com.orange.entity.text.Text;

import com.orange.res.FontRes;

import com.orange.util.color.Color;

import com.orange.block.control.TimerTool;

import com.orange.block.res.Res;

import com.orange.block.scene.GameScene;

import com.orange.block.util.ConstantUtil;

import com.orange.block.util.SharedUtil;

 

/**

 * 失败界面

 *

 * @author lch

 *

 */

public class FailGroup extends EntityGroup {

 

    private GameScene mGameScene;

    private Rectangle bgRect;

 

    private AnimatedSprite titelSprite;

    private AnimatedSprite modelSprite;

 

    private ButtonSprite btn_back; // 返回按钮

    private ButtonSprite btn_again;// 重来按钮

 

    private Text txt_big;

    private Text txt_small;

 

    public FailGroup(float pWidth, float pHeight, GameScene pGameScene) {

        super(pWidth, pHeight, pGameScene);

        this.mGameScene = pGameScene;

        initView();

    }

 

    private void initView() {

        // 背景

        bgRect = new Rectangle(0, 0, this.getWidth(), this.getHeight(),

                this.getVertexBufferObjectManager());

        this.attachChild(bgRect);

        bgRect.setColor(Color.RED);

        // 标题 “别踩白块儿”

        titelSprite = new AnimatedSprite(0, 10, Res.GAME_TITEL,

                this.getVertexBufferObjectManager());

        titelSprite.setRightPositionX(this.getRightX() - 10);

        this.attachChild(titelSprite);

 

        // 模式 “经典模式”

        modelSprite = new AnimatedSprite(0, 150, Res.GAME_MODEL,

                this.getVertexBufferObjectManager());

        modelSprite.setCentrePositionX(this.getCentreX());

        this.attachChild(modelSprite);

 

        // 大字

        txt_big = new Text(0, modelSprite.getBottomY() + 130,

                FontRes.getFont(ConstantUtil.FONT_NAME_RESULT), "", 15,

                this.getVertexBufferObjectManager());

        this.attachChild(txt_big);

        txt_big.setScale(1.9f);

        // 小字

        txt_small = new Text(0, txt_big.getBottomY() + 30,

                FontRes.getFont(ConstantUtil.FONT_NAME_RESULT), "", 15,

                this.getVertexBufferObjectManager());

        this.attachChild(txt_small);

        txt_small.setScale(0.7f);

        // 返回按钮

        btn_back = new ButtonSprite(80, 0, Res.BTN_BACK,

                this.getVertexBufferObjectManager());

        btn_back.setBottomPositionY(this.getBottomY() - 50);

        btn_back.setIgnoreTouch(false);

        this.attachChild(btn_back);

        btn_back.setOnClickListener(onClickListener);

        // 重来按钮

        btn_again = new ButtonSprite(0, 0, Res.BTN_AGAIN,

                this.getVertexBufferObjectManager());

        btn_again.setRightPositionX(this.getRightX() - 80);

        btn_again.setBottomPositionY(this.getBottomY() - 50);

        btn_again.setIgnoreTouch(false);

        this.attachChild(btn_again);

        btn_again.setOnClickListener(onClickListener);

       

        resetView();

    }

   

   

 

    public void showView() {

        setBtnEnable(true);

        this.setVisible(true);

        updateBigTxt("失败了!");

 

        if (SharedUtil.getRecord(getActivity()) > 0) {

            TimerTool mTimerTool = mGameScene.getmTimerTool();

            updateSmallTxt("最佳: "

                    + mTimerTool.millisToTimer(SharedUtil

                            .getRecord(getActivity())));

        }

       

        ScaleModifier scaleModifier = new ScaleModifier(0.2f, 0.0f, 1.0f);

        this.registerEntityModifier(scaleModifier);

 

    }

 

    private void updateBigTxt(String pText) {

        txt_big.setText(pText);

        txt_big.setCentrePositionX(this.getCentreX());

    }

 

    private void updateSmallTxt(String pText) {

        txt_small.setText(pText);

        txt_small.setCentrePositionX(this.getCentreX());

    }

 

    private OnClickListener onClickListener = new OnClickListener() {

 

        @Override

        public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX,

                float pTouchAreaLocalY) {

            if (btn_back == pButtonSprite) {

                showDialog();

            } else if (btn_again == pButtonSprite) {

                resetView();

                mGameScene.resetGame();

            }

 

        }

 

    };

   

    private void resetView(){

        this.setScale(0);

        this.setVisible(false);

        setBtnEnable(false);

    }

 

    private void setBtnEnable(boolean isEnable) {

        btn_back.setEnabled(isEnable);

        btn_again.setEnabled(isEnable);

    }

   

    /**

     * 退出游戏确认对话框

     */

    private void showDialog() {

        getActivity().runOnUiThread(new Runnable() {

 

            @Override

            public void run() {

 

                new AlertDialog.Builder(getActivity())

                        .setTitle("退出游戏")

                        .setMessage("是否要退出游戏!")

                        .setPositiveButton("确定",

                                new DialogInterface.OnClickListener() {

 

                                    @Override

                                    public void onClick(DialogInterface dialog,

                                            int which) {

                                        getActivity().finish();

 

                                    }

                                }).setNegativeButton("取消", null).show();

            }

        });

    }

}

 

 

package com.orange.block.entity;

 

import android.app.AlertDialog;

import android.content.DialogInterface;

 

import com.orange.entity.group.EntityGroup;

import com.orange.entity.primitive.Rectangle;

import com.orange.entity.sprite.AnimatedSprite;

import com.orange.entity.sprite.ButtonSprite;

import com.orange.entity.sprite.ButtonSprite.OnClickListener;

import com.orange.entity.text.Text;

import com.orange.res.FontRes;

import com.orange.util.color.Color;

import com.orange.block.control.TimerTool;

import com.orange.block.res.Res;

import com.orange.block.scene.GameScene;

import com.orange.block.util.ConstantUtil;

import com.orange.block.util.SharedUtil;

 

/**

 * 成功界面

 *

 * @author lch

 *

 */

public class SuccGroup extends EntityGroup {

 

    private GameScene mGameScene;

    private Rectangle bgRect;

 

    private AnimatedSprite titelSprite;

    private AnimatedSprite modelSprite;

 

    private ButtonSprite btn_back; // 返回按钮

    private ButtonSprite btn_again;// 重来按钮

 

    private Text txt_big;

    private Text txt_small;

 

    public SuccGroup(float pWidth, float pHeight, GameScene pGameScene) {

        super(pWidth, pHeight, pGameScene);

        initView();

        this.mGameScene = pGameScene;

    }

 

    private void initView() {

        // 背景

        bgRect = new Rectangle(0, 0, this.getWidth(), this.getHeight(),

                this.getVertexBufferObjectManager());

        this.attachChild(bgRect);

        bgRect.setColor(Color.GREEN);

        // 标题 “别踩白块儿”

        titelSprite = new AnimatedSprite(0, 10, Res.GAME_TITEL,

                this.getVertexBufferObjectManager());

        titelSprite.setRightPositionX(this.getRightX() - 10);

        this.attachChild(titelSprite);

        titelSprite.setVisible(false);

 

        // 模式 “经典模式”

        modelSprite = new AnimatedSprite(0, 150, Res.GAME_MODEL,

                this.getVertexBufferObjectManager());

        modelSprite.setCentrePositionX(this.getCentreX());

        this.attachChild(modelSprite);

        modelSprite.setVisible(false);

 

        // 大字

        txt_big = new Text(0, modelSprite.getBottomY() + 100,

                FontRes.getFont(ConstantUtil.FONT_NAME_RESULT), "", 15,

                this.getVertexBufferObjectManager());

        this.attachChild(txt_big);

        txt_big.setScale(1.5f);

        txt_big.setVisible(false);

        // 小字

        txt_small = new Text(0, txt_big.getBottomY() + 10,

                FontRes.getFont(ConstantUtil.FONT_NAME_RESULT), "", 15,

                this.getVertexBufferObjectManager());

        this.attachChild(txt_small);

        txt_small.setScale(0.7f);

        txt_small.setVisible(false);

        // 返回按钮

        btn_back = new ButtonSprite(80, 0, Res.BTN_BACK,

                this.getVertexBufferObjectManager());

        btn_back.setBottomPositionY(this.getBottomY() - 50);

        btn_back.setIgnoreTouch(false);

        this.attachChild(btn_back);

        btn_back.setOnClickListener(onClickListener);

        btn_back.setVisible(false);

        // 重来按钮

        btn_again = new ButtonSprite(0, 0, Res.BTN_AGAIN,

                this.getVertexBufferObjectManager());

        btn_again.setRightPositionX(this.getRightX() - 80);

        btn_again.setBottomPositionY(this.getBottomY() - 50);

        btn_again.setIgnoreTouch(false);

        this.attachChild(btn_again);

        btn_again.setOnClickListener(onClickListener);

        btn_again.setVisible(false);

    }

 

    public void showItems(boolean pVisible) {

        titelSprite.setVisible(pVisible);

        modelSprite.setVisible(pVisible);

        txt_big.setVisible(pVisible);

        txt_small.setVisible(pVisible);

        btn_back.setVisible(pVisible);

        btn_again.setVisible(pVisible);

 

        if (pVisible) {

            TimerTool mTimerTool = mGameScene.getmTimerTool();

            updateBigTxt(mTimerTool.millisToTimer(mTimerTool

                    .getMillisPass()));

 

            long mMillisPass = mTimerTool.getMillisPass();

 

            if (mMillisPass < SharedUtil.getRecord(getActivity())

                    || SharedUtil.getRecord(getActivity()) == 0) {

                // 新记录

                updateSmallTxt("新记录");

                SharedUtil.setRecord(getActivity(), mMillisPass);

 

            } else {

 

                updateSmallTxt("最佳: "

                        + mTimerTool.millisToTimer(SharedUtil

                                .getRecord(getActivity())));

            }

        }

 

    }

 

    public void updateBigTxt(String pText) {

        txt_big.setText(pText);

        txt_big.setCentrePositionX(this.getCentreX());

    }

 

    public void updateSmallTxt(String pText) {

        txt_small.setText(pText);

        txt_small.setCentrePositionX(this.getCentreX());

    }

 

    private OnClickListener onClickListener = new OnClickListener() {

 

        @Override

        public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX,

                float pTouchAreaLocalY) {

            if (btn_back == pButtonSprite) {

                showDialog();

            } else if (btn_again == pButtonSprite) {

                mGameScene.resetGame();

            }

 

        }

    };

 

    /**

     * 退出游戏确认对话框

     */

    private void showDialog() {

        getActivity().runOnUiThread(new Runnable() {

 

            @Override

            public void run() {

 

                new AlertDialog.Builder(getActivity())

                        .setTitle("退出游戏")

                        .setMessage("是否要退出游戏!")

                        .setPositiveButton("确定",

                                new DialogInterface.OnClickListener() {

 

                                    @Override

                                    public void onClick(DialogInterface dialog,

                                            int which) {

                                        getActivity().finish();

 

                                    }

                                }).setNegativeButton("取消", null).show();

            }

        });

    }

}

 

 

贺邦:负责代码的调试与测试

blogs:

codes:

 

 1 public class Test {

 2 private int fAmount;

 3 private String fCurrency;

 4

 5 public Test(int amount, String currency) {

 6 fAmount= amount;

 7 fCurrency= currency;

 8 }

 9

10 public int amount() {

11 return fAmount;

12 }

13

14 public String currency() {

15 return fCurrency;

16 }

17

18 public Test add(Test m) {

19 return new Test(amount()+m.amount(), currency());

20 }

21

22 public boolean equals(Object anObject) {

23 if (anObject instanceof Test) {

24 Test aMoney= (Test)anObject;

25 return aMoney.currency().equals(currency())

26 && amount() == aMoney.amount();

27 }

28 return false;

29 }

30

运行效果

 

 

 

 

步骤

耗时

百分比

需求分析

 20min

 3.8%

设计

50min 

 9.6%

代码实现

300min 

 57.7%

测试

120min 

 23.1%

分析总结

 30min

 5.8%

 

转载于:https://www.cnblogs.com/L1nke/p/4552819.html

你可能感兴趣的文章
8.1 Android Basic 数据存储 Preferences Structured(分组的Preferences)
查看>>
原因和证明
查看>>
VC6.0图像处理2--图像的反色
查看>>
Snoop, 对WPF程序有效的SPY++机制
查看>>
Does not contain a valid host;port authority解决方法
查看>>
JAVA程序猿怎么才干高速查找到学习资料?
查看>>
使用axel下载百度云文件
查看>>
Qt中图像的显示与基本操作
查看>>
详解软件工程之软件测试
查看>>
WCF(二) 使用配置文件实现WCF应用程序
查看>>
【CodeForces 803 C】Maximal GCD(GCD+思维)
查看>>
python 去掉换行符或者改为其他方式结尾的方法(end='')
查看>>
数据模型(LP32 ILP32 LP64 LLP64 ILP64 )
查看>>
REST构架风格介绍:状态表述转移
查看>>
struct {0}初始化
查看>>
c++ operator
查看>>
apache 添加 ssl_module
查看>>
java小技巧
查看>>
POJ 3204 Ikki's Story I - Road Reconstruction
查看>>
getQueryString
查看>>