Android:添加游戏完成后激活更改的功能

时间:2014-07-25 13:29:25

标签: android

我希望在游戏中添加以下功能:

- 当游戏完成时(屏幕上不再显示任何卡片),然后转到新活动

我知道如何使用意图转移到另一个活动但我不确定如何在这种情况下实现功能。

即。在移动到下一个活动之前,我可以使用什么变量/信息来确保游戏完成?

供参考,该游戏以this open source game为基础。此处显示游戏图像以提供想法。

当前代码:

public class Manager extends Activity {
    private static int ROW_COUNT = -1;
    private static int COL_COUNT = -1;
    private Context context;
    private Drawable backImage;
    private int [] [] cards;
    private List<Drawable> images;
    private Card firstCard;
    private Card seconedCard;
    private ButtonListener buttonListener;

    private static Object lock = new Object();

    int turns;
    private TableLayout mainTable;
    private UpdateCardsHandler handler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



        handler = new UpdateCardsHandler();
        loadImages();
        setContentView(R.layout.main);

        TextView url = ((TextView)findViewById(R.id.myWebSite));
        Linkify.addLinks(url, Linkify.WEB_URLS);

       backImage =  getResources().getDrawable(R.drawable.icon);

       /*
       ((Button)findViewById(R.id.ButtonNew)).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            newGame();

        }


    });*/

       buttonListener = new ButtonListener();

        mainTable = (TableLayout)findViewById(R.id.TableLayout03);


        context  = mainTable.getContext();

         Spinner s = (Spinner) findViewById(R.id.Spinner01);
            ArrayAdapter adapter = ArrayAdapter.createFromResource(
                    this, R.array.type, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            s.setAdapter(adapter);


            s.setOnItemSelectedListener(new OnItemSelectedListener(){

              @Override
              public void onItemSelected(
                      android.widget.AdapterView<?> arg0, 
                      View arg1, int pos, long arg3){

                  ((Spinner) findViewById(R.id.Spinner01)).setSelection(0);

                int x,y;

                switch (pos) {
                case 1:
                    x=4;y=4;
                    break;
                case 2:
                    x=4;y=5;
                    break;
                case 3:
                    x=4;y=6;
                    break;
                case 4:
                    x=5;y=6;
                    break;
                case 5:
                    x=6;y=6;
                    break;
                default:
                    return;
                }
                newGame(x,y);

            }


            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }

        });
    }

    private void newGame(int c, int r) {
        ROW_COUNT = r;
        COL_COUNT = c;

        cards = new int [COL_COUNT] [ROW_COUNT];


        mainTable.removeView(findViewById(R.id.TableRow01));
        mainTable.removeView(findViewById(R.id.TableRow02));

        TableRow tr = ((TableRow)findViewById(R.id.TableRow03));
        tr.removeAllViews();

        mainTable = new TableLayout(context);
        tr.addView(mainTable);

         for (int y = 0; y < ROW_COUNT; y++) {
             mainTable.addView(createRow(y));
          }

         firstCard=null;
         loadCards();

         turns=0;
         ((TextView)findViewById(R.id.tv1)).setText("Tries: "+turns);


    }

    private void loadImages() {
        images = new ArrayList<Drawable>();

        images.add(getResources().getDrawable(R.drawable.card1));
        images.add(getResources().getDrawable(R.drawable.card2));
        images.add(getResources().getDrawable(R.drawable.card3));
        images.add(getResources().getDrawable(R.drawable.card4));
        images.add(getResources().getDrawable(R.drawable.card5));
        images.add(getResources().getDrawable(R.drawable.card6));
        images.add(getResources().getDrawable(R.drawable.card7));
        images.add(getResources().getDrawable(R.drawable.card8));
        images.add(getResources().getDrawable(R.drawable.card9));
        images.add(getResources().getDrawable(R.drawable.card10));
        images.add(getResources().getDrawable(R.drawable.card11));
        images.add(getResources().getDrawable(R.drawable.card12));
        images.add(getResources().getDrawable(R.drawable.card13));
        images.add(getResources().getDrawable(R.drawable.card14));
        images.add(getResources().getDrawable(R.drawable.card15));
        images.add(getResources().getDrawable(R.drawable.card16));
        images.add(getResources().getDrawable(R.drawable.card17));
        images.add(getResources().getDrawable(R.drawable.card18));
        images.add(getResources().getDrawable(R.drawable.card19));
        images.add(getResources().getDrawable(R.drawable.card20));
        images.add(getResources().getDrawable(R.drawable.card21));

    }

    private void loadCards(){
        try{
            int size = ROW_COUNT*COL_COUNT;

            Log.i("loadCards()","size=" + size);

            ArrayList<Integer> list = new ArrayList<Integer>();

            for(int i=0;i<size;i++){
                list.add(new Integer(i));
            }


            Random r = new Random();

            for(int i=size-1;i>=0;i--){
                int t=0;

                if(i>0){
                    t = r.nextInt(i);
                }

                t=list.remove(t).intValue();
                cards[i%COL_COUNT][i/COL_COUNT]=t%(size/2);

                Log.i("loadCards()", "card["+(i%COL_COUNT)+
                        "]["+(i/COL_COUNT)+"]=" + cards[i%COL_COUNT][i/COL_COUNT]);
            }
        }
        catch (Exception e) {
            Log.e("loadCards()", e+"");
        }

    }

    private TableRow createRow(int y){
         TableRow row = new TableRow(context);
         row.setHorizontalGravity(Gravity.CENTER);

         for (int x = 0; x < COL_COUNT; x++) {
                 row.addView(createImageButton(x,y));
         }
         return row;
    }

    private View createImageButton(int x, int y){
        Button button = new Button(context);
        button.setBackgroundDrawable(backImage);
        button.setId(100*x+y);
        button.setOnClickListener(buttonListener);
        return button;
    }

    class ButtonListener implements OnClickListener {

        @Override
        public void onClick(View v) {

            synchronized (lock) {
                if(firstCard!=null && seconedCard != null){
                    return;
                }
                int id = v.getId();
                int x = id/100;
                int y = id%100;
                turnCard((Button)v,x,y);
            }

        }

        private void turnCard(Button button,int x, int y) {
            button.setBackgroundDrawable(images.get(cards[x][y]));

            if(firstCard==null){
                firstCard = new Card(button,x,y);
            }
            else{ 

                if(firstCard.x == x && firstCard.y == y){
                    return; //the user pressed the same card
                }

                seconedCard = new Card(button,x,y);

                turns++;
                ((TextView)findViewById(R.id.tv1)).setText("Tries: "+turns);


                TimerTask tt = new TimerTask() {

                    @Override
                    public void run() {
                        try{
                            synchronized (lock) {
                              handler.sendEmptyMessage(0);
                            }
                        }
                        catch (Exception e) {
                            Log.e("E1", e.getMessage());
                        }
                    }
                };

                  Timer t = new Timer(false);
                    t.schedule(tt, 1300);
            }


           }

        }

    class UpdateCardsHandler extends Handler{

        @Override
        public void handleMessage(Message msg) {
            synchronized (lock) {
                checkCards();
            }
        }
         public void checkCards(){
                if(cards[seconedCard.x][seconedCard.y] == cards[firstCard.x][firstCard.y]){
                    firstCard.button.setVisibility(View.INVISIBLE);
                    seconedCard.button.setVisibility(View.INVISIBLE);
                }
                else {
                    seconedCard.button.setBackgroundDrawable(backImage);
                    firstCard.button.setBackgroundDrawable(backImage);
                }

                firstCard=null;
                seconedCard=null;
            }
    }




}

1 个答案:

答案 0 :(得分:0)

最简单的方法是使用if语句检查win条件。这应该在实际进行转弯的方法中完成,我假设在turnCard()方法中发生转弯。

if (winConditionMet) {
    displayWinningScreen();
} else if (lossConditionMet) {
    displayLosingScreen();
}

如果已满足条件,则调用处理包装该屏幕的方法,然后启动新活动。例如,您可以在屏幕上添加一个按钮,其中包含您想要的任何文本,按下该按钮时,用户将进入下一个屏幕,无论是您的分数屏幕,重播屏幕,主菜单还是您拥有的内容。

编辑:好的,因为这是一个记忆游戏,你可以在每个回合结束时迭代卡片,并检查是否有任何卡片仍将其图像设置为backImage。如果没有剩下的设置为backImage,则可以使用if语句中的代码结束游戏。

或者,您可以使用某种形式的Map来跟踪每张卡是否已永久启用,而不是使用ArrayList。