Last active
May 3, 2020 04:42
-
-
Save moritree/13fa89e931a72c57dd2b9a3d0ef25610 to your computer and use it in GitHub Desktop.
centipede arcade game for vid screens in Cryptovoxels (WIP)
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
let player, frame, bullets, mushrooms | |
// main loop functions | |
feature.on('keys', e => { | |
if (!player) { return } | |
if (e.keys.left) { | |
player.xVel = -1 | |
} else if (e.keys.right) { | |
player.xVel = 1 | |
} else { | |
player.xVel = 0 | |
} | |
if (e.keys.up) { | |
player.yVel = -1 | |
} else if (e.keys.down) { | |
player.yVel = 1 | |
} else { | |
player.yVel = 0 | |
} | |
if (e.keys.a) { | |
if (bullets.length == 0 || frame - bullets[bullets.length - 1].timeBorn > 3) { | |
player.shoot() | |
} | |
} | |
}) | |
feature.on('start', e => { | |
reset() | |
}) | |
feature.on('frame', e => { | |
frame += 1 | |
update() | |
draw() | |
}) | |
function update() { | |
player.update() | |
let inactiveBullets = [] | |
for (let i = 0; i < bullets.length; i ++) { | |
bullets[i].update() | |
if (!bullets[i].active) { | |
inactiveBullets.push(i) | |
} | |
} | |
for (let i = 0; i < inactiveBullets.length; i ++) { | |
bullets.splice(inactiveBullets[i] - i, 1) | |
} | |
let deadMushrooms = [] | |
for (let i = 0; i < mushrooms.length; i ++) { | |
if (mushrooms[i].health <= 0) { | |
deadMushrooms.push(i) | |
} | |
} | |
for (let i = 0; i < deadMushrooms.length; i ++) { | |
mushrooms.splice(deadMushrooms[i] - i, 1) | |
} | |
} | |
function draw() { | |
feature.screen.fill(0) | |
player.draw() | |
for (let i = 0; i < bullets.length; i ++) { | |
bullets[i].draw() | |
} | |
for (let i = 0; i < mushrooms.length; i ++) { | |
mushrooms[i].draw() | |
} | |
} | |
function reset() { | |
frame = 0 | |
player = new Player(30, 58) | |
bullets = [] | |
mushrooms = [] | |
for (let i = 0; i < Math.round(Math.random() * 20) + 5; i ++) { | |
mushrooms.push(new Mushroom(2 * Math.round(Math.random() * 31), 2 * Math.round(Math.random() * 26))) | |
} | |
draw() | |
} | |
// helper functions | |
function drawPixel(x, y, rgb) { | |
if (rgb.length != 3) { return } | |
for (let z = 0; z < rgb.length; z ++) { | |
feature.screen[y * feature.screenWidth * 3 + x * 3 + z] = rgb[z] | |
} | |
} | |
function drawBlock(x, y, rgb, size) { | |
for (let u = 0; u < size; u ++) { | |
for (let r = 0; r < size; r ++) { | |
drawPixel(x + u, y + r, rgb) | |
} | |
} | |
} | |
// classes | |
class Player { | |
constructor(x, y) { | |
this.xPos = x | |
this.yPos = y | |
this.xVel = 0 | |
this.yVel = 0 | |
this.size = 4 | |
} | |
shoot() { | |
bullets.push(new Bullet(this.xPos + 1, this.yPos - 1)) | |
} | |
update() { | |
if (frame % 2 != 0) { | |
return | |
} | |
let nextX = this.xPos + this.xVel | |
let nextY = this.yPos + this.yVel | |
// wall collision detection | |
if (nextX >= 0 && nextX <= 63 - this.size + 1) { | |
this.xPos += this.xVel | |
} | |
if (nextY >= 0 && nextY <= 63 - this.size + 1) { | |
this.yPos += this.yVel | |
} | |
} | |
draw() { | |
drawBlock(this.xPos, this.yPos, [150, 255, 140], this.size) | |
} | |
} | |
class Bullet { | |
constructor(x, y) { | |
this.xPos = x | |
this.yPos = y | |
this.size = 2 | |
this.yVel = -2 | |
this.timeBorn = frame | |
this.active = true | |
} | |
update() { | |
this.yPos += this.yVel | |
if (this.yPos < 0 - this.size) { | |
this.active = false | |
} | |
// detect collision with mushroom | |
if (mushrooms) { | |
for (let i = 0; i < mushrooms.length; i ++) { | |
let nextY = this.yPos + this.yVel | |
let mushroom = mushrooms[i] | |
if (nextY >= mushroom.yPos && nextY < mushroom.yPos + mushroom.size - 1 | |
&& this.xPos >= mushroom.xPos - 1 && this.xPos < mushroom.xPos + mushroom.size) { | |
this.active = false | |
mushroom.health -= 1 | |
} | |
} | |
} | |
} | |
draw() { | |
drawBlock(this.xPos, this.yPos, [255, 255, 255], this.size) | |
} | |
} | |
class Mushroom { | |
constructor(x, y) { | |
this.xPos = x | |
this.yPos = y | |
this.size = 4 | |
this.colour = [240, 240, 10] | |
this.health = 3 | |
} | |
draw() { | |
if (this.health == 3) { | |
drawBlock(this.xPos, this.yPos, this.colour, this.size) | |
} else if (this.health == 2) { | |
for (let i = 0; i < this.size; i ++) { | |
drawPixel(this.xPos + i, this.yPos, this.colour) | |
drawPixel(this.xPos + i, this.yPos + this.size - 1, this.colour) | |
if (i >= 1 && i < this.size - 1) { | |
drawPixel(this.xPos, this.yPos + i, this.colour) | |
drawPixel(this.xPos + this.size - 1, this.yPos + i, this.colour) | |
} | |
} | |
} else if (this.health == 1) { | |
for (let i = 1; i < this.size - 1; i ++) { | |
drawPixel(this.xPos + i, this.yPos, this.colour) | |
drawPixel(this.xPos + i, this.yPos + this.size - 1, this.colour) | |
drawPixel(this.xPos, this.yPos + i, this.colour) | |
drawPixel(this.xPos + this.size - 1, this.yPos + i, this.colour) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment