AndroidSDK 例子剖析

贪吃蛇, 打开了手机游戏的先河。 是NOKIA公司在发展手机游戏上的一次成功尝试。让手机游戏玩家们爱不释手,引起了手机游戏的开发风暴,各个手机厂商纷纷加入这个阵营。手机游戏开发从此拉开了序幕。Android, OPhone OS作为手机开发的后起之秀, 肯定不会错过这份蛋糕的分享。它不仅解决了以住手机游戏的不足(屏幕分辨率小, 内存少)的毛病。而且还在手机游戏软件开发上提供更全, 更新的API ,更大的内存, 而且还可以用JAVA的泛形, 提高他的开发速度。 现在我们就以ANDROID SDK 里面自带的贪吃蛇作为例子, 去看看这个贪吃蛇是怎么去开发。

   开发过手机游戏的人就知道手机开发的三要素: 画布(用来绘画游戏的画面)键盘事件,实时刷新。 我们知道一般的游戏画面都是由地图, 精灵(由游戏的主角,怪物组成), 那我们现在就看看贪吃蛇是怎样他的地图的:

一、实现游戏的界面 

1、  先声明用来存放绘画图像的X,Y轴的位置的数组:

private int[][] mTileGrid;//

/***************

Tileindex 图片的索引

 X轴的位置:

 Y轴的位置:

    **************/

 2、 编写存放图片索引用图片的X,Y轴位置;

public void setTile(int tileindex, int x, int y) {

        mTileGrid[x][y] = tileindex;

}

 

 3、调用以上的方法以循环的方式位置数组赋值以及图片的索引,

private void updateWalls() {

 

        for (int x = 0; x < mXTileCount; x++) {

            setTile(GREEN_STAR, x, 0);//设置顶部的界线的位置

            setTile(GREEN_STAR, x, mYTileCount - 1);// 设置底部界线的

        }

        for (int y = 1; y < mYTileCount - 1; y++) {

            setTile(GREEN_STAR, 0, y);/设置左边的界线的位置

 

            setTile(GREEN_STARmXTileCount - 1, y);/设置右边的界线的位置

 

        }

}

4、重写VIEW 类里面的方法。 把界线画出。

 public void onDraw(Canvas canvas) {

        super.onDraw(canvas);

        for (int x = 0; x < mXTileCount; x += 1) {

            for (int y = 0; y < mYTileCount; y += 1) {

                if (mTileGrid[x][y] > 0) {

                    canvas.drawBitmap(mTileArray[mTileGrid[x][y]],

                           mXOffset + x * mTileSize,

                           mYOffset + y * mTileSize,

                           mPaint);

                }

同上可见: 地图其实就是由图片数组拼直面成的。 面图片又是通过他的图片索引找到,并在mTileGrid[x][y],获取他们的位置索引来确定图片的位置。 这样在一个手机的页面就形成了,简单吧。

苹果的位置就是更简单了,他是随机生成的, 而且必须在现在蛇的位置相对远距离:

看看他的代码:

 private void addRandomApple() {

        Coordinate newCoord = null;

        boolean found = false;

        while (!found) {    //

            // Choose a new location for our apple

// 随机生成新的X,Y位置

            int newX = 1 + RNG.nextInt(mXTileCount - 2);

            int newY = 1 + RNG.nextInt(mYTileCount - 2);

            newCoord = new Coordinate(newX, newY);

 

                     boolean collision = false;

            int snakelength = mSnakeTrail.size();

            for (int index = 0; index < snakelength; index++) {

//      检查一下是存放的位置是否合理流

                if (mSnakeTrail.get(index).equals(newCoord)) {

                    collision = true;

                }

            }

       

            found = !collision;

        }

        if (newCoord == null) {

            Log.e(TAG"Somehow ended up with a null newCoord!");

        }

        mAppleList.add(newCoord);// 添加到新苹果的列表中,

    }

2)  实现键盘事件:

          键盘主要起操作作用, 在任何的手机游戏中键盘都是起重要的用,要本游戏中, 他就是起控制蛇的行走方向: 现在我们分析他的代码:

        if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {

            if (mDirection != NORTH) {

                mNextDirection = SOUTH;

            }

            return (true);

        }

 

        if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {

            if (mDirection != EAST) {

                mNextDirection = WEST;

            }

            return (true);

        }

 

        if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {

            if (mDirection != WEST) {

                mNextDirection = EAST;

            }

            return (true);

        }

以上的代码大家一看就明白, 就是通过判断那个键按下, 然后再给要走的方向(mDirection)赋值。

以下的代码就是通

    switch (mDirection) {

        case EAST: {

            newHead = new Coordinate(head.x + 1, head.y);

            break;

        }

        case WEST: {

            newHead = new Coordinate(head.x - 1, head.y);

            break;

        }

        case NORTH: {

            newHead = new Coordinate(head.x, head.y - 1);

            break;

        }

        case SOUTH: {

            newHead = new Coordinate(head.x, head.y + 1);

            break;

        }

       

从以上的键盘代码我们可以看得出,程序中是通过触发来改变坐标(+1,-1)的方式来改蛇头的方向, 可见坐标在游戏编程中的作用, 这个也是根据手机的屏幕是点阵的方式来显示, 所以坐标就是一个定位器。 在这里大家可能还有一个疑问。 就是就这个蛇什么能够以“7”字形来移动行走, 其实我们稍微仔细观察一下就知道了,在这里面, 他们也是通过坐标的传递来实现的, 只要把头部的坐标点依次赋给下一个点, 后面的每一个点都走过了头部所走过的点,而蛇的头部就是负责去获取坐标,整个蛇的行走起来就很自然和连贯。  坐标的方向变换又是通过判断那个方向按键的按下来改变的, 这样一来, 键盘的作用就发挥出来了。

蛇吃苹果又是怎样去实现?上面我所说到的坐标就起了作用。在蛇所经过的每一个坐标, 他们都要在苹果所在的(ArrayList<Coordinate> mAppleList = new ArrayList<Coordinate>())坐标集里面集依次判断,若是坐标相同,那个这个苹果就被蛇吃了 。

 

 

3)  刷新:

      在J2ME中,刷新都是在canvas中通过调用线程结合repaint()来刷新, 他们使线程不断去循环,去调用canvas, 笔者在android 入门时也曾经想用J2ME的模式用在android 中,结果报异常了, 为什么呢? 很多人认为Dalvik虚拟机是一个Java虚拟机,因为Android的编程语言恰恰就是Java语言。但是这种说法并不准确,因为Dalvik虚拟机并不是按照Java虚拟机的规范来实现的,两者并不兼容;同时还要两个明显的不同: Java虚拟机运行的是Java字节码,而Dalvik虚拟机运行的则是其专有的文件格式DEXDalvik Executable)。所以在以前JAVA 里面能使用的模式, 可能在android 里面用不起来 。在自带的例子里面他是通过消息的机制来刷新的。而在android的消定义比较广泛, 比如,手机的暂停, 启动, 来电话、短信,键盘按下,弹起都是一个消息。总的来说, 事件就是消息;只要继承Handler类就可以对消息进行控制,或者处理, 根据具体情况进行具体处理:

class RefreshHandler extends Handler {

//响应消息。

        public void handleMessage(Message msg) {

            SnakeView.this.update();// 重要页面

            SnakeView.this.invalidate();刷新页面

        }

// 向外提供人工的调用消息的接口,

        public void sleep(long delayMillis) {

            this.removeMessages(0);//注消消息

// 添加消息,

            sendMessageDelayed(obtainMessage(0), delayMillis);

           

        }

刷新就是这样简单的实现了。 所开发出的游戏也是挺简单。

 

       最近我关注到,中国移动推出了OPhone手机,OPhone 手机兼容Android的所有应用,你开发的Android软件和游戏,很容易的就可以移植到OPhone手机上来。目前中国移动用户已经超过6.8亿,中国移动如果在这6.8个亿的市场里,推广OPhone手机,赚钱的机会可想而知。

现在,国内手机上网的用户突破8000万,2007年,中国手机游戏市场运营收入达到15亿元,成为继互联网企业之后又一就业热点,2008年手机网游仍将高速增长。随着3G的发展,到2009年底手机游戏市场规模可以达到16亿元,而以往的手机游戏市场都被一些有经济实力的游戏公司或者SP来运营, 对于我们技术人员只能是望洋兴叹了。Android OPhone OS在开发游戏方面更加简单便捷。 而中国移动推出mmarket手机软件商店 平台,提供了一个全新的模式,未来很有可能代替SP的地位,不管你是个人和还是公司,人人都可以参与的。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
List of Sample Apps The list below provides a summary of the sample applications that are available with the Android SDK. Using the links on this page, you can view the source files of the sample applications in your browser. You can also download the source of these samples into your SDK, then modify and reuse it as you need. For more information, see Getting the Samples. API Demos A variety of small applications that demonstrate an extensive collection of framework topics. Backup and Restore A simple example that illustrates a few different ways for an application to implement support for the Android data backup and restore mechanism. Bluetooth Chat An application for two-way text messaging over Bluetooth. BusinessCard An application that demonstrates how to launch the built-in contact picker from within an activity. This sample also uses reflection to ensure that the correct version of the contacts API is used, depending on which API level the application is running under. Contact Manager An application that demonstrates how to query the system contacts provider using the ContactsContract API, as well as insert contacts into a specific account. Home A home screen replacement application. JetBoy A game that demonstrates the SONiVOX JET interactive music technology, with JetPlayer. Live Wallpaper An application that demonstrates how to create a live wallpaper and bundle it in an application that users can install on their devices. Lunar Lander A classic Lunar Lander game. Multiple Resolutions A sample application that shows how to use resource directory qualifiers to provide different resources for different screen configurations. Note Pad An application for saving notes. Similar (but not identical) to the Notepad tutorial. SampleSyncAdapter Demonstrates how an application can communicate with a cloud-based service and synchronize its data with data stored locally in a content provider. The sample uses two related parts of the Android framework — the account manager and the synchronization manager (through a sync adapter). Searchable Dictionary A sample application that demonstrates Android's search framework, including how to provide search suggestions for Quick Search Box. Snake An implementation of the classic game "Snake." Soft Keyboard An example of writing an input method for a software keyboard. Spinner A simple application that serves as an application-under-test for the SpinnerTest sample application. SpinnerTest An example test application that contains test cases run against the Spinner sample application. To learn more about the application and how to run it, please read the Activity Testing tutorial. TicTacToeLib An example of an Android library project that provides a game-play Activity to any dependent application project. For an example of how an application can use the code and resources in an Android library project, see the TicTacToeMain sample application. TicTacToeMain An example of an Android application that makes use of code and resources provided in an Android library project. Specifically, this application uses code and resources provided in the TicTacToeLib library project. Wiktionary An example of creating interactive widgets for display on the Android home screen. Wiktionary (Simplified) A simple Android home screen widgets example.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值