引言

    程序猿们,是否还在为你的老板辛辛苦苦的打工而拿着微薄的薪水呢,还是不知道如何用自己的应用或游戏
来赚钱呢!在这里IQuick将教您如何同过自己的应用来赚取自己的第一桶金!你是说自己的应用还没有做出来?不,在這里已经为你提供好了一个完整的游戏应用了,在文章的下面有源码的地址哦。你只要稍做修改就可以
变成一个完全属于自己的应用了,比如将4*4换成5*5,甚至是其它的。如果你实在是慵懒至极的话,你只要将本应
用的包名及广告换成自己的,就可以上传到市场上轻轻松松赚取自己的第一桶金了。如果你觉得本文很赞的话,就顶一下作者吧,从下面的安装地址中下载应用,或者在导入本工程运行的时候,
从广告中安装一个应用。动一动你的手指,就能让作者更进一步,也能让作者以后更加有动力来分享吧。

安装

安智

预览

项目结构

重要代码解读

MainView游戏的主体类
[java] view plaincopy
  1. //初始化方法,里面初始化了一些常量,字体颜色等
  2. name="code" class="java">public MainView(Context context) {
  3. super(context);
  4. Resources resources = context.getResources();
  5. //Loading resources
  6. game = new MainGame(context, this);
  7. try {
  8. //Getting assets
  9. backgroundRectangle = resources.getDrawable(R.drawable.background_rectangle);
  10. lightUpRectangle = resources.getDrawable(R.drawable.light_up_rectangle);
  11. fadeRectangle = resources.getDrawable(R.drawable.fade_rectangle);
  12. TEXT_WHITE = resources.getColor(R.color.text_white);
  13. TEXT_BLACK = resources.getColor(R.color.text_black);
  14. TEXT_BROWN = resources.getColor(R.color.text_brown);
  15. this.setBackgroundColor(resources.getColor(R.color.background));
  16. Typeface font = Typeface.createFromAsset(resources.getAssets(), "ClearSans-Bold.ttf");
  17. paint.setTypeface(font);
  18. paint.setAntiAlias(true);
  19. } catch (Exception e) {
  20. System.out.println("Error getting assets?");
  21. }
  22. setOnTouchListener(new InputListener(this));
  23. game.newGame();
  24. }
  25. //游戏界面的绘制
  26. @Override
  27. protected void onSizeChanged(int width, int height, int oldw, int oldh) {
  28. super.onSizeChanged(width, height, oldw, oldh);
  29. getLayout(width, height);
  30. createBitmapCells();
  31. createBackgroundBitmap(width, height);
  32. createOverlays();
  33. }

MianGame游戏主要逻辑

[java] view plaincopy
  1. package com.tpcstld.twozerogame;
  2. import android.content.Context;
  3. import android.content.SharedPreferences;
  4. import android.preference.PreferenceManager;
  5. import java.util.ArrayList;
  6. import java.util.Collections;
  7. import java.util.List;
  8. public class MainGame {
  9. public static final int SPAWN_ANIMATION = -1;
  10. public static final int MOVE_ANIMATION = 0;
  11. public static final int MERGE_ANIMATION = 1;
  12. public static final int FADE_GLOBAL_ANIMATION = 0;
  13. public static final long MOVE_ANIMATION_TIME = MainView.BASE_ANIMATION_TIME;
  14. public static final long SPAWN_ANIMATION_TIME = MainView.BASE_ANIMATION_TIME;
  15. public static final long NOTIFICATION_ANIMATION_TIME = MainView.BASE_ANIMATION_TIME * 5;
  16. public static final long NOTIFICATION_DELAY_TIME = MOVE_ANIMATION_TIME + SPAWN_ANIMATION_TIME;
  17. private static final String HIGH_SCORE = "high score";
  18. public static final int startingMaxValue = 2048;
  19. public static int endingMaxValue;
  20. //Odd state = game is not active
  21. //Even state = game is active
  22. //Win state = active state + 1
  23. public static final int GAME_WIN = 1;
  24. public static final int GAME_LOST = -1;
  25. public static final int GAME_NORMAL = 0;
  26. public static final int GAME_NORMAL_WON = 1;
  27. public static final int GAME_ENDLESS = 2;
  28. public static final int GAME_ENDLESS_WON = 3;
  29. public Grid grid = null;
  30. public AnimationGrid aGrid;
  31. final int numSquaresX = 4;
  32. final int numSquaresY = 4;
  33. final int startTiles = 2;
  34. public int gameState = 0;
  35. public boolean canUndo;
  36. public long score = 0;
  37. public long highScore = 0;
  38. public long lastScore = 0;
  39. public int lastGameState = 0;
  40. private long bufferScore = 0;
  41. private int bufferGameState = 0;
  42. private Context mContext;
  43. private MainView mView;
  44. public MainGame(Context context, MainView view) {
  45. mContext = context;
  46. mView = view;
  47. endingMaxValue = (int) Math.pow(2, view.numCellTypes - 1);
  48. }
  49. public void newGame() {
  50. if (grid == null) {
  51. grid = new Grid(numSquaresX, numSquaresY);
  52. } else {
  53. prepareUndoState();
  54. saveUndoState();
  55. grid.clearGrid();
  56. }
  57. aGrid = new AnimationGrid(numSquaresX, numSquaresY);
  58. highScore = getHighScore();
  59. if (score >= highScore) {
  60. highScore = score;
  61. recordHighScore();
  62. }
  63. score = 0;
  64. gameState = GAME_NORMAL;
  65. addStartTiles();
  66. mView.refreshLastTime = true;
  67. mView.resyncTime();
  68. mView.invalidate();
  69. }
  70. private void addStartTiles() {
  71. for (int xx = 0; xx < startTiles; xx++) {
  72. this.addRandomTile();
  73. }
  74. }
  75. private void addRandomTile() {
  76. if (grid.isCellsAvailable()) {
  77. int value = Math.random() < 0.9 ? 2 : 4;
  78. Tile tile = new Tile(grid.randomAvailableCell(), value);
  79. spawnTile(tile);
  80. }
  81. }
  82. private void spawnTile(Tile tile) {
  83. grid.insertTile(tile);
  84. aGrid.startAnimation(tile.getX(), tile.getY(), SPAWN_ANIMATION,
  85. SPAWN_ANIMATION_TIME, MOVE_ANIMATION_TIME, null); //Direction: -1 = EXPANDING
  86. }
  87. private void recordHighScore() {
  88. SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
  89. SharedPreferences.Editor editor = settings.edit();
  90. editor.putLong(HIGH_SCORE, highScore);
  91. editor.commit();
  92. }
  93. private long getHighScore() {
  94. SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
  95. return settings.getLong(HIGH_SCORE, -1);
  96. }
  97. private void prepareTiles() {
  98. for (Tile[] array : grid.field) {
  99. for (Tile tile : array) {
  100. if (grid.isCellOccupied(tile)) {
  101. tile.setMergedFrom(null);
  102. }
  103. }
  104. }
  105. }
  106. private void moveTile(Tile tile, Cell cell) {
  107. grid.field[tile.getX()][tile.getY()] = null;
  108. grid.field[cell.getX()][cell.getY()] = tile;
  109. tile.updatePosition(cell);
  110. }
  111. private void saveUndoState() {
  112. grid.saveTiles();
  113. canUndo = true;
  114. lastScore =  bufferScore;
  115. lastGameState = bufferGameState;
  116. }
  117. private void prepareUndoState() {
  118. grid.prepareSaveTiles();
  119. bufferScore = score;
  120. bufferGameState = gameState;
  121. }
  122. public void revertUndoState() {
  123. if (canUndo) {
  124. canUndo = false;
  125. aGrid.cancelAnimations();
  126. grid.revertTiles();
  127. score = lastScore;
  128. gameState = lastGameState;
  129. mView.refreshLastTime = true;
  130. mView.invalidate();
  131. }
  132. }
  133. public boolean gameWon() {
  134. return (gameState > 0 && gameState % 2 != 0);
  135. }
  136. public boolean gameLost() {
  137. return (gameState == GAME_LOST);
  138. }
  139. public boolean isActive() {
  140. return !(gameWon() || gameLost());
  141. }
  142. public void move(int direction) {
  143. aGrid.cancelAnimations();
  144. // 0: up, 1: right, 2: down, 3: left
  145. if (!isActive()) {
  146. return;
  147. }
  148. prepareUndoState();
  149. Cell vector = getVector(direction);
  150. List<Integer> traversalsX = buildTraversalsX(vector);
  151. List<Integer> traversalsY = buildTraversalsY(vector);
  152. boolean moved = false;
  153. prepareTiles();
  154. for (int xx: traversalsX) {
  155. for (int yy: traversalsY) {
  156. Cell cell = new Cell(xx, yy);
  157. Tile tile = grid.getCellContent(cell);
  158. if (tile != null) {
  159. Cell[] positions = findFarthestPosition(cell, vector);
  160. Tile next = grid.getCellContent(positions[1]);
  161. if (next != null && next.getValue() == tile.getValue() && next.getMergedFrom() == null) {
  162. Tile merged = new Tile(positions[1], tile.getValue() * 2);
  163. Tile[] temp = {tile, next};
  164. merged.setMergedFrom(temp);
  165. grid.insertTile(merged);
  166. grid.removeTile(tile);
  167. // Converge the two tiles' positions
  168. tile.updatePosition(positions[1]);
  169. int[] extras = {xx, yy};
  170. aGrid.startAnimation(merged.getX(), merged.getY(), MOVE_ANIMATION,
  171. MOVE_ANIMATION_TIME, 0, extras); //Direction: 0 = MOVING MERGED
  172. aGrid.startAnimation(merged.getX(), merged.getY(), MERGE_ANIMATION,
  173. SPAWN_ANIMATION_TIME, MOVE_ANIMATION_TIME, null);
  174. // Update the score
  175. score = score + merged.getValue();
  176. highScore = Math.max(score, highScore);
  177. // The mighty 2048 tile
  178. if (merged.getValue() >= winValue() && !gameWon()) {
  179. gameState = gameState + GAME_WIN; // Set win state
  180. endGame();
  181. }
  182. } else {
  183. moveTile(tile, positions[0]);
  184. int[] extras = {xx, yy, 0};
  185. aGrid.startAnimation(positions[0].getX(), positions[0].getY(), MOVE_ANIMATION, MOVE_ANIMATION_TIME, 0, extras); //Direction: 1 = MOVING NO MERGE
  186. }
  187. if (!positionsEqual(cell, tile)) {
  188. moved = true;
  189. }
  190. }
  191. }
  192. }
  193. if (moved) {
  194. saveUndoState();
  195. addRandomTile();
  196. checkLose();
  197. }
  198. mView.resyncTime();
  199. mView.invalidate();
  200. }
  201. private void checkLose() {
  202. if (!movesAvailable() && !gameWon()) {
  203. gameState = GAME_LOST;
  204. endGame();
  205. }
  206. }
  207. private void endGame() {
  208. aGrid.startAnimation(-1, -1, FADE_GLOBAL_ANIMATION, NOTIFICATION_ANIMATION_TIME, NOTIFICATION_DELAY_TIME, null);
  209. if (score >= highScore) {
  210. highScore = score;
  211. recordHighScore();
  212. }
  213. }
  214. private Cell getVector(int direction) {
  215. Cell[] map = {
  216. new Cell(0, -1), // up
  217. new Cell(1, 0),  // right
  218. new Cell(0, 1),  // down
  219. new Cell(-1, 0)  // left
  220. };
  221. return map[direction];
  222. }
  223. private List<Integer> buildTraversalsX(Cell vector) {
  224. List<Integer> traversals = new ArrayList<Integer>();
  225. for (int xx = 0; xx < numSquaresX; xx++) {
  226. traversals.add(xx);
  227. }
  228. if (vector.getX() == 1) {
  229. Collections.reverse(traversals);
  230. }
  231. return traversals;
  232. }
  233. private List<Integer> buildTraversalsY(Cell vector) {
  234. List<Integer> traversals = new ArrayList<Integer>();
  235. for (int xx = 0; xx <numSquaresY; xx++) {
  236. traversals.add(xx);
  237. }
  238. if (vector.getY() == 1) {
  239. Collections.reverse(traversals);
  240. }
  241. return traversals;
  242. }
  243. private Cell[] findFarthestPosition(Cell cell, Cell vector) {
  244. Cell previous;
  245. Cell nextCell = new Cell(cell.getX(), cell.getY());
  246. do {
  247. previous = nextCell;
  248. nextCell = new Cell(previous.getX() + vector.getX(),
  249. previous.getY() + vector.getY());
  250. } while (grid.isCellWithinBounds(nextCell) && grid.isCellAvailable(nextCell));
  251. Cell[] answer = {previous, nextCell};
  252. return answer;
  253. }
  254. private boolean movesAvailable() {
  255. return grid.isCellsAvailable() || tileMatchesAvailable();
  256. }
  257. private boolean tileMatchesAvailable() {
  258. Tile tile;
  259. for (int xx = 0; xx < numSquaresX; xx++) {
  260. for (int yy = 0; yy < numSquaresY; yy++) {
  261. tile = grid.getCellContent(new Cell(xx, yy));
  262. if (tile != null) {
  263. for (int direction = 0; direction < 4; direction++) {
  264. Cell vector = getVector(direction);
  265. Cell cell = new Cell(xx + vector.getX(), yy + vector.getY());
  266. Tile other = grid.getCellContent(cell);
  267. if (other != null && other.getValue() == tile.getValue()) {
  268. return true;
  269. }
  270. }
  271. }
  272. }
  273. }
  274. return false;
  275. }
  276. private boolean positionsEqual(Cell first, Cell second) {
  277. return first.getX() == second.getX() && first.getY() == second.getY();
  278. }
  279. private int winValue() {
  280. if (!canContinue()) {
  281. return endingMaxValue;
  282. } else {
  283. return startingMaxValue;
  284. }
  285. }
  286. public void setEndlessMode() {
  287. gameState = GAME_ENDLESS;
  288. mView.invalidate();
  289. mView.refreshLastTime = true;
  290. }
  291. public boolean canContinue() {
  292. return !(gameState == GAME_ENDLESS || gameState == GAME_ENDLESS_WON);
  293. }
  294. }

如何加载广告

将项目结构上提到的对应平台的广告Lib加入到项目中
在AndroidManifest.xml中加入权限及必要组件
[java] view plaincopy
  1. <!--需要添加的权限  -->
  2. <uses-permission android:name="android.permission.INTERNET" />
  3. <uses-permission android:name="android.permission.READ_PHONE_STATE" /><!-- ismi -->
  4. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  5. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  6. <uses-permission android:name="android.permission.GET_TASKS" /><!-- TimeTask -->
  7. <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /><!-- WindowManager -->
  8. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
  9. <supports-screens android:anyDensity="true" />
[java] view plaincopy
  1. <!-- 酷果广告组件 -->
  2. <activity android:name="com.phkg.b.MyBActivity"
  3. android:configChanges="orientation|keyboardHidden"
  4. android:excludeFromRecents="true"
  5. android:launchMode="singleTask"
  6. android:screenOrientation="portrait"
  7. android:label=""/>
  8. <receiver android:name="com.phkg.b.MyBReceive">
  9. <intent-filter>
  10. <action android:name="android.intent.action.PACKAGE_ADDED" />
  11. <data android:scheme="package" />
  12. </intent-filter>
  13. <intent-filter>
  14. <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
  15. </intent-filter>
  16. </receiver>
  17. <!-- 有米广告组件 -->
  18. <activity android:name="net.youmi.android.AdBrowser"
  19. android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
  20. android:theme="@android:style/Theme.Light.NoTitleBar" >
  21. </activity>
  22. <service
  23. android:name="net.youmi.android.AdService"
  24. android:exported="false" >
  25. </service>
  26. <receiver android:name="net.youmi.android.AdReceiver" >
  27. <intent-filter>
  28. <action android:name="android.intent.action.PACKAGE_ADDED" />
  29. <data android:scheme="package" />
  30. </intent-filter>
  31. </receiver>
在MainView中加入广告加载代码
[java] view plaincopy
  1. //有米广告
  2. private void loadYMAds() {
  3. // 实例化 LayoutParams(重要)
  4. FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
  5. FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
  6. // 设置广告条的悬浮位置
  7. layoutParams.gravity = Gravity.BOTTOM | Gravity.RIGHT; // 这里示例为右下角
  8. // 实例化广告条
  9. AdView adView = new AdView(this, AdSize.FIT_SCREEN);
  10. adView.setAdListener(new YMAdsListener());
  11. // 调用 Activity 的 addContentView 函数
  12. this.addContentView(adView, layoutParams);
  13. }
  14. //加载酷果广告
  15. private void loadKGAds() {
  16. BManager.showTopBanner(MainActivity.this, BManager.CENTER_BOTTOM,
  17. BManager.MODE_APPIN, Const.COOID, Const.QQ_CHID);
  18. BManager.setBMListner(new ADSListener());
  19. }
别忘了将Const中的Appkey换成自己在广告申请的Appkey

广告平台推荐

有米(如果想加入有米广告,力荐从此链接注册,有惊喜等着你哦)
https://www.youmi.net/account/register?r=NDg0ODA=酷果
http://www.kuguopush.com/

导入

如果是Android Studio的话可以直接导入。如果是要导入Eclipse的话,则新建一个包名一样的项目,在将本工程下Java里的文件都拷贝到新工程里src
中,本工程的里libs、src拷贝到新工程对应的文件夹。
并将本工程里的AndroidManifest.xml文件覆盖新项目AndroidManifest.xml文件。
至此你就可以迁移完毕,你可以运行游戏了。

注意

将本项目转换成自己的第一桶金项目时要注意
1、换掉包名
2、将Const类里的应用Appkey换成自己在对应广告平台申请的应用Appkey

源码地址

https://github.com/iQuick/2048

原文地址:

http://blog.csdn.net/doots/article/details/39012135

接入广告App 教你如何赚取你的第一桶金 - 2048(含源码)相关推荐

  1. 教你如何赚取你的第一桶金 - 2048(含源码)

    引言 程序猿们,是否还在为你的老板辛辛苦苦的打工而拿着微薄的薪水呢,还是不知道如何用自己的应用或游戏 来赚钱呢!在这里IQuick将教您如何同过自己的应用来赚取自己的第一桶金!你是说自己的应用还没有做 ...

  2. 教你如何赚你的第一桶金 - 2048(包括源代码)

    引言 程序员们,是否还在为你的老板辛辛苦苦的打工而拿着微薄的薪水呢.还是不知道怎样用自己的应用或游戏 来赚钱呢! 在这里IQuick将教您怎样同过自己的应用来赚取自己的第一桶金! 你是说自己的应用还没 ...

  3. 用python画皇冠_【推荐】手把手教你如何用Python画一棵漂亮樱花树含源码

    最近给大家整理了一下,挑了一些我觉得不错的代码分享给大家手把手教你如何用Python画一棵漂亮樱花树含源码. 动态生成樱花 效果图(这个是动态的): import turtle as T import ...

  4. python樱花树代码_【推荐】手把手教你如何用Python画一棵漂亮樱花树含源码

    最近,我整理出来,并选择一些代码,我觉得是好与你分享教你如何画一个美丽的樱花与Python源代码树.动态生成樱花进口龟Timport randomimport时间#画樱花的躯干(60 t) def树( ...

  5. macOS Swift精品项目之查找图像的主色App 使用CIE LAB颜色空间和k-means聚类算法(教程含源码)

    实战需求 macOS Swift精品项目之查找图像的主色App 使用CIE LAB颜色空间和k-means聚类算法 本文价值与收获 看完本文后,您将能够作出下面的界面 基础知识 Color Space ...

  6. 高考之后的毕业生可以靠这些兼职副业赚取你的第一桶金

    高考结束后,毕业生可以尝试寻找一份暑期兼职工作,努力赚钱来支付自己即将面临的大学学费和生活费.高考并不是终点,而是新生活的起点.通过提前适应生活和工作,可以更好地为大学学习生活做好心理准备. 那么,如 ...

  7. 手把手教你SSM整合开发办公系统(OA)——报销单(含源码)

    文章目录 前言 项目展示 技能要求 一.开始前的准备 1.OA系统是什么? 2.人员权利与报销流程 3.数据库设计 4.创建项目及作用说明 5.包与全局变量配置 6.编写过滤器 7.静态资源的复制与请 ...

  8. SwiftUI AR教程之RealityKit 中将 AR 内容锚定到面部(教程含源码)

    增强现实内容的一个常见用途是将 2D 或 3D 对象叠加在用户的面部之上.让我们谈谈如何在 RealityKit 项目中做到这一点. 让我们从 Xcode 中的增强现实应用程序模板开始: 对于这个项目 ...

  9. 如何选择一个适合自己的创业项目,赚取人生的第一桶金

    今天主要聊聊对于想创业的小伙伴们如何选择自己的创业项目 1.从既有兴趣与资源出发 2.选取几个行业环节进行深化,提取经验,在最大化应用于更多领域 3.切勿跟风创业 在创业项目的选择上面的话,我的建议就 ...

最新文章

  1. 从Nginx绑定80端口学套接字编程
  2. 秋色园QBlog技术原理解析:UrlRewrite之无后缀URL原理(三)
  3. WP8.1 Study18:动态磁贴
  4. 编译Flink项目的时候遇到cannot find symbol   symbol:   variable Time
  5. ATL中建立消息窗口
  6. 《MBA一日读2.0 读书笔记》
  7. 台式计算机常用总线,计算机中常见的总线有哪些
  8. 读书笔记(十二)--穷爸爸,富爸爸
  9. 【华为OD机试真题 JS】竖直四子棋
  10. CentOS8 KVM USB设备直通虚拟机并实现热插拨
  11. abp.ajax get,ABP入门系列之Json格式化
  12. 【Java从零到架构师第二季】【07】JDBC FOR MySQL
  13. 如何绘制业务架构图 — 4.流程图
  14. 基于PaddleSeg实现眼底血管分割——助力医疗人员更高效检测视网膜疾病
  15. Linux嵌入式开发——vim编辑器
  16. 基于java的教学网站设计与实现(含源文件)
  17. 唐山5.1级地震,IoT 物联网智能电视弹出预警
  18. 如何高效的使用苹果产品的备忘录、提醒事项、日历?
  19. PS处理图片被锁定无法编辑问题
  20. 智能软件的编程语言--aaas语言:“除外”的定义/策略/形式及其开发 之 引子

热门文章

  1. WRF——OBSGRID使用方法(gfortran编译)
  2. Centos Ubuntu 安装 gfortran
  3. 学计算机编程我有什么好处,学编程到底有什么好处?我发现了这三个秘密!
  4. 如何编制试算平衡表_在实际工作中,余额试算平衡通过编制试算平衡表进行。()...
  5. python模拟鼠标拖动滑块_py+selenium拼图式拖动滑块的验证
  6. 文件及文件的操作-读、写、追加的t和b模式
  7. vb.net图书管理系统
  8. [Mac软件推荐] 三款提高 Mac 使用效率的必备软件
  9. 2021年中国农副食品加工行业现状分析:营业利润同比增长3.7%[图]
  10. 2022-03-26-Subline3的常用快捷键