Created
March 3, 2019 02:15
-
-
Save SharkFinPro/46663c18482f98398d9cf106ebd685d4 to your computer and use it in GitHub Desktop.
This file contains 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 keys = {}; | |
keyPressed = function() { | |
keys[keyCode] = true; | |
}; | |
keyReleased = function() { | |
delete keys[keyCode]; | |
}; | |
var Player = { | |
w: 50, | |
h: 50, | |
x: width / 2 - 25, | |
y: height - 50, | |
alive: true, | |
score: 0 | |
}; | |
Player.display = function() { | |
fill(129, 255, 139); | |
stroke(75); | |
strokeWeight(3); | |
rect(this.x, this.y, this.w, this.h); | |
}; | |
Player.update = function() { | |
if(keys[37]) { | |
this.x -= 3; | |
} | |
if(keys[39]) { | |
this.x += 3; | |
} | |
this.x = constrain(this.x, 0, width - this.w); | |
}; | |
var Block = function() { | |
this.w = 50; | |
this.h = 50; | |
this.x = random(0, width-50); | |
this.y = -50; | |
this.yvel = 0.01; | |
}; | |
Block.prototype.display = function() { | |
fill(0, 139, 255); | |
stroke(75); | |
strokeWeight(3); | |
rect(this.x, this.y, this.w, this.h); | |
}; | |
Block.prototype.update = function() { | |
this.yvel += 0.01 + frameCount/1000; | |
this.y += this.yvel; | |
this.y++; | |
}; | |
var blocks = []; | |
var collideWith = function(ply, obj) { | |
return ply.x + ply.w > obj.x && ply.x < obj.x + obj.w && ply.y + ply.h > obj.y && ply.y < obj.y + obj.h; | |
}; | |
textAlign(CENTER, CENTER); | |
var draw = function() { | |
background(100); | |
textFont(createFont('Arial Black'), 30); | |
if(!Player.alive) { | |
background(50); | |
textSize(50); | |
text('You Died!\nScore: ' + Player.score, width / 2, height / 2); | |
noLoop(); | |
return; | |
} | |
Player.score++; | |
if(frameCount % 50 - frameCount/1000 <= 0.5) { | |
blocks.push(new Block()); | |
} | |
Player.display(); | |
Player.update(); | |
blocks.forEach(function(block) { | |
if(collideWith(Player, block)) { | |
Player.alive = false; | |
} | |
block.display(); | |
block.update(); | |
}); | |
fill(139, 255, 0); | |
text('Score: ' + Player.score, width / 2, 100); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment