【实例简介】一款俄罗斯方块的源码,里面有三个模块,一个简单版,一个复杂版,还有一个扫雷。复杂版的可以暂停游戏还可以更换游戏背景跟主题。
【实例截图】
【核心代码】
package com.china.square;
import java.util.ArrayList;
import java.util.List;
import com.china.square.elos.Elos;
import com.china.square.eloscomplex.ElosComplex;
import com.china.square.elossap.ElosSap;
import com.china.square.linksee.LinkSee;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class Square extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PackageManager pm = getPackageManager();
Intent mainIntent = new Intent("SQUARE", null);
mainIntent.addCategory("android.intent.category.SQUARE");
List ls = pm.queryIntentActivities(mainIntent, 0);
int i = 0;
ArrayListal = new ArrayList();
setTitle(String.valueOf(ls.size()));
while (i < ls.size()){
ResolveInfo ri = ls.get(i);
al.add(ri.loadLabel(pm).toString());
i ;
}
ArrayAdaptera = new ArrayAdapter(this, android.R.layout.simple_list_item_1, al);
ListView lv = (ListView)this.findViewById(R.id.lv);
lv.setAdapter(a);
lv.setOnItemClickListener(new ItemClickListener());
}
private class ItemClickListener implements OnItemClickListener{
@Override
public void onItemClick(AdapterView> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String strText = ((TextView)arg1).getText().toString();
if (strText == null) return;
else if (strText.equals("俄罗斯方块")){
Intent in = new Intent(Square.this, Elos.class);
startActivity(in);
}else if (strText.equals("俄罗斯方块-复杂版")){
Intent in = new Intent(Square.this, ElosComplex.class);
startActivity(in);
}else if (strText.equals("连连看")){
Intent in = new Intent(Square.this, LinkSee.class);
startActivity(in);
}
else if (strText.equals("扫雷")){
Intent in = new Intent(Square.this, ElosSap.class);
startActivity(in);
}
}
}
}