简体   繁体   English

HTML5 Javascript游戏

[英]HTML5 Javascript game

I'm trying to make a HTML5 game in javascript. 我正在尝试用javascript制作HTML5游戏。 Similar to pong, but once the bubble hits the paddle or bottom screen it "bursts" or disappears. 类似于乒乓球,但一旦气泡击中桨或底部屏幕,它就会“爆裂”或消失。 I cant seem to get the Bubble to disappear or burst in my code. 我似乎无法让我的代码中的Bubble消失或破灭。 Could anyone help me? 谁能帮助我?

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 batonMovement = 200;
var moveBatonRight = false;
var moveBatonLeft = false;
function startGame()
{
    var r,g,b;
    var canvas,color;
    //Initialize
    canvasColor = '#EAEDDC';
    x = 10;
    y = 10;
    radius = 10;
    clearScreen();
    counter=0;


    while (counter <100)
    {

                //make bubble appear randomly
                x=Math.floor(Math.random()*450)

                //Set up a random color
                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('drawForever()',50);
}

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

function drawForever()
{
    var canvas, pen;

    canvas = document.getElementById('myCanvas');
    pen = canvas.getContext('2d');
    steps +=1
    clearScreen();
    if (steps%20==0 && lastBubble < 100){
        bubbles[lastBubble].active=true;
        lastBubble +=1;
    }
    drawBaton();
    counter=0;


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

            bubbles[counter].y+=2;

            }

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

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

    }
}

function clearScreen()
{
    var canvas, pen;

    canvas = document.getElementById('myCanvas');
    pen = canvas.getContext('2d');
    pen.fillStyle = canvasColor;
    pen.fillRect(0,0,450,300);

}
function drawBaton()
{
    var canvas, pen;
    if (moveBatonLeft == true && batonMovement > 0)
    {
        batonMovement -= 20;
    }
    else if (moveBatonRight == true && batonMovement < 400)
    {
        batonMovement += 20;
    }
//draw Baton (rectangle)
    canvas = document.getElementById('myCanvas');
    pen = canvas.getContext('2d');
    pen.fillStyle = '#0000FF';

    pen.fillRect(batonMovement,250,50,10);

}
function moveLeft()
{
        moveBatonLeft=true;
}
function moveRight()
{
        moveBatonRight=true;
}
function stopMove()
{
    moveBatonLeft=false;
    moveBatonRight=false;
}

Below is the HTML code... 以下是HTML代码......

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Bubble Burster</title>
        <script type="text/javascript" src="project.js"></script>
    </head>
    <body onload="startGame();">
        <div style="text-align:center;">
            <h2>Bubble Burster</h2>
            <canvas id="myCanvas" width="450" height="300" style="border:5px solid #000000; background-color: #f1f1f1;">
                Your browser does not support the canvas element.
            </canvas><br>
            <button onmousedown="moveLeft();" onmouseup="stopMove();">LEFT</button>
            <button onmousedown="moveRight();" onmouseup="stopMove();">RIGHT</button><br><br>
            <form>
                <input type="radio" name="difficulty" value="easy" onclick="check(this.value);" checked> Easy &nbsp;&nbsp;
                <input type="radio" name="difficulty" value="moderate" onclick="check(this.value)"> Moderate&nbsp;&nbsp;
                <input type="radio" name="difficulty" value="hard" onclick="check(this.value)"> Hard  <br><br>
            </form>
            <span id="burst">Burst:</span> &nbsp;&nbsp;
            <span id="escaped">Escaped: </span> &nbsp;&nbsp;
            <span id="steps">Steps elapsed:</span> &nbsp;&nbsp;
            <h3 id="output"></h3>
        </div>
    </body>
</html>

You are referring to x , y which are not defined in the drawForever function. 您指的是未在drawForever函数中定义的xy The global values of x and y may not refer to the right bubble. xy的全局值可能不是指正确的泡沫。 In fact y is set to 10 in the startGame function and never altered subsequently. 实际上,在startGame函数中y被设置为10,并且随后不会改变。 You also probably want to refer to bubbles[counter] rather than bubbles[lastBubble] . 你也可能想要引用bubbles[counter]而不是bubbles[lastBubble] The game seems to work if you make the changes below, referring to bubbles[counter].x and bubbles[counter].y instead of x and y . 如果您在下面进行更改,游戏似乎有效,指的是bubbles[counter].xbubbles[counter].y而不是xy

function drawForever() {
    var canvas, pen;

    canvas = document.getElementById('myCanvas');
    pen = canvas.getContext('2d');
    steps += 1
    clearScreen();
    if (steps % 20 == 0 && lastBubble < 100) {
        bubbles[lastBubble].active = true;
        lastBubble += 1;
    }
    drawBaton();
    counter = 0;


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

            bubbles[counter].y += 2;

        }
        y = bubbles[counter].y;    // ADDED (y was not defined in original code)
        x = bubbles[counter].x;    // ADDED (x was not defined in original code)
        if (y >= 240 && y <= 270 && x >= batonMovement - 10 && x <= batonMovement + 60) {
            bubbles[counter] = false;   // ALTERED (burst the current bubble, not whatever lastBubble refers to

        } else if (y >= 450) {
            bubbles[counter] = false;   // ALTERED (burst the current bubble, not whatever lastBubble refers to
        }
        counter += 1;

    }
}

https://jsfiddle.net/acLot87q/4/ https://jsfiddle.net/acLot87q/4/

You should also make x and y local variables within each function; 您还应该在每个函数中创建xy局部变量; they are not currently serving the purpose that you seem to think. 他们目前没有达到你想象中的目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM