《2048》是一款比较流行的数字游戏,最早于2014年3月20日发行。原版2048首先在GitHub上发布,原作者是Gabriele Cirulli,后被移植到各个平台。这款游戏是基于《1024》和《小3传奇》的玩法开发而成的新型数字游戏。游戏源地址:http://gabrielecirulli.github.io/2048/

1、新建android项目game2048

修改activity_main.xml文件

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. tools:context=".MainActivity" >
  7.  
  8. <TextView
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="@string/hello_world" />
  12.  
  13. </LinearLayout>

2、设计2048游戏布局

继续修改activity_main.xml文件

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. tools:context=".MainActivity" >
  7.  
  8. <LinearLayout
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:orientation="horizontal" >
  12.  
  13. <TextView
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="@string/score" />
  17.  
  18. <TextView
  19. android:id="@+id/tvScore"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content" />
  22. </LinearLayout>
  23.  
  24. <GridLayout
  25. android:id="@+id/gameView"
  26. android:layout_width="fill_parent"
  27. android:layout_height="0dp"
  28. android:layout_weight="1" >
  29. </GridLayout>
  30.  
  31. </LinearLayout>

3、实现2048游戏主类GameView

新建类GameView,继承自GridLayout

  1. package com.wuyudong.game2048;
  2.  
  3. import android.content.Context;
  4. import android.util.AttributeSet;
  5. import android.widget.GridLayout;
  6.  
  7. public class GameView extends GridLayout {
  8.  
  9. public GameView(Context context, AttributeSet attrs, int defStyle) {
  10. super(context, attrs, defStyle);
  11. initGameView();
  12. }
  13.  
  14. public GameView(Context context, AttributeSet attrs) {
  15. super(context, attrs);
  16. initGameView();
  17. }
  18.  
  19. public GameView(Context context) {
  20. super(context);
  21. initGameView();
  22. }
  23.  
  24. private void initGameView() {
  25.  
  26. }
  27.  
  28. }

继续修改activity_main.xml文件:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. tools:context=".MainActivity" >
  7.  
  8. <LinearLayout
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:orientation="horizontal" >
  12.  
  13. <TextView
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="@string/score" />
  17.  
  18. <TextView
  19. android:id="@+id/tvScore"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content" />
  22. </LinearLayout>
  23.  
  24. <!-- 把类GameView绑定到一起 -->
  25. <com.wuyudong.game2048.GameView
  26. android:id="@+id/gameView"
  27. android:layout_width="fill_parent"
  28. android:layout_height="0dp"
  29. android:layout_weight="1" >
  30. </com.wuyudong.game2048.GameView>
  31.  
  32. </LinearLayout>

4、游戏2048在Android平台的触控交互设计

添加触控交互相关代码

  1. private void initGameView() {
  2. setOnTouchListener(new OnTouchListener() {
  3. //记录起始位置和偏移坐标
  4. private float startX, startY, offsetX, offsetY;
  5.  
  6. @Override
  7. public boolean onTouch(View v, MotionEvent event) {
  8. switch (event.getAction()) {
  9. case MotionEvent.ACTION_DOWN: //监听手指按下的初始位置坐标
  10. startX = event.getX();
  11. startY = event.getY();
  12. break;
  13.  
  14. case MotionEvent.ACTION_UP: //监听手指离开时的位置坐标
  15. offsetX = event.getX() - startX;
  16. offsetY = event.getY() - startY;
  17.  
  18. if (Math.abs(offsetX) > Math.abs(offsetY)) {
  19. if (offsetX < -5) {
  20. System.out.println("left");
  21. } else if (offsetX > 5) {
  22. System.out.println("right");
  23. }
  24. } else {
  25. if (offsetY < -5) {
  26. System.out.println("up");
  27. } else if (offsetY > 5) {
  28. System.out.println("down");
  29. }
  30. }
  31. break;
  32. default:
  33. break;
  34. }
  35.  
  36. return true;
  37. }
  38. });
  39.  
  40. }

5、实现2048游戏的卡片类

编写卡片类Card.java

  1. package com.wuyudong.game2048;
  2.  
  3. import android.content.Context;
  4. import android.widget.FrameLayout;
  5. import android.widget.TextView;
  6.  
  7. public class Card extends FrameLayout {
  8.  
  9. public Card(Context context) {
  10. super(context);
  11. lable = new TextView(getContext());
  12. lable.setTextSize(32);
  13.  
  14. LayoutParams lp = new LayoutParams(-1, -1);
  15. addView(lable, lp);
  16. setNum(0);
  17. }
  18.  
  19. private int num = 0;
  20.  
  21. public int getNum() {
  22. return num;
  23. }
  24.  
  25. public void setNum(int num) {
  26. this.num = num;
  27. lable.setText(num + "");
  28. }
  29.  
  30. public boolean equals(Card o) {
  31. return getNum() == o.getNum();
  32. }
  33.  
  34. private TextView lable;
  35.  
  36. }

6、添加2048游戏卡片

先添加相关代码看看效果

  1. @Override
  2. protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  3. super.onSizeChanged(w, h, oldw, oldh);
  4. int cardWidth = (Math.min(w, h) - 10) / 4;
  5.  
  6. addCards(cardWidth, cardWidth);
  7.  
  8. }
  9.  
  10. private void addCards(int cardWith, int cardHeight){
  11. Card c;
  12. for (int y = 0; y < 4; y++) {
  13. for (int x = 0; x < 4; x++) {
  14. c=new Card(getContext());
  15. c.setNum(2);
  16. addView(c, cardWith, cardHeight);
  17. }
  18. }
  19. }

运行后的效果

出现问题,所有的2都出现在同一行中,解决办法:

在initGameView() 中添加代码,指定为四列:setColumnCount(4);

lable.setGravity(Gravity.CENTER);

接着添加相关的背景颜色以及卡片数字的背景颜色,还有卡片间距

  1. setBackgroundColor(0xffbbada0); // 设置整体背景
  2. lable.setBackgroundColor(0x33ffffff);
  3. lable.setGravity(Gravity.CENTER);
  4. lp.setMargins(10, 10, 0, 0);

运行后界面如下:

7、在2048游戏中添加随机数

  1. protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  2. super.onSizeChanged(w, h, oldw, oldh);
  3. int cardWidth = (Math.min(w, h) - 10) / 4;
  4.  
  5. addCards(cardWidth, cardWidth);
  6.  
  7. startGame();
  8.  
  9. }
  10.  
  11. private void startGame() {
  12. for (int y = 0; y < 4; y++) {
  13. for (int x = 0; x < 4; x++) {
  14. cardsMap[x][y].setNum(0);
  15. }
  16. }
  17. addRandomNum();
  18. addRandomNum();
  19. }
  20.  
  21. private void addRandomNum() {
  22. emptyPoints.clear();// 清空
  23. for (int y = 0; y < 4; y++) {
  24. for (int x = 0; x < 4; x++) {
  25. if (cardsMap[x][y].getNum() <= 0) {
  26. emptyPoints.add(new Point(x, y));
  27. }
  28.  
  29. }
  30. }
  31. Point p = emptyPoints.remove((int) (Math.random() * emptyPoints.size()));
  32. cardsMap[p.x][p.y].setNum(Math.random() > 0.1 ? 2 : 4);
  33.  
  34. }

8、实现2048游戏逻辑

  1. private void swipeLeft() { // 往左滑动手指
  2. for (int y = 0; y < 4; y++) {
  3. for (int x = 0; x < 4; x++) {
  4.  
  5. for (int x1 = x + 1; x1 < 4; x1++) {
  6. if (cardsMap[x1][y].getNum() > 0) {
  7.  
  8. if (cardsMap[x][y].getNum() <= 0) {
  9. cardsMap[x][y].setNum(cardsMap[x1][y].getNum());
  10. cardsMap[x1][y].setNum(0);
  11.  
  12. x--;
  13. break;
  14. } else if(cardsMap[x][y].equals(cardsMap[x1][y])){
  15. cardsMap[x][y].setNum(cardsMap[x][y].getNum()*2);
  16. cardsMap[x1][y].setNum(0);
  17. break;
  18. }
  19. }
  20. }
  21. }
  22. }
  23.  
  24. }
  25.  
  26. private void swipeRight() {
  27. for (int y = 0; y < 4; y++) {
  28. for (int x = 3; x >=0; x--) {
  29.  
  30. for (int x1 = x - 1; x1 >=0; x1--) {
  31. if (cardsMap[x1][y].getNum() > 0) {
  32.  
  33. if (cardsMap[x][y].getNum() <= 0) {
  34. cardsMap[x][y].setNum(cardsMap[x1][y].getNum());
  35. cardsMap[x1][y].setNum(0);
  36.  
  37. x++;
  38. break;
  39. } else if(cardsMap[x][y].equals(cardsMap[x1][y])){
  40. cardsMap[x][y].setNum(cardsMap[x][y].getNum()*2);
  41. cardsMap[x1][y].setNum(0);
  42. break;
  43. }
  44. }
  45. }
  46. }
  47. }
  48.  
  49. }
  50.  
  51. private void swipeUp() {
  52. for (int x = 0; x < 4; x++) {
  53. for (int y = 0; y < 4; y++) {
  54.  
  55. for (int y1 = y + 1; y1 < 4; y1++) {
  56. if (cardsMap[x][y1].getNum() > 0) {
  57.  
  58. if (cardsMap[x][y].getNum() <= 0) {
  59. cardsMap[x][y].setNum(cardsMap[x][y1].getNum());
  60. cardsMap[x][y1].setNum(0);
  61.  
  62. y--;
  63. break;
  64. } else if(cardsMap[x][y].equals(cardsMap[x][y1])){
  65. cardsMap[x][y].setNum(cardsMap[x][y].getNum()*2);
  66. cardsMap[x][y1].setNum(0);
  67. break;
  68. }
  69. }
  70. }
  71. }
  72. }
  73.  
  74. }
  75.  
  76. private void swipeDown() {
  77.  
  78. for (int x = 0; x < 4; x++) {
  79. for (int y = 3; y >=0; y--) {
  80.  
  81. for (int y1 = y - 1; y1 >= 0; y1--) {
  82. if (cardsMap[x][y1].getNum() > 0) {
  83.  
  84. if (cardsMap[x][y].getNum() <= 0) {
  85. cardsMap[x][y].setNum(cardsMap[x][y1].getNum());
  86. cardsMap[x][y1].setNum(0);
  87.  
  88. y++;
  89. break;
  90. } else if(cardsMap[x][y].equals(cardsMap[x][y1])){
  91. cardsMap[x][y].setNum(cardsMap[x][y].getNum()*2);
  92. cardsMap[x][y1].setNum(0);
  93. break;
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
  100.  
  101. private Card[][] cardsMap = new Card[4][4];
  102. private List<Point> emptyPoints = new ArrayList<Point>();

9、游戏2048计分

MainActivity.java 中添加相关代码:

  1. package com.wuyudong.game2048;
  2.  
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.view.Menu;
  6. import android.widget.TextView;
  7.  
  8. public class MainActivity extends Activity {
  9.  
  10. public MainActivity() {
  11. mainActivity = this;
  12. }
  13.  
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18.  
  19. tvScore = (TextView) findViewById(R.id.tvScore);
  20. }
  21.  
  22. @Override
  23. public boolean onCreateOptionsMenu(Menu menu) {
  24. // Inflate the menu; this adds items to the action bar if it is present.
  25. getMenuInflater().inflate(R.menu.main, menu);
  26. return true;
  27. }
  28.  
  29. public void clearScore() {
  30. score = 0;
  31. showScore();
  32. }
  33.  
  34. public void showScore() {
  35. tvScore.setText(score + "");
  36. }
  37.  
  38. public void addScore(int s) {
  39. score += s;
  40. showScore();
  41. }
  42.  
  43. private int score = 0;
  44. private TextView tvScore;
  45. private static MainActivity mainActivity = null;
  46.  
  47. public static MainActivity getMainActivity() {
  48. return mainActivity;
  49. }
  50.  
  51. }

10、检查2048游戏结束

  1. private void checkComplete() {
  2. boolean complete = true;
  3. ALL: for (int y = 0; y < 4; y++) {
  4. for (int x = 0; x < 4; x++) {
  5. if (cardsMap[x][y].getNum() == 0
  6. || (x > 0 && cardsMap[x][y].equals(cardsMap[x - 1][y]))
  7. || (x < 3 && cardsMap[x][y].equals(cardsMap[x + 1][y]))
  8. || (y > 0 && cardsMap[x][y].equals(cardsMap[x][y - 1]))
  9. || (y < 3 && cardsMap[x][y].equals(cardsMap[x][y + 1]))) {
  10.  
  11. complete = false;
  12. break ALL;
  13. }
  14. }
  15. }
  16. if (complete) {
  17. new AlertDialog.Builder(getContext())
  18. .setTitle("Oh")
  19. .setMessage("Game Over!")
  20. .setPositiveButton("restart",
  21. new DialogInterface.OnClickListener() {
  22.  
  23. @Override
  24. public void onClick(DialogInterface dialog,
  25. int which) {
  26. startGame();
  27.  
  28. }
  29. }).show();
  30. }
  31.  
  32. }

Android项目开发实战-2048游戏的更多相关文章

  1. Android 项目开发实战:聚合数据短信验证码

    聚合数据集成短信验证码官网: https://www.juhe.cn/docs/api/id/54 我根据文档集成了一个例子 效果: 源码下载:http://download.csdn.net/det ...

  2. Android项目开发第四周学习总结

    Android项目开发实战第四周 在本周,我们进行了Android项目第四周的项目开发,在本周,我们对原有的项目进行改进,我们的想法是使项目在原有的基础上增加一些新的功能,使得txt阅读器可以更加先进 ...

  3. 开源自己的一个小android项目(美女撕衣服游戏)

    这是自己的一个开源自己的一个小android项目(美女撕衣服游戏),也是前6个月开发的,有部分的资源来自网络上的,现在开源出来给大家吧,由于源码比较大,不上传了,我已经上传到源码天堂那个网站那里了,大 ...

  4. 《Android Studio开发实战 从零基础到App上线》资源下载和内容勘误

    转载于:https://blog.csdn.net/aqi00/article/details/73065392 资源下载 下面是<Android Studio开发实战 从零基础到App上线&g ...

  5. 《Android NFC 开发实战详解 》简介+源码+样章+勘误ING

    <Android NFC 开发实战详解>简介+源码+样章+勘误ING SkySeraph Mar. 14th  2014 Email:skyseraph00@163.com 更多精彩请直接 ...

  6. Swift项目开发实战-基于分层架构的多版本iPhone计算器-直播公开课

    Swift项目开发实战-基于分层架构的多版本iPhone计算器-直播公开课 本课程采用Q Q群直播方式进行直播,价值99元视频课程免费直播.完整的基于Swift项目实战,手把手教你做一个Swift版i ...

  7. Android项目开发全程(四)-- 将网络返回的json字符串轻松转换成listview列表

    前面几篇博文介绍了从项目搭建到获取网络字符串,对一个项目的前期整体工作进行了详细的介绍,本篇接着上篇介绍一下怎么样优雅将网络返回的json字符串轻松转换成listview列表. 先上图,看一下效果. ...

  8. Android项目开发全程(三)-- 项目的前期搭建、网络请求封装是怎样实现的

    在前两篇博文中已经做了铺垫,下面咱们就可以用前面介绍过的内容开始做一个小项目了(项目中会用到Afinal框架,不会用Afinal的童鞋可以先看一下上一篇博文),正所谓麻雀虽小,五脏俱全,这在里我会尽量 ...

  9. Android项目开发全程(二)--Afinal用法简单介绍

    本篇博文接上篇的<Android项目开发全程(一)--创建工程>,主要介绍一下在本项目中用到的一个很重要的框架-Afinal,由于本系列博文重点是项目开发全程,所以在这里就先介绍一下本项目 ...

随机推荐

  1. 【Swift学习】Swift编程之旅---继承(十七)

    在 Swift 中,继承是区分「类」与其它类型的一个基本特征.swift不支持多重继承.类可以调用和访问超类的方法,属性和subscripts下标,并且可以重写(override)这些方法,属性和附属 ...

  2. JSP动作元素

    JSP动作元素分类 <jsp:include page="content.jsp"></jsp:include> 使用<%@ include%> ...

  3. SRC单一职责原则

    一.定义 一个类应该只有一个发生变化的原因. 二.为什么要使用SRC 因为每一个职责都是变化的一个轴线.当需求变化时,这种变化就会反映为类的职责的变化.如果一个类承担了多于一个的职责,那么引起它变化的 ...

  4. 背水一战 Windows 10 (4) - UI: 多窗口

    [源码下载] 背水一战 Windows 10 (4) - UI: 多窗口 作者:webabcd 介绍背水一战 Windows 10 之 UI 多窗口 示例1.自定义帮助类,用于简化 Secondary ...

  5. 【Java每日一题】20161028

    package Oct2016; public class Ques1028 { public static void main(String[] args){ new B().out(); // 输 ...

  6. 添加项目到远程服务器(git)

    搞开发经常会用到把代码提交到远程服务器,之前也是懵懂的.今天来整理了一下.具体操作如下: 1.进入到远程服务器 ssh name -- 远程服务器地址 2.进入以后新建一个空的仓库 git init ...

  7. phpcms 二次开发数据过滤的技巧

    参数过滤 1,针对不能直接使用pdo进行参数绑定,可以使用sprintf模拟,并使用new_addslashes来过滤,然后使用query执行拼接的sql %% - 返回百分比符号 %b - 二进制数 ...

  8. Mac 连接阿里云服务器

    1. 通过命令行连接 Server 并设置 1.1 连接 Server #: ssh root@hctec.top ssh: 远程连接工具 root: 远程服务器用户名, 此处我用的是: root 用 ...

  9. PHP内核探索之变量(2)-理解引用

    本文主要内容: 引论 符号表与zval 引用原理 回到最初的问题 一.引论 很久之前写了一篇关于引用的文章,当时写的寥寥草草,很多原理都没有说清楚.最近在翻阅Derick Rethans(home: ...

  10. .Net中的并行编程-5.流水线模型实战

    自己在Excel整理了很多想写的话题,但苦于最近比较忙(其实这是借口).... 上篇文章<.Net中的并行编程-4.实现高性能异步队列>介绍了异步队列的实现,本篇文章介绍我实际工作者遇到了 ...