안드로이드 칼라매치게임 Android Color Match Game

 

1. 그림파일 준비한다. 바탕은 투명하게

 

2. 그림파일을 drawable에 복사한다. 

 

 

 

3.layout_button 액티비티를 만든다. 

 

4. xml 코드에 ImageView 2개를 넣는다. 

 

5. activity_main.xml에 TextView, ProgressBar, include(layout_button) 를 넣는다. 

 

6. MainActivity.java에 다음과 같이 코딩한다. 

 

package com.jwlee.colormatchgame;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    ImageView iv_button, iv_arrow;
    TextView tv_points;
    ProgressBar progressBar;

    Handler handler;
    Runnable runnable;

    Random r;

    private final static int STATE_BLUE = 1;
    private final static int STATE_RED = 2;
    private final static int STATE_ORANGE = 3;
    private final static int STATE_GREEN = 4;

    int buttonState = STATE_BLUE;
    int arrowState = STATE_BLUE;

    int currentTime = 4000;
    int startTime = 4000;

    int currentPoints = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv_button = findViewById(R.id.iv_button);
        iv_arrow = findViewById(R.id.iv_arrow);
        tv_points = findViewById(R.id.tv_points);
        progressBar = findViewById(R.id.progressBar);

        //set the initial progressbar time to 4 seconds
        progressBar.setMax(startTime);
        progressBar.setProgress(startTime);

        tv_points.setText("Points: "+currentPoints);

        r= new Random();
        arrowState = r.nextInt(4)+1;
        setArrowImage(arrowState);

        iv_button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                // rotate the button with the colors

                setButtonImage(setButtonPosition(buttonState));

            }
        });


       // 메인 게임 루프 game loop
        handler = new Handler();
        runnable = new Runnable() {
            @Override
            public void run() {
                currentTime = currentTime - 100;
                progressBar.setProgress(currentTime);

                // show progress
                if(currentTime > 0) {
                    handler.postDelayed(runnable, 100);

                }else {
                    // check if the colors of the arrow and the button are the same
                    if(buttonState == arrowState) {
                        //increase points and show them
                        currentPoints = currentPoints +1;
                        tv_points.setText("Points: "+currentPoints);

                        //make the speed higher after every turn / if the speed is 1 second make it again 2 seconds

                        startTime = startTime - 100;
                        if(startTime < 1000) {
                            startTime = 2000;
                        }

                        progressBar.setMax(startTime);
                        currentTime = startTime;
                        progressBar.setProgress(currentTime);

                        //genrate new color of the arrow
                        arrowState = r.nextInt(4)+1;
                        setArrowImage(arrowState);

                        handler.postDelayed(runnable, 100);
                    } else {
                        iv_button.setEnabled(false);
                        Toast.makeText(MainActivity.this, "Game OVER",Toast.LENGTH_SHORT).show();

                    }
                }
            }
        };

        //start the game loop
        handler.postDelayed(runnable, 100);
        
    }

    private void setArrowImage(int arrowState) {
        switch (arrowState){
            case STATE_BLUE:
                iv_arrow.setImageResource(R.drawable.blue);
                arrowState = STATE_BLUE;
                break;
            case STATE_RED:
                iv_arrow.setImageResource(R.drawable.red);
                arrowState = STATE_RED;
                break;
            case STATE_ORANGE:
                iv_arrow.setImageResource(R.drawable.orange);
                arrowState = STATE_ORANGE;
                break;
            case STATE_GREEN:
                iv_arrow.setImageResource(R.drawable.green);
                arrowState = STATE_GREEN;
                break;

        }
    }

    private void setRotation(final ImageView image, final int drawable){
        // 90도 회전
        RotateAnimation rotateAnimation = new RotateAnimation(0,90,
                Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        rotateAnimation.setDuration(100);
        rotateAnimation.setInterpolator(new LinearInterpolator());
        rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                image.setImageResource(drawable);

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        image.startAnimation(rotateAnimation);


    }

    private int setButtonPosition(int position) {
        position = position +1;
        if(position == 5) {
            position = 1;
        }
        return position;
    }

    private void setButtonImage(int state) {
        switch (state){
            case STATE_BLUE:
                setRotation(iv_button, R.drawable.color_blue);
                buttonState = STATE_BLUE;
                break;
            case STATE_RED:
                setRotation(iv_button, R.drawable.color_red);
                buttonState = STATE_RED;
                break;
            case STATE_ORANGE:
                setRotation(iv_button, R.drawable.color_orange);
                buttonState = STATE_ORANGE;
                break;
            case STATE_GREEN:
                setRotation(iv_button, R.drawable.color_green);
                buttonState = STATE_GREEN;
                break;

        }
    }

}

+ Recent posts