Skip to content

Instantly share code, notes, and snippets.

@bnolan
Last active April 21, 2020 09:31
Show Gist options
  • Save bnolan/faa429dffaac1f1be90b103a502df60c to your computer and use it in GitHub Desktop.
Save bnolan/faa429dffaac1f1be90b103a502df60c to your computer and use it in GitHub Desktop.
Snake for vidscreen in cryptovoxels
let x, y, xd, yd, z
feature.on('click', e => {
reset()
})
function reset () {
x = 32
y = 32
xd = 1
yd = 0
for (z = 0 ; z < feature.screenWidth * feature.screenHeight * 3; z++) {
feature.screen[z] = 0
}
}
function died () {
reset()
}
feature.on('frame', e => {
// Draw random background noise
for (z = 0 ; z < 100; z++) {
let i = Math.floor(Math.random() * 64 * 64) * 3
let r = Math.random() * 128
feature.screen[i + 0] = r
}
// Move the head
x += xd
y += yd
// Hit the edge of the screen
if (x >= feature.screenWidth || x <= 0 || y >= feature.screenHeight || y <= 0) {
died()
}
// Get position into the screen array
let i = (y * 64 + x) * 3
// Hit my tail
if (feature.screen[i + 1] > 0) {
died()
}
// Set green + blue
feature.screen[i + 1] = 255
feature.screen[i + 2] = 128
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment