Skip to content

Commit

Permalink
Merge pull request #814 from JPCat/master
Browse files Browse the repository at this point in the history
#90 #102 实验四-包括整体布局的xml和界面操作控制代码
  • Loading branch information
zengsn committed May 24, 2016
2 parents ca10af7 + c2f5883 commit a3965ed
Show file tree
Hide file tree
Showing 2 changed files with 295 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,36 +1,269 @@
package com.example.com1314080901204;
package com.example.Com1314080901204Activity;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.link2link.board.GameService;
import com.example.link2link.board.impl.GameServiceImpl;
import com.example.link2link.object.GameConf;
import com.example.link2link.object.LinkInfo;
import com.example.link2link.view.GameView;
import com.example.link2link.view.Piece;

public class Com1314080901204Activity extends Activity {

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


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
// 游戏配置对象
private GameConf config;
// 游戏业务逻辑接口
private GameService gameService;
// 游戏界面
private GameView gameView;
// 开始按钮
private Button startButton;
// 记录剩余时间的TextView
private TextView timeTextView;
// 失败后弹出的对话框
private AlertDialog.Builder lostDialog;
// 游戏胜利后的对话框
private AlertDialog.Builder successDialog;
// 定时器
private Timer timer = new Timer();
// 记录游戏的剩余时间
private int gameTime;
// 记录是否处于游戏状态
private boolean isPlaying;
// 播放音效
SoundPool soundPool = new SoundPool(2, AudioManager.STREAM_SYSTEM, 8);
int dis;
// 记录已经选中的方块
private Piece selected = null;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 0x123:
timeTextView.setText("剩余时间: " + gameTime);
gameTime--;
// 时间小于0, 游戏失败
if (gameTime < 0) {
stopTimer();
// 更改游戏的状态
isPlaying = false;
lostDialog.show();
return;
}
break;
}
}
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化界面
init();
}

// 初始化游戏的方法
private void init() {
config = new GameConf(8, 9, 2, 10, 100000, this);
// 得到游戏区域对象
gameView = (GameView) findViewById(R.id.gameView);
// 获取显示剩余时间的文本框
timeTextView = (TextView) findViewById(R.id.timeText);
// 获取开始按钮
startButton = (Button) this.findViewById(R.id.startButton);
// 初始化音效
dis = soundPool.load(this, R.raw.dis, 1);
gameService = new GameServiceImpl(this.config);
gameView.setGameService(gameService);
// 为开始按钮的单击事件绑定事件监听器
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View source) {
startGame(GameConf.DEFAULT_TIME);
}
});
// 为游戏区域的触碰事件绑定监听器
this.gameView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent e) {
if (!isPlaying) {
return false;
}
if (e.getAction() == MotionEvent.ACTION_DOWN) {
gameViewTouchDown(e);
}
if (e.getAction() == MotionEvent.ACTION_UP) {
gameViewTouchUp(e);
}
return true;
}
});
// 初始化游戏失败的对话框
lostDialog = createDialog("Lost", "游戏失败! 重新开始", R.drawable.lost)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
startGame(GameConf.DEFAULT_TIME);
}
});
// 初始化游戏胜利的对话框
successDialog = createDialog("Success", "游戏胜利! 重新开始",
R.drawable.success).setPositiveButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
startGame(GameConf.DEFAULT_TIME);
}
});
}

@Override
protected void onPause() {
// 暂停游戏
stopTimer();
super.onPause();
}

@Override
protected void onResume() {
// 如果处于游戏状态中
if (isPlaying) {
// 以剩余时间重写开始游戏
startGame(gameTime);
}
super.onResume();
}

// 触碰游戏区域的处理方法
private void gameViewTouchDown(MotionEvent event) {
// 获取GameServiceImpl中的Piece[][]数组
Piece[][] pieces = gameService.getPieces();
// 获取用户点击的x座标
float touchX = event.getX();
// 获取用户点击的y座标
float touchY = event.getY();
// 根据用户触碰的座标得到对应的Piece对象
Piece currentPiece = gameService.findPiece(touchX, touchY);
// 如果没有选中任何Piece对象(即鼠标点击的地方没有图片), 不再往下执行
if (currentPiece == null)
return;
// 将gameView中的选中方块设为当前方块
this.gameView.setSelectedPiece(currentPiece);
// 表示之前没有选中任何一个Piece
if (this.selected == null) {
// 将当前方块设为已选中的方块, 重新将GamePanel绘制, 并不再往下执行
this.selected = currentPiece;
this.gameView.postInvalidate();
return;
}
// 表示之前已经选择了一个
if (this.selected != null) {
// 在这里就要对currentPiece和prePiece进行判断并进行连接
LinkInfo linkInfo = this.gameService.link(this.selected,
currentPiece); // ③
// 两个Piece不可连, linkInfo为null
if (linkInfo == null) {
// 如果连接不成功, 将当前方块设为选中方块
this.selected = currentPiece;
this.gameView.postInvalidate();
} else {
// 处理成功连接
handleSuccessLink(linkInfo, this.selected, currentPiece, pieces);
}
}
}

// 触碰游戏区域的处理方法
private void gameViewTouchUp(MotionEvent e) {
this.gameView.postInvalidate();
}

// 以gameTime作为剩余时间开始或恢复游戏
private void startGame(int gameTime) {
// 如果之前的timer还未取消,取消timer
if (this.timer != null) {
stopTimer();
}
// 重新设置游戏时间
this.gameTime = gameTime;
// 如果游戏剩余时间与总游戏时间相等,即为重新开始新游戏
if (gameTime == GameConf.DEFAULT_TIME) {
// 开始新的游戏游戏
gameView.startGame();
}
isPlaying = true;
this.timer = new Timer();
// 启动计时器 , 每隔1秒发送一次消息
this.timer.schedule(new TimerTask() {
public void run() {
handler.sendEmptyMessage(0x123);
}
}, 0, 1000);
// 将选中方块设为null。
this.selected = null;
}

/**
* 成功连接后处理
*
* @param linkInfo
* 连接信息
* @param prePiece
* 前一个选中方块
* @param currentPiece
* 当前选择方块
* @param pieces
* 系统中还剩的全部方块
*/
private void handleSuccessLink(LinkInfo linkInfo, Piece prePiece,
Piece currentPiece, Piece[][] pieces) {
// 它们可以相连, 让GamePanel处理LinkInfo
this.gameView.setLinkInfo(linkInfo);
// 将gameView中的选中方块设为null
this.gameView.setSelectedPiece(null);
this.gameView.postInvalidate();
// 将两个Piece对象从数组中删除
pieces[prePiece.getIndexX()][prePiece.getIndexY()] = null;
pieces[currentPiece.getIndexX()][currentPiece.getIndexY()] = null;
// 将选中的方块设置null。
this.selected = null;
// 播放音效
soundPool.play(dis, 1, 1, 0, 0, 1);
// 判断是否还有剩下的方块, 如果没有, 游戏胜利
if (!this.gameService.hasPieces()) {
// 游戏胜利
this.successDialog.show();
// 停止定时器
stopTimer();
// 更改游戏状态
isPlaying = false;
}
}

// 创建对话框的工具方法
private AlertDialog.Builder createDialog(String title, String message,
int imageResource) {
return new AlertDialog.Builder(this).setTitle(title)
.setMessage(message).setIcon(imageResource);
}

private void stopTimer() {
// 停止定时器
this.timer.cancel();
this.timer = null;
}

}

45 changes: 34 additions & 11 deletions app/src/main/res/layout/activity_com1314080901204.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,38 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/Com1314080901204bg"
tools:context="com.example.com1314080901204.Com1314080901204Activity" >
android:background="@drawable/bg" >

<ImageView
android:id="@+id/sun"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/Com1314080901204sun" />

</RelativeLayout>
<!-- 连连看布局 -->
<com.example.link2link.view.GameView
android:id="@+id/gameView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<!-- 开始按钮和显示剩余时间布局 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#1e72bb"
android:gravity="center"
android:orientation="horizontal" >

<Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_selector" />

<TextView
android:id="@+id/timeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#ff9"
android:textSize="20dp"
android:width="150dp" />

</LinearLayout>

</RelativeLayout>

0 comments on commit a3965ed

Please sign in to comment.