Last active
April 22, 2020 02:30
-
-
Save bnolan/e6d6cdad7c845f5a5245d4a0d3a92b01 to your computer and use it in GitHub Desktop.
snek.js
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
// Full snake game implementation | |
let x, y, xd, yd, z | |
feature.on('keys', e => { | |
if (e.keys.up && yd >= 0) { | |
yd = -1 | |
xd = 0 | |
} else if (e.keys.left && xd >= 0) { | |
yd = 0 | |
xd = -1 | |
} else if (e.keys.right && xd <= 0) { | |
yd = 0 | |
xd = 1 | |
} else if (e.keys.down && yd <= 0) { | |
yd = 1 | |
xd = 0 | |
} | |
}) | |
feature.on('start', 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 | |
} | |
// Set green + blue | |
x += xd | |
y += yd | |
// Hit the edge of the screen | |
if (x >= feature.screenWidth || x <= 0 || y >= feature.screenHeight || y <= 0) { | |
died() | |
} | |
let i = (y * 64 + x) * 3 | |
// Hit my tail | |
if (feature.screen[i + 1] > 0) { | |
died() | |
} | |
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