xxxxxxxxxx
99
let box, player, floor, trap, title_screen
let position
let lost = false
async function setup() {
createCanvas(700, 400);
enemies = new Group();
coins = new Group();
floor = new Sprite(0, height-5, width*2, 5, 'static');
box = new Sprite(width/2, height-58, 50, 100, 'static');
trap = new Sprite(width/2+width/3.5, height-18, 175, 20, 'static');
trap.shapeColor = color(255, 0, 0);
trap.addToGroup(enemies);
trap2 = new Sprite(width/2+width/2.53, height-115, 20, 175, 'static');
trap2.shapeColor = color(255, 0, 0);
trap2.addToGroup(enemies);
coin = new Sprite(width/2+width/3.5, height-100, 25);
coin.shapeColor = color(255, 255, 0);
coin.addToGroup(coins);
position = width/2-width/4;
player = new Sprite(position, height-60, 50, 50);
player.drag = 1;
player.mass = 100;
player.rotationDrag = 100;
player.lives = 2;
player.points = 0;
player.draw = () => {
fill(237, 205, 0);
push();
rotate(player.direction);
ellipse(0, 0, 50 + player.speed, 50 - player.speed);
pop();
};
title_screen = new Sprite(0, 0, width*2, height*2, 'none');
title_screen.shapeColor = color(0, 100, 50);
title_screen.visible = false;
}
function draw() {
if (lost == true){
for (let s of allSprites) { s.remove(); }
title_screen.visible = true;
textSize(50);
textAlign(CENTER);
color(100, 0, 255);
text("you loose...", width/2, height/2);
button = createButton('click to restart');
button.position(width/2-50, height/2+50);
button.mousePressed(function() { window.location.reload() });
} else if (lost == false) {
background(255);
player.rotation = 0;
for (let s of allSprites) {
if (s.x < 0) { s.x = 1; s.vel.x = abs(s.vel.x); }
if (s.x > width) { s.x = width - 1; s.vel.x = -abs(s.vel.x); }
if (s.y < 0) { s.y = 1; s.vel.y = abs(s.vel.y); }
if (s.y > height) { s.y = height - 1; s.vel.y = -abs(s.vel.y); }
}
textSize(16);
textAlign(CENTER);
text("points... " + player.points, 50, 25);
text("lives... " + player.lives, width-50, 25);
player.collide(enemies, loose_life);
player.collide(coins, pickup_coin);
}
}
function pickup_coin(spriteA, spriteB) { spriteB.remove(); spriteA.points++; }
function loose_life(spriteA) {
spriteA.lives--;
if (spriteA.lives <= 0){ lost = true; }
}
function keyPressed() {
if (keyCode === LEFT_ARROW) {
player.moveTowards(player.x-10, player.y, 0.5);
} else if (keyCode === RIGHT_ARROW) {
player.moveTowards(player.x+10, player.y, 0.5);
} else if (keyCode === UP_ARROW) {
player.moveTowards(player.x, player.y-10, 0.5);
} else if (keyCode === DOWN_ARROW) {
player.moveTowards(player.x, player.y+10, 0.5);
} else {
}
}