Android 2048游戏源代码(Java)下载
程序员文章站
2022-03-01 13:44:44
...
java仿2048游戏实例源码、网上看到的、觉得不错上传出来、供大家一起共同分享学习、主要实现了2048游戏的实现方式、滑动累加数字并出来新的数字、大家可以下载源代码研究研究、运行效果如下
initGameView代码
private void initGameView(){ setColumnCount(4); setBackgroundColor(0xffbbada0); setOnTouchListener(new View.OnTouchListener() { private float startX,startY,offsetX,offsetY; @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: startX=event.getX(); startY=event.getY(); break; case MotionEvent.ACTION_UP: offsetX=event.getX()-startX; offsetY=event.getY()-startY; if (Math.abs(offsetX) > Math.abs(offsetY)) { if (offsetX > 5) { swipeRight(); } else if (offsetX < -5) { swipeLeft(); } } else { if (offsetY > 5) { swipeDown(); } else if (offsetY < -5) { swipeUp(); } } break; default: break; } return true; } }); }
onCreate代码
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); root=(LinearLayout) findViewById(R.id.container); root.setBackgroundColor(0xfffaf8ef); tvScore=(TextView) findViewById(R.id.tvScore); tvBestScore=(TextView) findViewById(R.id.tvBestScore); gameView=(GameView) findViewById(R.id.gameView); animlayer=(AnimLayer) findViewById(R.id.animlayer); btnRestartGame=(Button) findViewById(R.id.btnNewGame); btnRestartGame.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { gameView.startGame(); } }); }
getRandomNum代码
private void getRandomNum(){ emptyPoint.clear(); for (int y = 0; y < 4; y ) { for (int x = 0; x < 4; x ) { if(cardsMap[x][y].getNum()<=0){ emptyPoint.add(new Point(x,y)); } } } if(emptyPoint.size()>0){ Point p=emptyPoint.remove((int)(Math. random()*emptyPoint.size())); cardsMap[p.x][p.y].setNum(Math.random()>0.1?2:4); MainActivity.getMainActivity() .getAnimLayer() .creatScaleTo1(cardsMap[p.x][p.y]); } }
源代码下载链接: http://dwtedx.com/download.html?bdkey=s/1nt9Q1nj 密码: tjb2