硬币

硬币测试

适配设备:RW

作者:dengyuhang

下载量:12 · 点赞:1

可信源码风险提示:保存或下载前请阅读源码和权限声明。
SHA-256607b7ca450aa32b5bfa4ff134608bff1d2adcded704b1b8d9ebac68e8470605d
let stats = { wins: 0, losses: 0 };
let isFlipping = false;
let currentResult = null;
let userChoice = null;
let message = '请猜硬币正反面';

async function main() {
    try {
        const saved = await script.data.get('coin_stats', null);
        if (saved) {
            stats = saved;
        }
    } catch (e) {
        console.error('读取数据失败', e);
    }
    drawUI();
}

function drawUI() {
    ui.render([
        ui.heading('猜硬币', { align: 'center', lines: 1 }),
        ui.spacer(16),
        ui.column([
            ui.text(isFlipping ? '🪙 翻转中...' : (currentResult ? (currentResult === 'heads' ? '🪙 正面' : '🪙 反面') : '🪙 准备'), { size: 36, align: 'center', lines: 1 }),
            ui.text(message, { align: 'center', color: currentResult === userChoice ? '#1a8f38' : (currentResult ? '#ff453a' : '#888888'), lines: 2 })
        ], { background: '#1c1c1e', radius: 8, height: 140, justify: 'center', padding: 10 }),
        ui.spacer(16),
        ui.row([
            ui.text(`胜: ${stats.wins}`, { flex: 1, align: 'center', color: '#888888', lines: 1 }),
            ui.text(`负: ${stats.losses}`, { flex: 1, align: 'center', color: '#888888', lines: 1 })
        ]),
        ui.spacer(16),
        ui.row([
            ui.button('猜正面', () => play('heads'), { flex: 1, tone: 'primary', height: 56, disabled: isFlipping }),
            ui.button('猜反面', () => play('tails'), { flex: 1, tone: 'neutral', height: 56, disabled: isFlipping })
        ], { gap: 10 }),
        ui.spacer(20),
        ui.button('重置记录', async () => {
            if (isFlipping) return;
            const res = await dialog.confirm({ title: '重置', message: '确定要清空胜负记录吗?' });
            if (res.action === 'confirm') {
                stats = { wins: 0, losses: 0 };
                currentResult = null;
                userChoice = null;
                message = '请猜硬币正反面';
                await script.data.set('coin_stats', stats);
                drawUI();
            }
        }, { tone: 'neutral', height: 48 })
    ]);
}

function play(choice) {
    if (isFlipping) return;
    isFlipping = true;
    userChoice = choice;
    currentResult = null;
    message = '正在翻转...';
    drawUI();

    setTimeout(async () => {
        const result = Math.random() < 0.5 ? 'heads' : 'tails';
        isFlipping = false;
        currentResult = result;
        
        if (result === userChoice) {
            stats.wins++;
            message = '🎉 恭喜你,猜对了!';
            if (script.canUse('@system.vibrator.vibrate')) {
                system.vibration.vibrate({ mode: 'short' });
            }
        } else {
            stats.losses++;
            message = '😢 很遗憾,猜错了。';
        }
        
        try {
            await script.data.set('coin_stats', stats);
        } catch (e) {
            console.error('保存数据失败', e);
        }
        
        drawUI();
    }, 1200);
}

return main();