Last active
June 30, 2020 02:06
-
-
Save erning/820f81d0ceec5cd9ab2689be296c4950 to your computer and use it in GitHub Desktop.
Flappy Bird - code.org GameLab
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var FLYING_SPEED = -3; | |
var GRAVITATIONAL_ACCELERATION = 0.5; | |
// | |
// background | |
// | |
var backgrounds = [ | |
createSprite(World.width*0.5, World.height*0.5-28), | |
createSprite(World.width*1.5, World.height*0.5-28) | |
]; | |
var grounds = [ | |
createSprite(World.width*0.5, World.height), | |
createSprite(World.width*1.5, World.height) | |
]; | |
for (var i=0; i<=1; i++) { | |
backgrounds[i].setAnimation("background"); | |
backgrounds[i].width = World.width; | |
if (backgrounds[i].height < World.height) { | |
backgrounds[i].height = World.height; | |
} | |
backgrounds[i].velocityX = FLYING_SPEED * 0.2; | |
} | |
for (var i=0; i<=1; i++) { | |
grounds[i].setAnimation("base"); | |
grounds[i].width = World.width; | |
grounds[i].velocityX = FLYING_SPEED; | |
} | |
function updateBackground() { | |
for (var i=0; i<=1; i++) { | |
if (backgrounds[i].x <= -World.width*0.5) { | |
backgrounds[i].x += World.width*2; | |
} | |
if (grounds[i].x <= -World.width*0.5) { | |
grounds[i].x += World.width*2; | |
} | |
bird.collide(grounds[i]); | |
} | |
} | |
// | |
// bird | |
// | |
var bird = createSprite(World.width*0.25, World.height*0.5); | |
bird.setAnimation("bird"); | |
bird.play(); | |
// | |
// pipe | |
// | |
var pipes = []; | |
var pipesToCount = []; | |
function createPipes() { | |
var height = randomNumber(80, 120); | |
var postion = randomNumber(height*0.5+56, World.height-height*0.5-112); | |
var pipeUp = createSprite(World.width, postion-height*0.5-160); | |
pipeUp.setAnimation("pipe"); | |
pipeUp.width = 26; | |
pipeUp.mirrorY(-1); | |
pipeUp.velocityX = FLYING_SPEED; | |
var pipeDown = createSprite(World.width, postion+height*0.5+160); | |
pipeDown.setAnimation("pipe"); | |
pipeDown.width = 26; | |
pipeDown.velocityX = FLYING_SPEED; | |
pipes.push([pipeUp, pipeDown]); | |
pipesToCount.push(pipeUp); | |
} | |
var randomPipePos = randomNumber(World.width*0.4, World.width*0.7); | |
function updatePipes() { | |
while (pipes.length > 0 && pipes[0][0].x < -pipes[0][0].width*0.5) { | |
var v = pipes.shift(); | |
v[0].destroy(); | |
v[1].destroy(); | |
} | |
if (pipes.length <= 0 || pipes[pipes.length-1][0].x < randomPipePos) { | |
createPipes(); | |
randomPipePos = randomNumber(World.width*0.4, World.width*0.7); | |
} | |
for (var i=0; i<pipes.length; i++) { | |
bird.collide(pipes[i][0]); | |
bird.collide(pipes[i][1]); | |
} | |
while (pipesToCount.length > 0 && pipesToCount[0].x < bird.x - bird.width) { | |
score++; | |
// if (score % 10 == 0) { | |
// FLYING_SPEED--; | |
// for (var i=0; i<pipes.length; i++) { | |
// pipes[i][0].velocityX = FLYING_SPEED; | |
// pipes[i][1].velocityX = FLYING_SPEED; | |
// } | |
// grounds[0].velocityX = FLYING_SPEED; | |
// grounds[1].velocityX = FLYING_SPEED; | |
// } | |
pipesToCount.shift(); | |
} | |
} | |
// | |
// state | |
// | |
var state = 0; | |
var score = 0; | |
var maxScore = 0; | |
function drawScore() { | |
if (maxScore < score) { | |
maxScore = score; | |
} | |
fill("white"); | |
textSize(16); | |
textAlign("left", "top"); | |
text("Score: " + score + "\nMax Score: " + maxScore, 10, 10); | |
} | |
// | |
// draw | |
// | |
function draw() { | |
updateBackground(); | |
if (state == 0) { | |
// ready to play | |
bird.x = World.width*0.5; | |
bird.y = World.height*0.5; | |
bird.play(); | |
drawSprites(); | |
fill("white"); | |
textSize(24); | |
textAlign("center", "center"); | |
text("Press [SPACE] to start", 0, 0, World.width, World.height*0.5); | |
if (keyWentDown('space')) { | |
state = 1; | |
score = 0; | |
for (var i=0; i<pipes.length; i++) { | |
pipes[i][0].destroy(); | |
pipes[i][1].destroy(); | |
} | |
pipes=[]; | |
for (var i=0; i<pipesToCount.length; i++) { | |
pipesToCount[i].destroy(); | |
} | |
pipesToCount=[]; | |
} | |
return; | |
} | |
updatePipes(); | |
if (state == 2) { | |
// game over | |
drawSprites(); | |
drawScore(); | |
fill("White"); | |
textSize(24); | |
textAlign("center", "center"); | |
text("Game Over\nPress [R] to replay", 0, 0, World.width, World.height*0.5); | |
if (keyWentDown('r')) { | |
while (pipes.length > 0) { | |
var v = pipes.shift(); | |
v[0].destroy(); | |
v[1].destroy(); | |
} | |
state = 0; | |
} | |
return; | |
} | |
var accel = GRAVITATIONAL_ACCELERATION; | |
if (keyDown('space')) { | |
accel = -GRAVITATIONAL_ACCELERATION * 2; | |
} | |
if (accel < 0) { | |
bird.nextFrame(); | |
} else { | |
bird.play(); | |
} | |
bird.velocityY += accel; | |
if (bird.y < 0) { | |
bird.y = 0; | |
bird.velocityY = 0; | |
} | |
bird.velocityX=0; | |
if (bird.x < 0) { | |
bird.x = 0; | |
state = 2; | |
} | |
drawSprites(); | |
drawScore(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://studio.code.org/projects/gamelab/yeTumzoI6xltLWJGykd5vDV40JABIcfgPmep8lBRNl0