码迷,mamicode.com
首页 > 移动开发 > 详细

Android --- 斗地主 [牌桌实现源码]

时间:2016-08-28 22:30:35      阅读:404      评论:0      收藏:0      [点我收藏+]

标签:

1、主Activity

[java] view plain copy
 
 print?
  1. <span style="font-size:18px;color:#3333ff;">package com.bison;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.pm.ActivityInfo;  
  5. import android.os.Bundle;  
  6. import android.view.Window;  
  7. import android.view.WindowManager;  
  8.   
  9. /**  
  10.  * </span>  
[java] view plain copy
 
 print?
  1. <span style="font-size:18px;color:#3333ff;"> * @author Bison  
  2.  *   
  3.  */  
  4. public class PukeActivity extends Activity {  
  5.     /** Called when the activity is first created. */  
  6.     @Override  
  7.     public void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         // 这个事隐藏标题栏,不解释  
  10.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  11.         // 隐藏状态栏,你懂的  
  12.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
  13.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  14.         /* 
  15.          * 开始有考虑使屏幕上扑克的排列随屏幕的分辨率变动 结果貌似不好做,注释掉了 Display display = 
  16.          * getWindowManager().getDefaultDisplay(); int screenWidth = 
  17.          * display.getWidth(); int screenHeight = display.getHeight(); 
  18.          */  
  19.   
  20.         // 使用代码锁定横屏  
  21.         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  
  22.         // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);这个是竖屏  
  23.         setContentView(new GameView(this));  
  24.     }  
  25. }</span>  

2、牌桌页面

[java] view plain copy
 
 print?
  1. <span style="color:#3333ff;">package com.bison;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.BitmapFactory;  
  6. import android.graphics.Canvas;  
  7. import android.graphics.Rect;  
  8. import android.view.MotionEvent;  
  9. import android.view.SurfaceHolder;  
  10. import android.view.SurfaceView;  
  11.   
  12. import com.bison.utils.Person;  
  13.   
  14. /** 
  15.  *  
  16.  * @author Bison 
  17.  *  
  18.  */  
  19. public class GameView extends SurfaceView implements SurfaceHolder.Callback {  
  20.     private FlushThread thread = null;// 刷帧线程  
  21.     private Bitmap sourceBitmap = null;// 扑克图片来源  
  22.     private Bitmap backgroundDesk = null;// 牌桌背景  
  23.     private Bitmap backgroundPuke = null;// 扑克背面  
  24.   
  25.     private final Person person;  
  26.     private int pukeWidth = 0;// 扑克的宽  
  27.     private int pukeHeight = 0;// 扑克的高  
  28.     private int deskWidth = 0;// 牌桌的宽  
  29.     private int deskHeight = 0;// 牌桌的高  
  30.     private int left = 0;// 我自己首张牌左距离  
  31.   
  32.     public GameView(Context context) {  
  33.         super(context);  
  34.         getHolder().addCallback(this);  
  35.         this.thread = new FlushThread(getHolder(), this);// 实例化线程  
  36.         initBitmap();// 实例化图片  
  37.         this.person = new Person();// 实例化Person类  
  38.         this.left = deskWidth / 2 - (16 * 25 + pukeWidth) / 2;// 左距开始时赋值  
  39.     }  
  40.   
  41.     private void initBitmap() {// 初始化图片  
  42.         sourceBitmap = BitmapFactory.decodeResource(getResources(),  
  43.                 R.drawable.smallcard);  
  44.         pukeWidth = sourceBitmap.getWidth() / 14;// 每张扑克的宽高  
  45.         pukeHeight = sourceBitmap.getHeight() / 4;  
  46.   
  47.         backgroundDesk = BitmapFactory.decodeResource(getResources(),  
  48.                 R.drawable.gameback2);  
  49.   
  50.         deskWidth = backgroundDesk.getWidth();// 牌桌的宽高  
  51.         deskHeight = backgroundDesk.getHeight();  
  52.   
  53.         backgroundPuke = BitmapFactory.decodeResource(getResources(),  
  54.                 R.drawable.cardback);  
  55.     }  
  56.   
  57.     @Override  
  58.     protected void onDraw(Canvas canvas) {  
  59.         // 绘制牌桌  
  60.         canvas.drawBitmap(backgroundDesk, 0, 0, null);  
  61.         personPaint(canvas, pukeWidth, pukeHeight);  
  62.         deskthreePukes(canvas, pukeWidth, pukeHeight);  
  63.     }  
  64.   
  65.     /** 绘制每个玩家手里的牌 */  
  66.     public void personPaint(Canvas c, int pukeWidth, int pukeHeight) {  
  67.         Rect src = new Rect();  
  68.         Rect dst = new Rect();  
  69.   
  70.         // 遍历数组  
  71.         for (int i = 0; i < 3; i++) {  
  72.             for (int j = 0; j < 17; j++) {  
  73.                 if (i == 0) {// 左手边玩家,不用绘出正面  
  74.                     // src = person.cardRect(person.person1[j], pukeWidth,  
  75.                     // pukeHeight);  
  76.                     // dst.set(10, j * 20, 10 + pukeWidth, j * 20 + pukeHeight);  
  77.                     c.drawBitmap(backgroundPuke, 35, 85, null);  
  78.                 }  
  79.                 if (i == 1) {// 自己  
  80.                     src = person.cardRect(person.person2[j], pukeWidth,  
  81.                             pukeHeight);  
  82.                     dst.set(left + j * 25, this.deskHeight - 20 - pukeHeight,  
  83.                             left + j * 25 + pukeWidth, deskHeight - 20);  
  84.                     c.drawBitmap(sourceBitmap, src, dst, null);  
  85.                 }  
  86.                 if (i == 2) {// 右手边玩家,同样不用绘出正面  
  87.                     // src = person.cardRect(person.person3[j], pukeWidth,  
  88.                     // pukeHeight);  
  89.                     // dst.set(this.screenWidth - 10 - pukeWidth, j * 20,  
  90.                     // this.screenWidth - 10, j * 20 + pukeHeight);  
  91.                     c.drawBitmap(backgroundPuke, deskWidth - 35 - pukeWidth,  
  92.                             85, null);  
  93.                 }  
  94.             }  
  95.         }  
  96.     }  
  97.   
  98.     /** 绘制三张底牌 */  
  99.     private void deskthreePukes(Canvas c, int pukeWidth, int pukeHeight) {  
  100.         Rect src = new Rect();  
  101.         Rect dst = new Rect();  
  102.         for (int i = 0; i < 3; i++) {  
  103.             src = person.cardRect(person.threePukes[i], pukeWidth, pukeHeight);  
  104.             dst.set(280 + i * pukeWidth, 12, 280 + (i + 1) * pukeWidth,  
  105.                     12 + pukeHeight);  
  106.             c.drawBitmap(sourceBitmap, src, dst, null);  
  107.         }  
  108.     }  
  109.   
  110.     @Override  
  111.     public boolean onTouchEvent(MotionEvent event) {  
  112.         // 正在研究点击弹出相应的扑克  
  113.         return super.onTouchEvent(event);  
  114.     }  
  115.   
  116.     @Override  
  117.     public void surfaceChanged(SurfaceHolder holder, int format, int width,  
  118.             int height) {  
  119.     }  
  120.   
  121.     @Override  
  122.     public void surfaceCreated(SurfaceHolder holder) {  
  123.         this.thread.setFlag(true);  
  124.         this.thread.start();  
  125.     }  
  126.   
  127.     @Override  
  128.     public void surfaceDestroyed(SurfaceHolder holder) {  
  129.         boolean retry = true;  
  130.         this.thread.setFlag(false);  
  131.         while (retry) {  
  132.             try {  
  133.                 thread.join();  
  134.                 retry = false;  
  135.             } catch (InterruptedException e) {  
  136.                 e.printStackTrace();  
  137.             }  
  138.         }  
  139.   
  140.     }  
  141.   
  142.     // 刷帧线程</span>  
[java] view plain copy
 
 print?
  1. <span style="color:#3333ff;">   class FlushThread extends Thread {  
  2.         private boolean flag = false;  
  3.         private final int span = 500;  
  4.         private final GameView gameView;  
  5.         private final SurfaceHolder holder;  
  6.   
  7.         public FlushThread(SurfaceHolder holder, GameView gameView) {  
  8.             this.gameView = gameView;  
  9.             this.holder = holder;  
  10.         }  
  11.   
  12.         @Override  
  13.         public void run() {  
  14.             Canvas canvas;  
  15.             while (this.flag) {  
  16.                 canvas = null;  
  17.                 try {  
  18.                     canvas = this.holder.lockCanvas(null);  
  19.                     synchronized (this.holder) {  
  20.                         this.gameView.onDraw(canvas);  
  21.                     }  
  22.                 } finally {  
  23.                     if (canvas != null) {  
  24.                         this.holder.unlockCanvasAndPost(canvas);  
  25.                     }  
  26.                 }  
  27.   
  28.                 try {  
  29.                     Thread.sleep(span);  
  30.                 } catch (InterruptedException e) {  
  31.                     e.printStackTrace();  
  32.                 }  
  33.             }  
  34.         }  
  35.   
  36.         public boolean isFlag() {  
  37.             return flag;  
  38.         }  
  39.   
  40.         public void setFlag(boolean flag) {  
  41.             this.flag = flag;  
  42.         }  
  43.   
  44.     }  
  45.   
  46. }  
  47. </span>  


 

3、相关实体类

扑克牌类:

[java] view plain copy
 
 print?
  1. <span style="font-size:18px;color:#3333ff;">package com.bison.utils;  
  2.   
  3. import java.util.Random;  
  4.   
  5. /** 
  6.  * 生成一副洗好的牌,并且 设计为单例模式 
  7.  *  
  8.  * @author Bison 
  9.  *  
  10.  */  
  11. public class Cards {  
  12.     // 声明一副扑克牌  
  13.     public int[] pukes = new int[54];  
  14.   
  15.     private static Cards cardsInstance = null;  
  16.   
  17.     private Cards() {  
  18.         setPuke();  
  19.         shuffle();  
  20.     }  
  21.   
  22.     public static Cards getInstance() {  
  23.         if (cardsInstance == null) {  
  24.             cardsInstance = new Cards();  
  25.         }  
  26.         return cardsInstance;  
  27.     }  
  28.   
  29.     /** 给54张扑克牌赋值 :1~54 */  
  30.     private void setPuke() {  
  31.         for (int i = 0; i < 54; i++) {  
  32.             pukes[i] = i + 1;  
  33.         }  
  34.     }  
  35.   
  36.     /** 洗牌 */  
  37.     private void shuffle() {  
  38.         Random rdm = new Random();  
  39.         for (int i = 0; i < 54; i++) {  
  40.             // random.nextInt();是个前闭后开的方法:0~53  
  41.             int rdmNo = rdm.nextInt(54);  
  42.             int temp = pukes[i];  
  43.             pukes[i] = pukes[rdmNo];  
  44.             pukes[rdmNo] = temp;  
  45.         }  
  46.     }  
  47. }  
  48. </span>  

 

玩家类:

[java] view plain copy
 
 print?
  1. <span style="font-size:18px;color:#3333ff;">package com.bison.utils;  
  2.   
  3. import android.graphics.Rect;  
  4.   
  5. /** 
  6.  * 这个是玩家的实体类 
  7.  *  
  8.  * @author Bison 
  9.  *  
  10.  */  
  11. public class Person {  
  12.     private final Cards mCards = Cards.getInstance();  
  13.   
  14.     public int[] person1 = new int[17];  
  15.     public int[] person2 = new int[17];  
  16.     public int[] person3 = new int[17];  
  17.   
  18.     // 余下三张属于地主的  
  19.     public int[] threePukes = new int[3];  
  20.   
  21.     public Person() {  
  22.         personHold(mCards.pukes);  
  23.     }  
  24.   
  25.     /** 分牌 */  
  26.     private void personHold(int[] pukes) {  
  27.         int k = 0;  
  28.         for (int i = 0; i < 3; i++) {  
  29.             if (i == 0) {  
  30.                 for (int j = 0; j < 17; j++) {  
  31.                     person1[j] = pukes[k++];  
  32.                 }  
  33.                 // 将其排序  
  34.                 sort(person1);  
  35.             }  
  36.             if (i == 1) {  
  37.                 for (int j = 0; j < 17; j++) {  
  38.                     person2[j] = pukes[k++];  
  39.                 }  
  40.                 // 将其排序  
  41.                 sort(person2);  
  42.             }  
  43.             if (i == 2) {  
  44.                 for (int j = 0; j < 17; j++) {  
  45.                     person3[j] = pukes[k++];  
  46.                 }  
  47.                 // 将其排序  
  48.                 sort(person3);  
  49.             }  
  50.         }  
  51.   
  52.         threePukes[0] = pukes[51];  
  53.         threePukes[1] = pukes[52];  
  54.         threePukes[2] = pukes[53];  
  55.     }  
  56.   
  57.     /** 对每个玩家手里的牌排序:使用冒泡排序 */  
  58.     private void sort(int[] ary) {  
  59.         for (int i = 0; i < ary.length; i++) {  
  60.             for (int j = 0; j < ary.length - i - 1; j++) {  
  61.                 if (ary[j] > ary[j + 1]) {  
  62.                     int temp = ary[j];  
  63.                     ary[j] = ary[j + 1];  
  64.                     ary[j + 1] = temp;  
  65.                 }  
  66.             }  
  67.         }  
  68.     }  
  69.   
  70.     /** 
  71.      * 对应扑克所在图片上的位置  
  72.      * 1 5 9 ………… 53  
  73.      * 2 6 10 ………… 54  
  74.      * 3 7 11  
  75.      * 4 8 12 
  76.      */  
  77.     public Rect cardRect(int cardValue, int width, int height) {  
  78.         int x = 0, y = 0;  
  79.         if (cardValue % 4 == 0) {  
  80.             x = cardValue / 4 - 1;  
  81.             y = 4;  
  82.         } else {  
  83.             x = cardValue / 4;  
  84.             y = cardValue % 4;  
  85.         }  
  86.   
  87.         int left = x * width;  
  88.         int top = (y - 1) * height;  
  89.         int right = (x + 1) * width;  
  90.         int bottom = (y) * height;  
  91.         return new Rect(left, top, right, bottom);  
  92.     }  
  93. }  
  94. </span>  


 

PS:斗地主还是可以做成很复杂的。相关图片

技术分享

 
 

Android --- 斗地主 [牌桌实现源码]

标签:

原文地址:http://www.cnblogs.com/endv/p/5816099.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!