最近一直在研究关于游戏编程,人在深圳,工作不好找啊!
发一个斗地主游戏的牌桌实现。
为了节约内存资源,每张扑克牌都是剪切形成的,当然这也是当前编程的主流
方法
快递客服问题件处理详细方法山木方法pdf计算方法pdf华与华方法下载八字理论方法下载
。
1、主Activity
2、牌桌页面
3、相关实体类
扑克牌类:
玩家类:
PS:斗地主还是可以做成很复杂的。相关图片
[java] view plain copy print ?
1.
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. * 求某公司包养
11. *
12. * @author Bison
13. *
14. */
15. public class PukeActivity extends Activity {
16. /** Called when the activity is first created. */
17. @Override
18. public void onCreate(Bundle savedInstanceState) {
19. super.onCreate(savedInstanceState);
20. // 这个事隐藏标题栏,不解释
21. requestWindowFeature(Window.FEATURE_NO_TITLE);
22. // 隐藏状态栏,你懂的
23. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
24. WindowManager.LayoutParams.FLAG_FULLSCREEN);
25. /*
26. * 开始有考虑使屏幕上扑克的排列随屏幕的分辨率变动 结果貌似不好做,注释掉了 Display display =
27. * getWindowManager().getDefaultDisplay(); int screenWidth =
28. * display.getWidth(); int screenHeight = display.getHeight();
29. */
30.
31. // 使用代码锁定横屏
32. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
33. // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);这个是竖屏
34. setContentView(new GameView(this));
35. }
36. }
[java] view plain copy print ?
1.
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. *
17. * 扑克图片来源,和牌桌背景在文章的下面。 扑克背面图等我没上传,玩家自行百度
18. *
19. * @author Bison
20. *
21. */
22. public class GameView extends SurfaceView implements SurfaceHolder.Callback {
23. private FlushThread thread = null;// 刷帧线程
24. private Bitmap sourceBitmap = null;// 扑克图片来源
25. private Bitmap backgroundDesk = null;// 牌桌背景
26. private Bitmap backgroundPuke = null;// 扑克背面
27.
28. private final Person person;
29. private int pukeWidth = 0;// 扑克的宽
30. private int pukeHeight = 0;// 扑克的高
31. private int deskWidth = 0;// 牌桌的宽
32. private int deskHeight = 0;// 牌桌的高
33. private int left = 0;// 我自己首张牌左距离
34.
35. public GameView(Context context) {
36. super(context);
37. getHolder().addCallback(this);
38. this.thread = new FlushThread(getHolder(), this);// 实例化线程
39. initBitmap();// 实例化图片
40. this.person = new Person();// 实例化Person类
41. this.left = deskWidth / 2 - (16 * 25 + pukeWidth) / 2;// 左距开始时赋值
42. }
43.
44. private void initBitmap() {// 初始化图片
45. sourceBitmap = BitmapFactory.decodeResource(getResources(),
46. R.drawable.smallcard);
47. pukeWidth = sourceBitmap.getWidth() / 14;// 每张扑克的宽高
48. pukeHeight = sourceBitmap.getHeight() / 4;
49.
50. backgroundDesk = BitmapFactory.decodeResource(getResources(),
51. R.drawable.gameback2);
52.
53. deskWidth = backgroundDesk.getWidth();// 牌桌的宽高
54. deskHeight = backgroundDesk.getHeight();
55.
56. backgroundPuke = BitmapFactory.decodeResource(getResources(),
57. R.drawable.cardback);
58. }
59.
60. @Override
61. protected void onDraw(Canvas canvas) {
62. // 绘制牌桌
63. canvas.drawBitmap(backgroundDesk, 0, 0, null);
64. personPaint(canvas, pukeWidth, pukeHeight);
65. deskthreePukes(canvas, pukeWidth, pukeHeight);
66. }
67.
68. /** 绘制每个玩家手里的牌 */
69. public void personPaint(Canvas c, int pukeWidth, int pukeHeight) {
70. Rect src = new Rect();
71. Rect dst = new Rect();
72.
73. // 遍历数组
74. for (int i = 0; i < 3; i++) {
75. for (int j = 0; j < 17; j++) {
76. if (i == 0) {// 左手边玩家,不用绘出正面
77. // src = person.cardRect(person.person1[j], pukeWidth,
78. // pukeHeight);
79. // dst.set(10, j * 20, 10 + pukeWidth, j * 20 + pukeHeight);
80. c.drawBitmap(backgroundPuke, 35, 85, null);
81. }
82. if (i == 1) {// 自己
83. src = person.cardRect(person.person2[j], pukeWidth,
84. pukeHeight);
85. dst.set(left + j * 25, this.deskHeight - 20 - pukeHeight,
86. left + j * 25 + pukeWidth, deskHeight - 20);
87. c.drawBitmap(sourceBitmap, src, dst, null);
88. }
89. if (i == 2) {// 右手边玩家,同样不用绘出正面
90. // src = person.cardRect(person.person3[j], pukeWidth,
91. // pukeHeight);
92. // dst.set(this.screenWidth - 10 - pukeWidth, j * 20,
93. // this.screenWidth - 10, j * 20 + pukeHeight);
94. c.drawBitmap(backgroundPuke, deskWidth - 35 - pukeWidth,
95. 85, null);
96. }
97. }
98. }
99. }
100.
101. /** 绘制三张底牌 */
102. private void deskthreePukes(Canvas c, int pukeWidth, int pukeHeight) {
103. Rect src = new Rect();
104. Rect dst = new Rect();
105. for (int i = 0; i < 3; i++) {
106. src = person.cardRect(person.threePukes[i], pukeWidth, pukeHeight);
107. dst.set(280 + i * pukeWidth, 12, 280 + (i + 1) * pukeWidth,
108. 12 + pukeHeight);
109. c.drawBitmap(sourceBitmap, src, dst, null);
110. }
111. }
112.
113. @Override
114. public boolean onTouchEvent(MotionEvent event) {
115. // 正在研究点击弹出相应的扑克
116. return super.onTouchEvent(event);
117. }
118.
119. @Override
120. public void surfaceChanged(SurfaceHolder holder, int format, int width,
121. int height) {
122. }
123.
124. @Override
125. public void surfaceCreated(SurfaceHolder holder) {
126. this.thread.setFlag(true);
127. this.thread.start();
128. }
129.
130. @Override
131. public void surfaceDestroyed(SurfaceHolder holder) {
132. boolean retry = true;
133. this.thread.setFlag(false);
134. while (retry) {
135. try {
136. thread.join();
137. retry = false;
138. } catch (InterruptedException e) {
139. e.printStackTrace();
140. }
141. }
142.
143. }
144.
145. // 刷帧线程,这个不解释,实在看不懂,M我:289302487@qq.com
146. class FlushThread extends Thread {
147. private boolean flag = false;
148. private final int span = 500;
149. private final GameView gameView;
150. private final SurfaceHolder holder;
151.
152. public FlushThread(SurfaceHolder holder, GameView gameView) {
153. this.gameView = gameView;
154. this.holder = holder;
155. }
156.
157. @Override
158. public void run() {
159. Canvas canvas;
160. while (this.flag) {
161. canvas = null;
162. try {
163. canvas = this.holder.lockCanvas(null);
164. synchronized (this.holder) {
165. this.gameView.onDraw(canvas);
166. }
167. } finally {
168. if (canvas != null) {
169. this.holder.unlockCanvasAndPost(canvas);
170. }
171. }
172.
173. try {
174. Thread.sleep(span);
175. } catch (InterruptedException e) {
176. e.printStackTrace();
177. }
178. }
179. }
180.
181. public boolean isFlag() {
182. return flag;
183. }
184.
185. public void setFlag(boolean flag) {
186. this.flag = flag;
187. }
188.
189. }
190.
191. }
192.
[java] view plain copy print ?
1.
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.
[java] view plain copy print ?
1.
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.