프로그램언어/android & iPhone

[ 안드로이드 ] 간단한 게임 만들어보자

에블릿 2011. 6. 10. 23:34
안드로이드 게임 개발 책을 보면서 블로깅을 통해 정리를 하고자 합니다. 처음으로 안드로이드를 공부하는 입장에서는 새로운 모든것들이 

너무나도 재미가 있습니다. ㅋㅋ 이러다가 히트 상품 만들지 모르겠네요.  

간단하 예제를 책에 있는데로 따라하면서 책 내용도 좋구 간단하게 만들 수 있도록 유도하고 있다는 생각이 들었습니다.

저의 경우에는 android sdk에서 사용하고 있는 에뮬레이터를 사용하다가 속터져서 도저히 할 수 없다는 판단에서

혹시 빠르게 결과를 보면서 할 수 있는게 없나 찾던중에 갤럭시 s와 연결을 해서 할 수 있다는 사실을 알았습니다.

제 헨드폰이 갤럭이기 때문에 다행이다라는 생각을 하였습니다.

일단 프로젝트 생성을 하겠습니다.

 

빨간색 박스를 중점으로 봐주시기 바랍니다. 저의 경우 아까 언급 했듯이 갤럭s라고 이야기 했습니다. 갤럭s의 경우 현재 버전 2.3.3 을

사용하고 있기에 버전을 선태하였습니다. 


 
main.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:id="@+id/TextView01"  

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text="500-1,000 사이의 값을 입력하세요"

    android:textSize="20sp"

    />

<EditText  

android:id="@+id/EditText1"

android:layout_height="wrap_content"

android:layout_width="wrap_content" 

android:textSize="20sp"

android:width="120dip"

/>

<Button 

android:text="Button" 

android:id="@+id/Button01" 

android:layout_width="wrap_content" 

android:layout_height="wrap_content"

/>

<TextView 

android:text="확인 결과 : " 

android:id="@+id/textView02" 

android:layout_width="wrap_content" 

android:layout_height="wrap_content"

android:textSize="20sp"

/>

</LinearLayout>

 

소스을 수정하기 위해서 FirstGame.java 을 클릭합니다.


 소스코드 

package com.game.StartGame;


//import android.app.Activity;

//import android.os.Bundle;

import android.app.*;

import android.os.*;

import android.view.*;

import android.widget.*;


import java.util.*;


public class FirstGame extends Activity {

    /** Called when the activity is first created. */

int Counter; // 사용자가 입력환 횟수

int n; // 난수 발생용

EditText edit; // 사용자가 입력하는 컨트롤

TextView tResult; // 처리 결과를 입력할 컨트롤

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        

        Counter = 0;

        n = new Random().nextInt(501) + 500;

        edit = (EditText)findViewById(R.id.EditText1);

        

        tResult = (TextView)findViewById( R.id.textView02);

        

        findViewById(R.id.Button01).setOnClickListener(myButtonClick);

    

    }

    

    Button.OnClickListener myButtonClick = new Button.OnClickListener() {

    public void onClick(View v) {

    String s;

    Counter++;

   

    int p = Integer.parseInt(edit.getText().toString());

    if( p < 500 || p > 1000 ) {

    s = "입력한 값이 500-1000 을 벗어났습니다.";

    } else if( p == n)

    s = Counter + "번째에 맞추셨습니다.";

    else if( p > n )

    s = p + "보다 작은 값입니다.";

    else

    s = p + "보다 큰 값입니다.";

    tResult.setText( s );

    }

    };

}