본문 바로가기

프로그램언어/android & iPhone

[ 안드로이드 ] 안드로이드 간단한 게임 개발 2편



간단한 예제를 만들어 보던중 로또 프로그램을 만들던중 이게 좀 더 쉬운걸 해본후 로또 발생기를 이용하여 가위 바위보 / 주사위 게임 / 윷놀이 /

로또 발생기등 게임을 만든 후, 그래픽을 입히는 순서를 정의해서 해보도록 하겠습니다.

지금의 버전은 단순 가위바위 보 게임입니다.


 
프로젝트 생성 저의 경우 갤럭s에서 테스트를 하기 때문에 현재 버전에 맞쳐서 설정하였습니다. 버전이 높을 경우에는 인식이 안되는 

문제가 있었습니다. 본인이 테스트하고자 하는 버전을 선택하시는것이 가장 올바른 자세라 말씀드립니다.

 
 프로젝트를 생성한 후,  다음과 같이 [ res -> layout -> main.xml ] 을 수정하시면 됩니다. 

아래 프로젝트를 설정하시면 됩니다. 

 
간단한 xml 소스 입니다.

 

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

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

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<TextView  

    android:text="이기는 자는 왕! 지는자는 하인!"

    android:textSize="20sp"

    android:layout_width="fill_parent" 

    android:layout_height="120dip"

    android:gravity="center" 

    />

    <LinearLayout

android:orientation="horizontal"

android:layout_height="wrap_content"

android:layout_width="fill_parent">

<Button

android:id="@+id/Button01"

android:text="가위"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="18sp"

android:layout_marginLeft="10dip"

android:layout_marginRight="5dip"

android:tag="1"

android:layout_weight="1"> 

</Button>

<Button

android:id="@+id/Button02"

android:text="바위"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="18sp"

android:layout_marginLeft="5dip"

android:layout_marginRight="5dip"

android:tag="2"

android:layout_weight="1"/> 

<Button

android:id="@+id/Button03"

android:text="보"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="18sp"

android:layout_marginLeft="5dip"

android:layout_marginRight="10dip"

android:tag="3"

android:layout_weight="1"/>  

    </LinearLayout>

    <TextView

    android:id="@+id/TextView01"

    android:text="결과 : "

    android:textSize="18sp"

    android:layout_width="fill_parent"

    android:gravity="center"

    android:layout_height="100dip" android:drawingCacheQuality="high"/>


</LinearLayout>



layout을 수정하였다면 이제는 소스를 수정하셔야 합니다.

다음은 소스를 수정해야 합니다. 위치는 다음과 같습니다.

 
소스를 다음과 같이 수정하시면 됩니다. 안드로이드 프로그래밍 게임을 하시려는 분들은 대부분 간단한 프로그램을 하신다는 전제로

 
전체 소스는 아래와 같습니다.


가져다가 실행하시면 됩니다. 
package com.game.gawibawibo;

//import android.app.Activity;
//import android.os.Bundle;

import java.util.*;

import android.R.integer;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;

public class gawibawibo extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // 버튼의 리스트 할당.
        findViewById(R.id.Button01).setOnClickListener(myButtonClick);
        findViewById(R.id.Button02).setOnClickListener(myButtonClick);
        findViewById(R.id.Button03).setOnClickListener(myButtonClick);
    }
    
    public int dicision( int n, int r ) {
    	int result = 0;
    	if (n == r) result = 0;								// 비김
    	else if (n - r == 1 || n - r == -2) result = 1;     // 이김.
    	else result = 2;									// 짐.
    	return result;
    }
    
    Button.OnClickListener myButtonClick = new Button.OnClickListener() {
    	public void onClick(View v) {
    		int n = 0;
    		String s = null;
    		int r = new Random().nextInt( 3 ) + 1;
    		
    		switch( v.getId())
    		{
    		case R.id.Button01:
    			n = 1;				// 가위
    			break;
    		case R.id.Button02:
    			n = 2;				// 바위
    			break;
    		case R.id.Button03:
    			n = 3;				// 보
    			break;
    		}
    		
    		int result = dicision(n, r);
    		
    		switch( result )
    		{
    		case 0:
    			s = "비겼습니다.";
    			break;
    		case 1:
    			s = "이겼습니다.";
    			break;
    		case 2:
    			s = "안타깝습니다!!!!!";
    			break;
    		default:
    			s = "default";
    			break;
    			
    		}

    		((TextView)findViewById(R.id.TextView01)).setText(s);
    	}
    };
    
}