学习java朋友们,福利来了,今天小编给大家带来了一款仙剑源码。注意:此源码仅供学习使用!!
源码搭建和讲解
启动main入口:
//************************************************************************
// ************完整源码移步: gitee典康姆/hadluo/java_game01.git *********
//************************************************************************package cn.tedu.xjqxz;import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;/*** 游戏案例的窗口类* 窗口大小:1024 * 768**/
public class GameFrame {public static void main(String[] args) {/** 1.创建窗口类的实例化对象* 2.让窗口对象显示出来* 3.设置窗口对象的相关属性*/final int width = 1024;final int height = 768;JFrame jFrame = new JFrame();jFrame.setSize(width, height);jFrame.setLocationRelativeTo(null);jFrame.setResizable(false);jFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);jFrame.addWindowListener(new WindowAdapter() {// 当窗口正要关闭的时候,会自动调用该方法@Overridepublic void windowClosing(WindowEvent e) {super.windowClosing(e);// 弹出确认对话框int ch = JOptionPane.showConfirmDialog(null,"确认退出游戏吗?","提示",JOptionPane.YES_NO_OPTION);if (ch == JOptionPane.YES_OPTION) {System.exit(0);}}});jFrame.setTitle("仙剑奇侠传 - Version 1.0");GamePanel gamePanel = new GamePanel();jFrame.addKeyListener(gamePanel);jFrame.add(gamePanel);jFrame.setVisible(true);}
}
NPC 对象
package cn.tedu.xjqxz;import java.awt.*;/*** 配角人物Npc 类**/
public class Npc {private String name;private boolean chatOver = false;// npc在背景图片中的坐标private int x;private int y;private int index = 0;int chatIndex = 0;private Image[] image;private String[] words;public Npc(String[] words, Image[] image, int x, int y, String name) {this.words = words;this.image = image;this.x = x;this.y = y;this.name = name;}public int getX() {return x;}public int getY() {return y;}public String getName() {return name;}public int getWidth() {return image[0].getWidth(null);}public int getHeight() {return image[0].getHeight(null);}public Image getImage() {return image[index];}public void updateIndex() {index++;if (index > image.length - 1) {index = 0;}}public void updateChatContent() {chatIndex++;if (chatIndex > words.length - 1) {chatOver = true;chatIndex = 0;}}public String getWords() {return words[chatIndex];}public boolean isChatOver() {return chatOver;}public void setChatOver(boolean b) {chatOver = b;}
}
运行工具
代码是一个普通的java工程,我们直接导入到eclipse或者idea就可以运行了。
操作方式
- 方向键控制角色上下左右移动
- 空格键与npc对话
- ESC键退出对话或退出游戏
- 回车键切换场景(切换位置在地图最右侧小路尽头)
结尾语
我是分享好物+教程+源码 的老罗,欢迎关注,更多精品源码!