Move shield up or down with mouse to block arrows. Click to restart.
A fork of Arrow Blocker by Kellmer McFarling
xxxxxxxxxx
let arrow1x = [];
let arrow1y = [];
let px = [],
py = [];
const poly = [];
let arrowSpeed = 35
let startX = 230
let handY = 0;
let mouseYMin = 355
let mouseYMax = 605;
function setup() {
createCanvas(4*windowHeight/3, windowHeight);
frameRate(30)
startX = findStartX()
makeArrow()
}
function draw() {
clear();
findHandY();
background('lightblue');
// drawMouseLines();
updateArrow();
drawArrows();
person();
sword();
updateSword();
shield();
arrowCounter();
}
function findHandY() {
handY = mouseY
if (handY > mouseYMax) {
handY = mouseYMax
}
if (handY < mouseYMin){
handY = mouseYMin
}
}
function findStartX(thisY = 'default') {
let thisX = startX;
if (thisY != 'default') thisX = map(thisY, 550, 700, 225, 255) - 5;
while (thisX < width) {
thisX += arrowSpeed;
}
return thisX;
}
function redArrow(x, y) {
translate(x, y)
strokeWeight(0)
fill('saddleBrown')
rect(0, -2, 70, 4, 10, 10, 10, 10)
fill('gainsboro')
strokeWeight(0.3)
beginShape()
vertex(0, 0)
vertex(5, -10)
vertex(-20, 0)
vertex(5, 10)
vertex(0, 0)
endShape()
fill('red')
beginShape()
vertex(40, -2)
vertex(45, -7)
vertex(80, -7)
vertex(75, -2)
vertex(0, -2)
endShape()
beginShape()
vertex(40, 2)
vertex(45, 7)
vertex(80, 7)
vertex(75, 2)
vertex(40, 2)
vertex()
endShape()
resetMatrix()
}
function person() {
fill('black')
circle(220, 300, 100)
strokeWeight(8)
line(220, 300, 220, 550) //Body
line(220, 395, 290, 465) //Right Arm
line(290, 465, 331, handY)
line(220, 550, 250, 700) //Right Leg
line(220, 550, 200, 700) //Left Leg
fill(0, 0, 0, 0) //Hitbox
strokeWeight(0)
line(225, 300, 225, 570) //Body
line(225, 550, 255, 700) //leg
}
function sword() {
fill(0, 0, 0)
strokeWeight(8)
line(220, 395, 130, 460) //Left Arm
line(130, 460, 90, 425)
rect(90, 425, 2, 20)
circle(91, 450, 6)
strokeWeight(5)
line(75, 420, 105, 420)
beginShape();
vertex(96, 420)
vertex(100, 315)
vertex(90, 300)
vertex(80, 315)
vertex(86, 420)
endShape();
}
function shield() {
rect(330, handY - 100, 3, 200)
// rect(330, mouseY - 100, 3, 100)
}
function drawArrows() {
let i;
for (i = 0; i < arrow1x.length - 1; i += 1) {
redArrow(arrow1x[i], arrow1y[i] + handY)
}
redArrow(arrow1x[i], arrow1y[i])
}
function makeArrow() {
//let thisY = 410;
//while (thisY > 400 && thisY < 430) {
thisY = random(270, 620); // use 450 for creating screenshot or thumb, otherwise 620
//}
arrow1y.push(thisY)
//225, 550, 255, 700
if (thisY < 550) arrow1x.push(startX)
else {
let thisX = findStartX(thisY);
arrow1x.push(thisX);
}
}
function updateArrow() {
let i = arrow1x.length - 1
arrow1x[i] -= 35
poly[0] = createVector(arrow1x[i], arrow1y[i]);
poly[1] = createVector(arrow1x[i] + 5, arrow1y[i] - 10);
poly[2] = createVector(arrow1x[i] - 20, arrow1y[i]);
poly[3] = createVector(arrow1x[i] + 5, arrow1y[i] + 10);
hit = collideRectPoly(330, handY - 100, 3, 200, poly) //Shield
if (hit) {
arrow1y[i] -= handY
arrow1x[i] = 340
makeArrow();
}
hit = collideCirclePoly(220, 300, 100, poly); //Head
if (hit) {
arrow1x[i] += arrowSpeed/4
endScreen();
}
hit = collideLinePoly(225, 550, 255, 700, poly) //Leg
if (hit) {
arrow1x[i] = map(arrow1y[i], 550, 700, 225, 255) + arrowSpeed/4;
endScreen();
}
hit = collideLinePoly(225, 300, 225, 570, poly) //Body
if (hit) {
arrow1x[i] = 235
endScreen();
}
}
function updateSword() {
let i = sword.height + 1
sword[i] += 15
}
function endScreen() {
strokeWeight(0)
fill(235, 0, 0, 100)
rect(0, 0, width, height)
fill(0, 0, 0)
textSize(40)
textAlign(CENTER,CENTER)
text("Hit!", width/2, 140)
noLoop();
}
function drawMouseLines() {
push();
stroke('black');
strokeWeight(1);
if (px.length > 0) {
let i = 0;
for (i = 0; i < px.length - 1; i++) {
line(px[i], py[i], px[i + 1], py[i + 1]);
}
line(px[i], py[i], mouseX, mouseY);
}
pop();
}
function arrowCounter() {
textSize(40);
textAlign(CENTER, CENTER);
fill("black");
text("Block the Arrows ", width/2, 40);
textSize(30);
fill("blue");
text("Number of Arrows " + arrow1x.length, width/2, 90)
}
function mousePressed() {
arrow1x = []
arrow1y = []
makeArrow();
loop();
}
function keyPressed(){
save("img_" + month() + '-' + day() + '_' + hour() + '-' + minute() + '-' + second() + ".jpg");
}