DevStore首页 > DEV资源 > 源码下载 > 源码详情

游戏2048

 
石崖 | 2014-11-07 16:52    浏览量(3559)    评论(5)    收藏(1)
功能分类:休闲益智 支持平台:Android 运行环境:Eclipse
开发语言:Java 开发工具:Eclipse 源码大小:10.53MB
2048  游戏 
下载源码 729 人下载

源码简介

仿游戏2048,能达到预期效果,并很好的实现完的感觉。

源码截图

  • 游戏首页
  • 运行图
DevStore所有源码来自用户上传分享,版权问题及牵扯到商业纠纷均与DevStore无关。

源码片段

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences.Editor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private MediaPlayer mMediaPlayer;
	protected int initMusic = R.raw.music1;

	private TextView tvScore, tvBestScore;
	private LinearLayout root = null;
	private Button btnNewGame;
	private Button btnAutoGame;
	private Button btnMusic;
	private Button btnHelpInfo;
	private boolean isMusicOn;

	static final int SIZE = 4;
	static final int DELAY = 1000;
	private int score = 0;

	private GameView gameView;
	private AnimLayer animLayer = null;

	private static MainActivity mainActivity = null;

	public static final String SP_KEY_BEST_SCORE = "bestScore";

	public MainActivity() {
		mainActivity = this;
	}

	public static MainActivity getMainActivity() {
		return mainActivity;
	}

	@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);
		gameView.initGameView(SIZE, DELAY);

		btnNewGame = (Button) findViewById(R.id.btnNewGame);
		btnNewGame.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				gameView.startGame();
			}
		});

		btnAutoGame = (Button) findViewById(R.id.btnAutoGame);
		btnAutoGame.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (gameView.isAutoPlaying() == false) {
					gameView.autoPlay();
					btnAutoGame.setText(R.string.gamepause);
				} else if (gameView.isPaused()) {
					gameView.resume();
					btnAutoGame.setText(R.string.gamepause);
				} else if (gameView.isPaused() == false) {
					gameView.pause();
					btnAutoGame.setText(R.string.gameresume);
				}
			}
		});

		btnMusic = (Button) findViewById(R.id.btnBackgroundMusic);
		btnMusic.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (isMusicOn) {
					isMusicOn = false;
					btnMusic.setText(R.string.openmusic);
					stopBackgroundMusic();
				} else {
					isMusicOn = true;
					btnMusic.setText(R.string.closemusic);
					startBackgroundMusic();
				}
			}
		});

		btnHelpInfo = (Button) findViewById(R.id.btnHelpInfo);
		btnHelpInfo.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				showGameHelpInfo();
			}
		});

		animLayer = (AnimLayer) findViewById(R.id.animLayer);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_helpinfo:
			showGameHelpInfo();
			break;
		case R.id.action_exit:
			showConfirmExit();
			break;
		}

		return super.onMenuItemSelected(featureId, item);
	}

	private boolean isSecondBack = true;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			}
		}
	};

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK)
				&& (event.getAction() == KeyEvent.ACTION_UP)) {
			if (isSecondBack) {
				Toast.makeText(this, getResources().getString(R.string.more),
						Toast.LENGTH_SHORT).show();
				handler.postDelayed(new Runnable() {
					@Override
					public void run() {
						isSecondBack = true;
					}
				}, 2000);

				isSecondBack = false;
			} else {
				System.exit(0);
			}
			return true;
		}
		return super.dispatchKeyEvent(event);
	}

	// @Override
	protected void onDestroy() {
		super.onDestroy();
		// TODO Auto-generated method stub
		if (mMediaPlayer != null) {
			mMediaPlayer.release();
			mMediaPlayer = null;
		}
	}

	public void clearScore() {
		score = 0;
		showScore();
	}

	public void showScore() {
		tvScore.setText(score + "");
	}

	public void addScore(int s) {
		score += s;
		showScore();

		int maxScore = Math.max(score, getBestScore());
		saveBestScore(maxScore);
		showBestScore(maxScore);
	}

	public void saveBestScore(int s) {
		Editor e = getPreferences(MODE_PRIVATE).edit();
		e.putInt(SP_KEY_BEST_SCORE, s);
		e.commit();
	}

	public int getBestScore() {
		return getPreferences(MODE_PRIVATE).getInt(SP_KEY_BEST_SCORE, 0);
	}

	public void showBestScore(int s) {
		tvBestScore.setText(s + "");
	}

	public AnimLayer getAnimLayer() {
		return animLayer;
	}

	public void setAutoPlayButtonText() {
		btnAutoGame.setText(R.string.autogame);
	}

	public void startBackgroundMusic() {
		if ((mMediaPlayer != null) && (mMediaPlayer.isPlaying())) {
			mMediaPlayer.stop();
			mMediaPlayer.reset();
		}
		mMediaPlayer = MediaPlayer.create(this, initMusic);
		mMediaPlayer.setLooping(true);
		mMediaPlayer.start();
	}

	public void stopBackgroundMusic() {
		if ((mMediaPlayer != null) && (mMediaPlayer.isPlaying())) {
			mMediaPlayer.stop();
			mMediaPlayer.reset();
		}
	}

	private void showGameHelpInfo() {
		TextView view = new TextView(MainActivity.this);
		view.setText(R.string.gametactics);
		view.setMaxLines(10);
		new AlertDialog.Builder(MainActivity.this).setTitle("2048��Ϸ����")
				.setView(view)
				.setNeutralButton("OK", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				}).show();
	}

	private void showConfirmExit() {
		new AlertDialog.Builder(MainActivity.this).setTitle("��ʾ")
				.setMessage("��ȷ���˳���Ϸ��")
				.setPositiveButton("��", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				})
				.setNegativeButton("��", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				}).show();
	}

	public void setCardWidth(int cardWidth) {
		animLayer.setCardWidth(cardWidth);
	}

}
  •   赞(0) 赞 +1 赞(0) 已赞
  •   收藏(1) 收藏 +1 已收藏 取消
  •   下载(729)
评论(5)
jiae

jiae 2014-12-10 07:50:51

非常感谢~~
回复(0) 赞(0) 赞(1)
helis

helis 2014-11-11 20:39:03

方格游戏?》
回复(0) 赞(0) 赞(1)
sryz

sryz 2014-11-07 18:05:38

中文怎么是乱码的?
回复(1) 赞(0) 赞(1)
 
  • 石崖:
    GBK和UTF-8切换一下
    回复2014-11-29 15:23:57
Boss_Huang

Boss_Huang 2014-11-07 17:56:53

还不错。。。
回复(0) 赞(0) 赞(1)
jilimi

jilimi 2014-11-07 17:52:29

正好需要学习, 需要游戏引擎吗?
回复(0) 赞(0) 赞(1)
  • 推荐
  • 审核
  • 收录
  • 获取活力值

源码上传作者

  • 4 能力值
  • 2 源码
  • 0 评测
  • 0 资料

热门源码