在画布上放大图像的运动

时间:2016-03-24 12:20:29

标签: javascript html5 canvas

问题

我正在创造一个涉及躲避射弹的游戏。玩家控制着船只的形象,我不希望船只完全一起移动,因为这看起来非常不现实。

Image of game

问题

有没有办法控制图像移动的速度,如何减慢图像的移动速度?

守则

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

    //music
    var snd = new Audio("Menu.mp3");
    snd.loop = true;
    snd.play();
        document.getElementById('mute').addEventListener('click', function (evt) {
        if ( snd.muted ) {
            snd.muted = false
            evt.target.innerHTML = 'mute'
        }
        else {
            snd.muted = true
            evt.target.innerHTML = 'unmute'
        }
    })

    function create_game() {
        debugger;
        var level = 1;
        var projectiles_per_level = 1;
        var min_speed_per_level = 1;
        var max_speed_per_level = 2;
        var last_projectile_time = 0;
        var next_projectile_time = 0;
        var width = 600;
        var height = 500;
        var delay = 1000;
        var item_width = 30;
        var item_height = 30;
        var total_projectiles = 0;
        var projectile_img = new Image();
        var projectile_w = 30;
        var projectile_h = 30;
        var player_img = new Image();
        var c, ctx;

        var projectiles = [];
        var player = {
            x: 200,
            y: 400,
            score: 0
        };

        function init() {
            projectile_img.src = "projectile.png";
            player_img.src = "player.png";

            level = 1;
            total_projectiles = 0;
            projectiles = [];

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

            c.addEventListener("mousemove", function (e) {
                //moving over the canvas.
                var bounding_box = c.getBoundingClientRect();
                player.x = (e.clientX - bounding_box.left) * (c.width / bounding_box.width) - player_img.width / 2;
            }, false);

            setupProjectiles(); 
            requestAnimationFrame(tick);
        }

        function setupProjectiles() {
            var max_projectiles = level * projectiles_per_level;
            while (projectiles.length < max_projectiles) {
                initProjectile(projectiles.length);
            }
        }

        function initProjectile(index) {
            var max_speed = max_speed_per_level * level;
            var min_speed = min_speed_per_level * level;
            projectiles[index] = {
                x: Math.round(Math.random() * (width - 2 * projectile_w)) + projectile_w,
                y: -projectile_h,
                v: Math.round(Math.random() * (max_speed - min_speed)) + min_speed,
                delay: Date.now() + Math.random() * delay
            }
            total_projectiles++;
        }

        function collision(projectile) {
            if (projectile.y + projectile_img.height < player.y + 74) {
                return false;
            }
            if (projectile.y > player.y + 74) {
                return false;
            }
            if (projectile.x + projectile_img.width < player.x + 177) {
                return false;
            }
            if (projectile.x > player.x + 177) {
                return false;
            }

            return true;
        }

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

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

            ctx.drawImage(player_img, player.x, player.y);
            maybeIncreaseDifficulty();
            requestAnimationFrame(tick);
        }

        return {
            init: init
        };
    }   

https://jsfiddle.net/a6nmy804/4/ (已破碎)

1 个答案:

答案 0 :(得分:1)

使用&#34;超时&#34;来限制玩家的移动。倒计时

  • 创建全局var playerFreezeCountdown=0

  • 仅在mousemove更改player.x playerFreezeCountdown<=0

    • 如果playerFreezeCountdown> 0,则不会更改player.x.
    • 如果playerFreezeCountdown&lt; = 0,则两者更改player.x并将playerFreezeCountdown设置为所需的&#34; tick timeout&#34;价值:playerFreezeCountdown=5。此超时将导致玩家无法移动他们的船只,直到5个滴答过去。
  • tick中,始终递减playerFreezeCountdown--。这将间接允许在playerFreezeCountdown减少到零或低于零之后更改为player.x。