-1

So im almost at the end of my school homework project im just missing two major things i can't seem to figure out:

1.How to spawn the pipe obstacles with a random position for the gap so the bird can fly through (tought of using a function which changes the css 'right' attr of the pipe div for the gap position), and removing the pipe when it goes off screen bottom . (Its a inverted flappy bird game like btw..)

2.Second i need a help with the collision detection function so i know when game is over (this is less important tough cause i think i can figure it out after ill solve the first problem)

$(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);
}
});

Check this : http://jsfiddle.net/u38ratk9/6/

Pimgd
  • 5,983
  • 1
  • 30
  • 45
Arie Pinto
  • 1,274
  • 1
  • 11
  • 19
  • collision detection is done by getting the coordinate positions of all four corners of an element - which is easily done with jQuery - then you can compare to see if another element's corner is within the 'box' of the first element. – nicholaswmin Dec 25 '14 at 02:18
  • I just merged an [exact duplicate](http://stackoverflow.com/questions/27665666/jquery-spawning-div-randomly-and-removing-it-when-offscreen?noredirect=10) into this question. Please do not continue posting duplicate questions like this. – Andrew Barber Jan 04 '15 at 02:47

2 Answers2

2

How to spawn the pipe obstacles

The pipes happen at a regular interval or distance. You can either check the time elapsed or you can check the distance traveled by existing pipes.

Second i need a help with the collision detection

Pipes have width and height, as well as position. Essentially, your pipes are boxes. This also means the same for the bird. I believe it's called a "bounding box". You can check if any of the edges of the bird if it intersects with the edges of the box.

Joseph
  • 117,725
  • 30
  • 181
  • 234
1

first of all i've changed your CSS little bit to arrange the code and set the different width groups classes ('.zero', '.one', etc.) for the pipes, you can add more width groups or edit it later but notice the ratio between the pipes sides width and the bird width.

#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%;
}

in The JS, notice the condition inside the setInterval(), i set it for now to '700' for the demo but after you will set the collision ready, you can replace it with condition of if the pipes and the body not in collision (out of the frame) then reset the pipes and set new width group class.

    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);

example: http://jsfiddle.net/u38ratk9/8/

about the collision, there are a lot of options, you can check this question for example: Please recommend a JQuery plugin that handles collision detection for draggable elements

or: Basic 2d collision detection

there are a lot, just search.

Community
  • 1
  • 1
Eran.E
  • 940
  • 5
  • 8
  • I tried to do something samiliar but instead of using if astY < 700 i tried using the Screenheight variable so it wont overflow but didnt succeed..well , you didnt solve all my problems but you got me back on the right track..thanks mysterious programmer – Arie Pinto Dec 28 '14 at 23:17
  • You didn't got it wrong, you can get the window height and put it inside a variable (var WindowHeight = $(window).height) and then check if '#fullPipe' position top smaller than WindowHeight (astY < WindowHeight), it should work but you don't want to call the pipes every time they get out of the window so you will need to set a delay. I won't solve all your problems, you need to think...that's way you will learn the best from it. After all it's your school project. Good luck my friend! – Eran.E Dec 29 '14 at 07:26