首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HTML5/Javascript游戏

HTML5/Javascript游戏
EN

Stack Overflow用户
提问于 2017-05-13 02:01:15
回答 1查看 39关注 0票数 0

我目前正在做一个游戏,我需要一堆随机的气泡落下来,然后在它下面的一个矩形可以移动来“击破”这些气泡。我目前有一个不会丢弃这些气泡的代码。有没有人能告诉我哪里出错了?下面是我的代码:

代码语言:javascript
复制
var canvasColor;
var x,y,radius,color;
var x=50, y=30;
var bubbles= [];
var counter;
var lastBubble=0;
var steps=0, burst=0, escaped=0;
var xx=200; 
var moveRectangleRight=false;
var moveRectangleLeft=false;


function startGame()
{
    var r, g, b;
    canvasColor= '#f1f1f1';
    x=10;
    y=10;
    radius=10;
    clearScreen();
    counter=0;

    while (counter<100)
    {
        x= Math.floor(450*Math.random()+1);

        r = Math.floor(Math.random()*256);
        g = Math.floor(Math.random()*256);
        b = Math.floor(Math.random()*256);

        color='rgb('+r+','+g+','+b+')';

        bubbles[counter]=new Bubble(x,y,radius,color);
        counter+=1;
    }
    setInterval('drawEverything()',50); 
}

function Bubble(x,y,radius,color)
{   
        this.x=x;
        this.y=y;
        this.radius=radius;
        this.color=color;
        this.active=false;
}

function drawEverything()
{   
    var canvas=document.getElementById('myCanvas');
    var context= canvas.getContext('2d');

    steps+=1;
    clearScreen();

    if (steps%20==0 && lastBubble <100) {
        bubbles[lastBubble].active=true;
        lastBubble+=1;
    }

    drawRectangle();
    counter=0;

    while(counter<100)
        {
        if (bubbles[counter].active==true) {
            context.fillStyle= bubbles[counter].color;
            context.beginPath();
            context.arc(bubbles[counter].x, bubbles[counter].y,
                        bubbles[counter.radius], 0, 2*Math.PI);
            context.fill();
            bubbles[counter].y+=2;
        }

        y=bubbles[counter].y;
        x=bubbles[counter].x;

        if (y>=240 && y<=270 && x>=xx-10 && x<=xx+60)
        {
            bubbles[counter]=false;
        }


        else if (y>=450)
        {
            bubbles[counter]=false;
        }
        counter+=1;
    }
}

function clearScreen ()
{
    var canvas=document.getElementById('myCanvas');
    var context= canvas.getContext('2d');
    context.fillStyle= canvasColor;
    context.fillRect(0,0,450,300);
}


function drawRectangle()
{   var canvas, context;

    if (moveRectangleRight==true && xx<400){
        xx+=20;
    }

    if (moveRectangleLeft==true && xx>0){
        xx-=20;
    }

    canvas=document.getElementById('myCanvas');
    context= canvas.getContext('2d');
    context.fillStyle = 'blue';
    context.fillRect(xx,250,50,10);

}

function moveLeft () {
    moveRectangleLeft=true;
}

function moveRight() {
    moveRectangleRight=true;
}

function stopMove () {
    moveRectangleRight=false;
    moveRectangleLeft=false;
}
EN

回答 1

Stack Overflow用户

发布于 2017-05-13 02:43:14

支架位于错误的位置,这解决了问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43943921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档