Pong Demo Experiment Run Experiment

If you are a registered user and signed in, you can here copy this script and its stimuli to your own account, where you can edit it and change it in any way you want.

It is absolutely free to register (no credit card info asked!). You can then instantly copy this experiment with one click and edit it, change its accompanying texts, its landing page, stimuli, etc. Invite your colleagues, friends, or students to check out your experiment. There is no limit on how many people can do your experiment, even with a free account.

The catch? There is no catch! Just keep in mind that with a free account, you cannot collect data. For teaching that is usually not a problem. For research, prepaid data collection (unlimited subjects) starts as low as €10.00 for a 10-day period.

var dx = 1, dy = 1.3, frames = 1, e, stop = false, score = 0, foul = 0,
    field = new Box('white','black','black'),
    score_block = field.addblock(20,10,20,20).text("0").style("color","grey")
                       .style("font-size","5em"),
    foul_block = field.addblock(70,10,20,20).text("0").style("color","tomato")
                       .style("font-size","5em"),
    ball = field.addblock(10,0,5,5,"grey"),
    bat = field.addblock(45,95,20,5,"darkgrey");

preload_sound("pong.mp3");
preload_sound("bat.mp3");
preload_sound("miss.mp3");

on(document,"keydown",function(e) {
    switch (e.keyCode)
    {
        case keys.LEFT_ARROW: if (bat.left >= 3) {bat.move(-3,0)}; break;
        case keys.RIGHT_ARROW: if (bat.left <= 100 - 22) {bat.move(3,0)}; break;
        case keys.ESCAPE: stop = true; break;
    }
});

function collide() {
    return ball.top >= bat.top-5 && ball.left >= bat.left && ball.left < bat.left + 20;
}

function bounce(d) {
    play("pong.mp3");
    return -d;
}

while (1) {
    await(17*frames);
    ball.move(dx,dy);
    dx = ball.left < 1 || ball.left > 94? bounce(dx) : dx;
    dy = ball.top < 1 || ball.top > 94 ? bounce(dy) : dy;
    if (collide() && dy > 0) {
        dy = -dy;
        play("bat.mp3");
        score_block.text(++score);
    }
    else if (ball.top > 94) {
        foul_block.text(++foul);
        play("miss.mp3");
    }
    if (stop) {
        break;
    }
}