vue 小游戏 案例

最终效果:

game.vue中所有代码:

<template>
    <div>
      <div id="app">
        <div class="img-box" v-bind:class="{imgburst:ended}"></div>
        <div v-show="!ended">生命值剩余:{{health}} %</div>
        <div v-show="ended">你赢了!</div>
        <div class="progress">
          <div class="progress-child" v-bind:style="{width: health + '%'}"></div>
        </div>
        <div class="button-box">
          <button class="btn1" v-on:click="blow" v-bind:class="{disabled: ended}" >使劲敲</button>
          <button v-on:click="restart">重新开始</button>
        </div>
      </div>
    </div>
</template>

<script>
    export default {
        name: "game",
      data(){
          return{
            health: 100,
            ended: false
          }
      },
      methods: {
        blow: function(){
          this.health -= 10;
          if(this.health <= 0){
            this.ended = true;
            this.health = 0
          }
          // console.log(this.health,this.ended)
        },
        restart: function(){
          this.health = 100;
          this.ended = false;
          // console.log(this.health,this.ended)
        }
      }
    }
</script>

<style scoped>
  * {
    margin: 0;
    padding: 0;
    font-family: "微软雅黑"
  }
  em,i {
    font-style: normal
  }
  li {
    list-style: none
  }
  img {
    border: 0;
    vertical-align: middle
  }
  button {
    cursor: pointer
  }
  a {
    color: #666;
    text-decoration: none
  }
  a:hover {
    color: #c81623
  }
  div{
    text-align: center;
    margin-bottom: 5px;
  }
  .img-box{
    width: 200px;
    height: 539px;
    margin: 0 auto;
    background: url("../../assets/img/bag.jpg") no-repeat;
  }
  .imgburst{
    background: url("../../assets/img/bag2.jpg") no-repeat;
  }
  .progress{
    width: 200px;
    height: 20px;
    margin: 0 auto;
    overflow: hidden;
    background: #fff;
    border-radius: 5px;
    border: 2px solid red;
  }
  .progress-child{
    width: 100px;
    height: 20px;
    background: red;
  }
  .button-box{
    width: 213px;
    margin: 20px auto;
    overflow: hidden;
  }
  button{
    padding: 10px 20px;
    margin-left: 10px;
    border-radius: 5px;
    border: 1px solid #999;
    background: #e5ffc9;
  }
  button:hover,button:focus{
    outline: none;}
  button:hover{
    background: #4488ff;
    border-color: #4488ff;
    color: #fff;
  }
  .btn1:hover{
    background: red;
    border-color: red;
  }
  button.disabled{
    cursor: not-allowed;
    background: #999
  }

</style>

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值