Skip to content

Instantly share code, notes, and snippets.

@SamyBencherif
Last active January 25, 2020 18:47
Show Gist options
  • Save SamyBencherif/7cd8069305425c63353a367d94ccc40e to your computer and use it in GitHub Desktop.
Save SamyBencherif/7cd8069305425c63353a367d94ccc40e to your computer and use it in GitHub Desktop.
SCRIPT-8
// title: Bunny Guns
// I used my own sprite representation in code
// so I could have bigger textures.
var sprites = {
bunny:
{
width: 16,
height: 16,
tex: " 111 111 11 11 11 11 11 11 11 11 11551551 11151511 11411141 11111111 11133311 11111111 1116666666 122266666666 1122261222 0011111111 0011111111 " +
" 11 11 11 1 11 1 11 11 11 11 11 11 11551551 11151511 11411141 11111111 11133311 11111111 1116666666 122266666666 1122261222 0011111111 0011111111 "
},
dude:
{
width: 10,
height: 11,
tex: " 444 444 444 4 44444 4 4 4 4 4 4 4 " +
" 444 444 444 4 444 4 4 4 4 4 4 4 4 "
}
}
const REVERSED = 1;
function drawSprite(spr, x, y, frame, reversed)
{
var i=spr.width*spr.height*frame;
for (var u=0; u<spr.height; u++)
{
for (var v=0; v<spr.width; v++)
{
if (spr.tex[i] != ' ')
{
if (reversed)
setPixel(x-v+spr.width,y+u,spr.tex[i]);
else
setPixel(x+v,y+u,spr.tex[i]);
}
i++;
}
}
}
init = state => {
state.time = 0;
state.score = 0;
state.bulletClock = 0;
state.bulletsTillBunnySwitch = 10;
state.bunnyShooting = 0;
state.bullets = [];
state.bunny0Health = 300;
state.bunny1Health = 300;
state.bunny0OutClock = 0;
state.bunny1OutClock = 0;
state.bunny0HealthPenalty = 0;
state.bunny1HealthPenalty = 0;
state.healthClock = 0;
state.dude = {
x: 0,
y: 0,
vx: 0,
vy: 0,
health: 300,
penalty: 0,
}
}
function manageBullets(state, input, elapsed)
{
state.bulletClock += elapsed;
// fire, on average, every 2 seconds
var i = (Math.cos(state.time/20000) + 1) / 2 * 700;
if (2 * Math.random() * state.bulletClock > (1300 + i) && state.bullets.length < 20)
{
if (state.bunnyShooting && state.bunny1Health > 0)
{
state.bullets.push({x:128-16,y:124,v:-30});
state.bulletsTillBunnySwitch -= 1;
}
else if (state.bunny0Health > 0)
{
state.bullets.push({x:16,y:124,v:30});
state.bulletsTillBunnySwitch -= 1;
}
state.bulletClock = 0;
}
if (state.bulletsTillBunnySwitch <= 0)
{
state.bulletsTillBunnySwitch = 10;
state.bunnyShooting = 1-state.bunnyShooting;
}
// bullet physics
var nxt_bullets = [];
state.bullets.forEach(bullet => {
bullet.x += bullet.v * elapsed / 1000;
// bullets hurt
if (bullet.x > 64+state.dude.x && bullet.x < 64+state.dude.x+5 &&
bullet.y > 117-state.dude.y+2 && bullet.y < 117-state.dude.y+12)
{
state.dude.penalty += 20;
}
else if (bullet.x > 128-10)
{
// bunny 2 is hit
state.bunny1HealthPenalty += 100;
}
else if (bullet.x < 10)
{
// bunny 1 is hit
state.bunny0HealthPenalty += 100;
}
else if (bullet.x > 0 && bullet.x < 128)
nxt_bullets.push(bullet);
});
// remove bullets that are outside of view or hit the player
state.bullets = nxt_bullets;
}
function int(x)
{
return ~~x;
}
update = (state, input, elapsed) => {
if (state.dude.health <= 0)
{
if (input.up || input.right || input.down || input.left || input.start || input.select)
{
init(state);
}
return;
}
state.time += elapsed
if (state.bunny0Health <= 0)
{
if (state.bunny0OutClock == 0)
{
state.score += 1;
}
state.bunny0OutClock += elapsed | 1;
}
if (state.bunny1Health <= 0)
{
if (state.bunny1OutClock == 0)
{
state.score += 1;
}
state.bunny1OutClock += elapsed | 1;
}
if (state.bunny0OutClock > 2000)
{
state.bunny0OutClock = 0;
state.bunny0Health = 300;
}
if (state.bunny1OutClock > 2000)
{
state.bunny1OutClock = 0;
state.bunny1Health = 300;
}
// health number animation
if (state.healthClock <= 0)
{
state.healthClock = 10;
if (state.bunny1HealthPenalty > 0)
{
state.bunny1Health -= 10;
state.bunny1HealthPenalty -= 10;
}
if (state.bunny0HealthPenalty > 0)
{
state.bunny0Health -= 10;
state.bunny0HealthPenalty -= 10;
}
if (state.dude.penalty > 0)
{
state.dude.penalty -= 1;
state.dude.health -= 1;
}
}
state.healthClock -= elapsed;
manageBullets(state, input, elapsed);
// player physics
state.dude.x += state.dude.vx * elapsed / 10
state.dude.y += state.dude.vy * elapsed / 10
// gravity
if (state.dude.y > 0)
state.dude.vy -= .3;
// ground is solid
if (state.dude.y < 0)
state.dude.y = 0;
// running into bunnies hurts
if (state.dude.x < -64+16 && state.dude.y < 16 && state.bunny0Health > 0)
{
state.dude.vx += 10;
state.dude.penalty += 30;
state.bunny0HealthPenalty += 10;
}
if (state.dude.x > 64-20 && state.dude.y < 16 && state.bunny1Health > 0)
{
state.dude.vx -= 10;
state.dude.penalty += 30;
state.bunny1HealthPenalty += 10;
}
// player left-right controls
if (state.dude.y == 0)
{
// on the ground high friction / high influence
state.dude.vx = (int(input.right) - int(input.left)) * .4 + state.dude.vx * .8;
}
else
{
// in the air low friction / low influence
state.dude.vx = (int(input.right) - int(input.left)) * .2 + state.dude.vx * .87;
}
// player jump
if (input.up && state.dude.y <= 0)
{
state.dude.vy = 3
}
// keep player in world
if (state.dude.x > 58)
{
state.dude.x = 58;
}
if (state.dude.x < -65)
{
state.dude.x = -65;
}
}
draw = state => {
clear()
if (state.dude.health <= 0)
{
rectStroke(35,35,57,15);
print(40,40,"You are dead.");
print(20,60,"You defeated " + state.score + " bunnies");
line(20,67,105,67);
print(5,80,"- Press any key to play again -");
return;
}
// bunny recoil effect
let frame = 0;
if (state.bullets.length >= 1)
frame = state.bullets[state.bullets.length-1].x < 30;
if (state.bunny0Health > 0)
{
// draw bunny
drawSprite(sprites.bunny,0,112,frame);
// draw bunny health
line(3-frame,128-20,3-frame+10,128-20,4);
line(3-frame,128-20,3-frame+state.bunny0Health/30,128-20);
}
frame = 0;
state.bullets.forEach(bullet => {
frame = bullet.x > 128-30;
});
if (state.bunny1Health > 0)
{
// draw bunny 2
drawSprite(sprites.bunny,110,112,frame,REVERSED);
// draw bunny health
var hbpx = 116;
line(hbpx-3+frame,128-20,hbpx-3+frame+10,128-20,4);
line(hbpx-3+frame,128-20,hbpx-3+frame+state.bunny1Health/30,128-20);
}
// draw bullets
state.bullets.forEach(bullet => {
setPixel(bullet.x, bullet.y, 0);
});
// draw dude
frame = ~~(state.time/500 % 2); // frame = 1;
drawSprite(sprites.dude,64+state.dude.x,117-state.dude.y,frame);
print(10, 10, "Health: " + state.dude.health + "/300", 3)
print(10, 20, "Bunnies Got: " + state.score, 3)
}
{
"iframeVersion": "0.1.276",
"lines": [
317,
0,
0,
0,
0,
0,
0,
0
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment