当前位置:   article > 正文

2048小游戏 java版(代码+注释)_2028小游戏java,包括最高分

2028小游戏java,包括最高分

        一个纯纯小白,想写点什么,也想学习一下怎么在这里写东西,就简单的写个2048小游戏。写的不好,大佬就不用看了,希望和大家交流学习,有写的不好或有更好的建议也欢迎提出来。(需要用的可直接粘贴复制)(轻喷)

目录

游戏展示

讲解 

代码


游戏展示

        基本上做到了和手机上一样。 我会按照我的思路为大家讲解一下,有需要的可以看看,想直接看效果或者需要用的,可以直接滑倒最后粘贴复制。


讲解 

目录结构如下

 按照我自己的思路讲一下,可能有点乱。

        1.首先初始化一下画板,将不会改变的部分先完成。

  1. private void initPaint(Graphics g) {
  2. //TODO 画方块所造区域
  3. g.setColor(new Color(192, 175, 159));
  4. g.fillRect(10, 120, 360 + 8, 360 + 8);
  5. //TODO 画得分区域以及说明区域
  6. g.setColor(new Color(209, 132, 102));
  7. g.fillRect(200, 15, 80, 50);
  8. g.fillRect(290, 15, 80, 50);
  9. g.setColor(Color.black);
  10. g.setFont(new Font("宋体", Font.BOLD, 20));
  11. g.drawString("得分", 220, 35);
  12. g.drawString("最高分", 300, 35);
  13. g.setFont(new Font("宋体", Font.BOLD, 35));
  14. g.drawString("游戏说明:", 15, 55);
  15. g.setFont(new Font("宋体", Font.BOLD, 20));
  16. g.drawString("按上下左右键控制;按空格键重新游戏", 15, 100);
  17. //画空白方块
  18. for (int i = 0; i < 4; i++) {
  19. for (int j = 0; j < 4; j++) {
  20. paintBlock(g, new Block(table[i][j]), j, i);
  21. }
  22. }
  23. }

         2.创建带有数字的方块,这里创建了一个block类,主要存储了,每种方块所显示的数字,数字颜色,背景颜色等属性。并且有一个setAllFont方法,来设置这些属性。

  1. package game_2048;
  2. import java.awt.*;
  3. public class Block {
  4. int kind;
  5. Font wFont;
  6. Color wColor;
  7. Color bColor;
  8. String s;
  9. public Block(int kind) {
  10. this.kind = kind;
  11. }
  12. private final Font font1 = new Font("宋体", Font.BOLD, 46);
  13. private final Font font2 = new Font("宋体", Font.BOLD, 40);
  14. private final Font font3 = new Font("宋体", Font.BOLD, 34);
  15. private final Font font4 = new Font("宋体", Font.BOLD, 28);
  16. private final Color color1 = new Color(119,108,99);
  17. private final Color color2 = new Color(254,254,254);
  18. public void setAllFont() {
  19. switch (kind) {
  20. case 0 -> bColor = new Color(206, 194, 180);
  21. case 1 -> {
  22. wFont = font1;
  23. wColor = color1;
  24. bColor = new Color(237, 229, 218);
  25. s = 2 + "";
  26. }
  27. case 2 -> {
  28. wFont = font1;
  29. wColor = color1;
  30. bColor = new Color(237, 220, 190);
  31. s = 4 + "";
  32. }
  33. case 3 -> {
  34. wFont = font1;
  35. wColor = color1;
  36. bColor = new Color(242, 177, 123);
  37. s = 8 + "";
  38. }
  39. case 4 -> {
  40. wFont = font2;
  41. wColor = color2;
  42. bColor = new Color(246, 147, 92);
  43. s = 16 + "";
  44. }
  45. case 5 -> {
  46. wFont = font2;
  47. wColor = color2;
  48. bColor = new Color(245, 118, 86);
  49. s = 32 + "";
  50. }
  51. case 6 -> {
  52. wFont = font2;
  53. wColor = color2;
  54. bColor = new Color(245, 83, 45);
  55. s = 64 + "";
  56. }
  57. case 7 -> {
  58. wFont = font3;
  59. wColor = color2;
  60. bColor = new Color(237, 206, 115);
  61. s = 128 + "";
  62. }
  63. case 8 -> {
  64. wFont = font3;
  65. wColor = color2;
  66. bColor = new Color(230,209,81);
  67. s = 256 + "";
  68. }
  69. case 9 -> {
  70. wFont = font3;
  71. wColor = color2;
  72. bColor = new Color(207,163,12);
  73. s = 512 + "";
  74. }
  75. case 10 -> {
  76. wFont = font4;
  77. wColor = color2;
  78. bColor = new Color(229,180,6);
  79. s = 1024 + "";
  80. }
  81. case 11 -> {
  82. wFont = font4;
  83. wColor = color2;
  84. bColor = new Color(161,131,115);
  85. s = 2048 + "";
  86. }
  87. default -> {
  88. }
  89. }
  90. }
  91. }

        3.绘制每个方块(这里在1里面已经用过了,当时是画空白的)

  1. //画方块
  2. private void paintBlock(Graphics g, Block block, int x, int y) {
  3. block.setAllFont();
  4. g.setColor(block.bColor);
  5. g.fillRect(10 + x * 90 + 7, 120 + y * 90 + 7, 83, 83);
  6. if (block.kind > 0) {
  7. g.setColor(block.wColor);
  8. g.setFont(block.wFont);
  9. FontMetrics fm = getFontMetrics(block.wFont);
  10. String value = block.s;
  11. g.drawString(value,
  12. 10 + x * 90 + 7 + (83 - fm.stringWidth(value)) / 2,
  13. 120 + y * 90 + 7 + (83 - fm.getAscent() - fm.getDescent()) / 2 + fm.getAscent());
  14. }
  15. }

         4.初始化界面,包括分数,和最开始显示的方块。

        用random类来随机刷新方块存在的位置。

        这里的getS()和后面的setS()分别是读、取文件,为了显示最高分用。(这两个方法写了不是很好,就不讲了)

  1. private void init() {
  2. isOver = false;
  3. source = 0;
  4. try {
  5. best = getS();
  6. } catch (IOException e) {
  7. throw new RuntimeException(e);
  8. }
  9. for (int i = 0; i < 4; i++) {
  10. for (int j = 0; j < 4; j++) {
  11. table[i][j] = 0;
  12. }
  13. }
  14. bX = rand1.nextInt(4);
  15. bY = rand2.nextInt(4);
  16. table[bX][bY] = 1;
  17. }

        5. 控制所有方块的移动。

        通过一系列控制语句,来决定是否能移动或者合并。我这里是为了做滑动的效果,可能写的有点啰嗦,反正能用就行吧。

  1. private void bDown() {
  2. for (int j = 0; j <= 3; j++) {
  3. //第一个if是为了一种稍微特殊的地方
  4. //后面的每个方向都一样
  5. if (table[0][j] == table[1][j] && table[2][j] == table[3][j]
  6. && table[0][j] != 0 && table[2][j] != 0) {
  7. table[1][j]++;
  8. table[0][j] = 0;
  9. }
  10. for (int i = 2; i >= 0; i--) {
  11. if (table[i + 1][j] == 0) {
  12. table[i + 1][j] = table[i][j];
  13. table[i][j] = 0;
  14. }
  15. if (table[i][j] != 0 && table[i][j] == table[i + 1][j]) {
  16. if (c[j]) {
  17. table[i + 1][j]++;
  18. table[i][j] = 0;
  19. c[j] = false;
  20. source += (int) Math.pow(2, table[i + 1][j]);
  21. }
  22. }
  23. }
  24. }
  25. }

        6.我这里是用线程的方法,来实现滑动的效果。

        每次按按键后,将改变fx的值,然后启动线程,每次会执行三次方块的移动,每次延迟40ms,依次来达到滑动的效果。

  1. @Override
  2. public void run() {
  3. for (int i = 0; i < 4; i++) {
  4. c[i] = true;
  5. }
  6. int[][] tt = new int[4][4];
  7. for (int i = 0; i < 4; i++) {
  8. for (int j = 0; j < 4; j++) {
  9. tt[i][j] = table[i][j];
  10. }
  11. }
  12. for (int k = 0; k < 3; k++) {
  13. switch (fx) {
  14. case 0 -> bUp();
  15. case 1 -> bRight();
  16. case 2 -> bDown();
  17. case 3 -> bLeft();
  18. default -> {
  19. }
  20. }
  21. try {
  22. Thread.sleep(40);
  23. } catch (InterruptedException e) {
  24. throw new RuntimeException(e);
  25. }
  26. if (best < source) {
  27. best = source;
  28. try {
  29. setS(source);
  30. } catch (IOException ioException) {
  31. ioException.printStackTrace();
  32. }
  33. }
  34. repaint();
  35. }
  36. isMove = false;
  37. loop:
  38. for (int i = 0; i < 4; i++) {
  39. for (int j = 0; j < 4; j++) {
  40. if (tt[i][j] != table[i][j]) {
  41. isMove = true;
  42. break loop;
  43. }
  44. }
  45. }
  46. randomBlock();
  47. }

        8.随机生成新的方块。 每次移动之后,就会随机产生新的方块,并且也在这里设置了结束的条件。

  1. private void randomBlock() {
  2. v.clear();
  3. for (int i = 0; i < 4; i++) {
  4. for (int j = 0; j < 4; j++) {
  5. if (table[i][j] == 0) {
  6. v.add(i);
  7. v.add(j);
  8. }
  9. }
  10. }
  11. if (v.size() > 0 && isMove) {
  12. int kk = rand.nextInt(v.size() / 2);
  13. table[v.get(kk * 2)][v.get(kk * 2 + 1)] = (rand.nextInt(7) % 6 == 0) ? 2 : 1;
  14. }
  15. if (v.size() == 0) {
  16. boolean isSame = false;
  17. loop1:
  18. for (int i = 0; i < 3; i++) {
  19. for (int j = 0; j < 4; j++) {
  20. if ((table[i][j] == table[i + 1][j]) || (table[j][i] == table[j][i + 1])) {
  21. isSame = true;
  22. break loop1;
  23. }
  24. }
  25. }
  26. if (!isSame) {
  27. isOver = true;
  28. repaint();
  29. }
  30. }
  31. }

就这样吧,,,有啥问题也可以问,我也会回答。。。。。。


代码

best.txt

        best.txt主要用来记录最高得分,创建时候需要在里面存储个0,不然读取的时候会报错。

MPanel

        Panel类,主要的代码都在这个类里。 

  1. package game_2048;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.util.Random;
  9. import java.util.Vector;
  10. public class MPanel extends JPanel implements KeyListener, Runnable {
  11. int[][] table = new int[4][4];
  12. int fx; //上下左右0123
  13. Random rand1 = new Random();
  14. Random rand2 = new Random();
  15. Random rand = new Random();
  16. int bX;
  17. int bY;
  18. Vector<Integer> v = new Vector<>();
  19. boolean isMove;
  20. boolean[] c = new boolean[4];
  21. int source = 0;
  22. int best;
  23. boolean isOver;
  24. public MPanel() {
  25. setFocusable(true);
  26. setBackground(new Color(241, 228, 219));
  27. this.addKeyListener(this);
  28. init();
  29. }
  30. private void init() {
  31. isOver = false;
  32. source = 0;
  33. try {
  34. best = getS();
  35. } catch (IOException e) {
  36. throw new RuntimeException(e);
  37. }
  38. for (int i = 0; i < 4; i++) {
  39. for (int j = 0; j < 4; j++) {
  40. table[i][j] = 0;
  41. }
  42. }
  43. bX = rand1.nextInt(4);
  44. bY = rand2.nextInt(4);
  45. table[bX][bY] = 1;
  46. }
  47. @Override
  48. public void paint(Graphics g) {
  49. super.paint(g);
  50. initPaint(g);
  51. g.setColor(Color.white);
  52. g.setFont(new Font("宋体", Font.BOLD, 25));
  53. FontMetrics fm = getFontMetrics(new Font("宋体", Font.BOLD, 20));
  54. String value1 = source + "";
  55. g.drawString(value1,
  56. 195 + (80 - fm.stringWidth(value1)) / 2,
  57. 30 + (50 - fm.getAscent() - fm.getDescent()) / 2 + fm.getAscent());
  58. String value2 = best + "";
  59. g.drawString(value2,
  60. 285 + (80 - fm.stringWidth(value2)) / 2,
  61. 30 + (50 - fm.getAscent() - fm.getDescent()) / 2 + fm.getAscent());
  62. if (isOver) {
  63. g.setColor(new Color(0, 0, 0, 0.5f));
  64. g.fillRect(0, 0, getWidth(), getHeight());
  65. g.setFont(new Font("宋体", Font.BOLD, 60));
  66. fm = getFontMetrics(new Font("宋体", Font.BOLD, 60));
  67. String value = "游戏结束";
  68. g.drawString(value,
  69. (getWidth() - fm.stringWidth(value)) / 2,
  70. (getHeight() - fm.getAscent() - fm.getDescent()) / 2 + fm.getAscent());
  71. }
  72. }
  73. //初始化画板
  74. private void initPaint(Graphics g) {
  75. //TODO 画方块所造区域
  76. g.setColor(new Color(192, 175, 159));
  77. g.fillRect(10, 120, 360 + 8, 360 + 8);
  78. //TODO 画得分区域以及说明区域
  79. g.setColor(new Color(209, 132, 102));
  80. g.fillRect(200, 15, 80, 50);
  81. g.fillRect(290, 15, 80, 50);
  82. g.setColor(Color.black);
  83. g.setFont(new Font("宋体", Font.BOLD, 20));
  84. g.drawString("得分", 220, 35);
  85. g.drawString("最高分", 300, 35);
  86. g.setFont(new Font("宋体", Font.BOLD, 35));
  87. g.drawString("游戏说明:", 15, 55);
  88. g.setFont(new Font("宋体", Font.BOLD, 20));
  89. g.drawString("按上下左右键控制;按空格键重新游戏", 15, 100);
  90. for (int i = 0; i < 4; i++) {
  91. for (int j = 0; j < 4; j++) {
  92. paintBlock(g, new Block(table[i][j]), j, i);
  93. }
  94. }
  95. }
  96. //画方块
  97. private void paintBlock(Graphics g, Block block, int x, int y) {
  98. block.setAllFont();
  99. g.setColor(block.bColor);
  100. g.fillRect(10 + x * 90 + 7, 120 + y * 90 + 7, 83, 83);
  101. if (block.kind > 0) {
  102. g.setColor(block.wColor);
  103. g.setFont(block.wFont);
  104. FontMetrics fm = getFontMetrics(block.wFont);
  105. String value = block.s;
  106. g.drawString(value,
  107. 10 + x * 90 + 7 + (83 - fm.stringWidth(value)) / 2,
  108. 120 + y * 90 + 7 + (83 - fm.getAscent() - fm.getDescent()) / 2 + fm.getAscent());
  109. }
  110. }
  111. @Override
  112. public void keyTyped(KeyEvent e) {
  113. }
  114. @Override
  115. public void keyPressed(KeyEvent e) {
  116. int keyCode = e.getKeyCode();
  117. if (!isOver) {
  118. Thread thread = new Thread(this);
  119. if (keyCode == KeyEvent.VK_DOWN) {
  120. fx = 2;
  121. thread.start();
  122. } else if (keyCode == KeyEvent.VK_UP) {
  123. fx = 0;
  124. thread.start();
  125. } else if (keyCode == KeyEvent.VK_RIGHT) {
  126. fx = 1;
  127. thread.start();
  128. } else if (keyCode == KeyEvent.VK_LEFT) {
  129. fx = 3;
  130. thread.start();
  131. }
  132. }
  133. if (keyCode == KeyEvent.VK_SPACE) {
  134. init();
  135. }
  136. repaint();
  137. }
  138. @Override
  139. public void keyReleased(KeyEvent e) {
  140. }
  141. @Override
  142. public void run() {
  143. for (int i = 0; i < 4; i++) {
  144. c[i] = true;
  145. }
  146. int[][] tt = new int[4][4];
  147. for (int i = 0; i < 4; i++) {
  148. for (int j = 0; j < 4; j++) {
  149. tt[i][j] = table[i][j];
  150. }
  151. }
  152. for (int k = 0; k < 3; k++) {
  153. switch (fx) {
  154. case 0 -> bUp();
  155. case 1 -> bRight();
  156. case 2 -> bDown();
  157. case 3 -> bLeft();
  158. default -> {
  159. }
  160. }
  161. try {
  162. Thread.sleep(40);
  163. } catch (InterruptedException e) {
  164. throw new RuntimeException(e);
  165. }
  166. if (best < source) {
  167. best = source;
  168. try {
  169. setS(source);
  170. } catch (IOException ioException) {
  171. ioException.printStackTrace();
  172. }
  173. }
  174. repaint();
  175. }
  176. isMove = false;
  177. loop:
  178. for (int i = 0; i < 4; i++) {
  179. for (int j = 0; j < 4; j++) {
  180. if (tt[i][j] != table[i][j]) {
  181. isMove = true;
  182. break loop;
  183. }
  184. }
  185. }
  186. randomBlock();
  187. }
  188. private void randomBlock() {
  189. v.clear();
  190. for (int i = 0; i < 4; i++) {
  191. for (int j = 0; j < 4; j++) {
  192. if (table[i][j] == 0) {
  193. v.add(i);
  194. v.add(j);
  195. }
  196. }
  197. }
  198. if (v.size() > 0 && isMove) {
  199. int kk = rand.nextInt(v.size() / 2);
  200. table[v.get(kk * 2)][v.get(kk * 2 + 1)] = (rand.nextInt(7) % 6 == 0) ? 2 : 1;
  201. }
  202. if (v.size() == 0) {
  203. boolean isSame = false;
  204. loop1:
  205. for (int i = 0; i < 3; i++) {
  206. for (int j = 0; j < 4; j++) {
  207. if ((table[i][j] == table[i + 1][j]) || (table[j][i] == table[j][i + 1])) {
  208. isSame = true;
  209. break loop1;
  210. }
  211. }
  212. }
  213. if (!isSame) {
  214. isOver = true;
  215. repaint();
  216. }
  217. }
  218. }
  219. private void bDown() {
  220. for (int j = 0; j <= 3; j++) {
  221. //第一个if是为了一种稍微特殊的地方
  222. //后面的每个方向都一样
  223. if (table[0][j] == table[1][j] && table[2][j] == table[3][j]
  224. && table[0][j] != 0 && table[2][j] != 0) {
  225. table[1][j]++;
  226. table[0][j] = 0;
  227. }
  228. for (int i = 2; i >= 0; i--) {
  229. if (table[i + 1][j] == 0) {
  230. table[i + 1][j] = table[i][j];
  231. table[i][j] = 0;
  232. }
  233. if (table[i][j] != 0 && table[i][j] == table[i + 1][j]) {
  234. if (c[j]) {
  235. table[i + 1][j]++;
  236. table[i][j] = 0;
  237. c[j] = false;
  238. source += (int) Math.pow(2, table[i + 1][j]);
  239. }
  240. }
  241. }
  242. }
  243. }
  244. private void bLeft() {
  245. for (int i = 0; i <= 3; i++) {
  246. if (table[i][0] == table[i][1] && table[i][2] == table[i][3]
  247. && table[i][0] != 0 && table[i][2] != 0) {
  248. table[i][2]++;
  249. table[i][3] = 0;
  250. }
  251. for (int j = 1; j <= 3; j++) {
  252. if (table[i][j - 1] == 0) {
  253. table[i][j - 1] = table[i][j];
  254. table[i][j] = 0;
  255. }
  256. if (table[i][j] != 0 && table[i][j] == table[i][j - 1]) {
  257. if (c[i]) {
  258. table[i][j - 1]++;
  259. table[i][j] = 0;
  260. c[i] = false;
  261. source += (int) Math.pow(2, table[i][j - 1]);
  262. }
  263. }
  264. }
  265. }
  266. }
  267. private void bRight() {
  268. for (int i = 0; i <= 3; i++) {
  269. if (table[i][0] == table[i][1] && table[i][2] == table[i][3]
  270. && table[i][0] != 0 && table[i][2] != 0) {
  271. table[i][1]++;
  272. table[i][0] = 0;
  273. }
  274. for (int j = 2; j >= 0; j--) {
  275. if (table[i][j + 1] == 0) {
  276. table[i][j + 1] = table[i][j];
  277. table[i][j] = 0;
  278. }
  279. if (table[i][j] != 0 && table[i][j] == table[i][j + 1]) {
  280. if (c[i]) {
  281. table[i][j + 1]++;
  282. table[i][j] = 0;
  283. c[i] = false;
  284. source += (int) Math.pow(2, table[i][j + 1]);
  285. }
  286. }
  287. }
  288. }
  289. }
  290. private void bUp() {
  291. for (int j = 0; j <= 3; j++) {
  292. if (table[0][j] == table[1][j] && table[2][j] == table[3][j]
  293. && table[0][j] != 0 && table[2][j] != 0) {
  294. table[2][j]++;
  295. table[3][j] = 0;
  296. }
  297. for (int i = 1; i <= 3; i++) {
  298. if (table[i - 1][j] == 0) {
  299. table[i - 1][j] = table[i][j];
  300. table[i][j] = 0;
  301. }
  302. if (table[i][j] != 0 && table[i][j] == table[i - 1][j]) {
  303. if (c[j]) {
  304. table[i - 1][j]++;
  305. table[i][j] = 0;
  306. c[j] = false;
  307. source += (int) Math.pow(2, table[i - 1][j]);
  308. }
  309. }
  310. }
  311. }
  312. }
  313. public int getS() throws IOException {
  314. String filePath = "src\\game_2048\\best.txt";
  315. FileReader fileReader = new FileReader(filePath);
  316. int data = 0;
  317. String s = "";
  318. while ((data = fileReader.read()) != -1) {
  319. s += (char) data;
  320. }
  321. int y = Integer.parseInt(s);
  322. fileReader.close();
  323. return y;
  324. }
  325. private void setS(int y) throws IOException {
  326. String filePath = "src\\game_2048\\best.txt";
  327. FileWriter fileWriter = new FileWriter(filePath);
  328. String yy = y + "";
  329. fileWriter.write(yy);
  330. fileWriter.close();
  331. }
  332. }

Block 

        每个方块创建的类。 

  1. package game_2048;
  2. import java.awt.*;
  3. public class Block {
  4. int kind;
  5. Font wFont;
  6. Color wColor;
  7. Color bColor;
  8. String s;
  9. public Block(int kind) {
  10. this.kind = kind;
  11. }
  12. private final Font font1 = new Font("宋体", Font.BOLD, 46);
  13. private final Font font2 = new Font("宋体", Font.BOLD, 40);
  14. private final Font font3 = new Font("宋体", Font.BOLD, 34);
  15. private final Font font4 = new Font("宋体", Font.BOLD, 28);
  16. private final Color color1 = new Color(119,108,99);
  17. private final Color color2 = new Color(254,254,254);
  18. public void setAllFont() {
  19. switch (kind) {
  20. case 0 -> bColor = new Color(206, 194, 180);
  21. case 1 -> {
  22. wFont = font1;
  23. wColor = color1;
  24. bColor = new Color(237, 229, 218);
  25. s = 2 + "";
  26. }
  27. case 2 -> {
  28. wFont = font1;
  29. wColor = color1;
  30. bColor = new Color(237, 220, 190);
  31. s = 4 + "";
  32. }
  33. case 3 -> {
  34. wFont = font1;
  35. wColor = color1;
  36. bColor = new Color(242, 177, 123);
  37. s = 8 + "";
  38. }
  39. case 4 -> {
  40. wFont = font2;
  41. wColor = color2;
  42. bColor = new Color(246, 147, 92);
  43. s = 16 + "";
  44. }
  45. case 5 -> {
  46. wFont = font2;
  47. wColor = color2;
  48. bColor = new Color(245, 118, 86);
  49. s = 32 + "";
  50. }
  51. case 6 -> {
  52. wFont = font2;
  53. wColor = color2;
  54. bColor = new Color(245, 83, 45);
  55. s = 64 + "";
  56. }
  57. case 7 -> {
  58. wFont = font3;
  59. wColor = color2;
  60. bColor = new Color(237, 206, 115);
  61. s = 128 + "";
  62. }
  63. case 8 -> {
  64. wFont = font3;
  65. wColor = color2;
  66. bColor = new Color(230,209,81);
  67. s = 256 + "";
  68. }
  69. case 9 -> {
  70. wFont = font3;
  71. wColor = color2;
  72. bColor = new Color(207,163,12);
  73. s = 512 + "";
  74. }
  75. case 10 -> {
  76. wFont = font4;
  77. wColor = color2;
  78. bColor = new Color(229,180,6);
  79. s = 1024 + "";
  80. }
  81. case 11 -> {
  82. wFont = font4;
  83. wColor = color2;
  84. bColor = new Color(161,131,115);
  85. s = 2048 + "";
  86. }
  87. default -> {
  88. }
  89. }
  90. }
  91. }

MFrame

  1. package game_2048;
  2. /*
  3. *@author douhehe
  4. */
  5. import javax.swing.*;
  6. public class MFrame {
  7. public static void main(String[] args) {
  8. JFrame jf = new JFrame("2048小游戏");
  9. jf.add(new MPanel());
  10. jf.setSize(401,552);
  11. jf.setLocationRelativeTo(null); //居中
  12. jf.setResizable(false);
  13. jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  14. jf.setVisible(true);
  15. }
  16. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/191977
推荐阅读
相关标签
  

闽ICP备14008679号