文章目录
一、计划内容
1.1、当前得分
在下一块的预览下方,显示当前得分是多少。
1.2、最高分数
统计得到的最高分数,当前分数没有超过最高分时,最高分不改变;当前分数超过最高分时,最高分数更新。
1.3、补充说明
1)、添加了最高分数的显示;
2)、解决了重新开始时,当前分数延续上一次的当前分数的bug。
二、当前得分
2.1、布局
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="最高分"/>
<TextView
android:id="@+id/tv_max"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="当前分数"/>
<TextView
android:id="@+id/tv_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="0"/>
</LinearLayout>
2.2、计分的启动点
在每消一行的时候,就应该去计分一次。
//消行处理
public int deleteLine(){
int lines=0;
for (int y = maps[0].length-1; y>0 ; y--) {
//消行判断
if(checkLine(y)){
//执行消行
implementLine(y);
//执行消行后,y=y+1
y++;
lines++;
}
}
return lines;
}
2.3、分数模块
计分规则。
public class Scoremoudle {
public int score;
public int max;
public void number(int lines) {
if(lines==0)
return;
max=2*lines-1;
score=max+score;
}
}
2.4、显示当前分数
if(gamecontrol.scoremoudle.score==0){
//刷新重绘view
view.invalidate();
nextPanel.invalidate();
}else {
//刷新重绘view
view.invalidate();
nextPanel.invalidate();
//gamecontrol.scoremoudle.score数据是int型,需要转换。
//int i=gamecontrol.scoremoudle.score;
//String s=String.valueOf(i);
//tv_score.setText(s);
//也可以直接加个””
tv_score.setText(gamecontrol.scoremoudle.score+"");
}
三、最高分数
3.1、布局
在当前得分布局处添加:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="最高分"/>
<TextView
android:id="@+id/tv_scoreMax"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"/>
3.2、主函数
gamecontrol.scoremoudle.showmaxscore(MainActivity.this,tv_scoreMax);
//实例化游戏控制器
gamecontrol=new Gamecontrol(handler,this);
//newBoxs();
initview();
initButton();
gamecontrol.scoremoudle.showmaxscore(this,tv_scoreMax);
}
//添加到父容器里面
LinearLayout ll_11=(LinearLayout)findViewById(R.id.ll_11);
ll_11.setPadding(0,Config.DISTANCE,Config.DISTANCE,Config.DISTANCE);
fl_next.addView(nextPanel);
//当前分数
tv_score=(TextView)findViewById(R.id.tv_score);
//最大分数
tv_scoreMax=(TextView)findViewById(R.id.tv_scoreMax);
3.3、最高分的实现
public void showmaxscore(Context context, TextView tv_scoreMax) {
if (tv_scoreMax!=null){
updatescoreMax(context);
tv_scoreMax.setText(scoreMax+"");
}
}
//更新最高分数
public void updatescoreMax(Context context){
if (scoreMax==0){
scoreMax=(int)SPUtils.get(context,"scoreMax",0);
}
if(score>scoreMax){
scoreMax=score;
//储存到本地
SPUtils.put(context,"scoreMax",scoreMax);
}
}
四、报错
4.1、语法错误
一直显示异常错误,分析代码,发现:gamecontrol.scoremoudle.score
为int,而text不能直接输出,需要转换一下格式,
//gamecontrol.scoremoudle.score数据是int型,需要转换。
int i=gamecontrol.scoremoudle.score;
String s=String.valueOf(i);
tv_score.setText(s);
或者直接:
tv_score.setText(s+" ");
4.2、当前分数不会刷新
点击开始按钮后,会重新生成新的方块,但是当前分数,会停留在上一次的最后的当前分数。
//清除地图
mapsmodel.refersh();
//结束状态设置为false
isOver=false;
//暂停状态设置为false
isPause=false;
//点击开始按钮,生成新的游戏方块
boxsmoudle.newBoxs();
调试代码后,最终发现,在清除所有的方块团案后,留下空白的地图,此时,设置当前分数为0,并且显示。
//清除地图
mapsmodel.refersh();
scoremoudle.score=0;
MainActivity.tv_score.setText("0");
//结束状态设置为false
isOver=false;
//暂停状态设置为false
isPause=false;
//点击开始按钮,生成新的游戏方块
boxsmoudle.newBoxs();