我正在创建一个游戏(使用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);

                        }

最佳答案

我在您以前的问题上发布了此内容,因此在此重复。您将需要维护一系列苹果,但是您还需要签出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
    };
}

关于javascript - 在 Canvas 上,有办法让多个苹果加入游戏吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29870262/

10-11 07:33