xxxxxxxxxx
171
var bgColor = 255;
var changeDir1 = false;
var changeDir2 = false;
var changeDir3 = false;
var aim = false;
var clicked = false;
var targets = [];
let L3timer = 20;
let L3score = 0;
function preload() {
squirrel = loadImage('squirrel1.png');
}
function setup() {
createCanvas(700, 500);
obs1 = new Obs(width / 2 - 20, height / 2 + 80, 50, 60);
obs2 = new Obs(0, height / 2, 50, 60);
obs3 = new Obs(500, height / 2 - 80, 50, 60);
shooter = new Shooter(width / 2 - 25, height-60, 60,60);
bullet = new Bullet(shooter.x, 450, 3, 15);
for (var i = 0; i < 5; i++) {
targets[i] = new Target(i * 150, 0, 100, 100);
}
}
function draw() {
background(bgColor);
//image(squirrel, width/2, height/2, 30, 30);
textSize(22);
text('Time: '+L3timer, 20, 450);
text('Score: '+L3score, 20, 480);
if (frameCount % 60 == 0 && L3timer > 0) L3timer --;
// move obstacles
if (obs1.x > width - obs1.w) changeDir1 = true;
else if (obs1.x <= 0) changeDir1 = false;
if (obs1.x >= 0 && changeDir1 == false) obs1.x += 10;
else if (changeDir1 == true) obs1.x -= 10;
if (obs2.x > width - obs2.w) changeDir2 = true;
else if (obs2.x <= 0) changeDir2 = false;
if (obs2.x >= 0 && changeDir2 == false) obs2.x += 5;
else if (changeDir2 == true) obs2.x -= 5;
if (obs3.x > width - obs3.w) changeDir3 = true;
else if (obs3.x <= 0) changeDir3 = false;
if (obs3.x >= 0 && changeDir3 == false) obs3.x += 10;
else if (changeDir3 == true) obs3.x -= 10;
obs1.render();
obs1.collide();
obs2.render();
obs2.collide();
obs3.render();
obs3.collide();
shooter.render();
shooter.move();
bullet.render();
if (clicked) {
bullet.y -= 10;
bullet.x = shooter.x;
}
if (bullet.y == 0) bullet.y = 450;
for (var i = 0; i < 5; i++) {
targets[i].render();
targets[i].collide();
}
push();
fill(255, 117, 26);
if (L3timer <= 20 && L3timer >= 17) text('Hit the targets in the back, not the moving obstacles!', 100, height/2);
pop();
}
function mouseClicked() {
clicked = true;
}
class Obs {
constructor(x, y, w, h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
render() {
fill('red');
rect(this.x, this.y, this.w, this.h);
}
collide() {
if (bullet.y > this.y && bullet.y < this.y + this.h && bullet.x > this.x && bullet.x < this.x + this.w) L3score-=10;
}
}
class Shooter {
constructor(x, y, w, h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
render() {
image(squirrel, this.x, this.y, this.w, this.h);
}
move() {
if (keyIsDown(LEFT_ARROW)) {
// this.ax -= 5;
// this.bx -= 5;
// this.cx -= 5;
this.x-=5;
}
if (keyIsDown(RIGHT_ARROW)) {
// this.ax += 5;
// this.bx += 5;
// this.cx += 5;
this.x+=5;
}
}
// shoot() {
// if (mouseIsPressed) {
// rect(this.cx-1.5, shootY, 3, 15);
// shootY--;
// }
// }
}
class Bullet {
constructor(x, y, w, h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
render() {
rect(this.x, this.y, this.w, this.h);
}
}
class Target {
constructor(x, y, w, h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
render() {
rect(this.x, this.y, this.w, this.h);
}
collide() {
if (bullet.y > this.y && bullet.y < this.y + this.h && bullet.x > this.x && bullet.x < this.x + this.w) L3score+=2;
}
}