以翻纸牌为游戏背景
准备纸牌图片,我的是以 b(黑桃) h(红桃)m(梅花)f(方片)以数字代表值 b1==黑桃A.....
package com.youxi.zhipai.bean;
public class Card {
// 大小,从1到13,A到K,16表示王
private Integer value;
/**
* 一般指状态,1位显示背面,3位正面
*/
private int hse;
private int img;
// 名称,暂时不用
private int name;
// 花色4黑,3红,2花,1方
private Integer color;
private int id;
private boolean firstmove;
public Integer getValue() {
return value;
}
public void setValue(Integer value) {
this.value = value;
}
public int getHse() {
return hse;
}
public void setHse(int hse) {
this.hse = hse;
}
public int getImg() {
return img;
}
public void setImg(int img) {
this.img = img;
}
public int getName() {
return name;
}
public void setName(int name) {
this.name = name;
}
public Integer getColor() {
return color;
}
public void setColor(Integer color) {
this.color = color;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public boolean isFirstmove() {
return firstmove;
}
public void setFirstmove(boolean firstmove) {
this.firstmove = firstmove;
}
public Card(Integer value, int hse, int img, int name, Integer color, int id, boolean firstmove) {
super();
this.value = value;
this.hse = hse;
this.img = img;
this.name = name;
this.color = color;
this.id = id;
this.firstmove = firstmove;
}
public Card() {
super();
// TODO Auto-generated constructor stub
}
// @Override
// public String toString() {
// return "Card [value=" + value + ", hse=" + hse + ", img=" + img + ", name=" + name + ", color=" + color
// + ", id=" + id + ", firstmove=" + firstmove + "]";
// }
@Override
public String toString() {
return "{"+"\"value\":" + value + ", \"hse\":" + hse + ", \"img\":" + img + ", \"name\":" + name + ", \"color\":" + color
+ ", \"id\":" + id + ", \"firstmove\":" + firstmove + "}";
}
}
package com.youxi.zhipai.fanfankan;
import java.util.List;
import com.example.myzhipai.R;
import com.youxi.zhipai.bean.Card;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class MyGridViewAdapter extends BaseAdapter {
private int[] dakaids = { R.drawable.b1, R.drawable.b2, R.drawable.b3, R.drawable.b4, R.drawable.b5, R.drawable.b6,
R.drawable.b7, R.drawable.b8, R.drawable.b9, R.drawable.b10, R.drawable.b11, R.drawable.b12, R.drawable.b13,
R.drawable.h1, R.drawable.h2, R.drawable.h3, R.drawable.h4, R.drawable.h5, R.drawable.h6, R.drawable.h7,
R.drawable.h8, R.drawable.h9, R.drawable.h10, R.drawable.h11, R.drawable.h12, R.drawable.h13, R.drawable.m1,
R.drawable.m2, R.drawable.m3, R.drawable.m4, R.drawable.m5, R.drawable.m6, R.drawable.m7, R.drawable.m8,
R.drawable.m9, R.drawable.m10, R.drawable.m11, R.drawable.m12, R.drawable.m13, R.drawable.f1, R.drawable.f2,
R.drawable.f3, R.drawable.f4, R.drawable.f5, R.drawable.f6, R.drawable.f7, R.drawable.f8, R.drawable.f9,
R.drawable.f10, R.drawable.f11, R.drawable.f12, R.drawable.f13 };
private int[] kakingids={R.drawable.k1,R.drawable.k2};
LayoutInflater inflater;
private List
private Context context;
public MyGridViewAdapter(List
super();
this.data = data;
this.context = context;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View v, ViewGroup parent) {
ViewHolder vh;
if (v == null) {
v = inflater.inflate(R.layout.fanfan_grid_item, null);
vh = new ViewHolder();
vh.image = (ImageView) v.findViewById(R.id.grid_item_imageview);
v.setTag(vh);
} else {
vh = (ViewHolder) v.getTag();
}
if (data.get(position).getHse()==1) {
vh.image.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.bei));
}else if (data.get(position).getHse()==2) {
if (data.get(position).getValue()==99) {
vh.image.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), kakingids[0]));
}else {
vh.image.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), dakaids[data.get(position).getImg()]));
}
}else {
vh.image.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.bai));
}
return v;
}
class ViewHolder {
private ImageView image;
}
}
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
>
android:layout_height="fill_parent"
android:keepScreenOn="true"
android:orientation="vertical" >
android:layout_height="40dp"
android:orientation="horizontal" >
android:layout_height="fill_parent"
android:layout_weight="1" />
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="3"
android:gravity="center"
android:text="10:00" />
android:layout_height="fill_parent"
android:layout_weight="1" />
android:layout_height="30dp"
android:orientation="horizontal" >
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/bac_button"
android:gravity="center"
android:text="初级" />
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/bac_button"
android:gravity="center"
android:text="中级" />
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/bac_button"
android:gravity="center"
android:text="高级" />
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:gravity="center"
android:horizontalSpacing="5dp"
android:listSelector="#ffffff"
android:numColumns="4"
android:verticalSpacing="5dp" >
android:layout_width="400dp"
android:layout_height="400dp"
android:layout_marginLeft="100dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
package com.youxi.zhipai.fanfankan;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import com.ant.liao.GifView;
import com.ant.liao.GifView.GifImageType;
import com.example.myzhipai.MainActivity;
import com.example.myzhipai.R;
import com.youxi.zhipai.bean.Card;
import com.youxi.zhipai.utils.BaseActivity;
import com.youxi.zhipai.utils.BaseInterfaceAct;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
public class FanFanKan extends BaseActivity implements BaseInterfaceAct {
private int[] dakaids = { R.drawable.b1, R.drawable.b2, R.drawable.b3, R.drawable.b4, R.drawable.b5, R.drawable.b6,
R.drawable.b7, R.drawable.b8, R.drawable.b9, R.drawable.b10, R.drawable.b11, R.drawable.b12, R.drawable.b13,
R.drawable.h1, R.drawable.h2, R.drawable.h3, R.drawable.h4, R.drawable.h5, R.drawable.h6, R.drawable.h7,
R.drawable.h8, R.drawable.h9, R.drawable.h10, R.drawable.h11, R.drawable.h12, R.drawable.h13, R.drawable.m1,
R.drawable.m2, R.drawable.m3, R.drawable.m4, R.drawable.m5, R.drawable.m6, R.drawable.m7, R.drawable.m8,
R.drawable.m9, R.drawable.m10, R.drawable.m11, R.drawable.m12, R.drawable.m13, R.drawable.f1, R.drawable.f2,
R.drawable.f3, R.drawable.f4, R.drawable.f5, R.drawable.f6, R.drawable.f7, R.drawable.f8, R.drawable.f9,
R.drawable.f10, R.drawable.f11, R.drawable.f12, R.drawable.f13 };
private int[] kakingids = { R.drawable.k1, R.drawable.k2 };
private GridView grid;
List
private GifView gif;
int[] xuanzhong;
Vibrator vibrator;
Timer time;
TimerTask task;
/**
* 是否结束
*/
boolean finishfalg = false;
/**
* 是否是两次点击中的第一次 ,true 是第一次,false 不是第一次
*/
boolean dianFlag = true;
/**
* 记录上次启动的游戏级别等参数
*/
int[] lasttime = new int[3];
TextView chuji_tv, zhongji_tv, gaoji_tv, time_tv;
MyGridViewAdapter myadapter;
// 记录时间 秒数
int times = 0;
/**
* 牌序打乱后的牌
*/
private List
Handler handler = new Handler() {
public void handleMessage(Message msg) {
int flag = (Integer) msg.obj;
switch (flag) {
case 1:
times++;
time_tv.setText("用时" + times + "秒");
break;
case 2:
times = 0;
time_tv.setText("用时" + times + "秒");
gif.setVisibility(View.GONE);
break;
case 3:
gif.setVisibility(View.VISIBLE);
break;
case 4:
break;
}
};
};
/**
* 倒计时是否启动 true 启动, false 没启动
*/
boolean countflag = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_fanfankan);
// 竖屏
if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
data = new ArrayList
xuanzhong = new int[2];
grid = (GridView) findViewById(R.id.act_fanfankan_gridview);
grid.setNumColumns(4);
chuji_tv = (TextView) findViewById(R.id.act_fanfankan_chuji);
zhongji_tv = (TextView) findViewById(R.id.act_fanfankan_zhongji);
gaoji_tv = (TextView) findViewById(R.id.act_fanfankan_gaoji);
time_tv = (TextView) findViewById(R.id.act_fanfankan_time);
gif = (GifView) findViewById(R.id.gif111);
// 设置Gif图片源
gif.setGifImage(R.drawable.yanhua4);
// 设置显示的大小,拉伸或者压缩
gif.setShowDimension(800, 800);
// 设置加载方式:先加载后显示、边加载边显示、只显示第一帧再显示
gif.setGifImageType(GifImageType.COVER);
// 添加监听器
gif.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
StartGagme(lasttime[0], lasttime[1], lasttime[2]);
}
});
vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
// 初级点击事件
chuji_tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 上次是否结束,是直接开始,不是 终止计时器再开始
if (!finishfalg) {
time.cancel();
time = null;
task.cancel();
task = null;
}
StartGagme(28, 1, 4);
}
});
// 中级点击事件
zhongji_tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 上次是否结束,是直接开始,不是 终止计时器再开始
if (!finishfalg) {
time.cancel();
time = null;
task.cancel();
task = null;
}
StartGagme(54, 2, 6);
}
});
// 高级点击事件
gaoji_tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 上次是否结束,是直接开始,不是 终止计时器再开始
if (!finishfalg) {
time.cancel();
time = null;
task.cancel();
task = null;
}
StartGagme(104, 3, 8);
}
});
grid.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
// TODO Auto-generated method stub
if (newcards.get(position).getHse() == 3) {
// 如果已经扣牌 跳出点击
return;
}
// 如果已经结束,提示并跳出点击
if (finishfalg) {
Toast.makeText(FanFanKan.this, "游戏结束", Toast.LENGTH_SHORT).show();
return;
}
if (MainActivity.vibratorFlag) {
// 震动100毫秒
vibrator.vibrate(100);
}
// 点击翻牌
newcards.get(position).setHse(2);
myadapter.notifyDataSetChanged();
// 是否是第一次翻牌
if (dianFlag) {
// 是否正在判断上次翻牌
if (countflag) {
// 是 直接判断不用倒计时,并改为否
count.onFinish();
dianFlag = false;
}
// 记录这次翻牌
xuanzhong[0] = position;
dianFlag = false;
} else {
// 如果第二次翻牌和第一次不一样 记录 并对比
if (xuanzhong[0] != position) {
xuanzhong[1] = position;
DuiBi(xuanzhong);
dianFlag = true;
}
}
}
});
// 默认加载最低级别
StartGagme(28, 1, 4);
}
/**
* 开始游戏
*
* @param size
* 牌的张数
* @param level
* 游戏级别
* @param hang
* 要布几列
*/
public void StartGagme(int size, int level, int lie) {
// 将结束标识改为false
finishfalg = false;
// 记录这次游戏级别参数
lasttime[0] = size;
lasttime[1] = level;
lasttime[2] = lie;
// 用时归零
Message message = new Message();
message.obj = 2;
handler.sendMessage(message);
// 定义计时器
time = new Timer(true);
task = new TimerTask() {
public void run() {
Message message = new Message();
message.obj = 1;
handler.sendMessage(message);
}
};
// 发牌
newcards = new ArrayList
newcards = createRandomList(MakeCard(level), size);
grid.setNumColumns(lie);
myadapter = new MyGridViewAdapter(newcards, FanFanKan.this);
grid.setAdapter(myadapter);
// 计时器启动
time.schedule(task, 1000, 1000); // 延时1000ms后执行,1000ms执行一次
}
/**
* 判断游戏是否结束
*/
public void GameIsFinish() {
int falg = 0;
for (int i = 0; i < newcards.size(); i++) {
if (newcards.get(i).getHse() == 3) {
falg++;
Log.i("Log", falg + "========结束======falg");
if (falg == newcards.size()) {
finishfalg = true;
time.cancel();
time = null;
task.cancel();
task = null;
Message mgs = handler.obtainMessage();
mgs.obj = 3;
handler.sendMessage(mgs);
}
}
}
}
/**
* 0.5秒倒计时 ,倒计时结束时对比两张牌是否相同, 制作倒计时,方便玩家记牌。 要不然第二张还没点开就对比完了
*
*/
CountDownTimer count = new CountDownTimer(500, 500) {
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
}
@Override
public void onFinish() {
Log.i("Log", "执行一次");
if (newcards.get(xuanzhong[0]).getColor() == newcards.get(xuanzhong[1]).getColor()) {
if (newcards.get(xuanzhong[0]).getValue() == newcards.get(xuanzhong[1]).getValue()) {
newcards.get(xuanzhong[0]).setHse(3);
newcards.get(xuanzhong[1]).setHse(3);
myadapter.notifyDataSetChanged();
countflag = false;
GameIsFinish();
} else {
newcards.get(xuanzhong[0]).setHse(1);
newcards.get(xuanzhong[1]).setHse(1);
myadapter.notifyDataSetChanged();
countflag = false;
}
} else {
newcards.get(xuanzhong[0]).setHse(1);
newcards.get(xuanzhong[1]).setHse(1);
myadapter.notifyDataSetChanged();
countflag = false;
}
count.cancel();
}
};
/**
* 对比点开的两张牌
*
* @param xuanzhong
*/
public void DuiBi(final int[] xuanzhong) {
countflag = true;
count.start();
}
/**
* 制作黑桃
*
* @return
*/
public List
List
for (int i = 0; i < 13; i++) {
Card hei = new Card();
hei.setColor(1);
hei.setHse(1);
hei.setValue(i + 1);
hei.setImg(i);
Log.i("Log", "黑桃===" + (i + 1));
card.add(hei);
}
return card;
}
/**
* 制作红桃
*
* @return
*/
public List
List
for (int i = 0; i < 13; i++) {
Card hong = new Card();
hong.setColor(2);
hong.setHse(1);
hong.setValue(i + 1);
hong.setImg(i + 13);
Log.i("Log", "红桃===" + (i + 1));
card.add(hong);
}
return card;
}
/**
* 制作梅花
*
* @return
*/
public List
List
for (int i = 0; i < 13; i++) {
Card mei = new Card();
mei.setColor(3);
mei.setHse(1);
mei.setValue(i + 1);
mei.setImg(i + 26);
Log.i("Log", "梅花===" + (i + 1));
card.add(mei);
}
return card;
}
/**
* 制作方片
*
* @return
*/
public List
List
for (int i = 0; i < 13; i++) {
Card fang = new Card();
fang.setColor(4);
fang.setHse(1);
fang.setValue(i + 1);
fang.setImg(i + 39);
Log.i("Log", "方片===" + (i + 1));
card.add(fang);
}
return card;
}
/**
* 将list集合内容打散
*
* @param list
* 要打散的list
* @param n
* 要打散前多少个
* @return 返回打散后的list
*/
private List
Map
List
while (map.size() < n) {
int random = (int) (Math.random() * list.size());
if (!map.containsKey(random)) {
map.put(random, "");
listNew.add(list.get(random));
}
}
return listNew;
}
/**
* 制作牌
*
* @return
*/
public List
List
if (level == 1) {
cards = new ArrayList
cards.addAll(makeHei());
cards.addAll(makeHei());
Card k1 = new Card();
k1.setColor(5);
k1.setHse(1);
k1.setValue(99);
k1.setImg(0);
cards.add(k1);
Card k2 = new Card();
k2.setColor(5);
k2.setHse(1);
k2.setValue(99);
k2.setImg(0);
cards.add(k2);
Log.i("Log", cards.size() + "=======cards.size()");
return cards;
} else if (level == 2) {
cards = new ArrayList
cards.addAll(makeHei());
cards.addAll(makeHei());
cards.addAll(makeFang());
cards.addAll(makeFang());
Card k1 = new Card();
k1.setColor(5);
k1.setHse(1);
k1.setValue(99);
k1.setImg(0);
cards.add(k1);
Card k2 = new Card();
k2.setColor(5);
k2.setHse(1);
k2.setValue(99);
k2.setImg(0);
cards.add(k2);
Log.i("Log", cards.size() + "=======cards.size()");
return cards;
} else if (level == 3) {
cards = new ArrayList
cards.addAll(makeHei());
cards.addAll(makeHei());
cards.addAll(makeHong());
cards.addAll(makeHong());
cards.addAll(makeMei());
cards.addAll(makeMei());
cards.addAll(makeFang());
cards.addAll(makeFang());
Log.i("Log", cards.size() + "=======cards.size()");
}
return cards;
}
@Override
public void initViews() {
// TODO Auto-generated method stub
}
@Override
public void initDatas() {
// TODO Auto-generated method stub
}
@Override
public void initViewOper() {
// TODO Auto-generated method stub
}
@Override
public void IpText(String string) {
// TODO Auto-generated method stub
}
@Override
public void ServerText(String string) {
// TODO Auto-generated method stub
}
}
找一个gif图片作为完成的奖励吧