发一个Android斗地主游戏的牌桌实现。
为了节约内存资源,每张扑克牌都是剪切形成的,当然这也是当前编程的主流方法。
1、主Activity
- packagecom.bison;
- importandroid.app.Activity;
- importandroid.content.pm.ActivityInfo;
- importandroid.os.Bundle;
- importandroid.view.Window;
- importandroid.view.WindowManager;
- /**
- *求某公司包养
- *
- *@authorBison
- *
- */
- publicclassPukeActivityextendsActivity{
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- //这个事隐藏标题栏,不解释
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- //隐藏状态栏,你懂的
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- /*
- *开始有考虑使屏幕上扑克的排列随屏幕的分辨率变动结果貌似不好做,注释掉了Displaydisplay=
- *getWindowManager().getDefaultDisplay();intscreenWidth=
- *display.getWidth();intscreenHeight=display.getHeight();
- */
- //使用代码锁定横屏
- setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
- //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);这个是竖屏
- setContentView(newGameView(this));
- }
- }
2、牌桌页面
- packagecom.bison;
- importandroid.content.Context;
- importandroid.graphics.Bitmap;
- importandroid.graphics.BitmapFactory;
- importandroid.graphics.Canvas;
- importandroid.graphics.Rect;
- importandroid.view.MotionEvent;
- importandroid.view.SurfaceHolder;
- importandroid.view.SurfaceView;
- importcom.bison.utils.Person;
- /**
- *牌桌,会被老婆骂,最好不要上去,你懂的
- *
- *扑克图片来源,和牌桌背景在文章的下面。扑克背面图等我没上传,玩家自行百度
- *
- *@authorBison
- *
- */
- publicclassGameViewextendsSurfaceViewimplementsSurfaceHolder.Callback{
- privateFlushThreadthread=null;//刷帧线程
- privateBitmapsourceBitmap=null;//扑克图片来源
- privateBitmapbackgroundDesk=null;//牌桌背景
- privateBitmapbackgroundPuke=null;//扑克背面
- privatefinalPersonperson;
- privateintpukeWidth=0;//扑克的宽
- privateintpukeHeight=0;//扑克的高
- privateintdeskWidth=0;//牌桌的宽
- privateintdeskHeight=0;//牌桌的高
- privateintleft=0;//我自己首张牌左距离
- publicGameView(Contextcontext){
- super(context);
- getHolder().addCallback(this);
- this.thread=newFlushThread(getHolder(),this);//实例化线程
- initBitmap();//实例化图片
- this.person=newPerson();//实例化Person类
- this.left=deskWidth/2-(16*25+pukeWidth)/2;//左距开始时赋值
- }
- privatevoidinitBitmap(){//初始化图片
- sourceBitmap=BitmapFactory.decodeResource(getResources(),
- R.drawable.smallcard);
- pukeWidth=sourceBitmap.getWidth()/14;//每张扑克的宽高
- pukeHeight=sourceBitmap.getHeight()/4;
- backgroundDesk=BitmapFactory.decodeResource(getResources(),
- R.drawable.gameback2);
- deskWidth=backgroundDesk.getWidth();//牌桌的宽高
- deskHeight=backgroundDesk.getHeight();
- backgroundPuke=BitmapFactory.decodeResource(getResources(),
- R.drawable.cardback);
- }
- @Override
- protectedvoidonDraw(Canvascanvas){
- //绘制牌桌
- canvas.drawBitmap(backgroundDesk,0,0,null);
- personPaint(canvas,pukeWidth,pukeHeight);
- deskthreePukes(canvas,pukeWidth,pukeHeight);
- }
- /**绘制每个玩家手里的牌*/
- publicvoidpersonPaint(Canvasc,intpukeWidth,intpukeHeight){
- Rectsrc=newRect();
- Rectdst=newRect();
- //遍历数组
- for(inti=0;i<3;i++){
- for(intj=0;j<17;j++){
- if(i==0){//左手边玩家,不用绘出正面
- //src=person.cardRect(person.person1[j],pukeWidth,
- //pukeHeight);
- //dst.set(10,j*20,10+pukeWidth,j*20+pukeHeight);
- c.drawBitmap(backgroundPuke,35,85,null);
- }
- if(i==1){//自己
- src=person.cardRect(person.person2[j],pukeWidth,
- pukeHeight);
- dst.set(left+j*25,this.deskHeight-20-pukeHeight,
- left+j*25+pukeWidth,deskHeight-20);
- c.drawBitmap(sourceBitmap,src,dst,null);
- }
- if(i==2){//右手边玩家,同样不用绘出正面
- //src=person.cardRect(person.person3[j],pukeWidth,
- //pukeHeight);
- //dst.set(this.screenWidth-10-pukeWidth,j*20,
- //this.screenWidth-10,j*20+pukeHeight);
- c.drawBitmap(backgroundPuke,deskWidth-35-pukeWidth,
- 85,null);
- }
- }
- }
- }
- /**绘制三张底牌*/
- privatevoiddeskthreePukes(Canvasc,intpukeWidth,intpukeHeight){
- Rectsrc=newRect();
- Rectdst=newRect();
- for(inti=0;i<3;i++){
- src=person.cardRect(person.threePukes[i],pukeWidth,pukeHeight);
- dst.set(280+i*pukeWidth,12,280+(i+1)*pukeWidth,
- 12+pukeHeight);
- c.drawBitmap(sourceBitmap,src,dst,null);
- }
- }
- @Override
- publicbooleanonTouchEvent(MotionEventevent){
- //正在研究点击弹出相应的扑克
- returnsuper.onTouchEvent(event);
- }
- @Override
- publicvoidsurfaceChanged(SurfaceHolderholder,intformat,intwidth,
- intheight){
- }
- @Override
- publicvoidsurfaceCreated(SurfaceHolderholder){
- this.thread.setFlag(true);
- this.thread.start();
- }
- @Override
- publicvoidsurfaceDestroyed(SurfaceHolderholder){
- booleanretry=true;
- this.thread.setFlag(false);
- while(retry){
- try{
- thread.join();
- retry=false;
- }catch(InterruptedExceptione){
- e.printStackTrace();
- }
- }
- }
- //刷帧线程,这个不解释,实在看不懂,M我:289302487@qq.com
- classFlushThreadextendsThread{
- privatebooleanflag=false;
- privatefinalintspan=500;
- privatefinalGameViewgameView;
- privatefinalSurfaceHolderholder;
- publicFlushThread(SurfaceHolderholder,GameViewgameView){
- this.gameView=gameView;
- this.holder=holder;
- }
- @Override
- publicvoidrun(){
- Canvascanvas;
- while(this.flag){
- canvas=null;
- try{
- canvas=this.holder.lockCanvas(null);
- synchronized(this.holder){
- this.gameView.onDraw(canvas);
- }
- }finally{
- if(canvas!=null){
- this.holder.unlockCanvasAndPost(canvas);
- }
- }
- try{
- Thread.sleep(span);
- }catch(InterruptedExceptione){
- e.printStackTrace();
- }
- }
- }
- publicbooleanisFlag(){
- returnflag;
- }
- publicvoidsetFlag(booleanflag){
- this.flag=flag;
- }
- }
- }
3、相关实体类
扑克牌类:
- packagecom.bison.utils;
- importjava.util.Random;
- /**
- *生成一副洗好的牌,并且设计为单例模式
- *
- *@authorBison
- *
- */
- publicclassCards{
- //声明一副扑克牌
- publicint[]pukes=newint[54];
- privatestaticCardscardsInstance=null;
- privateCards(){
- setPuke();
- shuffle();
- }
- publicstaticCardsgetInstance(){
- if(cardsInstance==null){
- cardsInstance=newCards();
- }
- returncardsInstance;
- }
- /**给54张扑克牌赋值:1~54*/
- privatevoidsetPuke(){
- for(inti=0;i<54;i++){
- pukes[i]=i+1;
- }
- }
- /**洗牌*/
- privatevoidshuffle(){
- Randomrdm=newRandom();
- for(inti=0;i<54;i++){
- //random.nextInt();是个前闭后开的方法:0~53
- intrdmNo=rdm.nextInt(54);
- inttemp=pukes[i];
- pukes[i]=pukes[rdmNo];
- pukes[rdmNo]=temp;
- }
- }
- }
玩家类:
- packagecom.bison.utils;
- importandroid.graphics.Rect;
- /**
- *这个是玩家的实体类
- *
- *@authorBison
- *
- */
- publicclassPerson{
- privatefinalCardsmCards=Cards.getInstance();
- publicint[]person1=newint[17];
- publicint[]person2=newint[17];
- publicint[]person3=newint[17];
- //余下三张属于地主的
- publicint[]threePukes=newint[3];
- publicPerson(){
- personHold(mCards.pukes);
- }
- /**分牌*/
- privatevoidpersonHold(int[]pukes){
- intk=0;
- for(inti=0;i<3;i++){
- if(i==0){
- for(intj=0;j<17;j++){
- person1[j]=pukes[k++];
- }
- //将其排序
- sort(person1);
- }
- if(i==1){
- for(intj=0;j<17;j++){
- person2[j]=pukes[k++];
- }
- //将其排序
- sort(person2);
- }
- if(i==2){
- for(intj=0;j<17;j++){
- person3[j]=pukes[k++];
- }
- //将其排序
- sort(person3);
- }
- }
- threePukes[0]=pukes[51];
- threePukes[1]=pukes[52];
- threePukes[2]=pukes[53];
- }
- /**对每个玩家手里的牌排序:使用冒泡排序*/
- privatevoidsort(int[]ary){
- for(inti=0;i<ary.length;i++){
- for(intj=0;j<ary.length-i-1;j++){
- if(ary[j]>ary[j+1]){
- inttemp=ary[j];
- ary[j]=ary[j+1];
- ary[j+1]=temp;
- }
- }
- }
- }
- /**
- *对应扑克所在图片上的位置
- *159…………53
- *2610…………54
- *3711
- *4812
- */
- publicRectcardRect(intcardValue,intwidth,intheight){
- intx=0,y=0;
- if(cardValue%4==0){
- x=cardValue/4-1;
- y=4;
- }else{
- x=cardValue/4;
- y=cardValue%4;
- }
- intleft=x*width;
- inttop=(y-1)*height;
- intright=(x+1)*width;
- intbottom=(y)*height;
- returnnewRect(left,top,right,bottom);
- }
- }
PS:斗地主还是可以做成很复杂的。相关图片

