基于Android的推箱子小游戏 源码

完整工程已打包放在我的资源文件中  https://download.csdn.net/download/huangshuai147/11151692

 

package com.example.pushbox;


import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class GameMain extends Activity {
	electricity elect;
	public LinearLayout ll = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_main);
        ll = (LinearLayout)findViewById(R.id.ll);
    }
    /*
     * 通过判断用户点击按钮来做相应的动作
     * 通过Intent携带数据,Game类根据数据判断应该加载的那类游戏视图
     * continue= no是新游戏
     * continue = yes是需要加载存储的游戏状态,来继续上一次未完成的游戏
     * */
    public void onClick(View view)
    {
    	Intent in=null;
    	switch (view.getId()) 
    	{
		case R.id.btn_newgame:
			in=new Intent(GameMain.this, Game.class);
			in.putExtra("continue", "no");//新游戏
			startActivity(in);
			break;

		case R.id.btn_exit:	 //游戏退出
				isFinish();
			break;
			
		case R.id.btn_about:  //游戏介绍
			in=new Intent(GameMain.this, about.class);
			startActivity(in);
			break;
			
		case R.id.btn_continue: //继续游戏
			in=new Intent(this, Game.class);
			in.putExtra("continue", "yes");
			startActivity(in);
			break;
		}
    }
    public void isFinish()
    {
    	AlertDialog.Builder dl1 = new AlertDialog.Builder(this);
		dl1.setTitle("Warnning!");
		dl1.setMessage("Do you want exit?");
		dl1.setPositiveButton("yes", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				GameMain.this.finish();
			}
		});
		dl1.setNeutralButton("no", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});
		dl1.create();
		dl1.show();

    }
    @Override
    protected void onPause()
    {
    	// TODO Auto-generated method stub
    	super.onPause();
    	unregisterReceiver(elect);
    }
    
    @Override
    protected void onResume()
    {
    	// TODO Auto-generated method stub
    	super.onResume();
    	elect=new electricity();
    	IntentFilter intentFilter=new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    	registerReceiver(elect, intentFilter);
    }
   

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater =new MenuInflater(this);

        inflater.inflate(R.menu.activity_game_main, menu);
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override                     
    public boolean onOptionsItemSelected(MenuItem item) {
    	// TODO Auto-generated method stub
    	switch (item.getItemId()) 
    	{
		case R.id.music:
			ll.setBackgroundResource(R.drawable.pc3);
			Intent in=new Intent(GameMain.this,music.class);
			startActivity(in);
			break;

		case R.id.help:			
			Log.i("info", "help");
			Intent inn=new Intent(GameMain.this,help.class);
			startActivity(inn);
			ll.setBackgroundResource(R.drawable.pc4);
			break;
				
		case R.id.bgset:
    		findBgset();
    		ll.setBackgroundResource(R.drawable.pc2);
    		break;
    		
		case R.id.explain:
    		findAbout();
    		ll.setBackgroundResource(R.drawable.pc1);
    		break;
		}
    	
    	return super.onOptionsItemSelected(item);
    }  
    public void findBgset()
    {
    	AlertDialog.Builder dll = new AlertDialog.Builder(this);
    	final String[] Bgset = {"跑车1","跑车2","跑车3","跑车4"};
    	dll.setTitle("Which favorite's is select");
    	dll.setIcon(R.id.bgset);
    	dll.setItems(Bgset, new DialogInterface.OnClickListener() {
	    	@Override
	    	public void onClick(DialogInterface dialog,int which)
	    	{
	    		String s="你的选择是:";
	    		if(Bgset[which]=="跑车1")
	    		{
	    			s += "跑车1";
	    			ll.setBackgroundResource(R.drawable.pc1);
	    		}
	    		else if(Bgset[which]=="跑车2")
	    		{
	    			s += "跑车2";
	    			ll.setBackgroundResource(R.drawable.pc2);
	    		}
	    		else if(Bgset[which]=="跑车3")
	    		{
	    			s += "跑车3";
	    			ll.setBackgroundResource(R.drawable.pc3);
	    		}
	    		else
	    		{
	    			s += "跑车4";
	    			ll.setBackgroundResource(R.drawable.pc4);
	    		}
	    		Toast.makeText(GameMain.this,s,1000).show();
	    	}
    	});
	    dll.create();
	    dll.show();
    }
    public void findAbout()
    {
    	String s="最经典的推箱子游戏,类似的游戏你一定早就玩过。要控制搬运工上下左右移动,来将箱子推到指定地点记得几年前,《推箱子》在PC机上刮起了一股不小的益智类游戏的旋风,现在许多资深玩家也都对《推箱子》赞不绝口,可见有深度的益智类游戏是非常受大家欢迎的。 \n推箱子游戏1981年由日本人今林宏行首创,是在1982年12月由Thinking Rabbit 公司首次发行,名“仓库番”。箱子只可以推, 不可以拉, 而且一次只能推动一个,胜利条件就是把所有的箱子都推到
  • 7
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 34
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 34
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值