在画布上,一种方法可以使多个苹果落入游戏中?

时间:2015-04-25 20:17:17

标签: javascript html5 canvas

我正在创建一个游戏(使用HTML5画布),其中包括捕捉落苹果,我知道,它是多么原创!我很难找到一种方法让它变成多个苹果掉下来?

以下是JSFiddle的链接:https://jsfiddle.net/pgkL09j7/14/

var apple_x = 100;
var apple_y = 0;
var basket_x = 100;
var basket_y = 100;
var points = 0;

var basket_img = new Image();
basket_img.src = "http://s18.postimg.org/h0oe1vj91/basket.png";

var Countable = function() {}

     //Background colour of canvas
var c = document.getElementById("c");
var ctx = c.getContext("2d");
ctx.fillStyle = "#000";
ctx.fillRect(0, 0, 500, 500);

     //Here is the event listener
c.addEventListener("mousemove", seenmotion, false);



                        //////////////////////

      function seenmotion(e) {

            //This is the code for the mouse 
            //moving over the canvas.
       var bounding_box = c.getBoundingClientRect();
       basket_x = (e.clientX - bounding_box.left) * (c.width / bounding_box.width) - basket_img.width / 2;
       basket_y = (e.clientY - bounding_box.top) * (c.height / bounding_box.height) - basket_img.height / 2;
                        }

                        function start_game() {
                            setInterval(game_loop, 50);
                        }


                        function game_loop() {
                            // The code above is called every 50ms and is a 
                            // frame-redraw-game-animation loop.

                            c.width = c.width;

                            // Below is the code that draws the objects
                            draw_apple(apple_x, apple_y);
                            draw_basket(basket_x, basket_y);

                            // Below is the code that updates the balloons location
                            apple_y++;
                            if (apple_x > c.width) {
                                apple_x = 0;
                            }

                            //Here is the collision detection code
                            if (collision(apple_x, apple_y, basket_x, basket_y)) {
                                points -= 0.5;
                            }


                            //Here is the code for the point system
                            points += 1;

                            // and let's stick it in the top right. 
                            var integerpoints = Math.floor(points); // make it into an integer
                            ctx.font = "bold 24px sans-serif";
                            ctx.fillText(integerpoints, c.width - 50, 50);
                        }



                        context.clearRect(0, 0, 500, 500);

                        function collision(basket_x, basket_y, apple_x, apple_y) {
                            if (apple_y + 85 < basket_y) {
                                return false;
                            }
                            if (apple_y > basket_y + 91) {
                                return false;
                            }
                            if (apple_x + 80 < basket_x) {
                                return false;
                            }
                            if (apple_x > basket_x + 80) {
                                return false;
                            }

                            return true;
                        }

                        // Code to stop the game when we're finished playing
                        function stop_game() {

                        }

                        //Code for the ball
                        function draw_apple(x, y) {
                            var apple_img = new Image();
                            apple_img.src = "http://s15.postimg.org/3nwjmzsiv/apple.png";
                            ctx.drawImage(apple_img, x, y);

                        }

                        //Code for the basket
                        function draw_basket(x, y) {
                            ctx.drawImage(basket_img, x, y);

                        }

3 个答案:

答案 0 :(得分:2)

我在上一个问题上贴了这个,所以我在这里重复一遍。您需要维护一系列苹果,但您还需要查看requestAnimationFrame以提高性能。对你来说事情会变得很糟糕,当你移动水桶时,你可能已经注意到了它。我已经修改过您的小提琴,以准确演示如何修改程序以支持多个苹果以不同的速度下降。 (将apples_per_level设置为2或更多以立即看到多个苹果 - 或者只是玩游戏,并观察它们的累积!)。

https://jsfiddle.net/h82gv4xn/

改进包括:

  • 固定记分牌
  • 添加级别进度(级别每10个苹果增加)
  • 在屏幕上允许更多苹果(播放到第9级)。
  • 随着水平的增加,苹果会以不同的速度下降并加速。
  • 使用动画帧系统更多更流畅的动画。
  • 放松的碰撞处理(桶的中心必须接触苹果)

随着关卡的升级,这一切都变得非常愚蠢,但它应该是一个很好的例子来改进。相关的javascript如下(这将进入你的onLoad函数):

var game = create_game();
game.init();

function create_game() {
    debugger;
    var level = 1;
    var apples_per_level = 1;
    var min_speed_per_level = 1;
    var max_speed_per_level = 2;
    var last_apple_time = 0;
    var next_apple_time = 0;
    var width = 500;
    var height = 500;
    var delay = 1000;
    var item_width = 50;
    var item_height = 50;
    var total_apples = 0;
    var apple_img = new Image();
    var apple_w = 50;
    var apple_h = 50;
    var basket_img = new Image();
    var c, ctx;

    var apples = [];
    var basket = {
        x: 100,
        y: 100,
        score: 0
    };

    function init() {
        apple_img.src = "http://s15.postimg.org/3nwjmzsiv/apple.png";
        basket_img.src = "http://s18.postimg.org/h0oe1vj91/basket.png";

        level = 1;
        total_apples = 0;
        apples = [];

        c = document.getElementById("c");
        ctx = c.getContext("2d");
        ctx.fillStyle = "#000";
        ctx.fillRect(0, 0, 500, 500);

        c.addEventListener("mousemove", function (e) {
            //moving over the canvas.
            var bounding_box = c.getBoundingClientRect();
            basket.x = (e.clientX - bounding_box.left) * (c.width / bounding_box.width) - basket_img.width / 2;
            basket.y = (e.clientY - bounding_box.top) * (c.height / bounding_box.height) - basket_img.height / 2;
        }, false);

        setupApples();
        requestAnimationFrame(tick);
    }

    function setupApples() {
        var max_apples = level * apples_per_level;
        while (apples.length < max_apples) {
            initApple(apples.length);
        }
    }

    function initApple(index) {
        var max_speed = max_speed_per_level * level;
        var min_speed = min_speed_per_level * level;
        apples[index] = {
            x: Math.round(Math.random() * (width - 2 * apple_w)) + apple_w,
            y: -apple_h,
            v: Math.round(Math.random() * (max_speed - min_speed)) + min_speed,
            delay: Date.now() + Math.random() * delay
        }
        total_apples++;
    }

    function collision(apple) {
        if (apple.y + apple_img.height < basket.y + 50) {
            return false;
        }
        if (apple.y > basket.y + 50) {
            return false;
        }
        if (apple.x + apple_img.width < basket.x + 50) {
            return false;
        }
        if (apple.x > basket.x + 50) {
            return false;
        }

        return true;
    }

    function maybeIncreaseDifficulty() {
        level = Math.max(1, Math.ceil(basket.score / 10));
        setupApples();
    }

    function tick() {
        var i;
        var apple;
        var dateNow = Date.now();
        c.width = c.width;
        for (i = 0; i < apples.length; i++) {
            apple = apples[i];
            if (dateNow > apple.delay) {
                apple.y += apple.v;
                if (collision(apple)) {
                    initApple(i);
                    basket.score++;
                } else if (apple.y > height) {
                    initApple(i);
                } else {
                    ctx.drawImage(apple_img, apple.x, apple.y);
                }
            }
        }
        ctx.font = "bold 24px sans-serif";
        ctx.fillStyle = "#2FFF2F";
        ctx.fillText(basket.score, c.width - 50, 50);
        ctx.fillText("Level: " + level, 20, 50);

        ctx.drawImage(basket_img, basket.x, basket.y);
        maybeIncreaseDifficulty();
        requestAnimationFrame(tick);
    }

    return {
        init: init
    };
}

答案 1 :(得分:1)

您的下一个合乎逻辑的步骤是创建具有适当属性的苹果Object。然后,您可以将它们存储在Array中并为多个苹果设置动画。

答案 2 :(得分:0)

正如Gerard所说,你应该创建一个Array苹果Objects。您可以查看this example with balls而不是苹果。

你已经完成了这一点,我建议你在不同的屏幕外画布上绘制所有以相同速度落下的苹果,如果你打算添加其中许多,你可以动画那些而不是苹果的苹果。如果它们都以相同的速度下降,只需使用一个屏幕外画布并立即移动。

如果您无法以适当的速度保持FPS,请查看http://www.html5rocks.com/en/tutorials/canvas/performance/