1.  다음고 같은 권총 그림파일 세개를 만들어 리소트 drawable에 추가한다. 

 

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="20dp"

    tools:context=".MainActivity">

 

    <ImageView

        android:id="@+id/imageView"

        android:scaleType="centerInside"

        android:layout_width="300dp"

        android:layout_height="300dp"

        android:src="@drawable/gun"

        android:layout_centerInParent="true"

        />

 

    <Button

        android:id="@+id/button"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"

        android:text="ROLL"

        />

 

    <TextView

        android:id="@+id/textView"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentBottom="true"

        android:layout_centerHorizontal="true"

        android:gravity="center"

        android:text="Click on the image to shoot!"

        />

 

</RelativeLayout>

 

ㅁㅁㅁ

 

3. 액티비티 자바 코드 

 

package com.jwlee.russianroulettegame;

 

import androidx.appcompat.app.AppCompatActivity;

 

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.TextView;

 

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

 

public class MainActivity extends AppCompatActivity {

 

    ImageView imageView;

    Button button;

    TextView textView;

 

    List<String> bullets;

    boolean shuffled = true;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        imageView = findViewById(R.id.imageView);

        button = findViewById(R.id.button);

        textView = findViewById(R.id.textView);



        bullets = new ArrayList<>();



        bullets.add("NO");

        bullets.add("YES");

 

        //roll the barrel

        Collections.shuffle(bullets);

 

        button.setOnClickListener(new View.OnClickListener(){

            @Override

            public void onClick(View v) {

                Collections.shuffle(bullets);

                imageView.setImageResource(R.drawable.gun);

                shuffled = true;

                textView.setText("Click on the image to shoot!");

 

            }

        });

 

        imageView.setOnClickListener(new View.OnClickListener(){

            @Override

            public void onClick(View v) {

                if(shuffled) {

                    String currentBullet = bullets.get(0);

                    if(currentBullet.equals("YES")){

                        imageView.setImageResource(R.drawable.bang);

                        textView.setText("You are daed!");

                    }else {

                        imageView.setImageResource(R.drawable.click);

                        textView.setText("You are alive! Pass the gun to the next player!");

                    }

                    shuffled = false;

                }else {

                    textView.setText("First Roll the barrel!");

                }




            }

        });

    }

}

 

4. 동영상보고 따라하기 

 

 

안드로이드 랜덤카드 발생하기, android random card generator

 

 

1. 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"

    tools:context=".MainActivity">

 

   <Button

       android:id="@+id/button"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_centerInParent="true"

       android:text="GET A CARD"

       />

 

    <TextView

        android:id="@+id/textView"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_above="@+id/button"

        android:layout_centerHorizontal="true"

 

        android:layout_marginBottom="40dp"

        android:text=""

        android:textSize="30sp"

        />



</RelativeLayout>

 

 

2. 액티비티 자바 코드 

package com.jwlee.randomcardgenerator;

 

import androidx.appcompat.app.AppCompatActivity;

 

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

 

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

 

public class MainActivity extends AppCompatActivity {

 

    Button button;

    TextView textView;

 

    List<String> cardValues, cardsColors;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        button = findViewById(R.id.button);

        textView = findViewById(R.id.textView);

 

        cardValues = new ArrayList<>();

        cardsColors = new ArrayList<>();

 

        cardValues.add("2");

        cardValues.add("3");

        cardValues.add("4");

        cardValues.add("5");

        cardValues.add("6");

        cardValues.add("7");

        cardValues.add("8");

        cardValues.add("9");

        cardValues.add("10");

        cardValues.add("J");

        cardValues.add("Q");

        cardValues.add("K");

        cardValues.add("A");

 

        cardsColors.add("clubs");

        cardsColors.add("diamonds");

        cardsColors.add("heart");

        cardsColors.add("spades");




        button.setOnClickListener(new View.OnClickListener(){

            @Override

            public void onClick(View v) {

 

                Random r = new Random();

 

                String randomValue = cardValues.get(r.nextInt(cardValues.size()));

                String randomColor = cardsColors.get(r.nextInt(cardsColors.size()));

 

                textView.setText(randomValue + " of " + randomColor);

 

            }

        });

 

    }

}

 

안드로이드 패스트 탭핑게임 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);

 

            }

        };

    }

}

 

 

안드로이드 칼라매치게임 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