适合新手学习的数字拼图的源代码文件,欢迎大家来借鉴
package com.example.mygame;
import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private LinearLayout ll;
private TextView[][] tv_group;
private int number[]=new int[9] ;
Vibrator mVibrator;
private TextView tv_socer;
private Button btnrestart;
private int socer = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int i=0;i<9;i ){
number[i]=DataHelper.number[i];
}
initView();
initGame();
tv_socer.setText("移动了 " String.valueOf(socer) " 步");
mVibrator = (Vibrator)getApplication().getSystemService(VIBRATOR_SERVICE);
ll.setOnTouchListener(new OnTouchListener() {
private int x1, x2;
private int y1, y2;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x1 = (int) event.getX();
y1 = (int) event.getY();
break;
case MotionEvent.ACTION_UP:
x2 = (int) event.getX();
y2 = (int) event.getY();
if (y2 - y1 > 20 && y2 - y1 > Math.abs(x2 - x1)) {// 判断手势方向
System.out.println("下");
down();
overGame();
initGame();
} else if (y1 - y2 > 20 && y1 - y2 > Math.abs(x2 - x1)) {
System.out.println("上");
up();
overGame();
initGame();
} else if (x2 - x1 > 20 && x2 - x1 > Math.abs(y2 - y1)) {
System.out.println("左");
left();
overGame();
initGame();
} else if (x1 - x2 > 20 && x1 - x2 > Math.abs(y2 - y1)) {
System.out.println("右");
right();
overGame();
initGame();
}
break;
}
return true;
}
});
btnrestart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i=0;i<9;i ){
number[i]=DataHelper.number[i];
}
initGame();
socer = 0 ;
tv_socer.setText("移动了 " String.valueOf(socer) " 步");
}
});
}
protected void left() {
int numzero = FindZero();
int exchanged;
if(numzero%3==0){
//定义震动
mVibrator.vibrate( new long[]{100,10,100,200}, -1); //第一个{}里面是节奏数组, 第二个参数是重复次数,-1为不重复,非-1俄日从pattern的指定下标开始重复
}else{
exchanged = number[numzero];
number[numzero]=number[numzero-1];
number[numzero-1]=exchanged;
socer = socer 1;
tv_socer.setText("移动了 " String.valueOf(socer) " 步");
}
}
protected void right() {
int numzero = FindZero();
int exchanged;
if(numzero%3==2){
//定义震动
mVibrator.vibrate( new long[]{100,10,100,200}, -1); //第一个{}里面是节奏数组, 第二个参数是重复次数,-1为不重复,非-1俄日从pattern的指定下标开始重复
}else{
exchanged = number[numzero];
number[numzero]=number[numzero 1];
number[numzero 1]=exchanged;
socer = socer 1;
tv_socer.setText("移动了 " String.valueOf(socer) " 步");
}
}
protected void up() {
int numzero = FindZero();
int exchanged;
if(numzero>5){
//定义震动
mVibrator.vibrate( new long[]{100,10,100,200}, -1); //第一个{}里面是节奏数组, 第二个参数是重复次数,-1为不重复,非-1俄日从pattern的指定下标开始重复
}else{
exchanged = number[numzero];
number[numzero]=number[numzero 3];
number[numzero 3]=exchanged;
socer = socer 1;
tv_socer.setText("移动了 " String.valueOf(socer) " 步");
}
}
public void down() {
int numzero = FindZero();
int exchanged;
if(numzero<3){
//定义震动
mVibrator.vibrate( new long[]{100,10,100,200}, -1); //第一个{}里面是节奏数组, 第二个参数是重复次数,-1为不重复,非-1俄日从pattern的指定下标开始重复
}else{
exchanged = number[numzero];
number[numzero]=number[numzero-3];
number[numzero-3]=exchanged;
socer = socer 1;
tv_socer.setText("移动了 " String.valueOf(socer) " 步");
}
}
public int FindZero(){
for(int i=0;i<9;i ){
if(number[i]==0){
return i;
}
}
return 0;
}
private void initView() {
tv_group = new TextView[3][3];
ll= (LinearLayout)findViewById(R.id.ll);
tv_group[0][0] =(TextView) findViewById(R.id.tv_1);
tv_group[0][1] = (TextView)findViewById(R.id.tv_2);
tv_group[0][2] = (TextView)findViewById(R.id.tv_3);
tv_group[1][0] = (TextView)findViewById(R.id.tv_4);
tv_group[1][1] = (TextView)findViewById(R.id.tv_5);
tv_group[1][2] = (TextView)findViewById(R.id.tv_6);
tv_group[2][0] = (TextView)findViewById(R.id.tv_7);
tv_group[2][1] = (TextView)findViewById(R.id.tv_8);
tv_group[2][2] = (TextView)findViewById(R.id.tv_9);
tv_socer = (TextView)findViewById(R.id.tv_socer);
btnrestart=(Button)findViewById(R.id.btn_restart);
}
public void initGame(){
for(int i=0 ;i<3;i ){
for(int j=0;j<3;j ){
if(number[i*3 j]==0){
tv_group[i][j].setText(" ");
}else{
tv_group[i][j].setText(String.valueOf(number[i*3 j]));
}
}
}
}
public void overGame(){
int total=0;
for(int i = 0;i<7;i ){
if(number[i 1]-number[i]==1){
total=total 1;
}
}
if(total==7&&number[8]==0){
Toast.makeText(MainActivity.this, "GameOver", 0).show();
for(int i=0;i<9;i ){
number[i]=DataHelper.number[i];
}
socer = 0 ;
tv_socer.setText("移动了 " String.valueOf(socer) " 步");
System.out.println("GameOver");
}
}
}
资源文件列表
MyGame/.classpath , 475
MyGame/.project , 854
MyGame/.settings/org.eclipse.jdt.core.prefs , 177
MyGame/AndroidManifest.xml , 949
MyGame/bin/AndroidManifest.xml , 949
MyGame/bin/classes/com/example/mygame/BuildConfig.class , 345
MyGame/bin/classes/com/example/mygame/DataHelper.class , 427
MyGame/bin/classes/com/example/mygame/MainActivity$1.class , 1842
MyGame/bin/classes/com/example/mygame/MainActivity$2.class , 1614
MyGame/bin/classes/com/example/mygame/MainActivity.class , 5138
MyGame/bin/classes/com/example/mygame/R$attr.class , 340
MyGame/bin/classes/com/example/mygame/R$color.class , 508
MyGame/bin/classes/com/example/mygame/R$dimen.class , 461
MyGame/bin/classes/com/example/mygame/R$drawable.class , 437
MyGame/bin/classes/com/example/mygame/R$id.class , 771
MyGame/bin/classes/com/example/mygame/R$layout.class , 403
MyGame/bin/classes/com/example/mygame/R$menu.class , 388
MyGame/bin/classes/com/example/mygame/R$string.class , 472
MyGame/bin/classes/com/example/mygame/R$style.class , 431
MyGame/bin/classes/com/example/mygame/R.class , 721
MyGame/bin/classes.dex , 561620
MyGame/bin/dexedLibs/android-support-v4-4f8f7e5d4808e366c95e3f73f43957bf.jar , 189231
MyGame/bin/javaapk.com-MyGame.apk , 246979
MyGame/bin/res/drawable-hdpi/ic_launcher.png , 7537
MyGame/bin/res/drawable-mdpi/ic_launcher.png , 3815
MyGame/bin/res/drawable-xhdpi/ic_launcher.png , 11728
MyGame/bin/res/drawable-xxhdpi/ic_launcher.png , 22091
MyGame/bin/resources.ap_ , 51487
MyGame/gen/com/example/mygame/BuildConfig.java , 160
MyGame/gen/com/example/mygame/R.java , 3488
MyGame/ic_launcher-web.png , 89229
MyGame/libs/android-support-v4.jar , 484258
MyGame/proguard-project.txt , 781
MyGame/project.properties , 563
MyGame/res/drawable/border.xml , 383
MyGame/res/drawable-hdpi/ic_launcher.png , 8089
MyGame/res/drawable-mdpi/ic_launcher.png , 3976
MyGame/res/drawable-xhdpi/ic_launcher.png , 13075
MyGame/res/drawable-xxhdpi/ic_launcher.png , 26061
MyGame/res/layout/activity_main.xml , 4733
MyGame/res/menu/main.xml , 263
MyGame/res/values/colors.xml , 264
MyGame/res/values/dimens.xml , 220
MyGame/res/values/strings.xml , 223
MyGame/res/values/styles.xml , 697
MyGame/res/values-sw600dp/dimens.xml , 203
MyGame/res/values-sw720dp-land/dimens.xml , 277
MyGame/res/values-v11/styles.xml , 334
MyGame/res/values-v14/styles.xml , 391
MyGame/src/com/example/mygame/DataHelper.java , 128
MyGame/src/com/example/mygame/MainActivity.java , 5758
MyGame/说明.htm , 4317
