收藏
回答

开发对战功能,第一次获取微信好友信息后,一定要获取和保存好友信息到本地数据库比较好吗?

框架类型 问题类型 操作系统 操作系统版本 手机型号 微信版本
小游戏 需求 Android 13 红米K50pro 6.5.2

开发对战功能,第一次获取微信好友信息后,一定要获取和保存好友信息到本地数据库比较好吗?设计思路是怎样的?请大佬给指导指导。

import Button from './runtime/button.js' export default class FriendGame { constructor(game) { this.game = game; this.canvas = game.canvas; this.ctx = game.ctx; this.initButtons(); this.topFriends = this.getTopFriends(); this.currentState = 'waiting'; // 'waiting', 'choosing', 'inviting', 'waitingFriend', 'result' this.playerChoice = null; this.friendChoice = null; } initButtons() { const buttonWidth = 200; const buttonHeight = 50; const buttonX = (this.canvas.width - buttonWidth) / 2; const buttonY = 100; this.inviteButton = new Button( this.ctx, buttonX, buttonY, buttonWidth, buttonHeight, '邀好友,猜猜猜', () => this.startChoosing() ); // 添加猜拳按钮 const gameButtonWidth = 80; const gameButtonHeight = 40; const gameButtonSpacing = 20; const gameButtonY = this.canvas.height / 2; this.gameButtons = [ new Button(this.ctx, this.canvas.width / 2 - gameButtonWidth * 1.5 - gameButtonSpacing, gameButtonY, gameButtonWidth, gameButtonHeight, '石头', () => this.makeChoice(1)), new Button(this.ctx, this.canvas.width / 2 - gameButtonWidth / 2, gameButtonY, gameButtonWidth, gameButtonHeight, '剪刀', () => this.makeChoice(2)), new Button(this.ctx, this.canvas.width / 2 + gameButtonWidth / 2 + gameButtonSpacing, gameButtonY, gameButtonWidth, gameButtonHeight, '布', () => this.makeChoice(3)) ]; // 添加退出按钮 const exitButtonWidth = 80; const exitButtonHeight = 40; const exitButtonX = this.canvas.width - exitButtonWidth - 20; const exitButtonY = this.canvas.height - exitButtonHeight - 20; this.exitButton = new Button( this.ctx, exitButtonX, exitButtonY, exitButtonWidth, exitButtonHeight, '退出', () => this.game.exitToHome() ); } getTopFriends() { // 这里应该从服务器获取 top 5 好友列表 // 现在我们用模拟数据 return [ { name: '张三', avatar: 'path/to/avatar1.png' }, { name: '李四', avatar: 'path/to/avatar2.png' }, { name: '王五', avatar: 'path/to/avatar3.png' }, { name: '赵六', avatar: 'path/to/avatar4.png' }, { name: '孙七', avatar: 'path/to/avatar5.png' }, ]; } render() { this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height); // 渲染标题 this.ctx.fillStyle = 'black'; this.ctx.font = 'bold 24px Arial'; this.ctx.textAlign = 'center'; this.ctx.fillText('好友猜猜猜', this.canvas.width / 2, 50); switch (this.currentState) { case 'waiting': this.inviteButton.render(); this.renderFriendList(); break; case 'choosing': this.ctx.fillText('请选择你的出拳', this.canvas.width / 2, 100); this.gameButtons.forEach(button => button.render()); break; case 'inviting': this.ctx.fillText('正在邀请好友...', this.canvas.width / 2, 100); break; case 'waitingFriend': this.ctx.fillText('等待好友回应...', this.canvas.width / 2, 100); break; case 'result': this.renderResult(); break; } // 渲染退出按钮 this.exitButton.render(); // 渲染版权信息 this.game.renderCopyright(); } renderFriendList() { const startY = 200; const itemHeight = 60; const avatarSize = 40; this.ctx.font = '16px Arial'; this.ctx.textAlign = 'left'; this.topFriends.forEach((friend, index) => { const y = startY + index * itemHeight; // 渲染头像(这里用颜色块代替) this.ctx.fillStyle = `hsl(${index * 60}, 70%, 70%)`; this.ctx.fillRect(50, y, avatarSize, avatarSize); // 渲染名字 this.ctx.fillStyle = 'black'; this.ctx.fillText(friend.name, 100, y + avatarSize / 2); }); } handleTouch(x, y) { switch (this.currentState) { case 'waiting': if (this.inviteButton.isPointInside(x, y)) { this.inviteButton.onClick(); } else { this.checkFriendListTouch(x, y); } break; case 'choosing': this.gameButtons.forEach(button => { if (button.isPointInside(x, y)) { button.onClick(); } }); break; } if (this.exitButton.isPointInside(x, y)) { this.exitButton.onClick(); } } checkFriendListTouch(x, y) { const startY = 200; const itemHeight = 60; this.topFriends.forEach((friend, index) => { const itemY = startY + index * itemHeight; if (y >= itemY && y < itemY + itemHeight && x >= 50 && x < this.canvas.width - 50) { this.startGameWithFriend(friend); } }); } startChoosing() { this.currentState = 'choosing'; } makeChoice(choice) { this.playerChoice = choice; this.currentState = 'inviting'; this.inviteFriend(); } inviteFriend() { const choices = ['', '石头', '剪刀', '布']; wx.shareAppMessage({ title: `我选择了${choices[this.playerChoice]},来猜猜看吧!`, imageUrl: 'path/to/your/share/image.png', // 替换为你的分享图片路径 success: (res) => { if (res.shareTickets && res.shareTickets.length > 0) { // 用户选择了好友 console.log('邀请好友成功'); this.currentState = 'waitingFriend'; // 这里应该启动一个等待好友回应的计时器或监听器 } }, fail: (res) => { console.log('邀请好友失败', res); this.currentState = 'waiting'; // 返回初始状态 } }); } renderResult() { const choices = ['', '石头', '剪刀', '布']; this.ctx.fillText(`你的选择: ${choices[this.playerChoice]}`, this.canvas.width / 2, 100); this.ctx.fillText(`好友的选择: ${choices[this.friendChoice]}`, this.canvas.width / 2, 140); let result; if (this.playerChoice === this.friendChoice) { result = '平局'; } else if ( (this.playerChoice === 1 && this.friendChoice === 2) || (this.playerChoice === 2 && this.friendChoice === 3) || (this.playerChoice === 3 && this.friendChoice === 1) ) { result = '你赢了!'; } else { result = '你输了!'; } this.ctx.fillText(`结果: ${result}`, this.canvas.width / 2, 180); } startGameWithFriend(friend) { console.log(`开始与 ${friend.name} 的游戏`); // 这里应该实现开始游戏的逻辑 } }
回答关注问题邀请回答
收藏
登录 后发表内容