Android小项目--2048小游戏,flutter人脸识别插件

本文详细介绍了Android平台上实现2048小游戏的步骤,包括Cell类的设计、游戏初始化、游戏模式切换、分数记录和游戏结束判断。文章涉及Sqlite、SharedPreferences等技术,并提供了关键代码片段。
摘要由CSDN通过智能技术生成

} else {

return -1;

}

}

}

2. Cell类的设计:Cell用来表示游戏中的小格子

  • Cell表示游戏中移动的小格子,格子的颜色、显示数字等属性都在对象中进行设置,Cell类如下:

public class Cell extends FrameLayout {

//显示数字的TextView

private TextView cellShowText;

//显示的数字

private int digital;

public Cell(Context context) {

super(context);

}

public Cell(@NonNull Context context, int leftMargin, int topMargin, int bottomMargin) {

super(context);

init(context, leftMargin, topMargin, bottomMargin);

}

//初始化

private void init(@NonNull Context context, int leftMargin, int topMargin, int bottomMargin) {

cellShowText = new TextView(context);

// 不同难度设置不同字体大小

switch (Config.GRIDColumnCount) {

case 4:

cellShowText.setTextSize(36);

break;

case 5:

cellShowText.setTextSize(28);

break;

case 6:

cellShowText.setTextSize(20);

break;

default:

cellShowText.setTextSize(36);

break;

}

cellShowText.setGravity(Gravity.CENTER);

// 抗锯齿

cellShowText.getPaint().setAntiAlias(true);

// 粗体

cellShowText.getPaint().setFakeBoldText(true);

// 颜色

cellShowText.setTextColor(ContextCompat.getColor(context, R.color.colorWhite));

// 填充整个父容器

LayoutParams params = new LayoutParams(-1, -1);

params.setMargins(leftMargin, topMargin, 0, bottomMargin);

addView(cellShowText, params);

setDigital(0);

}

//获取卡片

public TextView getItemCell() {

return cellShowText;

}

//获取数字

public int getDigital() {

return digital;

}

//设置数字

public void setDigital(int digital) {

this.digital = digital;

cellShowText.setBackgroundResource(getBackgroundResource(digital));

if (digital <= 0) {

cellShowText.setText("");

} else {

cellShowText.setText(String.valueOf(digital));

}

}

//根据数字获取相应的背景

private int getBackgroundResource(int number) {

switch (number) {

case 0:

return

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值