以我這種準骨灰級玩家的視點來看(鄙人88年開始玩FC,時年6歲),早期的SLG遊戲,大體只是《三國志》(I由1985年開始發售)這類發佈指令擴充軍備並戰鬥的“命令下達式遊戲”,並沒有什麼分類上的難度。但自從《火焰紋章》(1990年開始發售)出現伊始,即策略遊戲與傳統RPG的分野變得模糊起來,這種具有故事情節的戰棋策略遊戲,同時兼具了SLG及RPG的雙特性,以後的歲月中人們習慣分類其爲——SRPG,火焰系列也據此被後人視作SRPG的鼻祖遊戲之一。但事實上講,此類遊戲仍舊具備着傳統SLG那樣如同下棋般戰鬥並採用回合制的特點,RPG的情節部分僅僅是作爲遊戲內容的補充罷了,始終擺脫不掉傳統策略遊戲地圖-〉指令-〉戰鬥的大框架,故此客觀上依然應被劃入了SLG範圍。再後來,隨着電腦的普及,如大衆軟件這些媒體雜誌又把文明和模擬城市乃至美少女夢工廠這樣的遊戲也劃分進SLG裏,但按照現在的說法,足球經理、模擬人生應該是“SIM”,即單純的Simulation,而美少女夢工廠則是TCG——不過在日式遊戲劃分中,這些依然都屬於SLG。
就鄙人看來,強分策略類遊戲類型是沒有什麼意義的,作爲最初源泉的SLG是能夠包含SRPG、RTS種種分支的。就好比有的人是博士、有的人是碩士,但我們依舊可以將其統稱爲“知識分子”,劃到一個大圈子裏面去。又比如我們平時可能常說“上海人怎樣”、“北京人如何”,但當我說“中國人”時,自然能夠將這些都包羅其中,無論好壞,誰都脫身不得。 而在此類遊戲中,包含策略因素的這個大圈子的統一稱謂,便是SLG無疑。
實際上,絕大多數英文站點也是將此類遊戲統一丟到Simulation Game下的(包括模擬城市之類的純SIM),並沒有進行SRPG(Strategies Role Play Games)或RTS(Real-Time Strategy Game)乃至其餘種種的細分。歸根究底,因爲這些遊戲近似的因素太多,在大多數時候已經難以區分其本來面貌,“名無實,實無名”,只能一概而論了。而今有不少新生代玩家喜歡硬分遊戲種類,竊以爲愚了。
——————————————————————————————————————————————————————————
閒話說了不少,現在開始進入正題。在本系列中,我將結合實例嘗試以Java實現典型戰棋類SLG的主要功能,本文爲第0節,也就是準備章節。
看過我以前寫的RPG及ACT系列的朋友們,應該已對Java中2D圖形繪製功能有了初步的認識,在本文中,我將闡述如何令繪製的窗體而非組件響應鼠標事件,及如何在窗體中自定義非標準大小的鼠標指針,作爲本系列的預備知識。
首先,我們都知道,在Java中可以通過Cursor組件自定義遊標樣式,比如下圖有一組取材自Langrisser2的光標圖片。
在Java桌面開發中,我們可以通過分解這組圖片來得到小圖,以此來自定義鼠標光標。
但是有一個問題,這時無論圖片原始大小如何,至多也只能是32x32大小,如果超出這個範圍,則無法作爲遊標在窗體中完整顯示。
也就是說,如上圖這樣46x46的大圖,要麼縮小顯示,要麼局部顯示,總之46x46的狀態下是無法完整的顯示在窗體中的。
可我們明明見到很多遊戲中的光標是不規則不成比例的,究竟如何做到呢?其實很簡單,自己繪製就好了。
絕大多數不合規矩的東西,我們都可以自己把它“畫出來”,只要能確定它的座標。
如下代碼記錄了鍵盤及鼠標狀態下的圖標移動:
- /**
- * 鍵盤事件設置
- *
- */
- public void setKeyset() {
- addKeyListener(new KeyAdapter() {
- public void keyTyped(KeyEvent e) {
- }
- public void keyReleased(KeyEvent e) {
- }
- public void keyPressed(KeyEvent e) {
- if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
- currentX = currentX + move;
- }
- if (e.getKeyCode() == KeyEvent.VK_LEFT) {
- currentX = currentX - move;
- }
- if (e.getKeyCode() == KeyEvent.VK_UP) {
- currentY = currentY - move;
- }
- if (e.getKeyCode() == KeyEvent.VK_DOWN) {
- currentY = currentY + move;
- }
- repaint();
- }
- });
- }
- /**
- * 鼠標事件設置
- *
- */
- public void setMouse() {
- addMouseListener(new MouseAdapter() {
- public void mousePressed(MouseEvent e) {
- }
- public void mouseReleased(MouseEvent e) {
- }
- });
- addMouseMotionListener(new MouseMotionAdapter() {
- public void mouseMoved(MouseEvent ex) {
- currentX = ex.getX();
- currentY = ex.getY();
- repaint();
- }
- public void mouseDragged(MouseEvent ex) {
- currentX = ex.getX();
- currentY = ex.getY();
- repaint();
- }
- });
- }
比如這樣:
- CursorFrame f = new CursorFrame();
- int[] pixels = new int[256];
- Image image = Toolkit.getDefaultToolkit().createImage(
- new MemoryImageSource(16, 16, pixels, 0, 16));
- Cursor transparentCursor = Toolkit.getDefaultToolkit()
- .createCustomCursor(image, new Point(0, 0), "hidden");
- f.setCursor(transparentCursor);
我們繪製一張16x16的透明圖作爲遊標,在使用者看來,就只能見到我們drawImage出的“僞遊標”罷了。
現在我們據此製作一個假單的仿Langrisser2開始界面,代碼如下:
- package org.slg.simple;
- import java.awt.Color;
- import java.awt.Cursor;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.Panel;
- import java.awt.Point;
- import java.awt.Toolkit;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseMotionAdapter;
- import java.awt.image.BufferedImage;
- import java.awt.image.MemoryImageSource;
- /**
- * Copyright 2008
- *
- * 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:[email protected]
- * @version 0.1
- */
- public class ExemplePanel extends Panel {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- final static int currentWidth = 480;
- final static int currentHeight = 360;
- //背景緩衝圖
- final Image _background;
- //鼠標指針圖形組
- final Image[] _mouses;
- //背景圖
- final Image _backgroundImage;
- Graphics _backgroundGraphics;
- Image _mouse;
- Image _arrow;
- // 選中項,默認指向第一條
- int _select = 1;
- // 遊標x軸
- int _currentX = 0;
- // 遊標y軸
- int _currentY = 0;
- int _move = 5;
- public ExemplePanel() {
- // 創建一個背景緩存用image
- _background = new BufferedImage(currentWidth, currentHeight,
- BufferedImage.TYPE_INT_ARGB);
- // 導入開始時背景圖像
- _backgroundImage = Utility.loadImage("image/start.gif");
- // 導入光標圖系列,以列寬46讀取到image數組
- _mouses = Utility.getImageColumns(
- Utility.loadImage("image/cursor.png"), 46);
- // 初始背景爲黑色
- setBackground(new Color(0, 0, 0));
- _arrow = Utility.loadImage("image/arrow.png");
- _backgroundGraphics = _background.getGraphics();
- // 設定鍵盤監聽
- setKeyset();
- // 設定鼠標監聽
- setMouse();
- // 設置鼠標動畫(本例只是一個簡單示例,實際應根據相應事件變更遊標造型)
- Thread mouseAnimation = new Thread() {
- public void run() {
- int cursorMax = _mouses.length;
- int cursorIndex = 0;
- do {
- if (cursorIndex < cursorMax) {
- _mouse = _mouses[cursorIndex];
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- repaint();
- } else {
- cursorIndex = 0;
- }
- cursorIndex++;
- } while (true);
- }
- };
- // 開啓鼠標動畫
- mouseAnimation.start();
- }
- public void paint(Graphics g) {
- // 繪製背景
- _backgroundGraphics.drawImage(_backgroundImage, 0, 0, this);
- // 繪製光標
- _backgroundGraphics.drawImage(_mouse, _currentX, _currentY, this);
- drawTitle((Graphics2D) _backgroundGraphics);
- // 加載緩存圖
- g.drawImage(_background, 0, 0, this);
- }
- public void update(Graphics g) {
- paint(g);
- }
- /**
- * 鍵盤事件設置
- *
- */
- public void setKeyset() {
- addKeyListener(new KeyAdapter() {
- public void keyTyped(KeyEvent e) {
- }
- public void keyReleased(KeyEvent e) {
- }
- public void keyPressed(KeyEvent e) {
- if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
- _currentX = _currentX + _move;
- }
- if (e.getKeyCode() == KeyEvent.VK_LEFT) {
- _currentX = _currentX - _move;
- }
- if (e.getKeyCode() == KeyEvent.VK_UP) {
- _currentY = _currentY - _move;
- }
- if (e.getKeyCode() == KeyEvent.VK_DOWN) {
- _currentY = _currentY + _move;
- }
- repaint();
- }
- });
- }
- /**
- * 鼠標事件設置
- *
- */
- public void setMouse() {
- int[] pixels = new int[256];
- Image image = Toolkit.getDefaultToolkit().createImage(
- new MemoryImageSource(16, 16, pixels, 0, 16));
- // 製作一個透明的遊標
- Cursor transparentCursor = Toolkit.getDefaultToolkit()
- .createCustomCursor(image, new Point(0, 0), "hidden");
- // 插入透明遊標,以此模擬無遊標狀態
- setCursor(transparentCursor);
- addMouseListener(new MouseAdapter() {
- public void mousePressed(MouseEvent e) {
- if (e.getButton() == 1) {
- State.l_clk = true;
- }
- if (e.getButton() == 3) {
- State.r_clk = true;
- }
- }
- public void mouseReleased(MouseEvent e) {
- if (e.getButton() == 1) {
- State.l_clk = false;
- }
- if (e.getButton() == 3) {
- State.r_clk = false;
- }
- }
- });
- addMouseMotionListener(new MouseMotionAdapter() {
- public void mouseMoved(MouseEvent ex) {
- _currentX = ex.getX();
- _currentY = ex.getY();
- repaint();
- }
- public void mouseDragged(MouseEvent ex) {
- _currentX = ex.getX();
- _currentY = ex.getY();
- repaint();
- }
- });
- }
- /**
- * 繪製標題選項
- *
- * @param g
- */
- void drawTitle(Graphics2D g) {
- Utility.font(g, 15, 1);
- Utility.color(g, 0, 0, 0);
- if (_select != 0) {
- g.drawImage(_arrow, 168, 227 + _select * 20, null);
- }
- //PS:如果不想在程序中繪製,也可以直接在準備好的背景圖上寫文字,pc版的Langrisser就是那樣......
- g.drawString("開始新遊戲", 195, 260);
- g.drawString("載入記錄", 203, 280);
- g.drawString("退出遊戲", 203, 300);
- for (int i = 0; i < 3; i++) {
- if (_currentX > 195 && _currentX < 270 && _currentY > i * 20 + 235
- && _currentY < i * 20 + 275) {
- _select = i + 1;
- }
- }
- repaint();
- Utility.wait(20);
- if (State.l_clk && !State.lock_lck) {
- State.lock_lck = true;
- if (_select == 1) {
- System.out.println("您選擇了:開始");
- }
- if (_select == 2) {
- System.out.println("您選擇了:繼續");
- }
- if (_select == 3) {
- System.out.println("您選擇了:結束");
- System.exit(0);
- }
- }
- if (!State.l_clk && State.lock_lck) {
- State.lock_lck = false;
- }
- }
- }
運行效果如下圖:
當您看見此行文字時,我的CSDN上傳功能還無法使用……