IT/MSSQL

[Android] 간단한 클릭커 게임 만들기 -1

유자애플 2020. 3. 18. 14:13
반응형

오늘은 안드로이드를 통한 간단한 클릭커 게임을 만들겠습니다.

몇개까지 진행할지 모르겠지만

조금씩 진행해서 만들어 보겠습니다.

 

 

activity_main.xml

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

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

    tools:ignore="MissingDefaultResource">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="top|center"
    android:orientation="vertical"
    >

<LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:background="@color/colorBeige">

             <TextView
              android:layout_width="200dp"
              android:layout_height="50dp"
              android:id="@+id/txtUp"/>

             <TextView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:id="@+id/txt1"
              android:gravity="right"
              />

      </LinearLayout

          <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:layout_gravity="top">

       </LinearLayout>
</LinearLayout>
    
        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:orientation="horizontal"
        android:gravity="center">

            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn1"
            android:text="코인증가"
            />

   </LinearLayout>
          

      <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorAccent">
           

            <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

                <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                   

                   <ImageView
                    android:layout_width="100dp"
                    android:layout_height="50dp"/>
                   

                    <TextView
                    android:layout_width="200dp"
                    android:layout_height="50dp"
                    android:id="@+id/txtCoin"/>
           

                  <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/btn2"
                    android:text="레벨업"
                    android:layout_gravity="center|top"/>
             </LinearLayout>

       </ScrollView>

   </RelativeLayout>

</LinearLayout>

 

XML 코드입니다. 

아직 완성본이 아닌 토대 부분이라 아직 구현 못한 부분도 있지만

버튼 클릭으로 증가, 그리고 레벨업 버튼으로 비용 증가를 구현하기 위한 

버튼 2개와 출력을 위한 텍스트 뷰가 존재합니다.

 

 

 

MainActivity.Java

import androidx.appcompat.app.AppCompatActivity; 

import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends AppCompatActivity { 
    double a  = 0; 
    double b  = 1; 
    double c  = 10; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 

       // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

        setContentView(R.layout.activity_main); 

        final TextView txt1  = (TextView) findViewById(R.id.txt1); 
        final TextView txtConin  = (TextView) findViewById(R.id.txtCoin); 
        final TextView txtUp  = (TextView) findViewById(R.id.txtUp); 
        Button btn1 = (Button) findViewById(R.id.btn1); 
        Button btn2 = (Button) findViewById(R.id.btn2); 

        String cs = String.format("%.2f",c); 
        String bs = String.format("%.2f",b); 

        txt1.setText("금액 : " + a); 
        txtConin.setText("비용 : " +cs); 
        txtUp.setText("클릭당 금액 : " +bs); 



        View.OnClickListener listener = new View.OnClickListener() { 
            @Override 
            public void onClick(View v) { 
                a = a + b; 

                String sa = String.format("%.2f",a); 

                txt1.setText("금액 : " + sa); 
            } 
        }; 

        View.OnClickListener listener2 = new View.OnClickListener() { 
            @Override 
            public void onClick(View v) { 
                if(a >= c) { 

                    b = b * 1.3; 
                    a = a - c; 

                    String bs = String.format("%.2f", b); 

                    txtUp.setText("클릭당 금액 : " + bs); 
                    String sa = String.format("%.2f",a); 

                    txt1.setText("금액 : " + sa); 
                    c = c * 1.3; 
                    String cs = String.format("%.2f", c); 
                    txtConin.setText("비용 : " + cs); 
                } 
                else{ 
                    Toast.makeText(getApplicationContext(),"비용이 부족합니다.",Toast.LENGTH_LONG).show(); 
                } 
            } 
        }; 

        btn1.setOnClickListener(listener); 
        btn2.setOnClickListener(listener2); 
    } 

 

그리고 XML에 연동해줄 JAVA 코드입니다.

아직 공부하는 단계라 불필요한 부분이 많고

지저분한 부분도 존재합니다.

 

프래그먼트를 통한 화면 전환 부분과 스크롤뷰를 사용하기 했지만 구현을 하지 않은 부분이 존재합니다.

그점 생각하시면서 보시면 될거 같습니다.

 

갑자기 안드로이드를 하게 되어서 공부도 할겸 그리고 숙달시킬겸 제작하는 것입니다.

반응형