匿名通过本文主要向大家介绍了微信开发,游戏等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
在这篇微信小程序开发教程中,我们将介绍如何使用微信小程序开发2048小游戏。
本文主要分为两个部分,小程序主体部分及小游戏页面部分
一、小程序主体部分
一个小程序主体部分由三个文件组成,必须放在项目的根目录,如下:

1. 小程序逻辑
App({
onLaunch: function() {
// Do something initial when launch. },
onShow: function() { // Do something when show. },
onHide: function() { // Do something when hide. },
globalData: 'I am global data'})2. 小程序公共设置
主要注册一个页面,即2048游戏主页
{
"pages":[
"pages/index/index"
],
"window":{
"navigationBarBackgroundColor":"#ffffff",
"navigationBarTextStyle":"#1AAD16",
"navigationBarTitleText":"2048游戏",
"backgroundColor":"#eeeeee",
"backgroundTextStyle":"light" },
"networkTimeout": {
"request": 10000,
"downloadFile": 10000 },
"debug": false
}二、小游戏页面部分
2048游戏小程序页面主要由以下文件组成。

1. 页面结构
其页面结构代码如下
<view class="container">
<view class="game-body">
<loading hidden="{{hidden}}">
加载中... </loading>
<view class="heading">
<text class="title">2048</text>
<view class="scores-container">
<view class="score-container">{{score}}</view>
<view class="best-container">{{highscore}}</view>
</view>
</view>
<view class="above-game">
<text class="game-intro">你能拿到2048吗?</text>
<text class="restart-button" bindtap="restart">新游戏</text>
</view>
<view class="game-container">
<view class="game-message game-{{over ? (win ? 'won' : 'over') : ''}}">
<text class="over-msg">{{overMsg}}</text>
<view class="lower">
<!-- <text class="keep-playing-button">继续</text> -->
<text class="retry-button" bindtap="restart">再试一次</text>
</view>
</view>
<view class="grid-container" bindtouchstart="touchStart" bindtouchmove="touchMove" bindtouchend="touchEnd">
<view wx:for="{{grids}}" wx:for-index="rowIdx" wx:for-item="row" class="grid-row">
<view wx:for="{{row}}" wx:for-index="colIdx" wx:for-item="cell" class="grid-cell">
<view class="tile tile-{{cell.value}}">
<view wx:if="{{cell}}" class="tile-inner">
{{cell.value}} </view>
</view>
</view>
</view>
</view>
</view>
<!-- <view class="game-explanation">
<view class="important">如何开始:</view> 手指上下左右滑动
</view> --></view></view>2. 样式表
样式代码如下所示
.container {
margin: 0;
padding: 20px 0;
background: #faf8ef;
color: #776e65;
font-family: "Helvetica Neue", Arial, sans-serif;
font-size: 18px;
}.heading:after {
content: "";
display: block;
clear: both;
}.title {
font-size: 80px;
font-weight: bold;
margin: 0;
display: block;
float: left;
}.scores-container {
float: right;
text-align: right;
}.score-container, .best-container {
position: relative;
display: inline-block;
background: #bbada0;
padding: 15px 25px;
font-size: 25px;
height: 25px;
line-height: 47px;
font-weight: bold;
border-radius: 3px;
color: white;
text-align: center;
margin: 8px 0 0 8px;
}.score-container:after, .best-container:after {
position: absolute;
width: 100%;
top: 10px;
left: 0;
text-transform: uppercase;
font-size: 13px;
line-height: 13px;
text-align: center;
color: #eee4da;
}.score-container .score-addition, .best-container .score-addition {
position: absolute;
right: 30px;
color: red;
font-size: 25px;
line-height: 25px;
font-weight: bold;
color: rgba(119, 110, 101, 0.9);
z-index: 100;
}.score-container:after {
content: "Score";
}.best-container:after {
content: "Best";
}p {
margin-top: 0;
margin-bottom: 10px;
line-height: 1.65;
}a {
color: #776e65;
font-weight: bold;
text-decoration: underline;
cursor: pointer;
}strong.important {
text-transform: uppercase;
}hr {
border: none;
border-bottom: 1px solid #d8d4d0;
margin-top: 20px;
margin-bottom: 30px;
}.game-container {
margin-top: 40px;
position: relative;
padding: 15px;
cursor: default;
-webkit-touch-callout: none;
-ms-touch-callout: none;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-ms-touch-action: none;
touch-action: none;
background: #bbada0;
border-radius: 6px;
width: 500px;
height: 500px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}.game-container .game-message { /*display: none;*/
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: rgba(238, 228, 218, 0.5);
z-index: 100;
text-align: center;
}.game-container .game-message p {
font-size: 60px;
font-weight: bold;
height: 60px;
line-height: 60px;
margin-top: 222px;
}.game-container .game-message .lower {
display: block;
margin-top: 59px;
}.game-container .game-message a {
display: inline-block;
background: #8f7a66;
border-radius: 3px;
padding: 0 20px;
text-decoration: none;
color: #f9f6f2;
height: 40px;
line-height: 42px;
margin-left: 9px;
}.game-container .game-message .keep-playing-button {
display: none;
}.game-container .game-message.game-won {
background: rgba(237, 194, 46, 0.5);
color: #f9f6f2;
}.game-container .game-message.game-won .keep-playing-button {
display: inline-block;
}.game-container .game-message.game-won, .game-container .game-message.game-over {
display: block;
}.grid-container {
position: absolute;
z-index: 1;
}.grid-row {
margin-bottom: 15px;
}.grid-row:last-child {
margin-bottom: 0;
}.grid-row:after {
content: "";
display: block;
clear: both;
}.grid-cell {
width: 106.25px;
height: 106.25px;
margin-right: 15px;
float: left;
border-radius: 3px;
background: rgba(238, 228, 218, 0.35);
}.grid-cell:last-child {
margin-right: 0;
}.tile-container {
position: absolute;
z-index: 2;
}.tile, .tile .tile-inner {
width: 107px;
height: 107px;
line-height: 107px;
}.tile.tile-position-1-1 {
-webkit-transform: translate(0px, 0px);
-moz-transform: translate(0px, 0px);
-ms-transform: translate(0px, 0px);
transform: translate(0px, 0px);
}.tile.tile-position-1-2 {
-w

