用乒乓球控制球速

时间:2017-03-23 13:14:12

标签: javascript pong

我在下面有这个代码,而且我很难解决这个问题。 在dotime功能上,我有球速:

/* HERE */
function dotime() {
    move1();
    if (myform != null) {
        myform.text3.value = display1();
        myform.score.value = "" + score;
    }
    /* ---Ball Speed--- */
    if (!oops_flag) timerID = setTimeout("dotime()", 190);
    /* ---trying to make ball speed faster--- */
    if (score == 1) {
        timerID = setTimeout("dotime()", 100 - 30);
    }
    timerRunning = true;
}

我试图让球移动得更快但是当我做第二次时#34;如果",球飞得太快了。

提前致谢, fufle。

完整代码:

var crlf = "\r\n";
var x = 0;
var y = 0;
var dx = 1;
var dy = 1;
var s = "";
var u = 0;
var oops_flag = false;
var score = 0;

function move1() {
    x += dx;
    if (x > 61) {
        x -= 2 * Math.abs(dx);
        if (dx > 0) dx = -dx;
    }
    if (x < 0) {
        x += 2 * Math.abs(dx);
        if (dx < 0) dx = -dx;
    }
    y += dy;
    if (y > 24) {
        y -= 2 * Math.abs(dy);
        if (dy > 0) dy = -dy;
        if (Math.abs(x - 2 * u - 1) > 2) {
            oops_flag = true;
        } else {
            score += 1;
        }
    }
    if (y < 0) {
        y += 2 * Math.abs(dy);
        if (dy < 0) dy = -dy;
    }
}

function display1() {
    var s1 = ""
    var i, j;
    if (oops_flag) return "                       Unlucky, Play again?"
    for (j = 0; j < 25; j++) {
        for (i = 0; i < 62; i++) {
            /* BALL */
            if (j == y && i == x) s1 += "";
            else s1 += " ";
        }
        s1 += crlf;
    }
    /* DEFENDER */
    var s2 = "";
    for (i = 0; i < 31; i++) {
        if (u == i) s2 += "▄▄▄▄▄";
        else s2 += "  ";
    }
    return (s1 + s2);
}
var timerID = null;
var timerRunning = false;
var myform;

function stopclock() {
    if (timerRunning) clearTimeout(timerID);
    timerRunning = false;
}

function startclock(form) {
    myform = form;
    oops_flag = false;
    score = 0;
    if (navigator.userAgent.indexOf("Mac") > 2) crlf = "\n";
    stopclock();
    dotime();
    // var id= setInterval(frameElement,10000);
}
/* HERE */
function dotime() {
    move1();
    if (myform != null) {
        myform.text3.value = display1();
        myform.score.value = "" + score;
    }
    if (!oops_flag) timerID = setTimeout("dotime()", 100);
    if (score == 1) {
        timerID = setTimeout("dotime()", 100 - 30);
    }
    timerRunning = true;
}

1 个答案:

答案 0 :(得分:0)

看起来你有两个计时器正在运行,所以你需要这样做才能运行它。

long

或使用三元运算符

if (!oops_flag) {
    var speed = 100;
    if (score===1) speed = 70;
    timerID = setTimeout(dotime, speed);
}