javascript - 在 Canvas 上,有什么方法可以让多个苹果在游戏中掉落?

标签 javascript html canvas

我正在创建一个游戏(使用 HTML5 Canvas ),其中涉及捕捉掉落的苹果,我知道,多么原创!我很难找到一种方法让它让多个苹果掉落?

这里是 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 以提高性能。事情会变得很糟糕,当你移动水桶时你可能已经注意到了这一点。我已经修改了你的 fiddle ,以准确演示如何修改你的程序以支持多个苹果以不同的速度下落。 (将 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/

相关文章:

javascript - 如何删除所有 jQuery 事件(或者只是不重新初始化两次)

javascript - jQuery sortColumns 插件 : How to sort correctly with rowspan

javascript - 弹出窗口的显示 id 按钮不起作用

javascript - 图标旁边没有换行的文字

javascript - 基于 4 个点变换 Canvas 上的图像

javascript - 检测矩形与圆形的碰撞

javascript - 我如何使用ajax发送动态值

javascript - Apollo GraphQL : use case for merge function in type policy?

javascript - 带圆 Angular 的 Canvas clearRect

Android 在多边形上绘制位图