xxxxxxxxxx
function Bird() {
this.y = height/2
this.x = 64
this.gravity = 1
this.lift = -20
this.velocity = 0
this.show = function() {
fill(255)
image(birdImg, this.x, this.y, 32, 32)
}
this.up = function() {
this.velocity += this.lift
}
this.update = function() {
this.velocity += this.gravity
this.velocity *= 0.9
this.y += this.velocity
if (this.y > height - 32) {
this.y = height - 32
this.velocity = 0
}
if (this.y < 0) {
this.y = 0
this.velocity = 0
}
}
}
function Pipe() {
this.top = random(height/2)
this.bottom = random(height/2)
this.x = width
this.w = 20
this.speed = 2
this.highlight = false
this.scored = false
this.scoreCounted = false
this.r = random(255)
this.g = random(255)
this.b = random(255)
this.hits = function(bird) {
if (bird.x + 32 > this.x && bird.x < this.x + this.w ) {
if (bird.y - 32 < this.top || bird.y > height - this.bottom) {
this.highlight = true
return true
} else {
this.scored = true
}
}
//this.highlight = false
return false
}
this.countScore = function(){
if (bird.x > this.x + this.w && this.scored && this.scoreCounted == false){
score += 1
console.clear()
//print(score)
this.scoreCounted = true
}
}
this.show = function() {
fill(255)
if (this.highlight) {
fill(255, 0, 0)
} else{
fill(this.r, this.g, this.b)
}
rect(this.x, 0, this.w, this.top)
rect(this.x, height-this.bottom, this.w, this.bottom)
}
this.update = function() {
this.x -= this.speed
}
this.offscreen = function() {
if (this.x < -this.w) {
return true
} else {
return false
}
}
}
var birdImg
var backImg
var bird
var pipes = []
var score = 0
var end = false
function setup() {
createCanvas(windowWidth, windowHeight)
bird = new Bird()
birdImg = loadImage("https://i.imgur.com/OrHqbiO.png")
backImg = loadImage("http://i.imgur.com/GP2oOj3.jpg")
pipes.push(new Pipe())
fill(100)
}
function draw() {
background(backImg)
fill(0)
text("score: " + score, 10, 20)
for (var i = pipes.length-1; i >= 0; i--){
pipes[i].show()
pipes[i].update()
if (pipes[i].hits(bird)) {
end = true
}
pipes[i].countScore()
if (pipes[i].offscreen()) {
pipes.splice(i, 1)
}
if (end == true) {
pipes[i].speed = 0
}
}
bird.show()
bird.update()
if (frameCount % 200 == 0){
pipes.push(new Pipe())
}
}
function keyPressed() {
if (key == ' ' && end == false){
bird.up()
}
}
function touchStarted() {
if (end == false){
bird.up()
}
}