Java版AVG游戏开发入门[0]——游戏模式转换中的事件交互

Java版AVG游戏开发入门[0]——游戏模式转换中的事件交互

   示例程序下载地址:http://download.csdn.net/source/999273(源码在jar内)

 

 AVG,即Adventure Game,可以直译为[冒险游戏]。但是通常情况下我们说AVG是指[文字冒险游戏],也有人更直白的解释成自己选择路线和结局的电子小说,与硬砍硬杀的RPG或者揉破键盘的ACT不同,AVG多以解谜或文字游戏等脑力攻关推动剧情发展。现在日本流行的ADV,可以看作是AVG英文全称的不同缩写方式,大体上讲,AVG == ADV

 

 由于商业化需要,现代主流的AVG往往是GalGame,也就是少女游戏,或称少女恋爱游戏,但GalGame != AVG,只是下属分支中的一环罢了,AVG包含GalGame,但GalGame并不能完全代表AVG/ADV。另外关于GalGame的详细介绍,在若木民喜《只有神才知道的世界》中演绎的相当生动,有兴趣的可以自己去看看~

 

  《只有神知道的世界》漫画图

 

  就技术角度而言,AVG开发可以算得所有游戏类型中最容易的。一款简单AVG游戏的制作难度甚至在贪食蛇、俄罗斯方块之下。由于实现的简易性,导致AVG的开发重心往往着重于策划及美工,程序员的作用则微乎其微。同时也正因AVG开发的门坎约等于0,所以此类型的同人游戏之多即可堪称世界之冠。另外,AVG开发工具普及的也促进了AVG的量产化。利用工具,即始是小说作者、漫画家等非软件专业出身的人士,往往也能轻易制作出顶级的AVG大作。(顺便一提,目前我所见过最好的AVG制作工具是鬼子的livemaker,采用类似思维导图的方式构造整个游戏,很多轻小说作者乃至网络漫画家用它制作自己作品的宣传游戏。但就技术角度上说,livemaker的开发依旧没什么难度......

 

 由于AVG的大泛滥,通常仅有文字、图片及语音的AVG往往无法满足用户需求(H除外-_-)。我们每每可在AVG游戏类型后发现个+号,比如《樱花大战》是AVG+SLG,《生化危机》是AVG+ACT。所以客观上说,AVG开发仅仅能进行字图的交互是不够的,还要解决多模块组件的协调问题。

 

 Java桌面应用开发中,我们都知道绘图是极为简单的,有ImageGraphics两个对象就可以Paint一个图形,即使图形对象再多,最后它们也必须统一在一个Paint中,所以Java中不存在图像的交互问题。

 

但问题在于,图像的显示可以统一,但是触发图像变化的事件却是很难统一的。比如现在有需求如下,在AVG模式中,触发键盘事件上、下、左、右时为控制画面的前进、后退,切换模式到SLG模式后,设定上、下、左、右是光标移动,那么如果我要在程序中实现,就必须记录当前模式,而后根据不同模式调用事件,再反馈到图形上。如果只有几个事件的区别,我们当然可以很容易用分支来实现;问题是,随着游戏规模的加大,这些分支将成几何倍数增多,单纯的分支判定到最后只能忙于应付,落个费力不讨好。

 

其实在这时,我们大可以使用一些技巧来轻松解决问题。

 

示例如下:

 

 

首先,我们构造一个接口,命名为IControl,继承鼠标及键盘监听,并在其中设定两个抽象方法:

 

    package org.loon.simple.avg; import java.awt.Graphics; import java.awt.event.KeyListener; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; /** * Copyright 2008 - 2009 * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. * * @project loonframework * @author chenpeng * @email:ceponline@yahoo.com.cn * @version 0.1 */ public interface IControl extends MouseListener, MouseMotionListener, KeyListener { public abstract void draw(final Graphics g); public abstract IControl invoke(); }

 

 

  而后,再构造一个接口,命名为IAVG,同样继承鼠标及键盘监听,并在其中设定三个抽象方法,用以操作IControl接口:


    package org.loon.simple.avg; import java.awt.Graphics; import java.awt.event.KeyListener; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; /** * Copyright 2008 - 2009 * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. * * @project loonframework * @author chenpeng * @email:ceponline@yahoo.com.cn * @version 0.1 */ public interface IAVG extends MouseListener, MouseMotionListener, KeyListener { public abstract void draw(final Graphics g); public abstract IControl getControl(); public abstract void setControl(final IControl control); } 


     再后,制作一个显示图像用组件,命名为AVGCanva,继承自Canvas。

 

     package org.loon.simple.avg; import java.awt.Canvas; import java.awt.Graphics; /** * Copyright 2008 - 2009 * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. * * @project loonframework * @author chenpeng * @email:ceponline@yahoo.com.cn * @version 0.1 */ public class AVGCanvas extends Canvas { /** * */ private static final long serialVersionUID = 1982278682597393958L; private boolean start; private IAVG avg; public AVGCanvas(IAVG handler) { this.avg = handler; this.start = false; this.addKeyListener(handler); this.addMouseListener(handler); this.addMouseMotionListener(handler); } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { if (this.start) { this.avg.draw(g); } } public void startPaint() { this.start = true; } public void endPaint() { this.start = false; } }

 

     这段代码中的paint方法中并没有现成的方法,而是调用了IAVG接口的draw。紧接着,我们再设定一个AVGFrame用以加载AVGCanvas。

 

    package org.loon.simple.avg; import java.awt.Color; import java.awt.Dimension; import java.awt.Frame; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; /** * Copyright 2008 - 2009 * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. * * @project loonframework * @author chenpeng * @email:ceponline@yahoo.com.cn * @version 0.1 */ public class AVGFrame extends Frame implements Runnable { /** * */ private static final long serialVersionUID = 198284399945549558L; private IAVG avg; private AVGCanvas canvas; private boolean fps; private String titleName; private Thread mainLoop; public AVGFrame(String titleName, int width, int height) { this(new AVG(), titleName, width, height); } public AVGFrame(IAVG avg, String titleName, int width, int height) { super(titleName); Lib.WIDTH = width; Lib.HEIGHT = height; this.avg = avg; this.titleName = titleName; this.addKeyListener(avg); this.setPreferredSize(new Dimension(width + 5, height + 25)); this.initCanvas(Lib.WIDTH, Lib.HEIGHT); this.pack(); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); this.setResizable(false); this.setLocationRelativeTo(null); this.setVisible(true); } public void run() { gameLoop(); } /** * 开始循环窗体图像 * */ private synchronized void gameLoop() { canvas.startPaint(); long second = 0L; int moveCount = 0; // 循环绘制 for (;;) { long start = System.currentTimeMillis(); this.paintScreen(); long end = System.currentTimeMillis(); long time = end - start; long sleepTime = 20L - time; if (sleepTime < 0L) sleepTime = 0L; try { Thread.sleep(sleepTime); } catch (InterruptedException e) { e.printStackTrace(); } if (this.fps) { moveCount++; second += System.currentTimeMillis() - start; if (second >= 1000L) { this.setTitle(new StringBuilder(titleName).append(" FPS:") .append(moveCount).toString()); moveCount = 0; second = 0L; } } } } /** * 启动游戏循环 * */ public void mainLoop() { this.mainLoop = new Thread(this); this.mainLoop.start(); } /** * 初始化背景帆布 * * @param width * @param height */ private void initCanvas(final int width, final int height) { canvas = new AVGCanvas(avg); canvas.setBackground(Color.black); canvas.setPreferredSize(new Dimension(width, height)); this.add(canvas); } public IAVG getAVG() { return this.avg; } protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); } public synchronized void paintScreen() { canvas.repaint(); } public boolean isShowFPS() { return fps; } public void setShowFPS(boolean fps) { this.fps = fps; } public Thread getMainLoop() { return mainLoop; } public String getTitleName() { return titleName; } }

 

  我们可以看到,在本例鼠标键盘事件及图像绘制完全通过接口方式实现。此时,只要让不同组件统一实现IControl接口,便可以轻松转换事件及图像的绘制。也正是我们都再熟悉不过的MVC模式中,通过Event导致Controller改变ModelView的基本原理。

 

   下一回,我们将具体讲解一个AVG游戏实现的基本流程。

 

  示例代码界面如下图:

 

  初始界面

 

 

 人物对话

 

  问题选择

 

  小游戏切换

 

  不同游戏模式切换

 

   示例程序下载地址:http://download.csdn.net/source/999273(源码在jar内)

 

posted on 2009-02-08 10:06 cping 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/cping1982/archive/2009/02/08/2257949.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/379761.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

FreeRTOS任务创建和删除

任务创建和删除的API函数 xTaskCreate()&#xff1a;使用动态方法创建一个任务xTaskCreateStatic()&#xff1a;使用静态方法创建一个任务xTaskCreateRestricated()&#xff1a;创建一个使用MPU进行限制的任务&#xff0c;相关内存使用动态内存分配vTaskDelete()&#xff1a;删…

Delphi 调试

调试&#xff1a;F9执行F8逐过程单步调试F7逐语句单步调试转载于:https://www.cnblogs.com/JackShao/archive/2012/04/30/2476931.html

1.创建单项链表

# include <stdio.h> # include <malloc.h> # include <stdlib.h>typedef struct Node{int data;//数据域struct Node *pNext;//指针域}NODE, *PNODE; //NODE等价于struct Node //PNOD等价于struct Node * //函数声明PNODE create_list(void); void traverse…

python 日本就业_日本的绘图标志 Python中的图像处理

python 日本就业Read basics of the drawing/image processing in python: Drawing flag of Thailand 阅读python中绘图/图像处理的基础知识&#xff1a; 泰国的绘图标志 The national flag of Japan is a rectangular white banner bearing a crimson-red disc at its center…

[windows phone 7 ]查看已安装程序GUID

首先介绍下wp7RootToolsSDK,这个功能相当强大&#xff0c;适合研究wp7高级功能。 它支持File&#xff0c;Register操作&#xff0c;比之前的COM调用要简单&#xff0c;方便。 功能:查看已安装程序的guid 开发心得: 用的是mozart,rom多&#xff0c;刷机吧&#xff0c;最麻烦的是…

FreeRTOS任务挂起和恢复

任务挂起&#xff1a;暂停某个任务的执行 任务恢复&#xff1a;让暂停的任务继续执行 通过任务挂起和恢复&#xff0c;可以达到让任务停止一段时间后重新运行。 相关API函数&#xff1a; vTaskSuspend void vTaskSuspend( TaskHandle_t xTaskToSuspend );xTaskToSuspend &am…

向oracle存储过程中传参值出现乱码

在页面中加入<meta http-equiv"Content-Type" content"text ml;charsetUTF-8"/>就可以解决这一问题 适用情况&#xff1a; 1.中文 2.特殊符号 转载于:https://www.cnblogs.com/GoalRyan/archive/2009/02/16/1391348.html

Scala程序将多行字符串转换为数组

Scala | 多行字符串到数组 (Scala | Multiline strings to an array) Scala programming language is employed in working with data logs and their manipulation. Data logs are entered into the code as a single string which might contain multiple lines of code and …

SQL 异常处理 Begin try end try begin catch end catch--转

SQL 异常处理 Begin try end try begin catch end catch 总结了一下错误捕捉方法:try catch ,error, raiserror 这是在数据库转换的时候用的的异常处理, Begin TryInsert into SDT.dbo.DYEmpLostTM(LogDate,ProdGroup,ShiftCode,EmployeeNo,MONo,OpNo,OTFlag,LostTypeID,OffStd…

FreeRTOS中断配置与临界段

Cortex-M中断 中断是指计算机运行过程中&#xff0c;出现某些意外情况需主机干预时&#xff0c;机器能自动停止正在运行的程序并转入处理新情况的程序&#xff08;中断服务程序&#xff09;&#xff0c;处理完毕后又返回原被暂停的程序继续运行。Cortex-M内核的MCU提供了一个用…

vector向量容器

一、vector向量容器 简介&#xff1a; Vector向量容器可以简单的理解为一个数组&#xff0c;它的下标也是从0开始的&#xff0c;使用时可以不用确定大小&#xff0c;但是它可以对于元素的插入和删除&#xff0c;可以进行动态调整所占用的内存空间&#xff0c;它里面有很多系统…

netsh(二)

netsh 来自微软的网络管理看家法宝很多时候&#xff0c;我们可能需要在不同的网络中工作&#xff0c;一遍又一遍地重复修改IP地址是一件比较麻烦的事。另外&#xff0c;系统崩溃了&#xff0c;重新配置网卡等相关参数也比较烦人&#xff08;尤其是无线网卡&#xff09;。事实上…

java uuid静态方法_Java UUID getLeastSignificantBits()方法与示例

java uuid静态方法UUID类getLeastSignificantBits()方法 (UUID Class getLeastSignificantBits() method) getLeastSignificantBits() method is available in java.util package. getLeastSignificantBits()方法在java.util包中可用。 getLeastSignificantBits() method is us…

Google C2Dm相关文章

Android C2DM学习——云端推送&#xff1a;http://blog.csdn.net/ichliebephone/article/details/6591071 Android C2DM学习——客户端代码开发&#xff1a;http://blog.csdn.net/ichliebephone/article/details/6626864 Android C2DM学习——服务器端代码开发&#xff1a;http…

FreeRTOS的列表和列表项

列表和列表项 列表 列表是FreeRTOS中的一个数据结构&#xff0c;概念上和链表有点类型&#xff0c;是一个循环双向链表&#xff0c;列表被用来跟踪FreeRTOS中的任务。列表的类型是List_T&#xff0c;具体定义如下&#xff1a; typedef struct xLIST {listFIRST_LIST_INTEGRI…

string基本字符系列容器

二、string基本字符系列容器 简介&#xff1a;C语言只提供了一个char类型来处理字符&#xff0c;而对于字符串&#xff0c;只能通过字符串数组来处理&#xff0c;显得十分不方便。CSTL提供了string基本字符系列容器来处理字符串&#xff0c;可以把string理解为字符串类&#x…

正则表达式(一)

正则表达式概述 1.1什么是正则表达式&#xff1f; 正则表达式(Regular Expression)起源于人类神经系统的早期研究。神经生理学家Warren McCulloch和Walter Pitts研究出一种使用数学方式描述神经网络的方法。1956年&#xff0c;数学家Stephen Kleene发表了一篇标题为“神经…

42.有“舍”才有“得”

大干世界&#xff0c;万种诱惑&#xff0c;什么都想要&#xff0c;会累死你&#xff0c;该放就放&#xff0c;该舍就舍。人必须先有所舍&#xff0c;才能有所得&#xff0c;舍如同种子撒播出去&#xff0c;转了一圈&#xff0c;又带了一大群子子孙孙回来。“舍”永远在“得”的…

Java StringBuilder codePointCount()方法与示例

StringBuilder类codePointCount()方法 (StringBuilder Class codePointCount() method) codePointCount() method is available in java.lang package. codePointCount()方法在java.lang包中可用。 codePointCount() method is used to count the number of Unicode code point…

FreeRTOS时间管理

在使用FreeRTOS的过程中&#xff0c;我们通常会在一个任务函数中使用延时函数对这个任务延时&#xff0c;当执行延时函数的时候就会进行任务切换&#xff0c;并且此任务就会进入阻塞太&#xff0c;直到延时完成&#xff0c;任务重新进入就绪态。延时函数舒属于FreeRTOS的时间管…