引言

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

安装

   安智

预览

项目结构

重要代码解读

MainView游戏的主体类
//初始化方法,里面初始化了一些常量,字体颜色等
name="code" class="java">public MainView(Context context) {super(context);Resources resources = context.getResources();//Loading resourcesgame = new MainGame(context, this);try {//Getting assetsbackgroundRectangle = resources.getDrawable(R.drawable.background_rectangle);lightUpRectangle = resources.getDrawable(R.drawable.light_up_rectangle);fadeRectangle = resources.getDrawable(R.drawable.fade_rectangle);TEXT_WHITE = resources.getColor(R.color.text_white);TEXT_BLACK = resources.getColor(R.color.text_black);TEXT_BROWN = resources.getColor(R.color.text_brown);this.setBackgroundColor(resources.getColor(R.color.background));Typeface font = Typeface.createFromAsset(resources.getAssets(), "ClearSans-Bold.ttf");paint.setTypeface(font);paint.setAntiAlias(true);} catch (Exception e) {System.out.println("Error getting assets?");}setOnTouchListener(new InputListener(this));game.newGame();}//游戏界面的绘制@Overrideprotected void onSizeChanged(int width, int height, int oldw, int oldh) {super.onSizeChanged(width, height, oldw, oldh);getLayout(width, height);createBitmapCells();createBackgroundBitmap(width, height);createOverlays();}

MianGame游戏主要逻辑

package com.tpcstld.twozerogame;import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;import java.util.ArrayList;
import java.util.Collections;
import java.util.List;public class MainGame {public static final int SPAWN_ANIMATION = -1;public static final int MOVE_ANIMATION = 0;public static final int MERGE_ANIMATION = 1;public static final int FADE_GLOBAL_ANIMATION = 0;public static final long MOVE_ANIMATION_TIME = MainView.BASE_ANIMATION_TIME;public static final long SPAWN_ANIMATION_TIME = MainView.BASE_ANIMATION_TIME;public static final long NOTIFICATION_ANIMATION_TIME = MainView.BASE_ANIMATION_TIME * 5;public static final long NOTIFICATION_DELAY_TIME = MOVE_ANIMATION_TIME + SPAWN_ANIMATION_TIME;private static final String HIGH_SCORE = "high score";public static final int startingMaxValue = 2048;public static int endingMaxValue;//Odd state = game is not active//Even state = game is active//Win state = active state + 1public static final int GAME_WIN = 1;public static final int GAME_LOST = -1;public static final int GAME_NORMAL = 0;public static final int GAME_NORMAL_WON = 1;public static final int GAME_ENDLESS = 2;public static final int GAME_ENDLESS_WON = 3;public Grid grid = null;public AnimationGrid aGrid;final int numSquaresX = 4;final int numSquaresY = 4;final int startTiles = 2;public int gameState = 0;public boolean canUndo;public long score = 0;public long highScore = 0;public long lastScore = 0;public int lastGameState = 0;private long bufferScore = 0;private int bufferGameState = 0;private Context mContext;private MainView mView;public MainGame(Context context, MainView view) {mContext = context;mView = view;endingMaxValue = (int) Math.pow(2, view.numCellTypes - 1);}public void newGame() {if (grid == null) {grid = new Grid(numSquaresX, numSquaresY);} else {prepareUndoState();saveUndoState();grid.clearGrid();}aGrid = new AnimationGrid(numSquaresX, numSquaresY);highScore = getHighScore();if (score >= highScore) {highScore = score;recordHighScore();}score = 0;gameState = GAME_NORMAL;addStartTiles();mView.refreshLastTime = true;mView.resyncTime();mView.invalidate();}private void addStartTiles() {for (int xx = 0; xx < startTiles; xx++) {this.addRandomTile();}}private void addRandomTile() {if (grid.isCellsAvailable()) {int value = Math.random() < 0.9 ? 2 : 4;Tile tile = new Tile(grid.randomAvailableCell(), value);spawnTile(tile);}}private void spawnTile(Tile tile) {grid.insertTile(tile);aGrid.startAnimation(tile.getX(), tile.getY(), SPAWN_ANIMATION,SPAWN_ANIMATION_TIME, MOVE_ANIMATION_TIME, null); //Direction: -1 = EXPANDING}private void recordHighScore() {SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);SharedPreferences.Editor editor = settings.edit();editor.putLong(HIGH_SCORE, highScore);editor.commit();}private long getHighScore() {SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);return settings.getLong(HIGH_SCORE, -1);}private void prepareTiles() {for (Tile[] array : grid.field) {for (Tile tile : array) {if (grid.isCellOccupied(tile)) {tile.setMergedFrom(null);}}}}private void moveTile(Tile tile, Cell cell) {grid.field[tile.getX()][tile.getY()] = null;grid.field[cell.getX()][cell.getY()] = tile;tile.updatePosition(cell);}private void saveUndoState() {grid.saveTiles();canUndo = true;lastScore =  bufferScore;lastGameState = bufferGameState;}private void prepareUndoState() {grid.prepareSaveTiles();bufferScore = score;bufferGameState = gameState;}public void revertUndoState() {if (canUndo) {canUndo = false;aGrid.cancelAnimations();grid.revertTiles();score = lastScore;gameState = lastGameState;mView.refreshLastTime = true;mView.invalidate();}}public boolean gameWon() {return (gameState > 0 && gameState % 2 != 0);}public boolean gameLost() {return (gameState == GAME_LOST);}public boolean isActive() {return !(gameWon() || gameLost());}public void move(int direction) {aGrid.cancelAnimations();// 0: up, 1: right, 2: down, 3: leftif (!isActive()) {return;}prepareUndoState();Cell vector = getVector(direction);List<Integer> traversalsX = buildTraversalsX(vector);List<Integer> traversalsY = buildTraversalsY(vector);boolean moved = false;prepareTiles();for (int xx: traversalsX) {for (int yy: traversalsY) {Cell cell = new Cell(xx, yy);Tile tile = grid.getCellContent(cell);if (tile != null) {Cell[] positions = findFarthestPosition(cell, vector);Tile next = grid.getCellContent(positions[1]);if (next != null && next.getValue() == tile.getValue() && next.getMergedFrom() == null) {Tile merged = new Tile(positions[1], tile.getValue() * 2);Tile[] temp = {tile, next};merged.setMergedFrom(temp);grid.insertTile(merged);grid.removeTile(tile);// Converge the two tiles' positionstile.updatePosition(positions[1]);int[] extras = {xx, yy};aGrid.startAnimation(merged.getX(), merged.getY(), MOVE_ANIMATION,MOVE_ANIMATION_TIME, 0, extras); //Direction: 0 = MOVING MERGEDaGrid.startAnimation(merged.getX(), merged.getY(), MERGE_ANIMATION,SPAWN_ANIMATION_TIME, MOVE_ANIMATION_TIME, null);// Update the scorescore = score + merged.getValue();highScore = Math.max(score, highScore);// The mighty 2048 tileif (merged.getValue() >= winValue() && !gameWon()) {gameState = gameState + GAME_WIN; // Set win stateendGame();}} else {moveTile(tile, positions[0]);int[] extras = {xx, yy, 0};aGrid.startAnimation(positions[0].getX(), positions[0].getY(), MOVE_ANIMATION, MOVE_ANIMATION_TIME, 0, extras); //Direction: 1 = MOVING NO MERGE}if (!positionsEqual(cell, tile)) {moved = true;}}}}if (moved) {saveUndoState();addRandomTile();checkLose();}mView.resyncTime();mView.invalidate();}private void checkLose() {if (!movesAvailable() && !gameWon()) {gameState = GAME_LOST;endGame();}}private void endGame() {aGrid.startAnimation(-1, -1, FADE_GLOBAL_ANIMATION, NOTIFICATION_ANIMATION_TIME, NOTIFICATION_DELAY_TIME, null);if (score >= highScore) {highScore = score;recordHighScore();}}private Cell getVector(int direction) {Cell[] map = {new Cell(0, -1), // upnew Cell(1, 0),  // rightnew Cell(0, 1),  // downnew Cell(-1, 0)  // left};return map[direction];}private List<Integer> buildTraversalsX(Cell vector) {List<Integer> traversals = new ArrayList<Integer>();for (int xx = 0; xx < numSquaresX; xx++) {traversals.add(xx);}if (vector.getX() == 1) {Collections.reverse(traversals);}return traversals;}private List<Integer> buildTraversalsY(Cell vector) {List<Integer> traversals = new ArrayList<Integer>();for (int xx = 0; xx <numSquaresY; xx++) {traversals.add(xx);}if (vector.getY() == 1) {Collections.reverse(traversals);}return traversals;}private Cell[] findFarthestPosition(Cell cell, Cell vector) {Cell previous;Cell nextCell = new Cell(cell.getX(), cell.getY());do {previous = nextCell;nextCell = new Cell(previous.getX() + vector.getX(),previous.getY() + vector.getY());} while (grid.isCellWithinBounds(nextCell) && grid.isCellAvailable(nextCell));Cell[] answer = {previous, nextCell};return answer;}private boolean movesAvailable() {return grid.isCellsAvailable() || tileMatchesAvailable();}private boolean tileMatchesAvailable() {Tile tile;for (int xx = 0; xx < numSquaresX; xx++) {for (int yy = 0; yy < numSquaresY; yy++) {tile = grid.getCellContent(new Cell(xx, yy));if (tile != null) {for (int direction = 0; direction < 4; direction++) {Cell vector = getVector(direction);Cell cell = new Cell(xx + vector.getX(), yy + vector.getY());Tile other = grid.getCellContent(cell);if (other != null && other.getValue() == tile.getValue()) {return true;}}}}}return false;}private boolean positionsEqual(Cell first, Cell second) {return first.getX() == second.getX() && first.getY() == second.getY();}private int winValue() {if (!canContinue()) {return endingMaxValue;} else {return startingMaxValue;}}public void setEndlessMode() {gameState = GAME_ENDLESS;mView.invalidate();mView.refreshLastTime = true;}public boolean canContinue() {return !(gameState == GAME_ENDLESS || gameState == GAME_ENDLESS_WON);}
}

如何加载广告

将项目结构上提到的对应平台的广告Lib加入到项目中
在AndroidManifest.xml中加入权限及必要组件
<!--需要添加的权限  --><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.READ_PHONE_STATE" /><!-- ismi --><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.GET_TASKS" /><!-- TimeTask --><uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /><!-- WindowManager --><uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/><supports-screens android:anyDensity="true" />
<!-- 酷果广告组件 --><activity android:name="com.phkg.b.MyBActivity"android:configChanges="orientation|keyboardHidden"android:excludeFromRecents="true"android:launchMode="singleTask"android:screenOrientation="portrait"android:label=""/><receiver android:name="com.phkg.b.MyBReceive"><intent-filter><action android:name="android.intent.action.PACKAGE_ADDED" /><data android:scheme="package" /></intent-filter><intent-filter><action android:name="android.net.conn.CONNECTIVITY_CHANGE" /></intent-filter></receiver><!-- 有米广告组件 --><activity android:name="net.youmi.android.AdBrowser" android:configChanges="keyboard|keyboardHidden|orientation|screenSize"android:theme="@android:style/Theme.Light.NoTitleBar" ></activity><service android:name="net.youmi.android.AdService"  android:exported="false" ></service><receiver android:name="net.youmi.android.AdReceiver" ><intent-filter><action android:name="android.intent.action.PACKAGE_ADDED" /><data android:scheme="package" /></intent-filter></receiver>
在MainView中加入广告加载代码
    //有米广告private void loadYMAds() {// 实例化 LayoutParams(重要)FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams( FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);// 设置广告条的悬浮位置layoutParams.gravity = Gravity.BOTTOM | Gravity.RIGHT; // 这里示例为右下角// 实例化广告条AdView adView = new AdView(this, AdSize.FIT_SCREEN);adView.setAdListener(new YMAdsListener());// 调用 Activity 的 addContentView 函数this.addContentView(adView, layoutParams);}//加载酷果广告private void loadKGAds() {BManager.showTopBanner(MainActivity.this, BManager.CENTER_BOTTOM, BManager.MODE_APPIN, Const.COOID, Const.QQ_CHID);BManager.setBMListner(new ADSListener());}
别忘了将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

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

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

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

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

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

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

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

  4. 怎样快速借助互联网赚取人生的第一桶金 ?

    互联网创业新思维让你赚钱变得轻松简单,现在这个社会最不缺的就是项目,最缺的是思维,思维不对走错路无论你如何努力,如何励志也赚不到钱,所以有钱的人都是少数的,这个社会最不缺的就是项目,最缺的是思维. 在 ...

  5. 自学Python兼职赚取人生的第一桶金,他能做到为什么我却不行?原因到底是.....

    1.学习经历 大一刚开学,还沉浸在暑假的快乐之中,每天浑浑噩噩,每天只知道按着课表上课,去开班会,参加学校活动,其余课余时间不是在干饭的路上就是在床上刷抖音打游戏,宿舍里说一句上号可能瞬间能凑齐五个人 ...

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

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

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

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

  8. 如何用你的Python代码赚取你的第一桶金

    菜鸟独白 Python语言非常优美,语法简洁而功能强大,容易上手,学好Python能干很多事情:比如爬虫,数据分析呀,机器学习啊,web开发,其实Python还能帮你赚钱,比如自己做一个量化分析的小工 ...

  9. 星球企划书 | 从这个星球赚取你的第一桶金

    不甘现状的上海土著                                                                                  插画师 /小胖 编 ...

最新文章

  1. mysql from_unixtime_MySQL 数据库中日期与时间函数 FROM_UNIXTIME(), UNIX_TIME() ...
  2. C/C++协程实现-学习笔记
  3. 机器学习分类算法_机器学习分类算法
  4. python 结尾回车_理解不了Python正则表达式?我帮你搞定
  5. C++/C高级数据类型
  6. Atitit hi dev eff topic by use dsl sql coll op 提升开发效率sql dsl查询内存集合列表 目录 1.1. Dsl api lib 1 1.2. R
  7. 2018年12月份计算机,CPU天梯图2018年12月最新版 十二月台式电脑CPU性能排行
  8. 【ASE学习】-测量石墨烯结构的碳碳键平均键长
  9. android系统应用更改内存,安卓root后必备神器:修改系统/清理内存神器
  10. Alibaba Arthas快速入门 简单易懂
  11. 《AngularJS深度剖析与最佳实践》一1.1 环境准备
  12. 麦克风有电流声怎么办?
  13. 视频教程-【吴刚】电商活动站设计初级入门标准视频教程-UI
  14. 新手如何第一次编写 “Hello World“ Windows 驱动程序 (KMDF)
  15. 雷电模拟器一键宏实现循环点击
  16. 2022 综合英语慕课(大学英语二)最新满分章节测试答案(题库中自找具体题目)
  17. 江苏小学计算机面试题目,2019下半年江苏省小学信息技术教师资格证面试试题(精选)(三)...
  18. tableView 取消 cell 默认下划线样式 - iOS
  19. 二月,适合一个人去的旅行地
  20. 使用RENREN-GENERATOR时遇到循环错误问题 Relying upon circular references is discouraged and they are prohibited

热门文章

  1. 阿里云无影云桌面服务关联角色设置及删除方法
  2. 如何编制试算平衡表_会计试算平衡表怎么编制
  3. 程序员参加年会,CTO 要求技术部门穿成这样
  4. BUGKU-成绩查询
  5. Google guava学习
  6. 小知识:什么是build.prop?
  7. 乱七八糟之处理器天梯图
  8. macOS 神器 Workflow ,让效率翻倍!
  9. python找零_python 找零问题 动态规划
  10. 数字化改革是逼不得已,最后却帮我提高了20%生产效率