xxxxxxxxxx
173
let heart = [
[0, 1, 1, 0, 1, 1, 0],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 1, 0, 0, 0]
];
let score = 0;
let lives = 3;
let px = 200;
let py;
let pw = 100;
let ph = 20;
let pvel = 0;
let pacc = 0.9;
let pfric = 0.9;
let br = 24;
let bvel;
function setup() {
createCanvas(800, 600);
py = height - 50;
resetBall();
}
function draw() {
background(20);
update_player();
noStroke();
fill(240);
rectMode(CENTER);
rect(px, py, pw, ph);
if (bpos.x < 4 || bpos.x+4 > width) {
bvel.x *= -1;
}
if (bpos.y <= 3) {
bvel.y *= -1;
}
if (bpos.y > height) {
if(lives > 1) {
lives -= 1
resetBall();
} else {
gameOver();
}
}
if (collide()) {
bvel.y *= -1;
score += 10
print('Collided!!!')
}
bpos.add(bvel);
show_score();
show_ball();
stroke(195);
strokeWeight(8);
noFill();
rectMode(CORNER);
rect(0, 0, width, height);
} // draw
function collide() {
let ball_left = bpos.x - br/2
let ball_right = bpos.x + br/2
let ball_top = bpos.y - br/2
let ball_bottom = bpos.y + br/2;
let plr_left = px -pw/2
let plr_right = px +pw/2
let plr_top = py -ph/2
let plr_bottom = py +ph/2
if(
ball_left < plr_right &&
ball_right > plr_left &&
ball_bottom > plr_top &&
ball_top < plr_bottom
) {
return true
}
}
function pixelheart() {
stroke(190);
strokeWeight(2);
noFill();
let pixelsize = width/20;
rectMode(CENTER)
for (let i = 0; i < heart.length; i++) {
for (let j = 0; j < heart[0].length; j++) {
if (heart[i][j] === 1) {
rect(
width/2 - (heart.length/2) * pixelsize + (j * pixelsize),
height/2 - (heart.length/3) * pixelsize + (i * pixelsize),
pixelsize, pixelsize);
}
}
}
}
function show_ball() {
noStroke(); fill(255);
circle(bpos.x, bpos.y, br);
}
function show_score() {
textFont("Consolas");
textSize(20);
fill(255);
textAlign(LEFT, CENTER);
text(`SCORE: ${score}`, 12, 20);
textAlign(RIGHT, CENTER);
text(`LIVES: ${lives}`, width-12, 20);
}
function update_player() {
if (keyIsDown(LEFT_ARROW)) {
pvel -= pacc;
} else if (keyIsDown(RIGHT_ARROW)) {
pvel += pacc;
}
if (px - pw / 2 < 0) {
px = 0 + pw / 2;
} else if (px + pw / 2 > width) {
px = width - pw / 2;
}
px += pvel;
pvel *= pfric;
}
function debug() {
noFill();
stroke(255,0,0);
strokeWeight (5);
point(px -pw/2, py );
point(px +pw/2, py );
point(px, py -ph/2 );
point(px, py +ph/2)
}
function gameOver() {
textAlign(CENTER, CENTER)
text('GAME OVER!', width/2, height/2)
}
function resetBall() {
bpos = createVector(100, 200);
bvel = createVector(4, -3);
}