총 게시물 1,104건, 최근 0 건 안내 RSS 글쓰기
이전글  다음글  목록 글쓰기
[소스코드]

미로 게임 소스

글쓴이 : 카이로 날짜 : 2013-03-01 (금) 08:47 조회 : 6101
글주소 : http://www.androidside.com/B56/6814
MazeGame.zip (60.2K), Down : 41, 2013-03-01 09:05:45
 
  웹툰 볼땐 투믹스
죽기전에 봐야할 미로게임, 요일 장르별 인기무료, 영화같이 기다려지는 웹툰확인!
www.toomics.com
  미로게임 텐바이텐
감성디자인 텐바이텐이 추천하는 HOT상품 365일 특가 SALE, 지금 만나요
10x10.co.kr
  미로게임 쇼핑은 티몬
미로게임, 합리적인 가격, 특가 SALE 알뜰구매 찬스! 초저가, 빠른배송!
www.ticketmonster.co.kr
신청하기


This is the first of many upcoming game programming tutorials on jforeach. In this tutorial, we’ll create a simple android game.

A basic familiarity with Android platform is assumed while writing this tutorial. If you are not familiar with basics of android or Java itself, then please go through the basic tutorials section on jforeach before reading this tutorial.

The game is a simple maze game which we’ve named MazeMan. Here’s the preview of the game,

gameplay1.gif


There is nothing fancy about the game. We’ve tried to keep it as simple as possible.

Understanding the application structure

These are the files in our project. We’ll quickly look at the role of each file:

directory_structure.png


There are five java files i.e. five classes. Menu class is an Activity which is opened when the application is launched. It creates two buttons to start a new game and exit the application.Game class is another activity which is launched when new game button is pressed.GameView is a custom View class which we use to draw the maze and tracks the ball’s movement in the maze. The Maze class is sort of a POJO class which stores information about the maze and the position of the ball. The MazeCreator is sort of our maze factory, we’ve hard coded three mazes into this class.

In the res (resources) we have 3 layout xml files and two xml files to store values. main.xmlfile contains the layout of the main menu which contains the new and exit buttons. Since our game has three mazes or levels, pressing the new game button opens a small dialog to choose the level, levels.xml contains the layout for that dialog. finish.xml contains the layout for the small dialog that appears when the game is finished. colors.xml contains the color codes for the ball, maze walls and maze background. strings.xml contains the string values for different buttons and labels in the application. Then there is the AndroidManifest.xml which is a core file in Android projects which you must be already familiar with.

Getting Started With The Menus and Dialogs

First lets create an Activity which will create two simple “New Game” and “Exit” buttons. This is the main activity which we’ve named Menu. We’ll also handle the click of the buttons in this class, so we’ll also implement OnClickListener in this class and override the onClick(Viewview) method. This is the declaration of this activity in the AndroidManifest.xml file

1
2
3
4
5
6
7
<activity android:name=".Menu"
         android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

In the intent-filter we’ve defined this Activity to be launched when we click on the application from the application list. We’re also using a string app_name in the manifest file. So we’ll add this String along with a couple of other Strings to the strings.xml file.

1
2
3
<string name="app_name">MazeMan</string>
<string name="btn_newgame">New Game</string>
<string name="btn_exit">Exit</string>

The layout for this activity is fairly simple. It uses a LinearLayout with two Button in it. The layout is saved as main.xml. This is the code of the layout,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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"
    android:gravity="center_vertical">
    <Button android:id="@+id/bNew"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="5px"
            android:text="@string/btn_newgame" />
    <Button android:id="@+id/bExit"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="5px"
            android:text="@string/btn_exit" />
</LinearLayout>

We’ve assigned ids to both the buttons which we’ll use to register events on these buttons. We’ll move to the activity now. This is the declaration of our Menu activity

1
public class Menu extends Activity implements OnClickListener

The Menu class extends the Activity class and implements OnClickListener interface. In the onCreate method of this activity, we’ll set main as the layout and add click listeners to the New Game and Exit buttons.

1
2
3
4
5
6
7
8
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button newGame = (Button)findViewById(R.id.bNew);
    Button exit = (Button)findViewById(R.id.bExit);
    newGame.setOnClickListener(this);
    exit.setOnClickListener(this);
}

As you can see, we set the activity instance itself, using this reference, as the click listeners for the two buttons (We could’ve used anonymous inner classes here as well but we chose not to). Here is the code that will handle the button clicks,

1
2
3
4
5
6
7
8
public void onClick(View view) {
    //check which button was clicked with its id
    switch(view.getId()) {
        case R.id.bExit:
            finish();
            break;
    }
}

When the Exit button is clicked, we’ll finish the Menu activity which will effectively close the application. For the New Game button, we’ll present the user with a list so that the user can select which stage he/she wants to play. Right now our game has only three levels so we’ll present the user with a dialog which has three options for the three levels. Lets see the code of the onClick method again with the dialog implemented.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void onClick(View view) {
    //check which button was clicked with its id
    switch(view.getId()) {
        case R.id.bExit:
            finish();
            break;
        case R.id.bNew:
            final String[] levels = {"1""2""3"}



종합평점 (참여 2명)
로그인 창이 있는 "정보 수정"에서
서명 이쁘게 등록해보세요 ^^

추천 버튼을 클릭하면 추천한 자와 추천받은 자 모두에게 포인트가 지급됩니다.

마장리 2015-03-23 (월) 15:40
감사합니다
댓글주소
시원한물 2015-05-14 (목) 12:08
감사합니당당
댓글주소
Kibi 2015-09-09 (수) 09:00
감사합니다!
댓글주소
netcon 2015-11-06 (금) 21:54
감사합니다

댓글주소
junho0706 2015-11-30 (월) 19:36
감사합니다
댓글주소
갤럭쉬S 2016-05-19 (목) 19:08
감사합니다.
댓글주소
jjeng 2016-05-23 (월) 15:34
감사합니다
댓글주소
최강을 2016-05-26 (목) 23:07
감사합니다.
댓글주소
핫도그군 2016-08-03 (수) 18:34
ㅎㅎㅎ
댓글주소
갓진수 2016-10-30 (일) 01:27
잘 보겠습니다
댓글주소
hsw728 2016-12-28 (수) 14:59
감사합니다
댓글주소
Argon 2017-12-04 (월) 19:54
감사합니다
댓글주소
popo 2018-06-04 (월) 10:59
감자
댓글주소
꼴떡 2018-08-07 (화) 17:02
감사합니다^^
댓글주소
뺑덕스 2018-09-13 (목) 13:35
감사합니다.

댓글주소
처음  1  2
이전글  다음글  목록 글쓰기

총 게시물 1,104건, 최근 0 건 안내 RSS
번호 분류 제목 글쓴이 날짜 추천 조회
1104 강좌  100 Days Google Dev, 3: Google Play Services 7.5 StanleyKou 01-24 1 879
1103 강좌  100 Days Google Dev, 2: The Magic of LRU Cache StanleyKou 01-20 0 846
1102 강좌  100 Days Google Dev, 1: Android Design Support Library 2 StanleyKou 01-18 0 980
1101 TIP&TECH  리스트뷰에서 스위치,체크박스사용시 5 에찌 05-08 5 2509
1100 TIP&TECH  뷰(RecyclerView/...)에 Max Height 주기/[Android] 김병희라고요 04-26 0 2215
1099 TIP&TECH  TabLayout ClickEvent 에찌 04-21 1 2000
1098 TIP&TECH  현재 사용하고 있는 어플 foreground인지 backgroun… 4 에찌 04-14 2 3005
1097 TIP&TECH  안드로이드 Drawble 에 해상도 마다 이미지를 … 1 에찌 04-14 1 2021
1096 TIP&TECH  fragment 에서 ButterKnife 사용 시 resource가져올 때… 1 에찌 04-12 0 1615
1095 TIP&TECH  Youtube 및 facebook 동영상 업로드 할때 1 희망과꿈을 03-28 0 2084
1094 TIP&TECH  google service lib 가져올 때 3 에찌 03-20 3 2098
1093 소스코드  CentralRV 프로젝트 - RecyclerView 가로/세로 가운… 1 김병희라고요 01-30 2 3698
1092 TIP&TECH  번개검색 시스템/[Android] 2 김병희라고요 12-29 2 2625
1091 TIP&TECH  RecyclerView 특정 포지션으로 스크롤 - smoothScroll… 1 김병희라고요 12-12 1 2657
1090 강좌  왕앱따 107. 자바/C++ 파일읽기 속도 비교/[Androi 김병희라고요 11-26 0 2739
1089 강좌  왕앱따 106. C++ 동영상 강좌/[Android] 김병희라고요 11-23 0 3245
1088 소스코드  AOSP용 한글 키보드 입력키 엘카이저 11-17 1 3075
1087 강좌  addDrawerListener, setDrawerListener(deprecated)/[Android] 김병희라고요 11-02 2 3291
1086 강좌  왕앱따 103. 아이콘 반복클릭 테스트/[Android] 김병희라고요 11-01 0 2488
1085 강좌  왕앱따 100. 안드로이드 왕초보의 앱개발 따라… 김병희라고요 10-29 0 3036
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  다음  맨끝
 

채팅 권한: 글쓰기 1개
2레벨 이상만 대화 가능
공개 채팅: 평일 !(9시 ~ 17시),토,일
안사2 변경사항 보러가기 챗방이 잘 안보이면 크롬에서 접속해주세요
챗방 숨기기 |  챗방 보이기