好家伙,天天拖,終于寫完了
?
代碼已開源(Gitee)
PH-planewar: 個(gè)人開發(fā)的全棧小游戲 前端:vue2 + element-ui 后端: Springboot + mybatis-plus 數(shù)據(jù)庫(kù): mysql 目前實(shí)現(xiàn)功能: 1.注冊(cè)登陸 2.游戲數(shù)據(jù)保存 3.游戲運(yùn)行 (gitee.com)
(前后端放一起了)
怎么說呢,感覺比較簡(jiǎn)潔,但是問題不大
實(shí)現(xiàn)了分?jǐn)?shù)保存的功能
?文章來源地址http://www.zghlxwxcb.cn/news/detail-461040.html
1.效果如下:
1.刷新頁(yè)面后依舊保存數(shù)據(jù)
?
2.重新登錄后,依舊保存數(shù)據(jù)
?
3.生命值為零后,游戲重置
?
2.代碼如下:
Game.vue

MyLogin.vue


<template>
<div class="login-container">
<div class="login-box">
<!-- 頭像區(qū)域 -->
<div class="text-center avatar-box">
<img src="../assets/logo.png" class="img-thumbnail avatar" alt="">
</div>
<!-- 表單區(qū)域 -->
<div class="form-login p-4">
<!-- 登錄名稱 -->
<div class="form-group form-inline">
<label for="username">賬號(hào):</label>
<input type="text" class="form-control ml-2" id="username" placeholder="請(qǐng)輸入賬號(hào)" autocomplete="off"
v-model.trim="loginForm.loginName" />
</div>
<!-- 登錄密碼 -->
<div class="form-group form-inline">
<label for="password">密碼:</label>
<input type="password" class="form-control ml-2" id="password" placeholder="請(qǐng)輸入密碼"
v-model.trim="loginForm.password" />
</div>
<!-- 登錄和重置按鈕 -->
<div class="form-group form-inline d-flex justify-content-end">
<button type="button" class="btn btn-secondary mr-2" @click="writenum">測(cè)試</button>
<button type="button" class="btn btn-secondary mr-2" @click="toregister">去注冊(cè)</button>
<button type="button" class="btn btn-primary" @click="login">登錄</button>
</div>
</div>
</div>
</div>
</template>
<script>
import bus from '../js/eventBus'
export default {
name: 'MyLogin',
data() {
return {
loginForm: {
id: '',
password: '',
life: null,
score: null,
loginName: null,
isFirst:true
}
}
},
methods: {
writenum() {
this.loginForm.loginName = 123456;
this.loginForm.password = 123456;
},
login() {
// console.log(this.$store.state.count)
// console.log('submit!',this.loginForm);
//表單驗(yàn)證
if (this.loginForm.loginName == "") {
this.$message({
message: '請(qǐng)輸入用戶名',
type: 'error'
});
return;
}
if (this.loginForm.password == "") {
this.$message({
message: '請(qǐng)輸入密碼',
type: 'error'
});
return;
}
//發(fā)送登陸請(qǐng)求
if (this.loginForm.loginName != "" && this.loginForm.password != "") {
this.axios.post('http://localhost:3312/sys-user/login', this.loginForm).then((resp) => {
console.log("this is login", resp);
let data = resp.data;
// console.log(this.$store.state.user)
console.log(resp.data.content)
//es6語(yǔ)法,擴(kuò)展操作符,找到resp.data.content的每一個(gè)屬性然后賦值給新的對(duì)象
// this.$store.state.user = { ...resp.data.content }
// console.log(this.$store.state.user)
//localStorage存
localStorage.setItem("insuranceCode", JSON.stringify(resp.data.content));
console.log(this.loginForm.isFirst)
localStorage.setItem("getisFirst", JSON.stringify(this.loginForm.isFirst));
console.log(JSON.parse(localStorage.getItem("getisFirst")))
//localStorage取
console.log(JSON.parse(localStorage.getItem("insuranceCode")))
if (data.success) {
this.loginForm = {};
this.$message({
message: '登陸成功!!!',
type: 'success'
});
this.$router.push({ path: '/game' })
} else {
this.$message({
message: '登陸失敗,密碼錯(cuò)誤或用戶名未注冊(cè)',
type: 'error'
});
console.log(data)
}
})
}
},
toregister() {
this.$router.push('/register')
},
},
mounted() {
// bus.$emit('getLoginName', this.loginForm)
}
}
</script>
<style lang="less" scoped>
.login-container {
background-color: #35495e;
height: 100%;
.login-box {
width: 400px;
height: 250px;
background-color: #fff;
border-radius: 3px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
box-shadow: 0 0 6px rgba(255, 255, 255, 0.5);
.form-login {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
box-sizing: border-box;
}
}
}
.form-control {
flex: 1;
}
.avatar-box {
position: absolute;
width: 100%;
top: -65px;
left: 0;
.avatar {
width: 120px;
height: 120px;
border-radius: 50% !important;
box-shadow: 0 0 6px #efefef;
}
}
</style>
?
?
3.代碼解釋:
這個(gè)怎么說呢,其實(shí)整個(gè)思路非常簡(jiǎn)單,就是寫的時(shí)候會(huì)有很多小毛病,小bug
思路:
?
?
3.1.登陸驗(yàn)證
首先我們?cè)诘顷懙臅r(shí)候,拿著用戶輸入的用戶名和密碼,發(fā)一次登陸請(qǐng)求,
后端驗(yàn)證密碼后,將用戶的數(shù)據(jù)返回(包括id,分?jǐn)?shù),生命...)
前端拿到數(shù)據(jù)之后,將數(shù)據(jù)保存到本地localStorage
localStorage.setItem("insuranceCode", JSON.stringify(resp.data.content));
?
3.2然后跳轉(zhuǎn)到我們的Game.vue中去
?
3.3.判斷是否首次進(jìn)入
我們?cè)诒韱螖?shù)據(jù)中添加一個(gè)isFirst屬性,來判斷是否首次進(jìn)入游戲界面
isFirst:true
localStorage.setItem("getisFirst", JSON.stringify(this.loginForm.isFirst));
?
?
3.3.1.若為首次進(jìn)入游戲界面
if (JSON.parse(localStorage.getItem("getisFirst")) == true) {
location.reload();
console.log("已刷新")
localStorage.setItem("getisFirst", JSON.stringify("false"));
}
將頁(yè)面刷新
隨后將isFirst的狀態(tài)改為"false"
(解釋一下,感覺是資源加載的問題,首次進(jìn)入游戲界面的時(shí)候,需要刷新一下,圖片資源才能加載出來,
這也是為什么沒有用其他的傳值方案.其他的傳值方案,刷新一下就沒了)
?
3.4.隨后拿到數(shù)據(jù)并賦值給this.player
//ES6對(duì)象的拓展運(yùn)算符{...Object}
//拓展運(yùn)算符(...)用于取出參數(shù)對(duì)象所有可遍歷屬性然后拷貝到當(dāng)前對(duì)象
this.player = { ...JSON.parse(localStorage.getItem("insuranceCode")) };
?
window.life = this.player.life
window.score = this.player.score
?
3.5.為游戲狀態(tài)賦值
window.life和window.score是我們的游戲參數(shù)
?
3.6.使用計(jì)時(shí)器
隨后就是我們的關(guān)鍵計(jì)時(shí)器了
setInterval(() => {
//當(dāng)生命值小于1,即為零時(shí),游戲重置
if (window.life < 1) {
// window.life = 3
// window.score = 0;
console.log("已重置")
this.player.life = 3;
this.player.score = 0;
localStorage.setItem("insuranceCode", JSON.stringify(this.player));
this.axios.post('http://localhost:3312/sys-user/update', this.player)
.then((resp) => {
console.log("this is update", resp);
let data = resp.data;
//
if (data.success) {
console.log({
message: '修改成功',
type: 'success'
});
}
})
window.life = 3
window.score = 0
}
this.player.life = window.life
this.player.score = window.score
console.log(this.player)
localStorage.setItem("insuranceCode", JSON.stringify(this.player));
console.log(this.player.life, this.player.score,window.life,window.score)
this.axios.post('http://localhost:3312/sys-user/update', this.player)
.then((resp) => {
console.log("this is update", resp);
let data = resp.data;
//
if (data.success) {
console.log({
message: '修改成功',
type: 'success'
});
}
})
}, 1000)
這里是一個(gè)每秒(1000毫秒)執(zhí)行一次的計(jì)時(shí)器
此處進(jìn)行判斷,
3.6.1.若生命值為零了,對(duì)游戲數(shù)據(jù)進(jìn)行初始化(分?jǐn)?shù)歸零,生命值為3)
隨后發(fā)一次請(qǐng)求,保存數(shù)據(jù)
?
3.6.2.若生命值不為0,則
this.player.life = window.life
this.player.score = window.score
更新分?jǐn)?shù)和生命值,然后發(fā)請(qǐng)求,將數(shù)據(jù)保存
?
解釋完畢文章來源:http://www.zghlxwxcb.cn/news/detail-461040.html
?
到了這里,關(guān)于我的第一個(gè)項(xiàng)目(十四) :完成數(shù)據(jù)保存功能(前端,增查改接口)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!