gpt4 book ai didi

javascript - 减慢 Canvas 上图像的移动速度

转载 作者:行者123 更新时间:2023-12-03 07:21:28 29 4
gpt4 key购买 nike

问题

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

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/ (损坏)

最佳答案

使用“超时”倒计时限制玩家的移动

  • 创建全局 var playerFreezeCountdown=0 .

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

    • 如果playerFreezeCountdown>0,则不会更改player.x。
    • 如果playerFreezeCountdown<=0,您两者都更改player.x,并将playerFreezeCountdown设置为所需的“刻度超时”值:playerFreezeCountdown=5 。此超时将导致玩家无法移动船只,直到 5 个周期过去。
  • tick ,始终递减 playerFreezeCountdown-- 。当playerFreezeCountdown 减少到零或低于零时,这将间接允许对player.x 进行更改。

关于javascript - 减慢 Canvas 上图像的移动速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36200084/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com