激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Android - Android游戲源碼分享之2048

Android游戲源碼分享之2048

2021-03-10 15:34Android開發網 Android

本文主要是給大家分享了安卓版的游戲2048的源碼,以及制作思路,是篇非常不錯的文章,有需要的朋友可以參考下

引言

程序猿們,是否還在為你的老板辛辛苦苦的打工而拿著微薄的薪水呢,還是不知道如何用自己的應用或游戲來賺錢呢!
在這里iquick將教您如何同過自己的應用來賺取自己的第一桶金!
你是說自己的應用還沒有做出來?
不,在這里已經為你提供好了一個完整的游戲應用了,在文章的下面有源碼的地址哦。你只要稍做修改就可以變成一個完全屬于自己的應用了,比如將4*4換成5*5,甚至是其它的。如果你實在是慵懶至極的話,你只要將本應用的包名及廣告換成自己的,就可以上傳到市場上輕輕松松賺取自己的第一桶金了。
如果你覺得本文很贊的話,就頂一下作者吧,從下面的安裝地址中下載應用,或者在導入本工程運行的時候,從廣告中安裝一個應用。動一動你的手指,就能讓作者更進一步,也能讓作者以后更加有動力來分享吧。

安裝

Android游戲源碼分享之2048

Android游戲源碼分享之2048

Android游戲源碼分享之2048

Android游戲源碼分享之2048

項目結構

Android游戲源碼分享之2048

重要代碼解讀mainview游戲的主體類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//初始化方法,里面初始化了一些常量,字體顏色等
name="code" class="java">public mainview(context context) {
    super(context);
 
    resources resources = context.getresources();
    //loading resources
    game = new maingame(context, this);
    try {
 
      //getting assets
      backgroundrectangle = 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();
  }
 
  //游戲界面的繪制
  @override
  protected 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游戲主要邏輯
 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
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 + 1
  public 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: left
    if (!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' positions
            tile.updateposition(positions[1]);
 
            int[] extras = {xx, yy};
            agrid.startanimation(merged.getx(), merged.gety(), move_animation,
                move_animation_time, 0, extras); //direction: 0 = moving merged
            agrid.startanimation(merged.getx(), merged.gety(), merge_animation,
                spawn_animation_time, move_animation_time, null);
 
            // update the score
            score = score + merged.getvalue();
            highscore = math.max(score, highscore);
 
            // the mighty 2048 tile
            if (merged.getvalue() >= winvalue() && !gamewon()) {
              gamestate = gamestate + game_win; // set win state
              endgame();
            }
          } 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), // up
        new cell(1, 0), // right
        new cell(0, 1), // down
        new 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中加入權限及必要組件

?
1
2
3
4
5
6
7
8
9
<!--需要添加的權限 -->
  <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" />

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!-- 酷果廣告組件 -->
  <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中加入廣告加載代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//有米廣告
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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 天天干天天透 | 久久久久成人免费 | 欧美日本亚洲视频 | 亚洲成人午夜精品 | 精品国产一区二区在线观看 | 国产精品视频二区不卡 | 色综合精品 | 黄网在线| 黄色网战在线观看 | 成人永久免费视频 | 91亚洲免费视频 | 美女视频网站黄色 | a网在线| 一级看片免费视频 | 污污网站入口 | 草b视频在线观看 | 中文字幕11| 久草视频手机在线观看 | 国产又白又嫩又紧又爽18p | 亚洲5区 | 国产九九 | 国产一级二级视频 | 亚洲第一精品在线 | 成年人免费视频大全 | 欧美精品v国产精品v日韩精品 | 久久凹凸 | 国产精品久久久久久久久久东京 | 草草影院地址 | 久久国产综合精品 | 国产91亚洲精品一区二区三区 | 蜜桃一本色道久久综合亚洲精品冫 | 91短视频在线播放 | 国产精品视频一区二区三区四区五区 | 91av在线免费| 天天操天天碰 | 激情大乳女做爰办公室韩国 | 日本网站一区二区三区 | 国产一区二区三区影视 | 国产91九色 | 欧美精品一区二区久久久 | 久久99精品久久久久久青青日本 |