`
bcyy
  • 浏览: 1830241 次
文章分类
社区版块
存档分类
最新评论

Android开发教程:斗地主 [牌桌实现源码]

 
阅读更多

发一个Android斗地主游戏的牌桌实现。

为了节约内存资源,每张扑克牌都是剪切形成的,当然这也是当前编程的主流方法。

1、主Activity

  1. packagecom.bison;
  2. importandroid.app.Activity;
  3. importandroid.content.pm.ActivityInfo;
  4. importandroid.os.Bundle;
  5. importandroid.view.Window;
  6. importandroid.view.WindowManager;
  7. /**
  8. *求某公司包养
  9. *
  10. *@authorBison
  11. *
  12. */
  13. publicclassPukeActivityextendsActivity{
  14. /**Calledwhentheactivityisfirstcreated.*/
  15. @Override
  16. publicvoidonCreate(BundlesavedInstanceState){
  17. super.onCreate(savedInstanceState);
  18. //这个事隐藏标题栏,不解释
  19. requestWindowFeature(Window.FEATURE_NO_TITLE);
  20. //隐藏状态栏,你懂的
  21. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  22. WindowManager.LayoutParams.FLAG_FULLSCREEN);
  23. /*
  24. *开始有考虑使屏幕上扑克的排列随屏幕的分辨率变动结果貌似不好做,注释掉了Displaydisplay=
  25. *getWindowManager().getDefaultDisplay();intscreenWidth=
  26. *display.getWidth();intscreenHeight=display.getHeight();
  27. */
  28. //使用代码锁定横屏
  29. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  30. //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);这个是竖屏
  31. setContentView(newGameView(this));
  32. }
  33. }

2、牌桌页面

  1. packagecom.bison;
  2. importandroid.content.Context;
  3. importandroid.graphics.Bitmap;
  4. importandroid.graphics.BitmapFactory;
  5. importandroid.graphics.Canvas;
  6. importandroid.graphics.Rect;
  7. importandroid.view.MotionEvent;
  8. importandroid.view.SurfaceHolder;
  9. importandroid.view.SurfaceView;
  10. importcom.bison.utils.Person;
  11. /**
  12. *牌桌,会被老婆骂,最好不要上去,你懂的
  13. *
  14. *扑克图片来源,和牌桌背景在文章的下面。扑克背面图等我没上传,玩家自行百度
  15. *
  16. *@authorBison
  17. *
  18. */
  19. publicclassGameViewextendsSurfaceViewimplementsSurfaceHolder.Callback{
  20. privateFlushThreadthread=null;//刷帧线程
  21. privateBitmapsourceBitmap=null;//扑克图片来源
  22. privateBitmapbackgroundDesk=null;//牌桌背景
  23. privateBitmapbackgroundPuke=null;//扑克背面
  24. privatefinalPersonperson;
  25. privateintpukeWidth=0;//扑克的宽
  26. privateintpukeHeight=0;//扑克的高
  27. privateintdeskWidth=0;//牌桌的宽
  28. privateintdeskHeight=0;//牌桌的高
  29. privateintleft=0;//我自己首张牌左距离
  30. publicGameView(Contextcontext){
  31. super(context);
  32. getHolder().addCallback(this);
  33. this.thread=newFlushThread(getHolder(),this);//实例化线程
  34. initBitmap();//实例化图片
  35. this.person=newPerson();//实例化Person类
  36. this.left=deskWidth/2-(16*25+pukeWidth)/2;//左距开始时赋值
  37. }
  38. privatevoidinitBitmap(){//初始化图片
  39. sourceBitmap=BitmapFactory.decodeResource(getResources(),
  40. R.drawable.smallcard);
  41. pukeWidth=sourceBitmap.getWidth()/14;//每张扑克的宽高
  42. pukeHeight=sourceBitmap.getHeight()/4;
  43. backgroundDesk=BitmapFactory.decodeResource(getResources(),
  44. R.drawable.gameback2);
  45. deskWidth=backgroundDesk.getWidth();//牌桌的宽高
  46. deskHeight=backgroundDesk.getHeight();
  47. backgroundPuke=BitmapFactory.decodeResource(getResources(),
  48. R.drawable.cardback);
  49. }
  50. @Override
  51. protectedvoidonDraw(Canvascanvas){
  52. //绘制牌桌
  53. canvas.drawBitmap(backgroundDesk,0,0,null);
  54. personPaint(canvas,pukeWidth,pukeHeight);
  55. deskthreePukes(canvas,pukeWidth,pukeHeight);
  56. }
  57. /**绘制每个玩家手里的牌*/
  58. publicvoidpersonPaint(Canvasc,intpukeWidth,intpukeHeight){
  59. Rectsrc=newRect();
  60. Rectdst=newRect();
  61. //遍历数组
  62. for(inti=0;i<3;i++){
  63. for(intj=0;j<17;j++){
  64. if(i==0){//左手边玩家,不用绘出正面
  65. //src=person.cardRect(person.person1[j],pukeWidth,
  66. //pukeHeight);
  67. //dst.set(10,j*20,10+pukeWidth,j*20+pukeHeight);
  68. c.drawBitmap(backgroundPuke,35,85,null);
  69. }
  70. if(i==1){//自己
  71. src=person.cardRect(person.person2[j],pukeWidth,
  72. pukeHeight);
  73. dst.set(left+j*25,this.deskHeight-20-pukeHeight,
  74. left+j*25+pukeWidth,deskHeight-20);
  75. c.drawBitmap(sourceBitmap,src,dst,null);
  76. }
  77. if(i==2){//右手边玩家,同样不用绘出正面
  78. //src=person.cardRect(person.person3[j],pukeWidth,
  79. //pukeHeight);
  80. //dst.set(this.screenWidth-10-pukeWidth,j*20,
  81. //this.screenWidth-10,j*20+pukeHeight);
  82. c.drawBitmap(backgroundPuke,deskWidth-35-pukeWidth,
  83. 85,null);
  84. }
  85. }
  86. }
  87. }
  88. /**绘制三张底牌*/
  89. privatevoiddeskthreePukes(Canvasc,intpukeWidth,intpukeHeight){
  90. Rectsrc=newRect();
  91. Rectdst=newRect();
  92. for(inti=0;i<3;i++){
  93. src=person.cardRect(person.threePukes[i],pukeWidth,pukeHeight);
  94. dst.set(280+i*pukeWidth,12,280+(i+1)*pukeWidth,
  95. 12+pukeHeight);
  96. c.drawBitmap(sourceBitmap,src,dst,null);
  97. }
  98. }
  99. @Override
  100. publicbooleanonTouchEvent(MotionEventevent){
  101. //正在研究点击弹出相应的扑克
  102. returnsuper.onTouchEvent(event);
  103. }
  104. @Override
  105. publicvoidsurfaceChanged(SurfaceHolderholder,intformat,intwidth,
  106. intheight){
  107. }
  108. @Override
  109. publicvoidsurfaceCreated(SurfaceHolderholder){
  110. this.thread.setFlag(true);
  111. this.thread.start();
  112. }
  113. @Override
  114. publicvoidsurfaceDestroyed(SurfaceHolderholder){
  115. booleanretry=true;
  116. this.thread.setFlag(false);
  117. while(retry){
  118. try{
  119. thread.join();
  120. retry=false;
  121. }catch(InterruptedExceptione){
  122. e.printStackTrace();
  123. }
  124. }
  125. }
  126. //刷帧线程,这个不解释,实在看不懂,M我:289302487@qq.com
  127. classFlushThreadextendsThread{
  128. privatebooleanflag=false;
  129. privatefinalintspan=500;
  130. privatefinalGameViewgameView;
  131. privatefinalSurfaceHolderholder;
  132. publicFlushThread(SurfaceHolderholder,GameViewgameView){
  133. this.gameView=gameView;
  134. this.holder=holder;
  135. }
  136. @Override
  137. publicvoidrun(){
  138. Canvascanvas;
  139. while(this.flag){
  140. canvas=null;
  141. try{
  142. canvas=this.holder.lockCanvas(null);
  143. synchronized(this.holder){
  144. this.gameView.onDraw(canvas);
  145. }
  146. }finally{
  147. if(canvas!=null){
  148. this.holder.unlockCanvasAndPost(canvas);
  149. }
  150. }
  151. try{
  152. Thread.sleep(span);
  153. }catch(InterruptedExceptione){
  154. e.printStackTrace();
  155. }
  156. }
  157. }
  158. publicbooleanisFlag(){
  159. returnflag;
  160. }
  161. publicvoidsetFlag(booleanflag){
  162. this.flag=flag;
  163. }
  164. }
  165. }

3、相关实体类

扑克牌类:

  1. packagecom.bison.utils;
  2. importjava.util.Random;
  3. /**
  4. *生成一副洗好的牌,并且设计为单例模式
  5. *
  6. *@authorBison
  7. *
  8. */
  9. publicclassCards{
  10. //声明一副扑克牌
  11. publicint[]pukes=newint[54];
  12. privatestaticCardscardsInstance=null;
  13. privateCards(){
  14. setPuke();
  15. shuffle();
  16. }
  17. publicstaticCardsgetInstance(){
  18. if(cardsInstance==null){
  19. cardsInstance=newCards();
  20. }
  21. returncardsInstance;
  22. }
  23. /**给54张扑克牌赋值:1~54*/
  24. privatevoidsetPuke(){
  25. for(inti=0;i<54;i++){
  26. pukes[i]=i+1;
  27. }
  28. }
  29. /**洗牌*/
  30. privatevoidshuffle(){
  31. Randomrdm=newRandom();
  32. for(inti=0;i<54;i++){
  33. //random.nextInt();是个前闭后开的方法:0~53
  34. intrdmNo=rdm.nextInt(54);
  35. inttemp=pukes[i];
  36. pukes[i]=pukes[rdmNo];
  37. pukes[rdmNo]=temp;
  38. }
  39. }
  40. }

玩家类:

  1. packagecom.bison.utils;
  2. importandroid.graphics.Rect;
  3. /**
  4. *这个是玩家的实体类
  5. *
  6. *@authorBison
  7. *
  8. */
  9. publicclassPerson{
  10. privatefinalCardsmCards=Cards.getInstance();
  11. publicint[]person1=newint[17];
  12. publicint[]person2=newint[17];
  13. publicint[]person3=newint[17];
  14. //余下三张属于地主的
  15. publicint[]threePukes=newint[3];
  16. publicPerson(){
  17. personHold(mCards.pukes);
  18. }
  19. /**分牌*/
  20. privatevoidpersonHold(int[]pukes){
  21. intk=0;
  22. for(inti=0;i<3;i++){
  23. if(i==0){
  24. for(intj=0;j<17;j++){
  25. person1[j]=pukes[k++];
  26. }
  27. //将其排序
  28. sort(person1);
  29. }
  30. if(i==1){
  31. for(intj=0;j<17;j++){
  32. person2[j]=pukes[k++];
  33. }
  34. //将其排序
  35. sort(person2);
  36. }
  37. if(i==2){
  38. for(intj=0;j<17;j++){
  39. person3[j]=pukes[k++];
  40. }
  41. //将其排序
  42. sort(person3);
  43. }
  44. }
  45. threePukes[0]=pukes[51];
  46. threePukes[1]=pukes[52];
  47. threePukes[2]=pukes[53];
  48. }
  49. /**对每个玩家手里的牌排序:使用冒泡排序*/
  50. privatevoidsort(int[]ary){
  51. for(inti=0;i<ary.length;i++){
  52. for(intj=0;j<ary.length-i-1;j++){
  53. if(ary[j]>ary[j+1]){
  54. inttemp=ary[j];
  55. ary[j]=ary[j+1];
  56. ary[j+1]=temp;
  57. }
  58. }
  59. }
  60. }
  61. /**
  62. *对应扑克所在图片上的位置
  63. *159…………53
  64. *2610…………54
  65. *3711
  66. *4812
  67. */
  68. publicRectcardRect(intcardValue,intwidth,intheight){
  69. intx=0,y=0;
  70. if(cardValue%4==0){
  71. x=cardValue/4-1;
  72. y=4;
  73. }else{
  74. x=cardValue/4;
  75. y=cardValue%4;
  76. }
  77. intleft=x*width;
  78. inttop=(y-1)*height;
  79. intright=(x+1)*width;
  80. intbottom=(y)*height;
  81. returnnewRect(left,top,right,bottom);
  82. }
  83. }

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






分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics