安卓Andriod使用入门(三)【2048游戏】

时间:2022-01-21 22:43:52

天才,就其本质而说,只不过是一种对事业、对工作过盛的热爱而已。——高尔基


game2048Activity.java代码:

package siso.geekworld;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;

import game.domain.AnimLayer;
import game.service.GameService;

public class game2048Activity extends ActionBarActivity {
private TextView tv_bestRecord;
private int score;
private TextView tv_score;
private static game2048Activity game2048activity = null;
private AnimLayer animLayer = null;
public AnimLayer getAnimLayer() {
return animLayer;
}
public game2048Activity(){
game2048activity = this;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game2048);
tv_score = (TextView) findViewById(R.id.tv_score);
animLayer = (AnimLayer) findViewById(R.id.animLayer);
tv_bestRecord = (TextView) findViewById(R.id.bestRecord);
}
public void showBestRecord(){
tv_bestRecord.setText(GameService.getBest()+"");
}
public void clearScore(){
score = 0;
showScore();
}
public void showScore() {
tv_score.setText(score+"");
}
public void addScore(int s){
score += s;
showScore();
}
public int getScore(){
return score;
}
public static game2048Activity getgame2048Activity() {
return game2048activity;
}

}

activity_game2048.xml内容:

<?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="siso.geekworld.game2048Activity" >


<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal" >


<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ffb3b3b3"
android:gravity="center_horizontal"
android:orientation="vertical" >


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/score"
android:textColor="#ffffffff"
android:textSize="25sp" />


<TextView
android:id="@+id/tv_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffffff"
android:textSize="25sp" />

</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ffE9DC77"
android:gravity="center_horizontal"
android:orientation="vertical" >


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffffff"
android:text="@string/best"
android:textSize="25sp" />


<TextView
android:id="@+id/bestRecord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffffff"
android:textSize="25sp" />

</LinearLayout>
</LinearLayout>

<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >


<game.domain.GameView
android:id="@+id/gameView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

</game.domain.GameView>

<game.domain.AnimLayer
android:id="@+id/animLayer"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

</game.domain.AnimLayer>
</FrameLayout>

</LinearLayout>

strings.xml内容:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name" translatable="false">2048</string>
<string name="hello_world" translatable="false">Hello world!</string>
<string name="action_settings" translatable="false">Settings</string>
<string name="score" translatable="false">得分</string>
<string name="best" translatable="false">最高分</string>
<string name="enterAnimation" translatable="false">An Animation during enter the game</string>
<string name="sign" translatable="false">©2014.10&#160;&#160;&#160;&#160;作者&#160;&#160;&#160;&#160;lz</string>

</resources>

styles.xml内容:

<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light"> </style>

<style name="AppTheme" parent="AppBaseTheme">
</style>

</resources>

其他层,结构,类

安卓Andriod使用入门(三)【2048游戏】

运行结果如图:

安卓Andriod使用入门(三)【2048游戏】