Last active
April 25, 2016 12:47
-
-
Save randompast/4b569587d1f08225e02c5e8a8d97c8cf to your computer and use it in GitHub Desktop.
Eh, 40 mins jam, while trying webaudio mic stuff, then stackgl particles... then panic...
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
document.title = "Blah" | |
var fit = require('canvas-fit') | |
var canvas = document.body.appendChild(document.createElement('canvas')) | |
window.addEventListener('resize', fit(canvas), false) | |
var ctx = canvas.getContext('2d') | |
player = {} | |
player.p = [] | |
player.p[0] = canvas.width/2 | |
player.p[1] = canvas.height/2 | |
food = [] | |
var keysDown = {} | |
document.addEventListener("keydown", function(e){ | |
keysDown[e.keyCode] = true | |
e.preventDefault(); | |
}) | |
document.addEventListener('keyup', function(e){ | |
delete keysDown[e.keyCode] | |
}) | |
update = function(s){ | |
if(keysDown[37]){ player.p[0] -= s } | |
if(keysDown[38]){ player.p[1] -= s } | |
if(keysDown[39]){ player.p[0] += s } | |
if(keysDown[40]){ player.p[1] += s } | |
// if(keysDown[32]){ player...?} | |
} | |
score = 0 | |
count = 0 | |
function render(){ | |
count++ | |
if(count > 10){ | |
food.push([Math.random()*canvas.width, Math.random()*canvas.height]) | |
count = 0 | |
} | |
t = Date.now() | |
osc = 5*Math.sin(t/400) + 2*Math.cos(t/400) | |
update(5) | |
ctx.fillStyle = "rgba(0,0,0,0.1)" | |
ctx.fillRect(0,0,canvas.width,canvas.height) | |
ctx.fillStyle = "hsla("+(180+180*Math.sin(t/100))+",100%,50%,1)" | |
// ctx.fillRect(player.p[0] - osc/2, player.p[1] - osc/2, 10+osc,10+osc) | |
ctx.fillRect(player.p[0] - osc/2 + 10*osc*Math.sin(t/150), player.p[1] - osc/2 + 10*osc*Math.sin(t/100), 4, 4) | |
ctx.fillRect(player.p[0] - osc/2 + 10*osc*Math.cos(t/100), player.p[1] - osc/2 + 10*osc*Math.cos(t/150), 4, 4) | |
ctx.fillRect(player.p[0] - osc/2 + 10*osc*Math.cos(t/200), player.p[1] - osc/2 + 10*osc*Math.cos(t/100), 4, 4) | |
ctx.fillStyle = "hsla("+(180+180*Math.sin(t/600)*Math.cos(t/400))+",100%,50%,1)" | |
foodTemp = [] | |
for(var i=0; i<food.length; i++){ | |
if( Math.abs(player.p[0] - food[i][0]) + Math.abs(player.p[1] - food[i][1]) > 75) { | |
foodTemp.push(food[i]) | |
} else { | |
score++ | |
} | |
} | |
food = foodTemp | |
for(var i=0; i<food.length; i++){ | |
ctx.fillRect(food[i][0], food[i][1], 10, 10) | |
} | |
ctx.font="50px Verdana"; | |
ctx.fillText(score,50,50); | |
window.requestAnimationFrame(render) | |
} | |
render() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Collisions were borked, forgot Math.abs a second time, should have fixed it before submitting, but eh.