Java开发冒险(AVG)游戏杂谈

2023-12-11 10:40

本文主要是介绍Java开发冒险(AVG)游戏杂谈,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

   之前用过Android,Python的pygame,c#编程语言开发过游戏,一般来说,Java开发的游戏通常是AVG(英文adwenture game的简称)冒险游戏。

就技术角度而言,使用Java AVG 开发可以算得所有游戏类型中最容易的。一款简单 AVG 游戏的制作难度甚至在贪食蛇、俄罗斯方块之下。由于实现的简易性,导致 AVG 的开发重心往往着重于策划及美工,程序员的作用则微乎其微。同时也正因 AVG 开发的门坎约等于 0 ,所以此类型的同人游戏之多即可堪称世界之冠。另外, AVG 开发工具普及的也促进了 AVG 的量产化。利用工具,即始是小说作者、漫画家等非软件专业出身的人士,往往也能轻易制作出顶级的 AVG 大作。(顺便一提,目前我所见过最好的 AVG 制作工具是鬼子的 livemaker ,采用类似思维导图的方式构造整个游戏,很多轻小说作者乃至网络漫画家用它制作自己作品的宣传游戏。但就技术角度上说, livemaker 的开发依旧没什么难度 ......
由于 AVG 的大泛滥,通常仅有文字、图片及语音的 AVG 往往无法满足用户需求( H 除外-_-)。我们每每可在 AVG 游戏类型后发现个 + 号,比如《樱花大战》是 AVG+SLG ,《生化危机》是 AVG+ACT 。所以客观上说, AVG 开发仅仅能进行字图的交互是不够的,还要解决多模块组件的协调问题。
Java 桌面应用开发中 , 我们都知道绘图是极为简单的,有 Image Graphics 两个对象就可以 Paint 一个图形,即使图形对象再多,最后它们也必须统一在一个 Paint 中,所以 Java 中不存在图像的交互问题。
但问题在于,图像的显示可以统一,但是触发图像变化的事件却是很难统一的。比如现在有需求如下,在 AVG 模式中,触发键盘事件上、下、左、右时为控制画面的前进、后退,切换模式到 SLG 模 式后,设定上、下、左、右是光标移动,那么如果我要在程序中实现,就必须记录当前模式,而后根据不同模式调用事件,再反馈到图形上。如果只有几个事件的区 别,我们当然可以很容易用分支来实现;问题是,随着游戏规模的加大,这些分支将成几何倍数增多,单纯的分支判定到最后只能忙于应付,落个费力不讨好。
其实在这时,我们大可以使用一些技巧来轻松解决问题。

首先,我们构造一个接口,命名为IControl,继承鼠标及键盘监听,并在其中设定两个抽象方法:
view plain copy to clipboard print ?
  1. package org.loon.simple.avg;  
  2. import java.awt.Graphics;  
  3. import java.awt.event.KeyListener;  
  4. import java.awt.event.MouseListener;  
  5. import java.awt.event.MouseMotionListener;  
  6. /** 
  7.  * Copyright 2008 - 2009 
  8.  *  
  9.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
  10.  * use this file except in compliance with the License. You may obtain a copy of 
  11.  * the License at 
  12.  *  
  13.  * [url]http://www.apache.org/licenses/LICENSE-2.0[/url] 
  14.  *  
  15.  * Unless required by applicable law or agreed to in writing, software 
  16.  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
  17.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
  18.  * License for the specific language governing permissions and limitations under 
  19.  * the License. 
  20.  *  
  21.  * @project loonframework 
  22.  * @author chenpeng 
  23.  * @email:ceponline@yahoo.com.cn 
  24.  * @version 0.1 
  25.  */  
  26. public interface IControl extends MouseListener, MouseMotionListener,  
  27.         KeyListener {  
  28.     public abstract void draw(final Graphics g);  
  29.     public abstract IControl invoke();  
  30. }  
  而后,再构造一个接口,命名为IAVG,同样继承鼠标及键盘监听,并在其中设定三个抽象方法,用以操作IControl接口:

view plain copy to clipboard print ?
  1. package org.loon.simple.avg;  
  2. import java.awt.Graphics;  
  3. import java.awt.event.KeyListener;  
  4. import java.awt.event.MouseListener;  
  5. import java.awt.event.MouseMotionListener;  
  6. /** 
  7.  * Copyright 2008 - 2009 
  8.  *  
  9.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
  10.  * use this file except in compliance with the License. You may obtain a copy of 
  11.  * the License at 
  12.  *  
  13.  * [url]http://www.apache.org/licenses/LICENSE-2.0[/url] 
  14.  *  
  15.  * Unless required by applicable law or agreed to in writing, software 
  16.  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
  17.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
  18.  * License for the specific language governing permissions and limitations under 
  19.  * the License. 
  20.  *  
  21.  * @project loonframework 
  22.  * @author chenpeng 
  23.  * @email:ceponline@yahoo.com.cn 
  24.  * @version 0.1 
  25.  */  
  26. public interface IAVG extends MouseListener, MouseMotionListener,  
  27.         KeyListener {  
  28.     public abstract void draw(final Graphics g);  
  29.     public abstract IControl getControl();  
  30.     public abstract void setControl(final IControl control);  
  31. }   


再后,制作一个显示图像用组件,命名为AVGCanva,继承自Canvas。
view plain copy to clipboard print ?
  1. package org.loon.simple.avg;  
  2. import java.awt.Canvas;  
  3. import java.awt.Graphics;  
  4. /** 
  5.  * Copyright 2008 - 2009 
  6.  *  
  7.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
  8.  * use this file except in compliance with the License. You may obtain a copy of 
  9.  * the License at 
  10.  *  
  11.  * [url]http://www.apache.org/licenses/LICENSE-2.0[/url] 
  12.  *  
  13.  * Unless required by applicable law or agreed to in writing, software 
  14.  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
  15.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
  16.  * License for the specific language governing permissions and limitations under 
  17.  * the License. 
  18.  *  
  19.  * @project loonframework 
  20.  * @author chenpeng 
  21.  * @email:ceponline@yahoo.com.cn 
  22.  * @version 0.1 
  23.  */  
  24. public class AVGCanvas extends Canvas {  
  25.     /** 
  26.      *  
  27.      */  
  28.     private static final long serialVersionUID = 1982278682597393958L;  
  29.     private boolean start;  
  30.     private IAVG avg;  
  31.     public AVGCanvas(IAVG handler) {  
  32.         this.avg = handler;  
  33.         this.start = false;  
  34.         this.addKeyListener(handler);  
  35.         this.addMouseListener(handler);  
  36.         this.addMouseMotionListener(handler);  
  37.     }  
  38.       
  39.     public void update(Graphics g) {  
  40.         paint(g);  
  41.     }  
  42.     public void paint(Graphics g) {  
  43.         if (this.start) {  
  44.             this.avg.draw(g);  
  45.         }  
  46.     }  
  47.     public void startPaint() {  
  48.         this.start = true;  
  49.     }  
  50.     public void endPaint() {  
  51.         this.start = false;  
  52.     }  
  53. }  
这段代码中的paint方法中并没有现成的方法,而是调用了IAVG接口的draw。紧接着,我们再设定一个AVGFrame用以加载AVGCanvas。
view plain copy to clipboard print ?
  1. package org.loon.simple.avg;  
  2. import java.awt.Color;  
  3. import java.awt.Dimension;  
  4. import java.awt.Frame;  
  5. import java.awt.event.WindowAdapter;  
  6. import java.awt.event.WindowEvent;  
  7.   
  8. /** 
  9.  * Copyright 2008 - 2009 
  10.  *  
  11.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
  12.  * use this file except in compliance with the License. You may obtain a copy of 
  13.  * the License at 
  14.  *  
  15.  * [url]http://www.apache.org/licenses/LICENSE-2.0[/url] 
  16.  *  
  17.  * Unless required by applicable law or agreed to in writing, software 
  18.  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
  19.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
  20.  * License for the specific language governing permissions and limitations under 
  21.  * the License. 
  22.  *  
  23.  * @project loonframework 
  24.  * @author chenpeng 
  25.  * @email:ceponline@yahoo.com.cn 
  26.  * @version 0.1 
  27.  */  
  28. public class AVGFrame extends Frame implements Runnable {  
  29.     /** 
  30.      *  
  31.      */  
  32.     private static final long serialVersionUID = 198284399945549558L;  
  33.     private IAVG avg;  
  34.     private AVGCanvas canvas;  
  35.     private boolean fps;  
  36.     private String titleName;  
  37.     private Thread mainLoop;  
  38.     public AVGFrame(String titleName, int width, int height) {  
  39.         this(new AVG(), titleName, width, height);  
  40.     }  
  41.     public AVGFrame(IAVG avg, String titleName, int width, int height) {  
  42.         super(titleName);  
  43.         Lib.WIDTH = width;  
  44.         Lib.HEIGHT = height;  
  45.         this.avg = avg;  
  46.         this.titleName = titleName;  
  47.         this.addKeyListener(avg);  
  48.         this.setPreferredSize(new Dimension(width + 5, height + 25));  
  49.         this.initCanvas(Lib.WIDTH, Lib.HEIGHT);  
  50.         this.pack();  
  51.         this.addWindowListener(new WindowAdapter() {  
  52.             public void windowClosing(WindowEvent e) {  
  53.                 System.exit(0);  
  54.             }  
  55.         });  
  56.         this.setResizable(false);  
  57.         this.setLocationRelativeTo(null);  
  58.         this.setVisible(true);  
  59.     }  
  60.     public void run() {  
  61.         gameLoop();  
  62.     }  
  63.     /** 
  64.      * 开始循环窗体图像 
  65.      *  
  66.      */  
  67.     private synchronized void gameLoop() {  
  68.         canvas.startPaint();  
  69.         long second = 0L;  
  70.         int moveCount = 0;  
  71.         // 循环绘制  
  72.         for (;;) {  
  73.             long start = System.currentTimeMillis();  
  74.             this.paintScreen();  
  75.             long end = System.currentTimeMillis();  
  76.             long time = end - start;  
  77.             long sleepTime = 20L - time;  
  78.             if (sleepTime < 0L)  
  79.                 sleepTime = 0L;  
  80.             try {  
  81.                 Thread.sleep(sleepTime);  
  82.             } catch (InterruptedException e) {  
  83.                 e.printStackTrace();  
  84.             }  
  85.             if (this.fps) {  
  86.                 moveCount++;  
  87.                 second += System.currentTimeMillis() - start;  
  88.                 if (second >= 1000L) {  
  89.                     this.setTitle(new StringBuilder(titleName).append(" FPS:")  
  90.                             .append(moveCount).toString());  
  91.                     moveCount = 0;  
  92.                     second = 0L;  
  93.                 }  
  94.             }  
  95.         }  
  96.     }  
  97.     /** 
  98.      * 启动游戏循环 
  99.      *  
  100.      */  
  101.     public void mainLoop() {  
  102.         this.mainLoop = new Thread(this);  
  103.         this.mainLoop.start();  
  104.     }  
  105.     /** 
  106.      * 初始化背景帆布 
  107.      *  
  108.      * @param width 
  109.      * @param height 
  110.      */  
  111.     private void initCanvas(final int width, final int height) {  
  112.         canvas = new AVGCanvas(avg);  
  113.         canvas.setBackground(Color.black);  
  114.         canvas.setPreferredSize(new Dimension(width, height));  
  115.         this.add(canvas);  
  116.     }  
  117.     public IAVG getAVG() {  
  118.         return this.avg;  
  119.     }  
  120.     protected void processWindowEvent(WindowEvent e) {  
  121.         super.processWindowEvent(e);  
  122.     }  
  123.     public synchronized void paintScreen() {  
  124.         canvas.repaint();  
  125.     }  
  126.     public boolean isShowFPS() {  
  127.         return fps;  
  128.     }  
  129.     public void setShowFPS(boolean fps) {  
  130.         this.fps = fps;  
  131.     }  
  132.     public Thread getMainLoop() {  
  133.         return mainLoop;  
  134.     }  
  135.     public String getTitleName() {  
  136.         return titleName;  
  137.     }  
  138. }  
我们可以看到,在本例鼠标键盘事件及图像绘制完全通过接口方式实现。 此时,只要让不同组件统一实现IControl接口,便可以轻松转换事件及图像的绘制。也正是我们都再熟悉不过的 MVC 模式中,通过 Event 导致 Controller 改变 Model View 的基本原理。

  例子 图下:

end,游戏编程语言之间的开发可以融会贯通

转载于:https://my.oschina.net/bigfool007139/blog/515503

这篇关于Java开发冒险(AVG)游戏杂谈的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/480401

相关文章

Java五子棋之坐标校正

上篇针对了Java项目中的解构思维,在这篇内容中我们不妨从整体项目中拆解拿出一个非常重要的五子棋逻辑实现:坐标校正,我们如何使漫无目的鼠标点击变得有序化和可控化呢? 目录 一、从鼠标监听到获取坐标 1.MouseListener和MouseAdapter 2.mousePressed方法 二、坐标校正的具体实现方法 1.关于fillOval方法 2.坐标获取 3.坐标转换 4.坐

Spring Cloud:构建分布式系统的利器

引言 在当今的云计算和微服务架构时代,构建高效、可靠的分布式系统成为软件开发的重要任务。Spring Cloud 提供了一套完整的解决方案,帮助开发者快速构建分布式系统中的一些常见模式(例如配置管理、服务发现、断路器等)。本文将探讨 Spring Cloud 的定义、核心组件、应用场景以及未来的发展趋势。 什么是 Spring Cloud Spring Cloud 是一个基于 Spring

Javascript高级程序设计(第四版)--学习记录之变量、内存

原始值与引用值 原始值:简单的数据即基础数据类型,按值访问。 引用值:由多个值构成的对象即复杂数据类型,按引用访问。 动态属性 对于引用值而言,可以随时添加、修改和删除其属性和方法。 let person = new Object();person.name = 'Jason';person.age = 42;console.log(person.name,person.age);//'J

java8的新特性之一(Java Lambda表达式)

1:Java8的新特性 Lambda 表达式: 允许以更简洁的方式表示匿名函数(或称为闭包)。可以将Lambda表达式作为参数传递给方法或赋值给函数式接口类型的变量。 Stream API: 提供了一种处理集合数据的流式处理方式,支持函数式编程风格。 允许以声明性方式处理数据集合(如List、Set等)。提供了一系列操作,如map、filter、reduce等,以支持复杂的查询和转

Java面试八股之怎么通过Java程序判断JVM是32位还是64位

怎么通过Java程序判断JVM是32位还是64位 可以通过Java程序内部检查系统属性来判断当前运行的JVM是32位还是64位。以下是一个简单的方法: public class JvmBitCheck {public static void main(String[] args) {String arch = System.getProperty("os.arch");String dataM

详细分析Springmvc中的@ModelAttribute基本知识(附Demo)

目录 前言1. 注解用法1.1 方法参数1.2 方法1.3 类 2. 注解场景2.1 表单参数2.2 AJAX请求2.3 文件上传 3. 实战4. 总结 前言 将请求参数绑定到模型对象上,或者在请求处理之前添加模型属性 可以在方法参数、方法或者类上使用 一般适用这几种场景: 表单处理:通过 @ModelAttribute 将表单数据绑定到模型对象上预处理逻辑:在请求处理之前

eclipse运行springboot项目,找不到主类

解决办法尝试了很多种,下载sts压缩包行不通。最后解决办法如图: help--->Eclipse Marketplace--->Popular--->找到Spring Tools 3---->Installed。

JAVA读取MongoDB中的二进制图片并显示在页面上

1:Jsp页面: <td><img src="${ctx}/mongoImg/show"></td> 2:xml配置: <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001

Java面试题:通过实例说明内连接、左外连接和右外连接的区别

在 SQL 中,连接(JOIN)用于在多个表之间组合行。最常用的连接类型是内连接(INNER JOIN)、左外连接(LEFT OUTER JOIN)和右外连接(RIGHT OUTER JOIN)。它们的主要区别在于它们如何处理表之间的匹配和不匹配行。下面是每种连接的详细说明和示例。 表示例 假设有两个表:Customers 和 Orders。 Customers CustomerIDCus

22.手绘Spring DI运行时序图

1.依赖注入发生的时间 当Spring loC容器完成了 Bean定义资源的定位、载入和解析注册以后,loC容器中已经管理类Bean 定义的相关数据,但是此时loC容器还没有对所管理的Bean进行依赖注入,依赖注入在以下两种情况 发生: 、用户第一次调用getBean()方法时,loC容器触发依赖注入。 、当用户在配置文件中将<bean>元素配置了 lazy-init二false属性,即让