所以,几乎在我的学校的作业项目IM只是缺少两个主要的事情我似乎无法找出结束IM:
1.How产卵与所述间隙的随机位置上的管障碍,使鸟能飞至(因子评分使用其改变的CSS“右” ATTR管DIV为间隙位置的函数),并且去除所述管道时它熄灭屏幕底部。 (它是一种倒笨鸟先飞游戏一样顺便说一句..)
2.Second我需要与碰撞检测功能的帮助,所以我知道什么时候游戏就结束了(这是不太重要的原因艰难,我认为我可以计算出来后感到不适解决第一个问题)
$(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/