你画我猜小程序游戏源码
时间: 2023-12-17 11:04:15 浏览: 120
微信小程序 你画我猜 (源码)
5星 · 资源好评率100%
很抱歉,我之前的回答可能有误。以下是一个简单的微信小程序“你画我猜”的源代码示例:
index.wxml:
```
<view class="canvas-container">
<canvas canvas-id="myCanvas" bindtouchstart="startDraw" bindtouchmove="onDraw" bindtouchend="endDraw" style="width: 100%; height: 100%;"></canvas>
</view>
<view class="bottom-bar">
<button class="btn-clear" bindtap="clearCanvas">清空</button>
<button class="btn-guess" bindtap="guess">猜</button>
</view>
<view class="modal-mask {{modalShow ? 'show' : ''}}">
<view class="modal-content">
<view class="modal-header">猜画</view>
<view class="modal-body">
<view class="modal-picture">
<image src="{{picture}}" mode="aspectFit"></image>
</view>
<view class="modal-input">
<input type="text" placeholder="请输入你的答案" bindinput="onInput">
</view>
</view>
<view class="modal-footer">
<button class="btn-close" bindtap="closeModal">关闭</button>
<button class="btn-confirm" bindtap="confirm">确定</button>
</view>
</view>
</view>
```
index.js:
```
Page({
data: {
startX: 0,
startY: 0,
isDrawing: false,
modalShow: false,
picture: '',
answer: ''
},
startDraw: function (e) {
this.setData({
isDrawing: true,
startX: e.touches[0].x,
startY: e.touches[0].y
})
},
onDraw: function (e) {
if (!this.data.isDrawing) return
let context = wx.createCanvasContext('myCanvas')
context.setStrokeStyle('#000')
context.setLineWidth(5)
context.beginPath()
context.moveTo(this.data.startX, this.data.startY)
context.lineTo(e.touches[0].x, e.touches[0].y)
context.stroke()
this.setData({
startX: e.touches[0].x,
startY: e.touches[0].y
})
context.draw(true)
},
endDraw: function (e) {
this.setData({
isDrawing: false
})
},
clearCanvas: function () {
let context = wx.createCanvasContext('myCanvas')
context.clearRect(0, 0, 300, 300)
context.draw()
},
guess: function () {
let that = this
wx.canvasToTempFilePath({
canvasId: 'myCanvas',
success: function (res) {
that.setData({
picture: res.tempFilePath,
modalShow: true
})
}
})
},
onInput: function (e) {
this.setData({
answer: e.detail.value
})
},
closeModal: function () {
this.setData({
modalShow: false
})
},
confirm: function () {
if (!this.data.answer) {
wx.showToast({
title: '请输入答案',
icon: 'none'
})
} else {
wx.showToast({
title: '你猜对了',
icon: 'success'
})
this.setData({
modalShow: false
})
}
}
})
```
index.wxss:
```
.canvas-container {
position: relative;
width: 300px;
height: 300px;
border: 1px solid #000;
}
.bottom-bar {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: 20px;
}
.btn-clear,
.btn-guess {
padding: 8px 16px;
background-color: #333;
color: #fff;
border: none;
}
.modal-mask {
position: fixed;
z-index: 9999;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.5);
display: none;
justify-content: center;
align-items: center;
}
.modal-mask.show {
display: flex;
}
.modal-content {
background-color: #fff;
border-radius: 8px;
width: 80%;
max-width: 600px;
padding: 16px;
box-shadow: 0 0 8px rgba(0,0,0,0.2);
display: flex;
flex-direction: column;
}
.modal-header {
font-size: 24px;
font-weight: bold;
text-align: center;
margin-bottom: 16px;
}
.modal-body {
display: flex;
flex-direction: column;
align-items: center;
}
.modal-picture {
width: 200px;
height: 200px;
margin-bottom: 16px;
border: 1px solid #000;
display: flex;
justify-content: center;
align-items: center;
}
.modal-picture image {
width: 100%;
height: 100%;
}
.modal-input {
width: 100%;
}
.modal-input input {
width: 100%;
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
margin-top: 8px;
box-sizing: border-box;
}
.modal-footer {
display: flex;
justify-content: space-between;
margin-top: 16px;
}
.btn-close,
.btn-confirm {
padding: 8px 16px;
background-color: #333;
color: #fff;
border: none;
border-radius: 4px;
}
.btn-confirm {
background-color: #f00;
}
.btn-close:hover,
.btn-confirm:hover {
cursor: pointer;
}
```
阅读全文