[笔记]微信小程序开发《番外》骰子 小游戏

简介: [笔记]微信小程序开发《番外》骰子 小游戏

前言

  • 微信小程序框架中的单向数据绑定应用
  • 微信小程序框架中按钮组件的使用以及的事件绑定技术
  • 微信小程序框架中视图模版的定义与使用
  • CSS3 flex弹性布局(参考的是阮一峰前辈的Flex教程一文中的骰子实例)

骰子 小游戏

创建一个默认小程序项目

Page/Index

index.js

Page({
  global : {
      timer : null ,
      isRand : false
  },
  data: {
    dice : ['first','second','third','fourth','fifth','sixth'],
    buttonType : 'primary',
    buttonValue : '摇一摇',
    num : 0
  },
  shake : function () { 
    let me = this;
    this.global.isRand = !this.global.isRand;
    if ( this.global.isRand ) {
      this.global.timer = setInterval(function (){
        let num = Math.floor(Math.random()*6);
        me.setData({num : num});
      },50);
    } else {
      clearInterval(this.global.timer);
    }
    this.setData({
      dice: this.data.dice, 
      buttonType : (this.global.isRand) ? 'default' : 'primary',
      buttonValue : (this.global.isRand) ? '点击停止' : '摇一摇',
    });
  },
})

index.wxml

<!-- 骰子模版视图 -->
<template name="first">
    <view class="first face">
        <span class="pip"></span>
    </view>
</template>
<template name="second">
    <view class="second face">
        <span class="pip"></span>
        <span class="pip"></span>
    </view>
</template>
<template name="third">
    <view class="third face">
        <span class="pip"></span>
        <span class="pip third-item-center"></span>
        <span class="pip"></span>
    </view>
</template>
<template name="fourth">
    <view class="fourth face">
        <view class="column">
            <span class="pip"></span>
            <span class="pip"></span>
        </view>
        <view class="column">
            <span class="pip"></span>
            <span class="pip"></span>
        </view>
    </view>
</template>
<template name="fifth">
    <view class="fifth face">
        <view class="column">
            <span class="pip"></span>
            <span class="pip"></span>
        </view>
        <view class="column fifth-column-center">
            <span class="pip"></span> 
        </view> 
        <view class="column">
            <span class="pip"></span>
            <span class="pip"></span>
        </view>
    </view>
</template>
<template name="sixth">
    <view class="sixth face">
        <view class="column">
            <span class="pip"></span>
            <span class="pip"></span>
            <span class="pip"></span>
        </view> 
        <view class="column">
            <span class="pip"></span>
            <span class="pip"></span>
            <span class="pip"></span>
        </view>
    </view>
</template>
<!--index.wxml-->
<view class="flex-container"> 
    <view class="dice-box">
        <block><template is="{{dice[num]}}"></template></block>
    </view>
    <view class="button-box">
        <button type="{{buttonType}}" size="mini" bindtap="shake" >{{buttonValue}}</button>
    </view>
</view>

index.wxss

.flex-container{
  display:flex;
  height: 100vh;
  background-color:#292929;
  flex-direction : column;
  justify-content: center;
  align-items: center;
  color: #fff;
}
.dice-box{
  padding: 10px;
} 
/* 筛子容器公用样式 */
.face {
  display: flex;
  margin: 5px;
  padding: 4px;
  background-color:#e7e7e7;
  width: 104px;
  height: 104px;
  object-fit: contain;
  border-radius:10%;
  box-shadow: inset 0 5px white,
              inset 0 -5px #bbb,
              inset 5px 0 #d7d7d7,
              inset -5px 0 #d7d7d7;
}
/* 筛子中的点的样式 */
.pip {
  display: block;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  margin:4px;
  background-color: #333;
  box-shadow: inset 0 3px #111,
              inset 0 -3px #555;
}
/* 面公用样式 */
/* 第一面布局方式 */
.first {
  justify-content: center;
  align-items: center;
}
/* 第二面布局方式 */
.second {
  justify-content: space-between;
}
.second .pip:last-child {
  align-self: flex-end;
}
/* 第三面布局方式 */
.third {
  justify-content: space-between;
}
.third .pip.third-item-center {
  align-self: center; 
}
.third .pip:last-child {
  align-self: flex-end;
}
/* 第四面布局方式 */
.fourth {
  justify-content: space-between; 
}
.fourth .column {
  display: flex;
  flex-direction: column;
  justify-content: space-between; 
}
/* 第五面布局方式 */
.fifth {
  justify-content: space-between; 
}
.fifth .column {
  display: flex;
  flex-direction: column;
  justify-content: space-between; 
}
.fifth .column.fifth-column-center{
  justify-content: center;
}
/* 第六面布局方式 */
.sixth { 
  justify-content:space-between;
}
.sixth .column {
  display: flex;
  flex-direction:column;
  justify-content:space-around;
}

效果图


相关文章
|
11天前
|
存储 编解码 小程序
抖音小程序开发中遇见的坑点
在抖音小程序开发中,需注意10大坑点:遵守小程序限制与规范;解决兼容性问题;优化数据加载速度;适应分享功能限制;处理视频播放挑战;优化图片加载显示;管理资源文件;提升用户体验;考虑安全性;及时更新维护。通过测试、优化和遵循官方文档,可克服这些问题,打造优质小程序。
|
21天前
|
小程序 前端开发 API
小程序全栈开发中的多端适配与响应式布局
【4月更文挑战第12天】本文探讨了小程序全栈开发中的多端适配与响应式布局。多端适配涉及平台和设备适应,确保统一用户体验;响应式布局利用媒体查询和弹性布局维持不同设备的布局一致性。实践中,开发者可借助跨平台框架实现多平台开发,运用响应式布局技术适应不同设备。同时,注意兼容性、性能优化和用户体验,以提升小程序质量和用户体验。通过这些方法,开发者能更好地掌握小程序全栈开发。
|
21天前
|
小程序 前端开发 API
微信小程序全栈开发中的异常处理与日志记录
【4月更文挑战第12天】本文探讨了微信小程序全栈开发中的异常处理和日志记录,强调其对确保应用稳定性和用户体验的重要性。异常处理涵盖前端(网络、页面跳转、用户输入、逻辑异常)和后端(数据库、API、业务逻辑)方面;日志记录则关注关键操作和异常情况的追踪。实践中,前端可利用try-catch处理异常,后端借助日志框架记录异常,同时采用集中式日志管理工具提升分析效率。开发者应注意安全性、性能和团队协作,以优化异常处理与日志记录流程。
|
3天前
|
小程序 前端开发 JavaScript
轻松学会微信小程序开发(一)
轻松学会微信小程序开发(一)
|
4天前
|
JSON 小程序 JavaScript
微信小程序开发1
微信小程序开发1
|
10天前
|
数据采集 存储 人工智能
【Python+微信】【企业微信开发入坑指北】4. 企业微信接入GPT,只需一个URL,自动获取文章总结
【Python+微信】【企业微信开发入坑指北】4. 企业微信接入GPT,只需一个URL,自动获取文章总结
26 0
|
10天前
|
人工智能 机器人 API
【Python+微信】【企业微信开发入坑指北】3. 如何利用企业微信API给微信群推送消息
【Python+微信】【企业微信开发入坑指北】3. 如何利用企业微信API给微信群推送消息
12 0
|
10天前
|
缓存 人工智能 API
【Python+微信】【企业微信开发入坑指北】2. 如何利用企业微信API主动给用户发应用消息
【Python+微信】【企业微信开发入坑指北】2. 如何利用企业微信API主动给用户发应用消息
9 0
|
10天前
|
XML 人工智能 数据安全/隐私保护
【Python+微信】【企业微信开发入坑指北】1. 数据链路打通:接收用户消息处理并回复
【Python+微信】【企业微信开发入坑指北】1. 数据链路打通:接收用户消息处理并回复
8 0
|
10天前
|
人工智能 算法 API
【Python+微信】【企业微信开发入坑指北】0. 创建自建应用并接入自己的服务
【Python+微信】【企业微信开发入坑指北】0. 创建自建应用并接入自己的服务
11 0
【Python+微信】【企业微信开发入坑指北】0. 创建自建应用并接入自己的服务