足球游戏:单击按钮时尝试使球向左或向右运动

时间:2019-05-03 20:50:30

标签: javascript

我在做足球比赛。当单击左按钮时,我希望球移至守门员的左侧,而当单击右按钮时,我希望球移至右侧的守门员。我希望守门员朝对方射门,与球碰撞以进行扑救。我想我很近但是很挣扎。这是我的主要脚本。

<script>
var ball = null;
var x = 1;
var score = 0;
var gameover;
document.getElementById('score').innerHTML = '0';
var animate;
ball = document.getElementById('ball');
var cheer = new sound('cheer.mp3')

function shootLeft(){
    var ran = Math.random();

    while(parseInt(ball.style.top) != 300){
        ball.style.left = parseInt(ball.style.left) - 1 + 'px';
        ball.style.top = parseInt(ball.style.top) - 1 + 'px';
        animate = setTimeout(shootLeft,20);
    }
    if(ran<0.3){
        score = score + 1;
        document.getElementById('score').innerHTML = score;
         {
            cheer.play();
            return;
        }

    }
    else{
        document.getElementById('score').innerHTML = score;
    }
    x++;
    if(x==10){
        document.getElementById('gameover').innerHTML = "Game over! You got a score of " + score;
    }
    ball.style.left = '300px';
    ball.style.top = '410px';
}

function shootRight()
{
    var ran = Math.random();

    while (parseInt(ball.style.top != 300))
    {
        ball.style.left = parseInt(ball.style.left) + 1 + 'px';
        ball.style.top = parseInt(ball.style.top) - 1 + 'px';
        animate = setTimeout(shootRight,20);
    }
    if(ran>0.7){
        score = score + 1;
        document.getElementById('score').innerHTML = score;
        {
            cheer.play();
            return;
        }
    }
    else{
        document.getElementById('score').innerHTML = score;
    }
    x++;
    if(x==10){
        document.getElementById('gameover').innerHTML = "Game over! You got a score of " + score;
    }
    ball.style.left = '300px';
    ball.style.top = '410px';
}
</script>

0 个答案:

没有答案