首页 文章 精选 留言 我的

精选列表

搜索[数据库设计],共10000篇文章
优秀的个人博客,低调大师

Android游戏开发 - NancyGLines设计

今天把之前用Python实现的NancyGLines游戏迁移到了Android中,虽然现在还只是算个毛坯版,界面比较丑陋,功能也不够完善,但是整个框架已经建立好,并且,游戏的基本功能已经实现了。见下图: 游戏规则: 1. 触摸某个球,然后选择一个需要移动到的没有球的地方。 2. 球移动过去后,如果满足横,竖,斜同颜色的球大于等于5个,则消去这些同颜色的球得分。 3. 如果没有消去,则会落下三个新的球。 4. 直到棋盘没有位置容下新的球,游戏结束。 下面是layout: Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> <? xmlversion="1.0"encoding="utf-8" ?> < FrameLayout xmlns:android ="http://schemas.android.com/apk/res/android" android:layout_width ="fill_parent" android:layout_height ="fill_parent" > < com.coderzh.nancyglines.GLinesView android:id ="@+id/glines" android:layout_width ="fill_parent" android:layout_height ="fill_parent" /> < RelativeLayout android:layout_width ="fill_parent" android:layout_height ="fill_parent" > < TextView android:id ="@+id/text" android:text ="@string/app_name" android:visibility ="visible" android:layout_width ="wrap_content" android:layout_height ="wrap_content" android:layout_centerInParent ="true" android:gravity ="center_horizontal" android:textColor ="#88ffffff" android:textSize ="24sp" /> </ RelativeLayout > </ FrameLayout > 嗯,我使用了自定义的View - GLinesView,在GLinesView的原型是这样的: Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> public class GLinesView extends SurfaceView implements SurfaceHolder.Callback{ } 在这里继承了SurfaceView ,因为SurfaceView 在游戏制作上有一些优势。接着,我参考了Sample里的LunarLander代码,在建立了一个SurfaceView内部线程类,用来处理游戏的逻辑和绘制游戏画面。 Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> public class GLinesView extends SurfaceView implements SurfaceHolder.Callback{ class GLinesThread extends Thread{ public void initGame(){ } public void setRunning( boolean running){ mRun = running; } @Override public void run(){ updateCanvas(); } } public GLinesView(Contextcontext,AttributeSetattrs){ super (context,attrs); SurfaceHolderholder = getHolder(); holder.addCallback( this ); thread = new GLinesThread(holder,context, new Handler(){ @Override public void handleMessage(Messagem){ mStatusText.setVisibility(m.getData().getInt( " viz " )); mStatusText.setText(m.getData().getString( " text " )); } }); } @Override public void surfaceCreated(SurfaceHolderholder){ thread.initGame(); thread.setRunning( true ); thread.start(); } } 当surfaceCreated事件发生时,触发游戏开始,initGame()做一些游戏的初始设置,setRunning设置游戏的当前状态,start将线程运行起来。 因为我不需要实时的刷新画面,所以,我没有在线程的run方法中使用一个while循环,而只是调用了一个刷新画面的方法updateCanvas(); 当用户触摸屏幕时,触发GLinesView 的onTouchEvent方法,因此,添加代码: Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> @Override public boolean onTouchEvent(MotionEventevent){ float x = event.getX(); float y = event.getY(); thread.doTouch(x,y); return super .onTouchEvent(event); } 然后,实现GLinesThread的doTouch方法: Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> public void doTouch( float posX, float posY){ // 激活或移动某个球 } 我会使用一个二维数组来保存棋盘上每个格子的状态: Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> private int mStatus[][]; 比如,mStatus[0][1] = Color.BLUE ,表示,第一行第二列的格子放置了一个蓝色的球。 当我需要移动某个球时,首先需要实现最短路径算法,因为如果有其他球的阻碍,是不能移动的。因此,我使用了一个类似的Dijkstra 最短路径算法,实现了球的移动函数: Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> private void moveBall( int currentX, int currentY, int targetX, int targetY){ } 然后,球移动过去后,还需要实现判断是否满足横竖斜大于等于5个的情况,如果满足,则消除那些球。因此,添加clearBalls方法: Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> private boolean clearBalls(Ballball){ } 在没有满足得分条件时,需要落下新的三个球,因此,实现getThreeBalls方法: Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> private void getThreeBalls(){ } 其实到这里,整个框架已经搭建起来了。整个的原理在与通过一些操作修改棋盘状态的mStatus数据结构,操作完成后,调用updateCanvas()刷新屏幕。 实现好上面的方法后,游戏已经可以运行起来了。就是上面截图中看到的效果了。之后我还需要做一些界面美化,加入菜单,关卡的操作。 最后附上: 完整代码:/Files/coderzh/Code/NancyGLines.rar 体验apk文件:/Files/coderzh/Code/NancyGLines.apk.rar 希望大家提宝贵意见,同时,我也会继续完善这个游戏。

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册