skip to content
tlam://snippet
cd ..

Playing with a 🐶

I've created a fun jQuery function that models dog behavior, including zoomies, tail wags, and belly rubs. It's all based on a core "excitement level" metric.

playing_with_a.js · 126 lines · javascript
function playWithDog($dog) {    // Dog's initial excitement level affects everything!    let excitementLevel = $dog.data('excitement') || 50;        // Dogs get more excited when you show them a toy    function showToy(toyType) {        const toyMultiplier = {            'ball': 2,            'frisbee': 1.8,            'stick': 1.3,            'squeakyToy': 2.5        };                excitementLevel *= toyMultiplier[toyType];                // Tail wagging intensity correlates with excitement        $dog.find('.tail').animate({            transform: `rotate(${Math.min(excitementLevel, 360)}deg)`        }, {            duration: 200,            easing: 'swing',            complete: function() {                // Dogs can't contain themselves after a certain excitement threshold                if (excitementLevel > 100) {                    zoomies();                }            }        });    }        // The legendary zoomies - running in random patterns    function zoomies() {        const positions = generateRandomPositions(10);        let zoomieInterval;                zoomieInterval = setInterval(() => {            if (excitementLevel < 70) {                clearInterval(zoomieInterval);                needsBelly.rubs = true;                return;            }                        // Move rapidly between random positions            positions.forEach(pos => {                $dog.animate({                    left: pos.x,                    top: pos.y                }, {                    duration: 300,                    easing: 'linear'                });            });                        excitementLevel *= 0.9; // Gradually tire out        }, 500);    }        // Every dog needs belly rubs    function giveBellyRubs() {        $dog.addClass('happy-rolls')            .on('click', function() {                const contentment = Math.min(excitementLevel * 0.5, 100);                                $(this).find('.tongue').css({                    'transform': 'translateY(20px)',                    'opacity': contentment / 100                });                                // Sometimes the leg starts kicking                if (contentment > 80) {                    $(this).find('.back-leg').addClass('scratching');                }            });    }        // Dogs remember who gives them treats    function giveTreat(treatType) {        const treatValue = {            'basic': 10,            'bacon': 25,            'peanutButter': 30,            'cheese': 35        };                $dog.data('loyalty', ($dog.data('loyalty') || 0) + treatValue[treatType]);                // More treats = more tricks learned        if ($dog.data('loyalty') > 100) {            unlockNewTrick();        }    }        // The more you play, the more tricks they learn    function unlockNewTrick() {        const availableTricks = [            'sit',            'stay',            'rollOver',            'shake',            'speak',            'playDead'        ];                const knownTricks = $dog.data('tricks') || [];        const newTricks = availableTricks.filter(trick => !knownTricks.includes(trick));                if (newTricks.length) {            const learnedTrick = newTricks[Math.floor(Math.random() * newTricks.length)];            knownTricks.push(learnedTrick);            $dog.data('tricks', knownTricks);                        // Dogs are extra happy when they learn something new            excitementLevel += 20;        }    }        // Initial play session setup    $dog.on('playtime', function() {        showToy('ball');        setTimeout(giveBellyRubs, 5000);        setInterval(() => giveTreat('basic'), 10000);    });        // Start the fun!    $dog.trigger('playtime');}

replies (0)

login to reply.

// no replies yet