안드로이드 패스트 탭핑게임 Android Fast Tapping Game

 

1. 리소스 drawable에 다음과 같은 그림 파일을 삽입한다. 이미지 이름은 tag.png 

저 빨간 대쉬 네모칸을 빠르게 탭핑(손가락으로 빠르게 계속 터치)하는 것이다. 

 

2. XML 코딩한다. 

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:padding="10dp"

    tools:context=".MainActivity">

 

    <TextView

        android:id="@+id/tv_result"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"

        android:text="Start Tapping"

        />

 

    <TextView

        android:id="@+id/tv_info"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerInParent="true"

        android:layout_centerHorizontal="true"

        android:text="Start Tapping"

        />

 

    <ImageView

        android:id="@+id/iv_tap"

        android:layout_width="300dp"

        android:layout_height="300dp"

        android:layout_centerInParent="true"

        android:src="@drawable/tap"

        />

 

</RelativeLayout>

 

 

ㅁㅁㅁ

 

3. 자바 액티비티 코딩한다. 

package com.jwlee.fasttappinggame;

 

import androidx.appcompat.app.AppCompatActivity;

 

import android.content.SharedPreferences;

import android.os.Bundle;

import android.os.CountDownTimer;

import android.os.Handler;

import android.view.View;

import android.widget.ImageView;

import android.widget.TextView;

 

public class MainActivity extends AppCompatActivity {

 

    ImageView iv_tap;

    TextView tv_result, tv_info;

 

    int currentTaps = 0;

    boolean gameStarted = false;

 

    CountDownTimer timer;

 

    int bestResult = 0;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        iv_tap = findViewById(R.id.iv_tap);

        tv_result = findViewById(R.id.tv_result);

        tv_info = findViewById(R.id.tv_info);

 

        final SharedPreferences preferences = getSharedPreferences("PREES",0);

        bestResult = preferences.getInt("High Score",0);

 

        tv_result.setText("Best Result: "+bestResult);

 

        iv_tap.setOnClickListener(new View.OnClickListener(){

            @Override

            public void onClick(View v) {

                if(gameStarted){

                    currentTaps++;

                }else {

                    tv_info.setText("Tap, tap, tap !!!");

                    gameStarted = true;

                    timer.start();

                }

            }

        });

 

        //timer for 10 sec with interval 1 sec

        timer = new CountDownTimer(10000,1000) {

            @Override

            public void onTick(long millisUntilFinished) {

                long timeTillEnd = (millisUntilFinished / 1000) +1;

                tv_result.setText("Time Remaining: "+timeTillEnd);

 

            }

 

            @Override

            public void onFinish() {

                iv_tap.setEnabled(false);

                gameStarted = false;

                tv_info.setText("Game Over!");

 

                if(currentTaps > bestResult) {

                    bestResult = currentTaps;

 

                    SharedPreferences preferences1 = getSharedPreferences("PREES",0);

                    SharedPreferences.Editor editor = preferences1.edit();

                    editor.putInt("High Score", bestResult);

                    editor.apply();

                }

 

                tv_result.setText("Best Result: "+bestResult + "\nCurrent Taps: "+currentTaps);

 

                new Handler().postDelayed(new Runnable() {

                    @Override

                    public void run() {

                        iv_tap.setEnabled(true);

                        tv_info.setText("Start Tapping");

                        currentTaps = 0;

                    }

                },2000);

 

            }

        };

    }

}

 

 

+ Recent posts