如何使两个图像在匹配时都停留并在不匹配时后退(记忆匹配游戏)?

时间:2018-10-25 05:50:43

标签: java android

我目前正在开发一个Android游戏应用程序(Memory Game),其中游戏的目的是匹配2个相同的图像,直到网格中的所有图像都匹配为止。一次仅显示2张图像,如果两个图像都匹配,它们将保留,否则,它们将向后翻转。我仍然是Java的初学者。

package jimosman311.gmail.com.j39712_co5025_asg;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.os.Handler;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.ToggleButton;

import java.util.Locale;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class GameActivity extends AppCompatActivity implements 
View.OnClickListener {

int count;
Button[] Buttons = new Button[9];
int[] myImageArr = new int[]{R.drawable.apple, R.drawable.apple, R.drawable.grape, R.drawable.grape, R.drawable.orange, R.drawable.orange, R.drawable.watermelon, R.drawable.watermelon};
int[] click1, click2;
Handler handler;
Runnable r;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });

    handler = new Handler();
    r = new Runnable() {
        public void run() {
            flipbacktimer();
        }
    };


    //Randomize Images
    randomize(myImageArr, myImageArr.length);

    //Reference FindViewID

    Buttons[0] = (Button) findViewById(R.id.one);
    Buttons[1] = (Button) findViewById(R.id.two);
    Buttons[2] = (Button) findViewById(R.id.three);
    Buttons[3] = (Button) findViewById(R.id.four);
    Buttons[4] = (Button) findViewById(R.id.five);
    Buttons[5] = (Button) findViewById(R.id.six);
    Buttons[6] = (Button) findViewById(R.id.seven);
    Buttons[7] = (Button) findViewById(R.id.eight);
    Buttons[8] = (Button) findViewById(R.id.nine);

    Button newgameButton = (Button) findViewById(R.id.newgamebutton2);

    //Set OnClick Listener

    Buttons[0].setOnClickListener(this);
    Buttons[1].setOnClickListener(this);
    Buttons[2].setOnClickListener(this);
    Buttons[3].setOnClickListener(this);
    Buttons[4].setOnClickListener(this);
    Buttons[5].setOnClickListener(this);
    Buttons[6].setOnClickListener(this);
    Buttons[7].setOnClickListener(this);
    Buttons[8].setOnClickListener(this);

    newgameButton.setOnClickListener(this);

    //Clear Grid of Numbers

    for (int i = 0; i <= 8; i++) {
        Buttons[i].setText("");
        Buttons[i].setEnabled(true);
    }
}

public void onUserInteraction() {
    super.onUserInteraction();
    stopHandler();//stop first and then start
    startHandler();
}

public void stopHandler() {
    handler.removeCallbacks(r);
}


public void startHandler() {
    handler.postDelayed(r, 3000);
}


public void cleargrid() {

    for (int i = 0; i <= 8; i++) {
        if (Buttons[i].getResources() != null) {
            Buttons[i].setBackgroundResource(0);
            Buttons[i].setEnabled(true);
        }
        randomize(myImageArr, myImageArr.length);
    }
}


@Override
public void onClick(View v) {

    startHandler();

    switch (v.getId()) {

        case R.id.one:
            count++;
            Buttons[0].setEnabled(false);
            if (count > 2) flipback();
            Buttons[0].setBackgroundResource(myImageArr[0]);
            MediaPlayer.create(this, R.raw.buttonsound2).start();
            break;

        case R.id.two:
            count++;
            Buttons[1].setEnabled(false);
            if (count > 2) flipback();
            Buttons[1].setBackgroundResource(myImageArr[1]);
            MediaPlayer.create(this, R.raw.buttonsound2).start();
            break;

        case R.id.three:
            count++;
            Buttons[2].setEnabled(false);
            if (count > 2) flipback();
            Buttons[2].setBackgroundResource(myImageArr[2]);
            MediaPlayer.create(this, R.raw.buttonsound2).start();
            break;

        case R.id.four:
            count++;
            Buttons[3].setEnabled(false);
            if (count > 2) flipback();
            Buttons[3].setBackgroundResource(myImageArr[3]);
            MediaPlayer.create(this, R.raw.buttonsound2).start();
            break;

        case R.id.five:
            count++;
            Buttons[4].setEnabled(false);
            if (count > 2) flipback();
            Buttons[4].setBackgroundResource(myImageArr[4]);
            MediaPlayer.create(this, R.raw.buttonsound2).start();
            break;

        case R.id.six:
            count++;
            Buttons[5].setEnabled(false);
            if (count > 2) flipback();
            Buttons[5].setBackgroundResource(myImageArr[5]);
            MediaPlayer.create(this, R.raw.buttonsound2).start();
            break;

        case R.id.seven:
            count++;
            Buttons[6].setEnabled(false);
            if (count > 2) flipback();
            Buttons[6].setBackgroundResource(myImageArr[6]);
            MediaPlayer.create(this, R.raw.buttonsound2).start();
            break;

        case R.id.eight:
            count++;
            Buttons[7].setEnabled(false);
            if (count > 2) flipback();
            Buttons[7].setBackgroundResource(myImageArr[7]);
            MediaPlayer.create(this, R.raw.buttonsound2).start();
            break;

        case R.id.nine:
            break;

        case R.id.newgamebutton2:
            cleargrid();
            MediaPlayer.create(this, R.raw.buttonsound).start();
            break;

    }

}

static void randomize(int arr[], int n) {
    Random r = new Random();

    for (int i = n - 2; i > 1; i--) {
        int j = r.nextInt(i);

        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

public void flipbacktimer() {
    for (int i = 0; i <= myImageArr.length; i++) {
        if (Buttons[i].getResources() != null) {
            Buttons[i].setBackgroundResource(0);
            Buttons[i].setEnabled(true);
        }
    }
}

public void flipback() {

    for (int i = 0; i <= myImageArr.length; i++){
        if(Buttons[i].getResources() != null) {
            Buttons[i].setBackgroundResource(0);
            Buttons[i].setEnabled(true);
            count = 1;
        }
    }
}
}

2 个答案:

答案 0 :(得分:0)

即使它们的名称不同,您也可以保留图像,甚至可以创建类来保存它们:

            public class GameImages {

                int image;
                boolean isMatched;

                public GameImages(int image) {

                    this.image = image;
                    this.isMatched = false;

        }

                public GameImages(int image, int id, isMatched) {

                    this.image = image;
                    this.isMatched = isMatched;

        }

现在创建GameImage的数组。您将使用id将图像与按钮进行匹配,并检查isMatched()是否保留,并将按钮的clickable属性设置为“ false”,或者将其翻转回去。

答案 1 :(得分:0)

使用图像和id ex {{image“:您的图像,” id“:图像的ID}创建对象 放置两张具有相同ID的图像,当它们匹配时,您就可以开始了