您的位置:首页 > 移动开发 > Android开发

Android&java的成长之路之五(2048小游戏①)

2016-02-22 23:37 627 查看
   距离上次写博客有两天空闲时间,之间就跟着极客学院写了一个小小的程序,但是其中有很多知识可以学到的,这里我就把小游戏给分享出来;

  首先看一下我这个项目的结构如下图:


由于游戏还是第一个版本,只实现了核心部分,所以代码量较少,结构简单。

游戏分为以下四个步骤进行实现的:Ⅰ 游戏的布局即UI。 Ⅱ 游戏的卡片实现(可以改变数字的卡片)。 Ⅲ游戏逻辑的实现(最主要的部分)。Ⅳ分数计算(也有知识点)

接下来 我就先说下游戏的布局吧。游戏界面主要采用线性布局LinearLayout 上部分是分数 下部分是游戏的主界面。在主界面有采用了GridLayout网格布局 (这里要替的是网格布局是自定义的 其实用原来的也是可以实现的) 这样就实现了布局 :



代码量如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.game2048.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/score"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvScore"
/>
</LinearLayout>
<com.example.administrator.game2048.GameView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/gameview"
>

</com.example.administrator.game2048.GameView>
</LinearLayout>


接下来 是卡片类的实现了:卡片类就是Card这个类  继承了FrameLayout(其实这里要提的就是这个,不管什么布局都可以 主要是给数字一个存放的区域),然后在定义一个TextView 用来存放数字,通过addView这个方法把TextView中的值加到这个布局中去,并且填充,然后设置背景颜色就类似于一张卡片了。具体代码如下:
package com.example.administrator.game2048;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.TextView;

/**
* Created by Administrator on 2016/2/20 0020.
*/
public class Card extends FrameLayout {

private TextView label;//在每张卡片的布局上存放数字的控件
private int num = 0;

public Card(Context context) {
super(context);
label = new TextView(getContext());
label.setTextSize(32);
label.setBackgroundColor(0x33ffffff);
label.setGravity(Gravity.CENTER);
LayoutParams lp = new LayoutParams(-1, -1);//设置成-1 是填充布局的意思
lp.setMargins(5, 5, 5, 5);
this.addView(label, lp);
setNum(0);
}

public int getNum() {
return num;
}

public void setNum(int num) {
this.num = num;
if (num <= 0) {
label.setText("");
} else {
label.setText(num + "");
}
}
//根据数字不同改变其卡片的背景颜色
public void setBGColor(int number){
switch (number){
case 0:
case 2:
label.setBackgroundColor(0x33ffffff);
break;
case 4:
label.setBackgroundColor(0x33ffffff);
break;
case 8:
label.setBackgroundColor(Color.rgb(218,112,214));
break;
case 16:
label.setBackgroundColor(Color.rgb(139,0,139));
break;
case 32:
label.setBackgroundColor(Color.rgb(106,90,205));
break;
case 64:
label.setBackgroundColor(Color.rgb(65,105,225));
break;
case 128:
label.setBackgroundColor(Color.rgb(0,250,154));
break;
case 256:
label.setBackgroundColor(Color.rgb(127,255,0));
break;
case 512:
label.setBackgroundColor(Color.rgb(255,215,0));
break;
case 1024:
label.setBackgroundColor(Color.rgb(255,0,0));
break;
case 2048:
label.setBackgroundColor(Color.rgb(255,228,215));
break;
case 4096:
label.setBackgroundColor(Color.rgb(0,100,0));
break;
case 9182:
label.setBackgroundColor(Color.rgb(0,0,0));
break;
}

}
}setBGColor那个函数顾名思义 根据每个卡片上的数字设置背景色。其余的函数大家都能看得懂就不做过多的说明了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 游戏2048