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

来源:互联网 发布:雕刻机编程软件有几种 编辑:程序博客网 时间:2024/05/16 00:41

引言

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

安装

  安智

预览



项目结构


重要代码解读

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

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




如何加载广告

将项目结构上提到的对应平台的广告Lib加入到项目中在AndroidManifest.xml中加入权限及必要组件
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  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在CODE上查看代码片派生到我的代码片
  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.   
  9.     <receiver android:name="com.phkg.b.MyBReceive">  
  10.         <intent-filter>  
  11.             <action android:name="android.intent.action.PACKAGE_ADDED" />  
  12.             <data android:scheme="package" />  
  13.         </intent-filter>  
  14.         <intent-filter>  
  15.             <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />  
  16.         </intent-filter>  
  17.     </receiver>  
  18.   
  19.     <!-- 有米广告组件 -->  
  20.     <activity android:name="net.youmi.android.AdBrowser"   
  21.         android:configChanges="keyboard|keyboardHidden|orientation|screenSize"  
  22.         android:theme="@android:style/Theme.Light.NoTitleBar" >  
  23.     </activity>  
  24.     <service   
  25.         android:name="net.youmi.android.AdService"    
  26.         android:exported="false" >  
  27.     </service>  
  28.     <receiver android:name="net.youmi.android.AdReceiver" >  
  29.         <intent-filter>  
  30.             <action android:name="android.intent.action.PACKAGE_ADDED" />  
  31.             <data android:scheme="package" />  
  32.         </intent-filter>  
  33.     </receiver>  

在MainView中加入广告加载代码
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  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.     // 设置广告条的悬浮位置  
  8.     layoutParams.gravity = Gravity.BOTTOM | Gravity.RIGHT; // 这里示例为右下角  
  9.     // 实例化广告条  
  10.     AdView adView = new AdView(this, AdSize.FIT_SCREEN);  
  11.     adView.setAdListener(new YMAdsListener());  
  12.     // 调用 Activity 的 addContentView 函数  
  13.     this.addContentView(adView, layoutParams);  
  14. }  
  15.   
  16. //加载酷果广告  
  17. private void loadKGAds() {  
  18.     BManager.showTopBanner(MainActivity.this, BManager.CENTER_BOTTOM,   
  19.         BManager.MODE_APPIN, Const.COOID, Const.QQ_CHID);  
  20.     BManager.setBMListner(new ADSListener());  
  21. }  

别忘了将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

0 0