jQuery 生成 div 和碰撞检测

标签 jquery css collision-detection flappy-bird-clone

所以我几乎在我的学校作业元素结束时我只是遗漏了两件我似乎无法弄清楚的主要事情:

1.如何在间隙的随机位置生成管道障碍物,以便小鸟可以飞过(考虑使用一个函数来更改间隙位置的管道 div 的 css 'right' attr),并移除管道离开屏幕底部时。 (它是一个倒立的飞鸟游戏,就像 btw..)

2.其次,我需要碰撞检测功能方面的帮助,以便我知道游戏何时结束(这不太重要,因为我认为我可以在解决第一个问题后解决它)

$(document).ready(function(){
//Variables
var screenWidth = $(window).width();//Screen width
var screenHeight = $(window).height();//Screen height
var birdWidth = $("#bird").width();//bird width
var y = 0;//Background y position
var astY = 0;//Pipe position

var bird = {//bird properties
    goingLeft: false,
    goingRight: false,
    lspeed: 0,
    rspeed: 0,
    maxSpeed: 10
};

setBirdPosition(screenWidth/2 - birdWidth/2, screenHeight/1.3 - birdWidth/2);
startBackgroundMovement();
spawnPipe();


//Start move the screen
function startBackgroundMovement(){
    setInterval(function()
    {
        y+=1;
    $('body').css('background-position-y',y + 'px');
    }, 10);
}


//bird starting position
function setBirdPosition(posX, posY) {
    $("#bird").css("left", posX);
    $("#bird").css("top", posY);
    bird.position = posX;
}
 (function birdLoop() {
    if (bird.goingLeft) {
        bird.lspeed = Math.min(bird.lspeed *1.1 || 1, bird.maxSpeed);
    } else {
        bird.lspeed = Math.max(bird.lspeed - 0.5, 0);
    }
    if (bird.goingRight) {
        bird.rspeed = Math.min(bird.rspeed *1.1 || 1, bird.maxSpeed);
    } else {
        bird.rspeed = Math.max(bird.rspeed - 0.5, 0);
    }
    bird.position = bird.position + (bird.rspeed - bird.lspeed);
    $("#bird").css('left', bird.position);
    requestAnimationFrame(birdLoop);
}());

//Move bird
$(document).keydown(function(e){
    switch(e.which){
        case 37://left
            bird.goingLeft= true; 
             //left edge of screen
      if (bird.position < 0) {
        bird.position = 0;
      }
      break;
        case 39://right
            bird.goingRight= true;
             //right edge of screen
      if (bird.position > screenWidth - birdWidth) {
        bird.position = screenWidth - birdWidth;
      }
        default: return;    
    e.preventDefault();//not sure if needed
    }
}).keyup(function(e){
    switch(e.which){
        case 37://left
            bird.goingLeft= false;
            //left edge of screen
       if (bird.position < 0) {
        bird.position = 0;
      }
            break;
        case 39://right
            bird.goingRight= false;
            //right edge of screen
      if (bird.position > screenWidth - birdWidth) {
        bird.position = screenWidth - birdWidth;
      }
        default: return;    
    e.preventDefault();//not sure if needed
    }
});

function spawnPipe()//spawn pipes
{
    setInterval(function()
    {
        astY += 1;
        $("#fullPipe").css("top", astY);              
    }, 10);
}
});

检查这个:http://jsfiddle.net/u38ratk9/6/

最佳答案

How to spawn the pipe obstacles

管道以规则的间隔或距离发生。您可以检查耗时,也可以检查现有管道移动的距离。

Second i need a help with the collision detection

管道有宽度和高度,还有位置。本质上,您的管道是盒子。这对鸟也意味着相同。我相信它被称为“边界框”。如果鸟的任何边缘与盒子的边缘相交,您可以检查它是否有任何边缘。

关于jQuery 生成 div 和碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27642471/

相关文章:

javascript - 选择图像区域并触发JS事件

javascript - 如何从vue js html中的日期中仅提取年份并仅打印该年份?

附加到 div 的 jquery 动画

java - LibGdx Sprite 之间的碰撞检测?

xna - Sprite 与线的碰撞

javascript - 在给定的 5 秒等待后无法找到动画的方法

javascript - 圆滑的轮播格式正确

javascript - 此 html 代码的 DRY 一致性

javascript - 使用 jquery 获取存储在 block 中的图像源

java - HashTables 如何处理冲突?