SSM實現RPG遊戲之角色生成器
阿新 • • 發佈:2018-12-12
功能描述
幾乎所有的RPG遊戲(一種源自《龍與地下城》的遊戲型別)在進入遊戲時都會讓使用者自己來建立自己喜歡的角色
遊戲角色應有的屬性
- 遊戲角色應有以下屬性:名字、性別、種族、職業、力量、敏捷、體力、智力、智慧、生命值、魔法值和技能。
- 名字:不超過50個字元。
- 性別:可以選擇男性和女性。
- 種族:一共可選五個種族,人類、精靈、獸人、矮人和元素。
- 職業:可選六種職業,狂戰士、聖騎士、刺客、獵手、祭司和巫師。
- 其餘屬性均為整數。
- 使用者輸入角色姓名,然後由使用者選擇角色性別,然後由使用者選擇種族,然後選擇職業,然後自動分配力量、敏捷、體力、智力和智慧屬性,並計算生命值和魔法值。
- 生命值=體力*20。
- 魔法值=(智力+智慧)*10。
職業限制
很多職業會限制某些種族選擇,例如獸人不能就職聖騎士等等,種族和職業的限制
初始屬性
力量、敏捷、體力、智力和智慧要求是隨機值(利用隨機數函式來取得隨機數),但是五項屬性的總和應該是100,並且應該和職業相關。例如狂戰士的體力和力量就要比較高,而巫師需要較高的智力,而祭司則需要較高的智慧。各職業初始屬性的大致比例應遵從下表:
顯示資訊
最後向用戶顯示該角色的所有資訊,然後詢問使用者是否滿意,如使用者不滿意則重新建立,若使用者滿意則程式結束,並將使用者建立角色的相關資訊儲存進資料庫。
編碼實現
1.首先建立web工程,在這裡採用的maven,先匯入ssm相關的以及測試的pom依賴 pom.xml
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!--log4j的依賴--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!--servlet的依賴--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <!--log4j的依賴--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!--mysql的依賴--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> <scope>runtime</scope> </dependency> <!--c3p0連線池--> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!--mybatis依賴--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.4</version> </dependency> <!--spring整合mybatis的依賴--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!--Spring的jdbc模組--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.10.RELEASE</version> </dependency> <!--匯入spring的依賴--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.10.RELEASE</version> </dependency> <!--Spring整合單元測試的依賴--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.10.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.15</version> </dependency> </dependencies>
2.建立相關的配置檔案
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--Spring的註解掃描器-->
<context:component-scan base-package="edu.xust"/>
<!-- 配置c3p0連線池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/role?characterEncoding=utf8"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
<!--mybatis整合spring-->
<bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-conf.xml"/>
</bean>
<!--mapper掃描器 bean的id為mapper類名首字母小寫-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="edu.xust.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
Springmvc.xml
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--配置前端控制器(在web.xml中配置)-->
<!--配置處理器對映器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!--處理器介面卡-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!--配置檢視解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
<!--配置處理器(註解開發無需配置)-->
<context:component-scan base-package="edu.xust.controller"/>
</beans>
mybatis-conf.xml 該配置檔案將在後面的起別名的時候使用
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
</typeAliases>
</configuration>
log4j.properties 用來列印日誌
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.rootLogger=debug, stdout
3.實體類的實現 為了節省篇幅,在這裡沒有寫出get和set方法
public class RoleBean {
String username;//姓名
String sex;//性別
String race;//種族
String occupation;//職業
int strength;//力量
int agility;//敏捷
int physicial;//體力
int intellige;//智力
int intelligence;//智慧
int life;//生命值
int magic;//魔法值
String skill;//技能
}
3.mapper代理 RoleMapper.class
package edu.xust.mapper;
import edu.xust.pojo.RoleBean;
import org.springframework.stereotype.Repository;
@Repository
public interface RoleMapper {
//使用mybatis自動代理實現持久層
void insertRole(RoleBean role);
}
RoleMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="edu.xust.mapper.RoleMapper">
<insert id="insertRole" parameterType="edu.xust.pojo.RoleBean">
insert into role values(#{username},#{sex},#{race},#{occupation},#{strength},#{agility},#{physicial},#{intellige},#{intelligence},#{life},#{magic},#{skill});
</insert>
</mapper>
4.生成屬性
package edu.xust.utils;
import java.util.Random;
public class RoleUtil {
/**
* 採用隨機數生成力量屬性,其他屬性在此基礎上按比例進行計算
* @param occupation
* @return
*/
public static Object[] getProperty(String occupation) {
Object [] pro=new Object[8];
int strength = 0;//力量
int physical = 0;//體力
int inttellige = 0;//智慧
int inttelligence = 0;//智力
int life = 0;//生命值
int magic = 0;//魔法值
String skill = "";//技能
int agility = 0;//敏捷
Random random = new Random();
switch (occupation) {
case "狂戰士":
strength = random.nextInt(3) + 39;//力量
agility = strength/2;//敏捷
physical = strength-10;//體力
inttelligence = strength/8;//智力
skill="狂戰之崩山裂地斬";
break;
case "聖騎士":
strength = random.nextInt(3) + 23;//力量
agility = strength-10;//敏捷
physical = agility*2;//體力
inttelligence = agility+5;//智力
skill="聖騎之聖騎之光";//技能
break;
case "刺客":
strength = random.nextInt(3) + 20;//力量
agility = strength*2-5;//敏捷
physical = strength;//體力
inttelligence = physical-5;//智力
skill="幻舞";
break;
case "獵手":
strength = random.nextInt(3) + 13;//力量
agility = strength*3-5;//敏捷
physical = strength;//體力
inttelligence = physical-5;//智力
skill="獵手之獵刃";
break;
case "祭司":
strength = random.nextInt(3) + 13;//力量
agility = strength+5;//敏捷
physical = strength;//體力
inttelligence = physical+agility;//智力
skill = "祭司之四魂";
break;
case "巫師":
strength = random.nextInt(3) + 8;//力量
agility = strength*2;//敏捷
physical = strength;//體力
inttelligence = physical*2;//智力
skill = "巫師之怨靈";
break;
}
inttellige = 100-strength-agility-physical-inttelligence;//智慧
magic = (inttelligence+inttellige)*10;//魔法
life = physical*20;//生命值
pro[0]=strength;
pro[1] = agility;
pro[2] = physical;
pro[3] = inttelligence;
pro[4] = inttellige;
pro[5] = magic;
pro[6] = life;
pro[7] = skill;
return pro;
}
}
5.service層
package edu.xust.service;
import edu.xust.mapper.RoleMapper;
import edu.xust.pojo.RoleBean;
import edu.xust.utils.RoleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class RoleService {
@Autowired//按型別注入屬性
RoleMapper roleMapper;
public void addRole(RoleBean role) {
roleMapper.insertRole(role);
}
public RoleBean SelectRole(String username, String sex, String race, String occupation) {
//獲取屬性
Object[] property = RoleUtil.getProperty(occupation);
RoleBean role = new RoleBean();
role.setUsername(username);//名字
role.setSex(sex);//性別
role.setStrength((int)property[0]);//力量
role.setAgility((int)property[1]);//敏捷
role.setPhysicial((int)property[2]);//體力
role.setIntelligence((int)property[3]);//智力
role.setIntellige((int)property[4]);//智慧
role.setMagic((int)property[5]);//魔法
role.setLife((int)property[6]);//生命值
role.setRace(race);//種族
role.setOccupation(occupation);//職業
role.setSkill(property[7].toString());//初始技能
return role;
}
}
6.controller控制層的實現
package edu.xust.controller;
import com.alibaba.fastjson.JSON;
import edu.xust.pojo.RoleBean;
import edu.xust.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class RoleController {
RoleBean roleBean;
@Autowired
private RoleService roleService;//按型別注入
@RequestMapping(value = "/create",produces = "application/json;charset=utf-8")
@ResponseBody
public String createRole(){
roleService.addRole(roleBean);//呼叫service層新增角色
return null;
}
@RequestMapping(value = "/get",produces = "application/json;charset=utf-8")
@ResponseBody
public String getRole(String username,String sex,String occupation,String race){
roleBean = roleService.SelectRole(username, sex, race, occupation);
String s = JSON.toJSONString(roleBean);//把物件轉換為JSON字串格式
return s;
}
}
7.頁面的實現 資料傳輸格式為JSON,使用ajax實現非同步請求
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>建立角色</title>
<style type="text/css">
#con {
background: url("image/background1.jpg") no-repeat;
background-size: cover;
position: absolute;
top: 0;
bottom: 0px;
right: 0px;
left: 0px;
}
#div{
width: 600px;
height: 600px;
margin-top: 200px;
margin-left: 600px;
}
</style>
</head>
<body>
<div id="con">
<div id="div">
<div>
<span>角色名</span>
<input type="text" id="username">
</div>
<br>
<div>
<span>性別</span>
<select id="sex">
<option >男</option>
<option >女</option>
</select>
</div>
<br>
<div>
<span>種族</span>
<select id="race">
</select>
<span>職業</span>
<select id="occupation">
</select>
</div>
<br>
<div>
<button id="ok" style="margin-left: 60px">確定</button>
<button id="cal" style="margin-left: 40px">取消</button>
</div>
</div>
</div>
</body>
<script type="text/javascript" src="jquery-1.9.1.js"></script>
<script type="text/javascript">
var race = document.getElementById("race");
var occupation = document.getElementById("occupation");
var data=[];
data["人類"]=["狂戰士","聖騎士","刺客","獵手","祭司","巫師"];
data["精靈"]=["刺客","獵手","祭司","巫師"];
data["獸人"]=["狂戰士","獵手","祭司","巫師"];
data["矮人"]=["狂戰士","聖騎士","祭司"];
data["元素"]=["祭司","巫師"];
//動態生成下拉框的選擇項
for (key in data) {
var option = document.createElement("option");
option.appendChild(document.createTextNode(key));
option.setAttribute("value", key);
race.appendChild(option);
}
getdata();
//當下拉框資料改變就觸發getdata()去更改聯動框的資料
race.onchange = getdata;
function getdata() {
occupation.length = 0;
var ss = data[race.value];
for (index in ss) {
var option = document.createElement("option");
option.appendChild(document.createTextNode(ss[index]));
occupation.appendChild(option);
}
}
</script>
<script type="text/javascript">
$ok = $("#ok");
//新增時間
$ok.click(function () {
//獲取資料
var username = $("#username").val();
var sex = $("#sex").val();
var race = $("#race").val();
var occupation = $("#occupation").val();
//ajax非同步請求
$.ajax({
"url": "/get.action",
"type": "get",
"data": {"username":username,"sex":sex,"race":race,"occupation":occupation},
"success": function (data, msg, xhr) {
if( confirm("使用者名稱: "+data.username+"\n"+
"性別: "+data.sex+"\n"+
"種族: "+data.race+"\n"+
"職業: "+data.occupation+"\n"+
"力量: "+data.strength+"\n"+
"體力: "+data.physicial+"\n"+
"智慧: "+data.intellige+"\n"+
"敏捷: "+data.agility+"\n"+
"智力: "+data.intelligence+"\n"+
"生命值: "+data.life+"\n"+
"魔法: "+data.magic+"\n"+
"初始技能: "+data.skill+"\n")==true){
f1();//點選確定以後正式新增角色
}
},
"error": function () {
console.log("失敗...");
}
});
})
</script>
<script type="text/javascript">
function f1() {
$.ajax({
"url": "/create.action",
"type": "get",
"success": function (data, msg, xhr) {
alert("建立成功")
},
"error": function () {
console.log("失敗...");
}
});
}
</script>
</html>
結果