-1

所以我几乎在我的学校作业项目结束时我只是错过了两件我似乎无法弄清楚的主要事情:

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/

4

2 回答 2

2

如何产生管道障碍物

管道以固定的间隔或距离发生。您可以检查经过的时间,也可以检查现有管道行进的距离。

其次,我需要碰撞检测方面的帮助

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

于 2014-12-27T08:50:17.197 回答
1

首先,我稍微更改了您的 CSS 以安排代码并为管道设置不同的宽度组类('.zero'、'.one' 等),您可以添加更多宽度组或稍后进行编辑但请注意管道边宽与鸟宽之间的比率。

#bird
{
    position:absolute;
    width:4%;
    height: auto;
    right:0;
}

#fullPipe
{
    position:absolute;
    width:100%;
    left:0%;
    height: 10%;
}

#leftPipe, #rightPipe
{
    position:absolute;
    top:0;
    width:48%;
    height: 100%;
}

#leftPipe
{
    left:0%;
}

#rightPipe
{
    right:0%;
}

.zero #leftPipe, .zero #rightPipe
{
    width:48%;
}

.one #leftPipe
{
    width:8%;
}

.one #rightPipe
{
    width:88%;
}

.two #leftPipe
{
    width:28%;
}

.two #rightPipe
{
    width:68%;
}

.three #leftPipe
{
    width:58%;
}

.three #rightPipe
{
    width:38%;
}

.four #leftPipe
{
    width:88%;
}

.four #rightPipe
{
    width:8%;
}

#leftPipe img, #rightPipe img
{
    width:100%;
    height: 100%;
}

在 JS 中,注意 setInterval() 中的条件,我现在将其设置为 '700' 以进行演示,但是在您将碰撞准备就绪后,您可以将其替换为管道和主体是否不发生碰撞的条件(超出框架)然后重置管道并设置新的宽度组类。

    var PipesRandom;
    var PipesWidth = ['zero', 'one', 'two', 'three', 'four'];  
    function spawnPipe(astY){ //spawn asteroids
        $('#fullPipe').css("top", astY);  
    }  
    setInterval(function(){
        astY += 1;
        if(astY < 700){
            spawnPipe(astY);
        } 
        else {
            astY = -100;
            PipesRandom = PipesWidth[Math.floor(Math.random() * PipesWidth.length)];
            $('#fullPipe').removeClass('zero one two three four');
            $('#fullPipe').addClass(PipesRandom);
            spawnPipe(astY);
        }
    } ,10);

例如:http: //jsfiddle.net/u38ratk9/8/

关于碰撞,有很多选项,你可以检查这个问题,例如:请推荐一个处理可拖动元素的碰撞检测的 JQuery 插件

或:基本二维碰撞检测

有很多,随便搜一下。

于 2014-12-28T21:18:38.850 回答